diff options
author | Trygve Laugstøl <trygvis@inamo.no> | 2020-12-29 20:52:52 +0100 |
---|---|---|
committer | Trygve Laugstøl <trygvis@inamo.no> | 2020-12-29 20:52:52 +0100 |
commit | e27264ff17bc102143a631495be86798a53cf6ca (patch) | |
tree | b5b671701c2264b784fa87ed35384dd9bcaffadb | |
parent | 9851dffc8d915ff01117a5ed8070e5b2a1ef2bb5 (diff) | |
download | rules-sandbox-e27264ff17bc102143a631495be86798a53cf6ca.tar.gz rules-sandbox-e27264ff17bc102143a631495be86798a53cf6ca.tar.bz2 rules-sandbox-e27264ff17bc102143a631495be86798a53cf6ca.tar.xz rules-sandbox-e27264ff17bc102143a631495be86798a53cf6ca.zip |
Working TF code.
28 files changed, 136 insertions, 65 deletions
@@ -2,7 +2,6 @@ all: env/.cookie run: env/.cookie env/bin/python main.py - find out gen env: virtualenv -p python3 env diff --git a/gen/dns/acme-1.machine.acme.corp.tf b/gen/dns/acme-1.tf index cb408cb..c1022ba 100644 --- a/gen/dns/acme-1.machine.acme.corp.tf +++ b/gen/dns/acme-1.tf @@ -1,8 +1,8 @@ -resource "google_dns_record_set" "acme-1.machine.acme.corp" { +resource "google_dns_record_set" "acme-1_machine_acme_corp" { name = "acme-1.machine.acme.corp" managed_zone = "None" type = "A" ttl = 300 - rrdatas = [None] + rrdatas = [var.addresses.acme-1] } diff --git a/gen/dns/acme-2.machine.acme.corp.tf b/gen/dns/acme-2.tf index 9edfe76..4847052 100644 --- a/gen/dns/acme-2.machine.acme.corp.tf +++ b/gen/dns/acme-2.tf @@ -1,8 +1,8 @@ -resource "google_dns_record_set" "acme-2.machine.acme.corp" { +resource "google_dns_record_set" "acme-2_machine_acme_corp" { name = "acme-2.machine.acme.corp" managed_zone = "None" type = "A" ttl = 300 - rrdatas = [None] + rrdatas = [var.addresses.acme-2] } diff --git a/gen/dns/acme-3.machine.acme.corp.tf b/gen/dns/acme-3.tf index 940dbf2..c192dd3 100644 --- a/gen/dns/acme-3.machine.acme.corp.tf +++ b/gen/dns/acme-3.tf @@ -1,8 +1,8 @@ -resource "google_dns_record_set" "acme-3.machine.acme.corp" { +resource "google_dns_record_set" "acme-3_machine_acme_corp" { name = "acme-3.machine.acme.corp" managed_zone = "None" type = "A" ttl = 300 - rrdatas = [None] + rrdatas = [var.addresses.acme-3] } diff --git a/gen/dns/inputs.tf b/gen/dns/inputs.tf new file mode 100644 index 0000000..615ea69 --- /dev/null +++ b/gen/dns/inputs.tf @@ -0,0 +1,3 @@ +variable "addresses" { + type = map(string) +} diff --git a/gen/platform/terraform/acme-1.tf b/gen/platform/terraform/acme-1.tf index 06b681f..8100533 100644 --- a/gen/platform/terraform/acme-1.tf +++ b/gen/platform/terraform/acme-1.tf @@ -9,7 +9,7 @@ resource "scaleway_instance_server" "acme-1" { resource "scaleway_instance_ip" "acme-1" {} -resource "scaleway_instance_ip_reverse_dns" "acme-1" { - ip_id = scaleway_instance_ip.acme-1.id - reverse = "acme-1.${google_dns_managed_zone.machine.dns_name}" -} +#resource "scaleway_instance_ip_reverse_dns" "acme-1" { +# ip_id = scaleway_instance_ip.acme-1.id +# reverse = "acme-1.${google_dns_managed_zone.machine.dns_name}" +#} diff --git a/gen/platform/terraform/acme-2.tf b/gen/platform/terraform/acme-2.tf index d2e41b0..07d5e8b 100644 --- a/gen/platform/terraform/acme-2.tf +++ b/gen/platform/terraform/acme-2.tf @@ -9,7 +9,7 @@ resource "scaleway_instance_server" "acme-2" { resource "scaleway_instance_ip" "acme-2" {} -resource "scaleway_instance_ip_reverse_dns" "acme-2" { - ip_id = scaleway_instance_ip.acme-2.id - reverse = "acme-2.${google_dns_managed_zone.machine.dns_name}" -} +#resource "scaleway_instance_ip_reverse_dns" "acme-2" { +# ip_id = scaleway_instance_ip.acme-2.id +# reverse = "acme-2.${google_dns_managed_zone.machine.dns_name}" +#} diff --git a/gen/platform/terraform/acme-3.tf b/gen/platform/terraform/acme-3.tf index 4a84d3f..a2246bc 100644 --- a/gen/platform/terraform/acme-3.tf +++ b/gen/platform/terraform/acme-3.tf @@ -9,7 +9,7 @@ resource "scaleway_instance_server" "acme-3" { resource "scaleway_instance_ip" "acme-3" {} -resource "scaleway_instance_ip_reverse_dns" "acme-3" { - ip_id = scaleway_instance_ip.acme-3.id - reverse = "acme-3.${google_dns_managed_zone.machine.dns_name}" -} +#resource "scaleway_instance_ip_reverse_dns" "acme-3" { +# ip_id = scaleway_instance_ip.acme-3.id +# reverse = "acme-3.${google_dns_managed_zone.machine.dns_name}" +#} diff --git a/gen/platform/terraform/outputs.tf b/gen/platform/terraform/outputs.tf new file mode 100644 index 0000000..87ccb0b --- /dev/null +++ b/gen/platform/terraform/outputs.tf @@ -0,0 +1,7 @@ +output "addresses" { + value = { + acme-2: scaleway_instance_ip.acme-2.address, + acme-3: scaleway_instance_ip.acme-3.address, + acme-1: scaleway_instance_ip.acme-1.address, + } +} diff --git a/j2/terraform-machine-outputs.j2 b/j2/terraform-machine-outputs.j2 new file mode 100644 index 0000000..438fac6 --- /dev/null +++ b/j2/terraform-machine-outputs.j2 @@ -0,0 +1,7 @@ +output "addresses" { + value = { +{%- for m in machines %} + {{ m.key }}: scaleway_instance_ip.{{ m.key }}.address, +{%- endfor %} + } +} diff --git a/j2/terraform-machine.j2 b/j2/terraform-machine.j2 index e6f0fe9..889a23d 100644 --- a/j2/terraform-machine.j2 +++ b/j2/terraform-machine.j2 @@ -9,7 +9,7 @@ resource "scaleway_instance_server" "{{ m.key }}" { resource "scaleway_instance_ip" "{{ m.key }}" {} -resource "scaleway_instance_ip_reverse_dns" "{{ m.key }}" { - ip_id = scaleway_instance_ip.{{ m.key }}.id - reverse = "{{ m.key }}.${google_dns_managed_zone.machine.dns_name}" -} +#resource "scaleway_instance_ip_reverse_dns" "{{ m.key }}" { +# ip_id = scaleway_instance_ip.{{ m.key }}.id +# reverse = "{{ m.key }}.${google_dns_managed_zone.machine.dns_name}" +#} diff --git a/j2/terraform-record-set.j2 b/j2/terraform-record-set.j2 index 7dea150..03304bc 100644 --- a/j2/terraform-record-set.j2 +++ b/j2/terraform-record-set.j2 @@ -1,8 +1,8 @@ -resource "google_dns_record_set" "{{ m.key }}" { +resource "google_dns_record_set" "{{ m.terraformId }}" { name = "{{ m.fqdn }}" managed_zone = "{{ m.terraformManagedZone }}" type = "{{ m.rrType }}" ttl = 300 - rrdatas = [{{ m.terraformIp }}] + rrdatas = [var.addresses.{{ m.key }}] } @@ -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") diff --git a/out/phase-1/dns-entry/acme-1.machine.acme.corp.yaml b/out/phase-1/dns-entry/acme-1.yaml index d9d83ed..aafae11 100644 --- a/out/phase-1/dns-entry/acme-1.machine.acme.corp.yaml +++ b/out/phase-1/dns-entry/acme-1.yaml @@ -1,4 +1,4 @@ fqdn: acme-1.machine.acme.corp -key: acme-1.machine.acme.corp +key: acme-1 rrType: A type: dns-entry diff --git a/out/phase-1/dns-entry/acme-2.machine.acme.corp.yaml b/out/phase-1/dns-entry/acme-2.yaml index b782af3..38ad9e9 100644 --- a/out/phase-1/dns-entry/acme-2.machine.acme.corp.yaml +++ b/out/phase-1/dns-entry/acme-2.yaml @@ -1,4 +1,4 @@ fqdn: acme-2.machine.acme.corp -key: acme-2.machine.acme.corp +key: acme-2 rrType: A type: dns-entry diff --git a/out/phase-1/dns-entry/acme-3.machine.acme.corp.yaml b/out/phase-1/dns-entry/acme-3.yaml index 28ee130..451ba7c 100644 --- a/out/phase-1/dns-entry/acme-3.machine.acme.corp.yaml +++ b/out/phase-1/dns-entry/acme-3.yaml @@ -1,4 +1,4 @@ fqdn: acme-3.machine.acme.corp -key: acme-3.machine.acme.corp +key: acme-3 rrType: A type: dns-entry diff --git a/out/phase-1/terraform-record-set/acme-1.machine.acme.corp.yaml b/out/phase-1/terraform-record-set/acme-1.machine.acme.corp.yaml deleted file mode 100644 index 9537d73..0000000 --- a/out/phase-1/terraform-record-set/acme-1.machine.acme.corp.yaml +++ /dev/null @@ -1,4 +0,0 @@ -fqdn: acme-1.machine.acme.corp -key: acme-1.machine.acme.corp -rrType: A -type: terraform-record-set diff --git a/out/phase-1/terraform-record-set/acme-1.yaml b/out/phase-1/terraform-record-set/acme-1.yaml new file mode 100644 index 0000000..ccf16c7 --- /dev/null +++ b/out/phase-1/terraform-record-set/acme-1.yaml @@ -0,0 +1,6 @@ +fqdn: acme-1.machine.acme.corp +key: acme-1 +rrData: scaleway_instance_ip.acme-1.address +rrType: A +terraformId: acme-1_machine_acme_corp +type: terraform-record-set diff --git a/out/phase-1/terraform-record-set/acme-2.machine.acme.corp.yaml b/out/phase-1/terraform-record-set/acme-2.machine.acme.corp.yaml deleted file mode 100644 index b00bb5c..0000000 --- a/out/phase-1/terraform-record-set/acme-2.machine.acme.corp.yaml +++ /dev/null @@ -1,4 +0,0 @@ -fqdn: acme-2.machine.acme.corp -key: acme-2.machine.acme.corp -rrType: A -type: terraform-record-set diff --git a/out/phase-1/terraform-record-set/acme-2.yaml b/out/phase-1/terraform-record-set/acme-2.yaml new file mode 100644 index 0000000..d95be1f --- /dev/null +++ b/out/phase-1/terraform-record-set/acme-2.yaml @@ -0,0 +1,6 @@ +fqdn: acme-2.machine.acme.corp +key: acme-2 +rrData: scaleway_instance_ip.acme-2.address +rrType: A +terraformId: acme-2_machine_acme_corp +type: terraform-record-set diff --git a/out/phase-1/terraform-record-set/acme-3.machine.acme.corp.yaml b/out/phase-1/terraform-record-set/acme-3.machine.acme.corp.yaml deleted file mode 100644 index 94a3056..0000000 --- a/out/phase-1/terraform-record-set/acme-3.machine.acme.corp.yaml +++ /dev/null @@ -1,4 +0,0 @@ -fqdn: acme-3.machine.acme.corp -key: acme-3.machine.acme.corp -rrType: A -type: terraform-record-set diff --git a/out/phase-1/terraform-record-set/acme-3.yaml b/out/phase-1/terraform-record-set/acme-3.yaml new file mode 100644 index 0000000..58c202e --- /dev/null +++ b/out/phase-1/terraform-record-set/acme-3.yaml @@ -0,0 +1,6 @@ +fqdn: acme-3.machine.acme.corp +key: acme-3 +rrData: scaleway_instance_ip.acme-3.address +rrType: A +terraformId: acme-3_machine_acme_corp +type: terraform-record-set diff --git a/out/phase-3/terraform-record-set/acme-1.machine.acme.corp.yaml b/out/phase-3/terraform-record-set/acme-1.machine.acme.corp.yaml deleted file mode 100644 index 9537d73..0000000 --- a/out/phase-3/terraform-record-set/acme-1.machine.acme.corp.yaml +++ /dev/null @@ -1,4 +0,0 @@ -fqdn: acme-1.machine.acme.corp -key: acme-1.machine.acme.corp -rrType: A -type: terraform-record-set diff --git a/out/phase-3/terraform-record-set/acme-1.yaml b/out/phase-3/terraform-record-set/acme-1.yaml new file mode 100644 index 0000000..ccf16c7 --- /dev/null +++ b/out/phase-3/terraform-record-set/acme-1.yaml @@ -0,0 +1,6 @@ +fqdn: acme-1.machine.acme.corp +key: acme-1 +rrData: scaleway_instance_ip.acme-1.address +rrType: A +terraformId: acme-1_machine_acme_corp +type: terraform-record-set diff --git a/out/phase-3/terraform-record-set/acme-2.machine.acme.corp.yaml b/out/phase-3/terraform-record-set/acme-2.machine.acme.corp.yaml deleted file mode 100644 index b00bb5c..0000000 --- a/out/phase-3/terraform-record-set/acme-2.machine.acme.corp.yaml +++ /dev/null @@ -1,4 +0,0 @@ -fqdn: acme-2.machine.acme.corp -key: acme-2.machine.acme.corp -rrType: A -type: terraform-record-set diff --git a/out/phase-3/terraform-record-set/acme-2.yaml b/out/phase-3/terraform-record-set/acme-2.yaml new file mode 100644 index 0000000..d95be1f --- /dev/null +++ b/out/phase-3/terraform-record-set/acme-2.yaml @@ -0,0 +1,6 @@ +fqdn: acme-2.machine.acme.corp +key: acme-2 +rrData: scaleway_instance_ip.acme-2.address +rrType: A +terraformId: acme-2_machine_acme_corp +type: terraform-record-set diff --git a/out/phase-3/terraform-record-set/acme-3.machine.acme.corp.yaml b/out/phase-3/terraform-record-set/acme-3.machine.acme.corp.yaml deleted file mode 100644 index 94a3056..0000000 --- a/out/phase-3/terraform-record-set/acme-3.machine.acme.corp.yaml +++ /dev/null @@ -1,4 +0,0 @@ -fqdn: acme-3.machine.acme.corp -key: acme-3.machine.acme.corp -rrType: A -type: terraform-record-set diff --git a/out/phase-3/terraform-record-set/acme-3.yaml b/out/phase-3/terraform-record-set/acme-3.yaml new file mode 100644 index 0000000..58c202e --- /dev/null +++ b/out/phase-3/terraform-record-set/acme-3.yaml @@ -0,0 +1,6 @@ +fqdn: acme-3.machine.acme.corp +key: acme-3 +rrData: scaleway_instance_ip.acme-3.address +rrType: A +terraformId: acme-3_machine_acme_corp +type: terraform-record-set |