diff options
Diffstat (limited to 'src/main')
12 files changed, 165 insertions, 9 deletions
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 |