summaryrefslogtreecommitdiff
path: root/main.py
diff options
context:
space:
mode:
authorTrygve Laugstøl <trygvis@inamo.no>2020-12-29 20:52:52 +0100
committerTrygve Laugstøl <trygvis@inamo.no>2020-12-29 20:52:52 +0100
commite27264ff17bc102143a631495be86798a53cf6ca (patch)
treeb5b671701c2264b784fa87ed35384dd9bcaffadb /main.py
parent9851dffc8d915ff01117a5ed8070e5b2a1ef2bb5 (diff)
downloadrules-sandbox-e27264ff17bc102143a631495be86798a53cf6ca.tar.gz
rules-sandbox-e27264ff17bc102143a631495be86798a53cf6ca.tar.bz2
rules-sandbox-e27264ff17bc102143a631495be86798a53cf6ca.tar.xz
rules-sandbox-e27264ff17bc102143a631495be86798a53cf6ca.zip
Working TF code.
Diffstat (limited to 'main.py')
-rw-r--r--main.py69
1 files changed, 56 insertions, 13 deletions
diff --git a/main.py b/main.py
index d6f380a..cf04fb8 100644
--- a/main.py
+++ b/main.py
@@ -9,9 +9,9 @@ from utils import *
class Dns:
@staticmethod
- def a(fqdn: str):
+ def a(key: str, fqdn: str):
return {"type": "dns-entry",
- "key": fqdn,
+ "key": key,
"fqdn": fqdn,
"rrType": "A"
}
@@ -25,15 +25,17 @@ class Machine:
def declare_rules():
@when_all((m.type == "machine"))
def acmeApp(c):
- c.assert_fact(Dns.a(f"{c.m.name}.machine.acme.corp"))
+ c.assert_fact(Dns.a(c.m.key, f"{c.m.name}.machine.acme.corp"))
class Terraform:
@staticmethod
- def recordSet(fqdn: str):
+ def recordSetForMachine(machineKey: str, terraformId: str, fqdn: str):
return {"type": "terraform-record-set",
- "key": fqdn,
+ "key": machineKey,
+ "terraformId": terraformId,
"fqdn": fqdn,
- "rrType": "A"
+ "rrType": "A",
+ "rrData": f"scaleway_instance_ip.{machineKey}.address",
}
@staticmethod
@@ -44,9 +46,14 @@ class Terraform:
@staticmethod
def declare_rules():
- @when_all((m.type == "dns-entry"))
+ @when_all(
+ c.dns << (m.type == "dns-entry"),
+ c.machine << ((m.type == "terraform-machine") & (m.key == c.dns.key)),
+ )
def onDnsEntry(c):
- c.assert_fact(Terraform.recordSet(c.m.fqdn))
+ 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):
@@ -270,9 +277,16 @@ file_loader = jinja2.FileSystemLoader("j2")
j2 = jinja2.Environment(loader=file_loader)
with ruleset("phase-3"):
- @when_all((m.type == "terraform-machine"), none(m.done == "platform/terraform/main.tf"))
+ @when_all(m.type == "meta")
+ def ignoreMeta(c):
+ pass
+
+ @when_all(
+ pri(1000),
+ (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("""
@@ -282,9 +296,22 @@ terraform {
source = "scaleway/scaleway"
}
}
-}""".strip())
+}
+""".strip())
f.write("\n")
+ machines = []
+ for f in c.get_facts():
+ if f.get("type") != "terraform-machine":
+ continue
+ machines.append(f)
+ print(f"machine: {f}")
+
+ template = j2.get_template("terraform-machine-outputs.j2")
+ with open(f"gen/platform/terraform/outputs.tf", "w") as f:
+ s = template.render(**{"machines": machines})
+ f.write(s.strip())
+ f.write("\n")
@when_all((m.type == "terraform-machine"))
def ansibleMachine(c):
@@ -310,7 +337,23 @@ terraform {
f.write(s.strip())
f.write("\n")
-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}")
+ @when_all(
+ (m.type == "terraform-record-set"),
+ none(m.done == "dns/inputs.tf"),
+ )
+ def mainTf(c):
+ c.assert_fact({"type": "meta", "done": "dns/inputs.tf"})
+ with open(f"gen/dns/inputs.tf", "w") as f:
+ f.write("""
+variable "addresses" {
+ type = map(string)
+}
+""".strip())
+ f.write("\n")
+
+facts = [f for f in get_facts("phase-1") if f["type"] in ("terraform-record-set", "terraform-machine")]
+#for f in facts:
+# x = assert_fact("phase-3", f); print(f"x: {x}")
+x = assert_facts("phase-3", facts); print(f"x: {x}")
write_facts("phase-3")