diff options
25 files changed, 374 insertions, 115 deletions
@@ -9,3 +9,17 @@ data: dockerTag: master --- type: io.trygvis.rules.acme.AcmeOps + + +--- +type: io.trygvis.rules.machine.Machine +data: + name: acme-1 +--- +type: io.trygvis.rules.machine.Machine +data: + name: acme-2 +--- +type: io.trygvis.rules.machine.Machine +data: + name: acme-3 diff --git a/gen/dns/acme-1.tf b/gen/dns/acme-1.tf new file mode 100644 index 0000000..2252410 --- /dev/null +++ b/gen/dns/acme-1.tf @@ -0,0 +1,8 @@ +resource "google_dns_record_set" "acme-1" { + name = "acme-1.machine.acme.com" + managed_zone = "" + type = "A" + ttl = 300 + + rrdatas = [var.addresses.scaleway_instance_ip.acme-1.address] +} diff --git a/gen/dns/acme-2.tf b/gen/dns/acme-2.tf new file mode 100644 index 0000000..04b79a2 --- /dev/null +++ b/gen/dns/acme-2.tf @@ -0,0 +1,8 @@ +resource "google_dns_record_set" "acme-2" { + name = "acme-2.machine.acme.com" + managed_zone = "" + type = "A" + ttl = 300 + + rrdatas = [var.addresses.scaleway_instance_ip.acme-2.address] +} diff --git a/gen/dns/acme-3.tf b/gen/dns/acme-3.tf new file mode 100644 index 0000000..57d9f45 --- /dev/null +++ b/gen/dns/acme-3.tf @@ -0,0 +1,8 @@ +resource "google_dns_record_set" "acme-3" { + name = "acme-3.machine.acme.com" + managed_zone = "" + type = "A" + ttl = 300 + + rrdatas = [var.addresses.scaleway_instance_ip.acme-3.address] +} diff --git a/gen/platform/terraform/acme-1.tf b/gen/platform/terraform/acme-1.tf new file mode 100644 index 0000000..8100533 --- /dev/null +++ b/gen/platform/terraform/acme-1.tf @@ -0,0 +1,15 @@ +resource "scaleway_instance_server" "acme-1" { + name = "acme-1" + type = "DEV1-S" + image = "b3042271-d2b1-4f87-b407-aedd3bbd1663" + ip_id = scaleway_instance_ip.acme-1.id + enable_dynamic_ip = false + enable_ipv6 = true +} + +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}" +#} diff --git a/gen/platform/terraform/acme-2.tf b/gen/platform/terraform/acme-2.tf new file mode 100644 index 0000000..07d5e8b --- /dev/null +++ b/gen/platform/terraform/acme-2.tf @@ -0,0 +1,15 @@ +resource "scaleway_instance_server" "acme-2" { + name = "acme-1" + type = "DEV1-S" + image = "b3042271-d2b1-4f87-b407-aedd3bbd1663" + ip_id = scaleway_instance_ip.acme-2.id + enable_dynamic_ip = false + enable_ipv6 = true +} + +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}" +#} diff --git a/gen/platform/terraform/acme-3.tf b/gen/platform/terraform/acme-3.tf new file mode 100644 index 0000000..a2246bc --- /dev/null +++ b/gen/platform/terraform/acme-3.tf @@ -0,0 +1,15 @@ +resource "scaleway_instance_server" "acme-3" { + name = "acme-1" + type = "DEV1-S" + image = "b3042271-d2b1-4f87-b407-aedd3bbd1663" + ip_id = scaleway_instance_ip.acme-3.id + enable_dynamic_ip = false + enable_ipv6 = true +} + +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}" +#} diff --git a/j2/platform-ansible.j2 b/j2/platform-ansible.j2 new file mode 100644 index 0000000..72e3247 --- /dev/null +++ b/j2/platform-ansible.j2 @@ -0,0 +1,6 @@ +# Ansible +- hosts: + - {{ m.key }} + tasks: + - import_role: + name: acme-platform 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 new file mode 100644 index 0000000..95a28eb --- /dev/null +++ b/j2/terraform-machine.j2 @@ -0,0 +1,15 @@ +resource "scaleway_instance_server" "{{ scw.key }}" { + name = "acme-1" + type = "DEV1-S" + image = "b3042271-d2b1-4f87-b407-aedd3bbd1663" + ip_id = scaleway_instance_ip.{{ scw.key }}.id + enable_dynamic_ip = false + enable_ipv6 = true +} + +resource "scaleway_instance_ip" "{{ scw.key }}" {} + +#resource "scaleway_instance_ip_reverse_dns" "{{ scw.key }}" { +# ip_id = scaleway_instance_ip.{{ scw.key }}.id +# reverse = "{{ scw.key }}.${google_dns_managed_zone.machine.dns_name}" +#} diff --git a/j2/terraform-record-set.j2 b/j2/terraform-record-set.j2 new file mode 100644 index 0000000..31bcfab --- /dev/null +++ b/j2/terraform-record-set.j2 @@ -0,0 +1,8 @@ +resource "google_dns_record_set" "{{ tf.key }}" { + name = "{{ entry.fqdn }}" + managed_zone = "{{ tf.terraformManagedZone }}" + type = "{{ entry.type }}" + ttl = 300 + + rrdatas = [var.addresses.{{ tf.expression }}] +} diff --git a/out/phase-1.yaml b/out/phase-1.yaml index 0b8d48b..2d7bbed 100644 --- a/out/phase-1.yaml +++ b/out/phase-1.yaml @@ -20,25 +20,16 @@ data: type: "io.trygvis.rules.dba.Container" data: cluster: - name: "acme-ci" + name: "acme-production" name: "db" - machineRole: "mdb" - image: "mongodb" - tag: "3.2" + machineRole: "pdb" + image: "postgresql" + tag: "13" --- type: "io.trygvis.rules.dba.Container" data: cluster: name: "acme-ci" - name: "app" - machineRole: "4tune-web" - image: "4tune-web" - tag: "development" ---- -type: "io.trygvis.rules.dba.Container" -data: - cluster: - name: "acme-production" name: "db" machineRole: "mdb" image: "mongodb" @@ -47,11 +38,11 @@ data: type: "io.trygvis.rules.dba.Container" data: cluster: - name: "acme-production" + name: "acme-ci" name: "app" - machineRole: "statera" - image: "statera" - tag: "master" + machineRole: "4tune-api" + image: "4tune-api" + tag: "development" --- type: "io.trygvis.rules.dba.Container" data: @@ -65,20 +56,11 @@ data: type: "io.trygvis.rules.dba.Container" data: cluster: - name: "acme-ci" - name: "app" - machineRole: "4tune-api" - image: "4tune-api" - tag: "development" ---- -type: "io.trygvis.rules.dba.Container" -data: - cluster: name: "acme-production" - name: "db" - machineRole: "pdb" - image: "postgresql" - tag: "13" + name: "app" + machineRole: "statera" + image: "statera" + tag: "master" --- type: "io.trygvis.rules.dba.Container" data: @@ -119,128 +101,118 @@ data: type: "io.trygvis.rules.dba.Container" data: cluster: + name: "acme-production" + name: "db" + machineRole: "mdb" + image: "mongodb" + tag: "3.2" +--- +type: "io.trygvis.rules.dba.Container" +data: + cluster: name: "acme-ci" name: "app" - machineRole: "statera-console" - image: "statera-console" + machineRole: "4tune-web" + image: "4tune-web" tag: "development" --- -type: "io.trygvis.rules.dns.DnsEntry" -data: - fqdn: "app.machine.acme.org" - type: "AAAA" ---- -type: "io.trygvis.rules.dns.DnsEntry" -data: - fqdn: "app.machine.acme.org" - type: "A" ---- -type: "io.trygvis.rules.dns.DnsEntry" -data: - fqdn: "app.machine.acme.org" - type: "A" ---- -type: "io.trygvis.rules.dns.DnsEntry" -data: - fqdn: "app.machine.acme.org" - type: "A" ---- -type: "io.trygvis.rules.dns.DnsEntry" -data: - fqdn: "db.machine.acme.org" - type: "A" ---- -type: "io.trygvis.rules.dns.DnsEntry" -data: - fqdn: "app.machine.acme.org" - type: "A" ---- -type: "io.trygvis.rules.dns.DnsEntry" +type: "io.trygvis.rules.dba.Container" data: - fqdn: "db.machine.acme.org" - type: "A" + cluster: + name: "acme-ci" + name: "app" + machineRole: "statera-console" + image: "statera-console" + tag: "development" --- type: "io.trygvis.rules.dns.DnsEntry" data: - fqdn: "app.machine.acme.org" + fqdn: "acme-3.machine.acme.com" type: "A" --- type: "io.trygvis.rules.dns.DnsEntry" data: - fqdn: "db.machine.acme.org" - type: "AAAA" ---- -type: "io.trygvis.rules.dns.DnsEntry" -data: - fqdn: "db.machine.acme.org" + fqdn: "acme-2.machine.acme.com" type: "A" --- type: "io.trygvis.rules.dns.DnsEntry" data: - fqdn: "app.machine.acme.org" + fqdn: "acme-2.machine.acme.com" type: "AAAA" --- type: "io.trygvis.rules.dns.DnsEntry" data: - fqdn: "db.machine.acme.org" + fqdn: "acme-1.machine.acme.com" type: "AAAA" --- type: "io.trygvis.rules.dns.DnsEntry" data: - fqdn: "db.machine.acme.org" + fqdn: "acme-3.machine.acme.com" type: "AAAA" --- type: "io.trygvis.rules.dns.DnsEntry" data: - fqdn: "db.machine.acme.org" + fqdn: "acme-1.machine.acme.com" type: "A" --- -type: "io.trygvis.rules.dns.DnsEntry" +type: "io.trygvis.rules.dns.DnsEntryTerraformExpression" data: - fqdn: "app.machine.acme.org" - type: "AAAA" + entry: + fqdn: "acme-3.machine.acme.com" + type: "A" + key: "acme-3" + expression: "scaleway_instance_ip.acme-3.address" --- -type: "io.trygvis.rules.dns.DnsEntry" +type: "io.trygvis.rules.dns.DnsEntryTerraformExpression" data: - fqdn: "app.machine.acme.org" - type: "AAAA" + entry: + fqdn: "acme-2.machine.acme.com" + type: "A" + key: "acme-2" + expression: "scaleway_instance_ip.acme-2.address" --- -type: "io.trygvis.rules.dns.DnsEntry" +type: "io.trygvis.rules.dns.DnsEntryTerraformExpression" data: - fqdn: "app.machine.acme.org" - type: "AAAA" + entry: + fqdn: "acme-1.machine.acme.com" + type: "A" + key: "acme-1" + expression: "scaleway_instance_ip.acme-1.address" --- -type: "io.trygvis.rules.dns.DnsEntry" +type: "io.trygvis.rules.engine.KeyValue" data: - fqdn: "app.machine.acme.org" - type: "AAAA" + key: "rm-gen" + value: null --- -type: "io.trygvis.rules.dns.DnsEntry" +type: "io.trygvis.rules.machine.Machine" data: - fqdn: "app.machine.acme.org" - type: "A" + name: "acme-2" --- -type: "io.trygvis.rules.dns.DnsEntry" +type: "io.trygvis.rules.machine.Machine" data: - fqdn: "db.machine.acme.org" - type: "AAAA" + name: "acme-3" --- -type: "io.trygvis.rules.dns.DnsEntry" +type: "io.trygvis.rules.machine.Machine" data: - fqdn: "app.machine.acme.org" - type: "A" + name: "acme-1" --- -type: "io.trygvis.rules.dns.DnsEntry" +type: "io.trygvis.rules.terraform.ScalewayMachine" data: - fqdn: "app.machine.acme.org" - type: "AAAA" + machine: + name: "acme-2" + key: "acme-2" + fqdn: "acme-2.machine.acme.com" --- -type: "io.trygvis.rules.dns.DnsEntry" +type: "io.trygvis.rules.terraform.ScalewayMachine" data: - fqdn: "app.machine.acme.org" - type: "A" + machine: + name: "acme-1" + key: "acme-1" + fqdn: "acme-1.machine.acme.com" --- -type: "io.trygvis.rules.dns.DnsEntry" +type: "io.trygvis.rules.terraform.ScalewayMachine" data: - fqdn: "app.machine.acme.org" - type: "AAAA" + machine: + name: "acme-3" + key: "acme-3" + fqdn: "acme-3.machine.acme.com" @@ -103,6 +103,18 @@ <artifactId>jackson-core</artifactId> </dependency> + <dependency> + <groupId>com.hubspot.jinjava</groupId> + <artifactId>jinjava</artifactId> + <version>2.5.6</version> + </dependency> + + <dependency> + <groupId>commons-io</groupId> + <artifactId>commons-io</artifactId> + <version>2.8.0</version> + </dependency> + </dependencies> <build> diff --git a/src/main/java/io/trygvis/rules/dns/DnsEntry.java b/src/main/java/io/trygvis/rules/dns/DnsEntry.java index e7d9f4c..105ef79 100644 --- a/src/main/java/io/trygvis/rules/dns/DnsEntry.java +++ b/src/main/java/io/trygvis/rules/dns/DnsEntry.java @@ -16,4 +16,12 @@ public class DnsEntry { public static DnsEntry aaaa(String fqdn) { return new DnsEntry(fqdn, "AAAA"); } + + public String getFqdn() { + return fqdn; + } + + public String getType() { + return type; + } } diff --git a/src/main/java/io/trygvis/rules/dns/DnsEntryTerraformExpression.java b/src/main/java/io/trygvis/rules/dns/DnsEntryTerraformExpression.java new file mode 100644 index 0000000..79bf934 --- /dev/null +++ b/src/main/java/io/trygvis/rules/dns/DnsEntryTerraformExpression.java @@ -0,0 +1,21 @@ +package io.trygvis.rules.dns; + +public class DnsEntryTerraformExpression { + public DnsEntry entry; + public String key; + public String expression; + + public DnsEntryTerraformExpression(DnsEntry entry, String key, String expression) { + this.entry = entry; + this.key = key; + this.expression = expression; + } + + public String getKey() { + return key; + } + + public String getExpression() { + return expression; + } +} diff --git a/src/main/java/io/trygvis/rules/engine/KeyValue.java b/src/main/java/io/trygvis/rules/engine/KeyValue.java new file mode 100644 index 0000000..5046169 --- /dev/null +++ b/src/main/java/io/trygvis/rules/engine/KeyValue.java @@ -0,0 +1,14 @@ +package io.trygvis.rules.engine; + +public class KeyValue { + public String key; + public String value; + + public KeyValue() { + } + + public KeyValue(String key, String value) { + this.key = key; + this.value = value; + } +} diff --git a/src/main/java/io/trygvis/rules/engine/Main.java b/src/main/java/io/trygvis/rules/engine/Main.java index 3730c28..d469f3a 100644 --- a/src/main/java/io/trygvis/rules/engine/Main.java +++ b/src/main/java/io/trygvis/rules/engine/Main.java @@ -2,7 +2,6 @@ package io.trygvis.rules.engine; import io.trygvis.rules.acme.AcmeIo; import org.drools.core.audit.WorkingMemoryConsoleLogger; -import org.drools.core.common.DefaultFactHandle; import org.kie.api.KieServices; import org.kie.api.event.rule.AgendaEventListener; import org.kie.api.event.rule.RuleRuntimeEventListener; @@ -19,6 +18,8 @@ public class Main { var container = services.getKieClasspathContainer(); var session = container.newKieSession(); + session.setGlobal("te", new TemplateEngine()); + for (var object : objects) { System.out.println("object = " + object); session.insert(object); @@ -27,6 +28,9 @@ public class Main { var logger = new WorkingMemoryConsoleLogger(session); session.addEventListener((AgendaEventListener) logger); session.addEventListener((RuleRuntimeEventListener) logger); + + session.getAgenda().getAgendaGroup("init").setFocus(); + session.fireAllRules(); io.dump("phase-1", session.getFactHandles()); diff --git a/src/main/java/io/trygvis/rules/engine/TemplateEngine.java b/src/main/java/io/trygvis/rules/engine/TemplateEngine.java new file mode 100644 index 0000000..ace7aaf --- /dev/null +++ b/src/main/java/io/trygvis/rules/engine/TemplateEngine.java @@ -0,0 +1,30 @@ +package io.trygvis.rules.engine; + +import ch.qos.logback.core.util.FileUtil; +import com.hubspot.jinjava.Jinjava; + +import java.io.File; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.Map; + +public class TemplateEngine { + private final Jinjava jinjava = new Jinjava(); + + public void template(String name, String output, Map<String, Object> params) throws IOException { + var template = Files.readString(Path.of("j2", name + ".j2")); + String renderedTemplate = jinjava.render(template, params); +// System.out.println("-----------------------------------"); +// for (var item : params.entrySet()) { +// System.out.printf(" %s : %s%n", item.getKey(), item.getValue()); +// } + System.out.println("-----------------------------------"); + System.out.println(renderedTemplate); + System.out.println("-----------------------------------"); + + var f = new File("gen", output); + FileUtil.createMissingParentDirectories(f); + Files.writeString(f.toPath(), renderedTemplate); + } +} diff --git a/src/main/java/io/trygvis/rules/machine/Machine.java b/src/main/java/io/trygvis/rules/machine/Machine.java index b58aeb2..c2a7460 100644 --- a/src/main/java/io/trygvis/rules/machine/Machine.java +++ b/src/main/java/io/trygvis/rules/machine/Machine.java @@ -3,6 +3,9 @@ package io.trygvis.rules.machine; public class Machine { public String name; + public Machine() { + } + public Machine(String name) { this.name = name; } diff --git a/src/main/java/io/trygvis/rules/terraform/Machine.java b/src/main/java/io/trygvis/rules/terraform/Machine.java deleted file mode 100644 index 01bea77..0000000 --- a/src/main/java/io/trygvis/rules/terraform/Machine.java +++ /dev/null @@ -1,4 +0,0 @@ -package io.trygvis.rules.terraform; - -public class Machine { -} diff --git a/src/main/resources/META-INF/kmodule.xml b/src/main/resources/META-INF/kmodule.xml index 911fdc6..ba88a47 100644 --- a/src/main/resources/META-INF/kmodule.xml +++ b/src/main/resources/META-INF/kmodule.xml @@ -3,8 +3,9 @@ xmlns="http://www.drools.org/xsd/kmodule" xsi:schemaLocation="http://www.drools.org/xsd/kmodule https://www.drools.org/xsd/kmodule_7_1.xsd"> - <kbase packages="io.trygvis.rules,io.trygvis.rules.acme,io.trygvis.rules.dba,io.trygvis.rules.machine" + <kbase packages="io.trygvis.rules,io.trygvis.rules.acme,io.trygvis.rules.dba,io.trygvis.rules.engine,io.trygvis.rules.machine,io.trygvis.rules.terraform" default="true"> - <ksession name="Default" default="true"/> + <ksession name="Default" default="true"> + </ksession> </kbase> </kmodule> diff --git a/src/main/resources/io/trygvis/rules/dba/dba.drl b/src/main/resources/io/trygvis/rules/dba/dba.drl index 7beceef..7df1c5d 100644 --- a/src/main/resources/io/trygvis/rules/dba/dba.drl +++ b/src/main/resources/io/trygvis/rules/dba/dba.drl @@ -1 +1,7 @@ package io.trygvis.rules.dba; + + +rule "Dba" + when + then +end diff --git a/src/main/resources/io/trygvis/rules/engine/default.drl b/src/main/resources/io/trygvis/rules/engine/default.drl new file mode 100644 index 0000000..090fddf --- /dev/null +++ b/src/main/resources/io/trygvis/rules/engine/default.drl @@ -0,0 +1,15 @@ +package io.trygvis.rules.engine; +import java.util.Map +import org.apache.commons.io.FileSystem +import org.apache.commons.io.FileUtils +import java.io.File + +rule "Clean directories" + agenda-group "init" +when + not(KeyValue(key == "rm-gen")); +then + System.out.println("Cleaning gen!"); + FileUtils.deleteDirectory(new File("gen")); + insert(new KeyValue("rm-gen", null)); +end diff --git a/src/main/resources/io/trygvis/rules/machine/machine.drl b/src/main/resources/io/trygvis/rules/machine/machine.drl index 45df925..df0d002 100644 --- a/src/main/resources/io/trygvis/rules/machine/machine.drl +++ b/src/main/resources/io/trygvis/rules/machine/machine.drl @@ -9,6 +9,4 @@ rule "New machine" when $container: Container() then - insert(DnsEntry.a($container.name + ".machine.acme.org")); - insert(DnsEntry.aaaa($container.name + ".machine.acme.org")); end diff --git a/src/main/resources/io/trygvis/rules/terraform/terraform.drl b/src/main/resources/io/trygvis/rules/terraform/terraform.drl new file mode 100644 index 0000000..1de3123 --- /dev/null +++ b/src/main/resources/io/trygvis/rules/terraform/terraform.drl @@ -0,0 +1,60 @@ +package io.trygvis.rules.terraform + +import io.trygvis.rules.dba.Cluster +import io.trygvis.rules.dba.Container +import io.trygvis.rules.machine.Machine +import io.trygvis.rules.dns.DnsEntry +import io.trygvis.rules.dns.DnsEntryTerraformExpression +import java.util.Map; + +global io.trygvis.rules.engine.TemplateEngine te; + +declare ScalewayMachine + machine : Machine + key : String + fqdn : String +end + +rule "Terraform for Machine" +when + $machine: Machine() +then + var fqdn = "%s.machine.acme.com".formatted($machine.name); + var a = DnsEntry.a(fqdn); + var aaaa = DnsEntry.aaaa(fqdn); + + var scw = new ScalewayMachine(); + scw.setKey($machine.name); + scw.setMachine($machine); + scw.setFqdn(fqdn); + + var ipv4 = "scaleway_instance_ip.%s.address".formatted($machine.name); +// var ipv6 = "scaleway_instance_server.%s.public_ipv6.address".formatted($machine.name); + + insert(scw); + insert(a); + insert(aaaa); + insert(new DnsEntryTerraformExpression(a, $machine.name, ipv4)); +// insert(new DnsEntryValue(aaaa, ipv6)); +end + +rule "TF for TerraformMachine" +when + $m: Machine() + $scw: ScalewayMachine(machine == $m) +then + var path = "platform/terraform/%s.tf".formatted($scw.getKey()); + te.template("terraform-machine", path, Map.of("m", $m, "scw", $scw)); +end + +rule "Terraform for DNS" +when + $entry: DnsEntry() + $tf : DnsEntryTerraformExpression(entry == $entry) +then + var path = "dns/%s.tf".formatted($tf.key); + te.template("terraform-record-set", path, Map.of( + "entry", $entry, + "tf", $tf) + ); +end |