summaryrefslogtreecommitdiff
path: root/main.py
diff options
context:
space:
mode:
Diffstat (limited to 'main.py')
-rw-r--r--main.py109
1 files changed, 94 insertions, 15 deletions
diff --git a/main.py b/main.py
index 9531bee..d6f380a 100644
--- a/main.py
+++ b/main.py
@@ -7,11 +7,60 @@ import jinja2
from utils import *
+class Dns:
+ @staticmethod
+ def a(fqdn: str):
+ return {"type": "dns-entry",
+ "key": fqdn,
+ "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(f"{c.m.name}.machine.acme.corp"))
+
+class Terraform:
+ @staticmethod
+ def recordSet(fqdn: str):
+ return {"type": "terraform-record-set",
+ "key": fqdn,
+ "fqdn": fqdn,
+ "rrType": "A"
+ }
+
+ @staticmethod
+ def machine(key: str):
+ return {"type": "terraform-machine",
+ "key": key,
+ }
+
+ @staticmethod
+ def declare_rules():
+ @when_all((m.type == "dns-entry"))
+ def onDnsEntry(c):
+ c.assert_fact(Terraform.recordSet(c.m.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}")
+
class Acme:
@staticmethod
def make(env: str, tag: str):
@@ -69,6 +118,8 @@ dba = DockerBasedApplications
with ruleset("phase-1"):
Acme.declare_rules()
AcmeOperations.declare_rules()
+ Machine.declare_rules()
+ Terraform.declare_rules()
@when_all(+s.exception)
def second(c):
@@ -82,7 +133,8 @@ with ruleset("phase-1"):
@when_all(pri(1000), (m.type == 'dba-container'))
def dba_container(c):
- print(f"dba-container: {c.m}")
+ pass
+ # print(f"dba-container: {c.m}")
@when_all(pri(900), (m.type == 'dba-container') & (m.image == "statera") & -m.ports)
def addPortsToStatera(c):
@@ -105,7 +157,7 @@ with ruleset("phase-1"):
cluster = c.container.cluster
try:
c.assert_fact(dba.cluster(cluster))
- print(f"NEW CLUSTER: c.container={c.container}")
+ # print(f"NEW CLUSTER: c.container={c.container}")
except MessageObservedException:
pass
@@ -134,7 +186,7 @@ x = assert_fact("phase-1", m1); print(f"x: {x}")
x = assert_fact("phase-1", m2); print(f"x: {x}")
x = assert_fact("phase-1", m3); print(f"x: {x}")
-if True:
+if False:
print("Facts:")
for f in get_facts("phase-1"):
print(f"fact: {f}")
@@ -186,13 +238,15 @@ with ruleset("phase-2"):
c.container << ((m.type == "dba-container") & +m.ports_classified & (m.public_ports > 0))
)
def container(c):
- print(f"public container")
- print(f" cluster: {c.cluster}")
- print(f" container: {c.container}")
+ pass
+ # print(f"public container")
+ # print(f" cluster: {c.cluster}")
+ # print(f" container: {c.container}")
@when_all(((m.type == "dba-container") & (+m.ports_classified) & (m.public_ports == 0)))
def container(c):
- print(f"private container: {c.m}")
+ pass
+ # print(f"private container: {c.m}")
print("PHASE 2")
@@ -208,30 +262,55 @@ os.mkdir("gen")
os.mkdir("gen/platform")
os.mkdir("gen/platform/terraform")
os.mkdir("gen/platform/ansible")
+os.mkdir("gen/dns")
-print("PHASE 3")
+print("PHASE 3: Generating stuff")
file_loader = jinja2.FileSystemLoader("j2")
j2 = jinja2.Environment(loader=file_loader)
with ruleset("phase-3"):
- @when_all(pri(1), (m.type == "machine"))
- def terraformForMachine(c):
+ @when_all((m.type == "terraform-machine"), none(m.done == "platform/terraform/main.tf"))
+ def mainTf(c):
+ print("yo")
+ c.assert_fact({"type": "meta", "done": "platform/terraform/main.tf"})
+ with open(f"gen/platform/terraform/main.tf", "w") as f:
+ f.write("""
+terraform {
+ required_providers {
+ scaleway = {
+ source = "scaleway/scaleway"
+ }
+ }
+}""".strip())
+ f.write("\n")
+
+
+ @when_all((m.type == "terraform-machine"))
+ def ansibleMachine(c):
+ template = j2.get_template("platform-ansible.j2")
+ with open(f"gen/platform/ansible/{c.m.key}.yml", "w") as f:
+ s = template.render(**{"m": c.m})
+ f.write(s.strip())
+ f.write("\n")
+
+ @when_all((m.type == "terraform-machine"))
+ def terraformMachine(c):
template = j2.get_template("terraform-machine.j2")
with open(f"gen/platform/terraform/{c.m.key}.tf", "w") as f:
s = template.render(**{"m": c.m})
f.write(s.strip())
f.write("\n")
- @when_all(pri(1), (m.type == "machine"))
- def ansibleForMachine(c):
- template = j2.get_template("platform-ansible.j2")
- with open(f"gen/platform/ansible/{c.m.key}.tf", "w") as f:
+ @when_all((m.type == "terraform-record-set"))
+ def terraformRecordSet(c):
+ template = j2.get_template("terraform-record-set.j2")
+ with open(f"gen/dns/{c.m.key}.tf", "w") as f:
s = template.render(**{"m": c.m})
f.write(s.strip())
f.write("\n")
-for f in [f for f in get_facts("phase-1") if f["type"] in ("machine")]:
+for f in [f for f in get_facts("phase-1") if f["type"] in ("terraform-record-set", "terraform-machine")]:
x = assert_fact("phase-3", f); print(f"x: {x}")
write_facts("phase-3")