diff options
-rw-r--r-- | Makefile | 5 | ||||
-rw-r--r-- | py/acme/rai/__main__.py (renamed from main.py) | 152 | ||||
-rw-r--r-- | py/acme/rai/acme.py | 39 | ||||
-rw-r--r-- | py/acme/rai/dba.py | 13 | ||||
-rw-r--r-- | py/acme/rai/machine.py | 22 | ||||
-rw-r--r-- | py/acme/rai/terraform.py | 42 | ||||
-rw-r--r-- | py/acme/rai/utils.py (renamed from utils.py) | 0 |
7 files changed, 145 insertions, 128 deletions
@@ -1,7 +1,10 @@ +PYTHONPATH=$(PWD)/py +export PYTHONPATH + all: env/.cookie run: env/.cookie - env/bin/python main.py + env/bin/python -m acme.rai env: virtualenv -p python3 env diff --git a/main.py b/py/acme/rai/__main__.py index 0e0ec3b..9319ec5 100644 --- a/main.py +++ b/py/acme/rai/__main__.py @@ -5,128 +5,18 @@ import os import os.path import jinja2 -from utils import * - -class Dns: - @staticmethod - def a(key: str, fqdn: str): - return {"type": "dns-entry", - "key": key, - "fqdn": fqdn, - "rrType": "A" - } - -class Machine: - @staticmethod - def make(name: str): - return {"type": "machine", "key": name, "name": name} - - @staticmethod - def declare_rules(): - @when_all((m.type == "machine")) - def acmeApp(c): - c.assert_fact(Dns.a(c.m.key, f"{c.m.name}.machine.acme.corp")) - -class Terraform: - @staticmethod - def recordSetForMachine(machineKey: str, terraformId: str, fqdn: str): - return {"type": "terraform-record-set", - "key": machineKey, - "terraformId": terraformId, - "fqdn": fqdn, - "rrType": "A", - "rrData": f"scaleway_instance_ip.{machineKey}.address", - } - - @staticmethod - def machine(key: str): - return {"type": "terraform-machine", - "key": key, - } - - @staticmethod - def declare_rules(): - @when_all( - c.dns << (m.type == "dns-entry"), - c.machine << ((m.type == "terraform-machine") & (m.key == c.dns.key)), - ) - def onDnsEntry(c): - print("yooooooooooooooooo") - terraformId = c.dns.fqdn.replace(".", "_") - c.assert_fact(Terraform.recordSetForMachine(c.machine.key, terraformId, c.dns.fqdn)) - - @when_all((m.type == "terraform-record-set")) - def defaultTerraformRecordSet(c): - pass +from .utils import * - @when_all((m.type == "machine")) - def onDnsEntry(c): - print(f"matched machine: {c.m}") - c.assert_fact(Terraform.machine(c.m.key)) - - @when_all((m.type == "terraform-machine")) - def defaultTerraformMachine(c): - print(f"matched terraform-machine: {c.m}") - -class Acme: - @staticmethod - def make(env: str, tag: str): - return {"type": "acme-application", "key": env, "env": env, "tag": tag} - - @staticmethod - def declare_rules(): - @when_all((m.type == "acme-application")) - def acmeApp(c): - cluster = f"acme-{c.m.env}" - tag = f"{c.m.tag}" - # c.assert_fact(dba.cluster(cluster)) - for f in [ - dba.container(cluster, "app", "statera", "statera", tag), - dba.container(cluster, "app", "statera-console", "statera-console", tag), - dba.container(cluster, "app", "4tune-web", "4tune-web", tag), - dba.container(cluster, "app", "4tune-api", "4tune-api", tag), - dba.container(cluster, "db", "pdb", "postgresql", "13"), - dba.container(cluster, "db", "mdb", "mongodb", "3.2"), - ]: - c.assert_fact(f) - -class AcmeOperations: - @staticmethod - def make(): - return {"type": "acme-ops", "key": "acme-ops"} - - @staticmethod - def declare_rules(): - @when_all((m.type == "acme-ops")) - def acmeOps(c): - cluster = "acme-ops" - c.assert_fact(dba.cluster(cluster)) - c.assert_fact(dba.container(cluster, "app", "pdb", "postgresql", "11")) - c.assert_fact(dba.container(cluster, "app", "n8n", "n8n", "0.84.1")) - # pgadmin, loki - -class DockerBasedApplications: - @staticmethod - def cluster(key: str): - return {"type": "dba-cluster", "key": key} - - @staticmethod - def container(cluster: str, machineRole: str, name: str, image: str, tag: str): - return {"type": "dba-container", - "key": f"{cluster}/{name}", - "cluster": cluster, - "machineRole": machineRole, - "name": name, - "image": image, - "tag": tag} - -dba = DockerBasedApplications +from . import acme +from . import dba +from . import machine +from . import terraform with ruleset("phase-1"): - Acme.declare_rules() - AcmeOperations.declare_rules() - Machine.declare_rules() - Terraform.declare_rules() + acme.Acme.declare_rules() + acme.AcmeOperations.declare_rules() + machine.Machine.declare_rules() + terraform.Terraform.declare_rules() @when_all(+s.exception) def second(c): @@ -178,13 +68,13 @@ with ruleset("phase-1"): # print(f"c.cluster: {c.cluster}") # pass -m1 = Machine.make("acme-1") -m2 = Machine.make("acme-2") -m3 = Machine.make("acme-3") +m1 = machine.Machine.make("acme-1") +m2 = machine.Machine.make("acme-2") +m3 = machine.Machine.make("acme-3") -acmeCi = Acme.make("ci", "development") -acmeProduction = Acme.make("production", "master") -acmeOps = AcmeOperations.make() +acmeCi = acme.Acme.make("ci", "development") +acmeProduction = acme.Acme.make("production", "master") +acmeOps = acme.AcmeOperations.make() x = assert_fact("phase-1", acmeCi); print(f"x: {x}") x = assert_fact("phase-1", acmeProduction); print(f"x: {x}") @@ -281,6 +171,12 @@ j2 = jinja2.Environment(loader=file_loader) with ruleset("phase-3"): @when_all(m.type == "meta") def ignoreMeta(c): + print(f"ignoring {c.m}") + pass + + @when_all(m.type == "meta") + def ignoreMeta(c): + print(f"ignoring 2 {c.m}") pass @when_all( @@ -289,7 +185,7 @@ with ruleset("phase-3"): none(m.done == "platform/terraform/main.tf"), ) def mainTf(c): - c.assert_fact({"type": "meta", "done": "platform/terraform/main.tf"}) + print(f"WRITING gen/platform/terraform/main.tf") with open(f"gen/platform/terraform/main.tf", "w") as f: f.write(""" terraform { @@ -301,6 +197,7 @@ terraform { } """.strip()) f.write("\n") + c.assert_fact({"type": "meta", "done": "platform/terraform/main.tf"}) machines = [] for f in c.get_facts(): @@ -344,7 +241,7 @@ terraform { none(m.done == "dns/inputs.tf"), ) def mainTf(c): - c.assert_fact({"type": "meta", "done": "dns/inputs.tf"}) + print("WRITING dns/inputs.tf") with open(f"gen/dns/inputs.tf", "w") as f: f.write(""" variable "addresses" { @@ -352,6 +249,7 @@ variable "addresses" { } """.strip()) f.write("\n") + c.assert_fact({"type": "meta", "done": "dns/inputs.tf"}) facts = [f for f in get_facts("phase-1") if f["type"] in ("terraform-record-set", "terraform-machine")] #for f in facts: diff --git a/py/acme/rai/acme.py b/py/acme/rai/acme.py new file mode 100644 index 0000000..e75d492 --- /dev/null +++ b/py/acme/rai/acme.py @@ -0,0 +1,39 @@ +from durable.lang import * + +from . import dba + +class Acme: + @staticmethod + def make(env: str, tag: str): + return {"type": "acme-application", "key": env, "env": env, "tag": tag} + + @staticmethod + def declare_rules(): + @when_all((m.type == "acme-application")) + def acmeApp(c): + cluster = f"acme-{c.m.env}" + tag = f"{c.m.tag}" + # c.assert_fact(dba.cluster(cluster)) + for f in [ + dba.container(cluster, "app", "statera", "statera", tag), + dba.container(cluster, "app", "statera-console", "statera-console", tag), + dba.container(cluster, "app", "4tune-web", "4tune-web", tag), + dba.container(cluster, "app", "4tune-api", "4tune-api", tag), + dba.container(cluster, "db", "pdb", "postgresql", "13"), + dba.container(cluster, "db", "mdb", "mongodb", "3.2"), + ]: + c.assert_fact(f) + +class AcmeOperations: + @staticmethod + def make(): + return {"type": "acme-ops", "key": "acme-ops"} + + @staticmethod + def declare_rules(): + @when_all((m.type == "acme-ops")) + def acmeOps(c): + cluster = "acme-ops" + c.assert_fact(dba.cluster(cluster)) + c.assert_fact(dba.container(cluster, "app", "pdb", "postgresql", "11")) + c.assert_fact(dba.container(cluster, "app", "n8n", "n8n", "0.84.1")) diff --git a/py/acme/rai/dba.py b/py/acme/rai/dba.py new file mode 100644 index 0000000..04d10bf --- /dev/null +++ b/py/acme/rai/dba.py @@ -0,0 +1,13 @@ +from durable.lang import * + +def cluster(key: str): + return {"type": "dba-cluster", "key": key} + +def container(cluster: str, machineRole: str, name: str, image: str, tag: str): + return {"type": "dba-container", + "key": f"{cluster}/{name}", + "cluster": cluster, + "machineRole": machineRole, + "name": name, + "image": image, + "tag": tag} diff --git a/py/acme/rai/machine.py b/py/acme/rai/machine.py new file mode 100644 index 0000000..5b5da22 --- /dev/null +++ b/py/acme/rai/machine.py @@ -0,0 +1,22 @@ +from durable.lang import * + +class Dns: + @staticmethod + def a(key: str, fqdn: str): + return {"type": "dns-entry", + "key": key, + "fqdn": fqdn, + "rrType": "A" + } + +class Machine: + @staticmethod + def make(name: str): + return {"type": "machine", "key": name, "name": name} + + @staticmethod + def declare_rules(): + @when_all((m.type == "machine")) + def acmeApp(c): + c.assert_fact(Dns.a(c.m.key, f"{c.m.name}.machine.acme.corp")) + diff --git a/py/acme/rai/terraform.py b/py/acme/rai/terraform.py new file mode 100644 index 0000000..c84a7e1 --- /dev/null +++ b/py/acme/rai/terraform.py @@ -0,0 +1,42 @@ +from durable.lang import * + +class Terraform: + @staticmethod + def recordSetForMachine(machineKey: str, terraformId: str, fqdn: str): + return {"type": "terraform-record-set", + "key": machineKey, + "terraformId": terraformId, + "fqdn": fqdn, + "rrType": "A", + "rrData": f"scaleway_instance_ip.{machineKey}.address", + } + + @staticmethod + def machine(key: str): + return {"type": "terraform-machine", + "key": key, + } + + @staticmethod + def declare_rules(): + @when_all( + c.dns << (m.type == "dns-entry"), + c.machine << ((m.type == "terraform-machine") & (m.key == c.dns.key)), + ) + def onDnsEntry(c): + print("yooooooooooooooooo") + terraformId = c.dns.fqdn.replace(".", "_") + c.assert_fact(Terraform.recordSetForMachine(c.machine.key, terraformId, c.dns.fqdn)) + + @when_all((m.type == "terraform-record-set")) + def defaultTerraformRecordSet(c): + pass + + @when_all((m.type == "machine")) + def onDnsEntry(c): + print(f"matched machine: {c.m}") + c.assert_fact(Terraform.machine(c.m.key)) + + @when_all((m.type == "terraform-machine")) + def defaultTerraformMachine(c): + print(f"matched terraform-machine: {c.m}") diff --git a/utils.py b/py/acme/rai/utils.py index bc47d8f..bc47d8f 100644 --- a/utils.py +++ b/py/acme/rai/utils.py |