summaryrefslogtreecommitdiff
path: root/main.py
diff options
context:
space:
mode:
authorTrygve Laugstøl <trygvis@inamo.no>2020-12-27 21:44:09 +0100
committerTrygve Laugstøl <trygvis@inamo.no>2020-12-27 21:44:09 +0100
commitb8af5cdf274a60f364d34192d4f483f4f4800282 (patch)
treef5977d9a13259da488edfebab392884cee394c39 /main.py
parent13d7de5dab25c830deacf335bb704aed0e2db3a7 (diff)
downloadrules-sandbox-b8af5cdf274a60f364d34192d4f483f4f4800282.tar.gz
rules-sandbox-b8af5cdf274a60f364d34192d4f483f4f4800282.tar.bz2
rules-sandbox-b8af5cdf274a60f364d34192d4f483f4f4800282.tar.xz
rules-sandbox-b8af5cdf274a60f364d34192d4f483f4f4800282.zip
Adding ansible and terraform configs.
Diffstat (limited to 'main.py')
-rw-r--r--main.py94
1 files changed, 74 insertions, 20 deletions
diff --git a/main.py b/main.py
index f3e8f42..9531bee 100644
--- a/main.py
+++ b/main.py
@@ -1,19 +1,27 @@
from durable.engine import MessageObservedException
from durable.lang import *
+import shutil
+import os
+import os.path
+import jinja2
from utils import *
-class VimScore:
+class Machine:
+ @staticmethod
+ def make(name: str):
+ return {"type": "machine", "key": name, "name": name}
+
+class Acme:
@staticmethod
def make(env: str, tag: str):
- return {"type": "vimscore-application", "key": env, "env": env, "tag": tag}
+ return {"type": "acme-application", "key": env, "env": env, "tag": tag}
@staticmethod
def declare_rules():
- @when_all((m.type == "vimscore-application"))
- def vsApp(c):
- print(f"vimscoreApplication: {c.m}")
- cluster = f"vimscore-{c.m.env}"
+ @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 [
@@ -26,16 +34,16 @@ class VimScore:
]:
c.assert_fact(f)
-class VsOperations:
+class AcmeOperations:
@staticmethod
def make():
- return {"type": "vimscore-ops", "key": "vimscore-ops"}
+ return {"type": "acme-ops", "key": "acme-ops"}
@staticmethod
def declare_rules():
- @when_all((m.type == "vimscore-ops"))
- def vsOps(c):
- cluster = "vimscore-ops"
+ @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"))
@@ -59,8 +67,8 @@ class DockerBasedApplications:
dba = DockerBasedApplications
with ruleset("phase-1"):
- VimScore.declare_rules()
- VsOperations.declare_rules()
+ Acme.declare_rules()
+ AcmeOperations.declare_rules()
@when_all(+s.exception)
def second(c):
@@ -68,6 +76,10 @@ with ruleset("phase-1"):
print(c.s["exception"])
c.s.exception = None
+ @when_all(pri(1000), (m.type == 'machine'))
+ def defaultMachine(c):
+ pass
+
@when_all(pri(1000), (m.type == 'dba-container'))
def dba_container(c):
print(f"dba-container: {c.m}")
@@ -107,13 +119,20 @@ with ruleset("phase-1"):
# print(f"c.cluster: {c.cluster}")
# pass
-vimscoreCi = VimScore.make("ci", "development")
-vimscoreProduction = VimScore.make("production", "master")
-vsOps = VsOperations.make()
+m1 = Machine.make("acme-1")
+m2 = Machine.make("acme-2")
+m3 = Machine.make("acme-3")
-x = assert_fact("phase-1", vimscoreCi); print(f"x: {x}")
-x = assert_fact("phase-1", vimscoreProduction); print(f"x: {x}")
-x = assert_fact("phase-1", vsOps); print(f"x: {x}")
+acmeCi = Acme.make("ci", "development")
+acmeProduction = Acme.make("production", "master")
+acmeOps = AcmeOperations.make()
+
+x = assert_fact("phase-1", acmeCi); print(f"x: {x}")
+x = assert_fact("phase-1", acmeProduction); print(f"x: {x}")
+x = assert_fact("phase-1", acmeOps); print(f"x: {x}")
+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:
print("Facts:")
@@ -178,6 +197,41 @@ with ruleset("phase-2"):
print("PHASE 2")
for f in [f for f in get_facts("phase-1") if f["type"] in ("dba-cluster", "dba-container")]:
- assert_fact("phase-2", f)
+ x = assert_fact("phase-2", f); print(f"x: {x}")
write_facts("phase-2")
+
+# Prepare
+if os.path.isdir("gen"):
+ shutil.rmtree("gen")
+os.mkdir("gen")
+os.mkdir("gen/platform")
+os.mkdir("gen/platform/terraform")
+os.mkdir("gen/platform/ansible")
+
+print("PHASE 3")
+
+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):
+ 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:
+ 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")]:
+ x = assert_fact("phase-3", f); print(f"x: {x}")
+
+write_facts("phase-3")