diff options
Diffstat (limited to 'module')
6 files changed, 53 insertions, 22 deletions
diff --git a/module/acme/src/main/java/io/trygvis/acme/AcmeAppsMain.java b/module/acme/src/main/java/io/trygvis/acme/AcmeAppsMain.java index f21ebc3..caab325 100644 --- a/module/acme/src/main/java/io/trygvis/acme/AcmeAppsMain.java +++ b/module/acme/src/main/java/io/trygvis/acme/AcmeAppsMain.java @@ -2,11 +2,16 @@ package io.trygvis.acme; import io.trygvis.rules.engine.Engine; +import java.io.File; import java.io.IOException; public class AcmeAppsMain { public static void main(String[] args) throws IOException { - try (var engine = new Engine("acme-apps", "acme.yaml")) { + try (var engine = new Engine( + "acme-apps", + "acme.yaml", + new File("acme-apps"), + new String[]{"init", "generate"})) { engine.io.dump("acme/apps", engine.session.getFactHandles()); } } diff --git a/module/acme/src/main/java/io/trygvis/acme/VpnMain.java b/module/acme/src/main/java/io/trygvis/acme/VpnMain.java index d6c8061..1859e2a 100644 --- a/module/acme/src/main/java/io/trygvis/acme/VpnMain.java +++ b/module/acme/src/main/java/io/trygvis/acme/VpnMain.java @@ -6,11 +6,16 @@ import io.trygvis.rules.machine.Machine; import io.trygvis.rules.network.Ipv4Address; import io.trygvis.rules.network.Ipv4Cidr; +import java.io.File; import java.io.IOException; public class VpnMain { public static void main(String[] args) throws IOException { - try (var engine = new Engine("acme-wireguard", "acme.yaml")) { + try (var engine = new Engine( + "acme-wireguard", + "acme.yaml", + new File("acme-wireguard"), + new String[]{"init", "generate"})) { engine.io.dump("acme/wireguard", engine.session.getFactHandles(), (Object o) -> o.getClass().getName().contains("Wg") || o instanceof Machine diff --git a/module/ri-engine/src/main/java/io/trygvis/rules/engine/Engine.java b/module/ri-engine/src/main/java/io/trygvis/rules/engine/Engine.java index f624603..b73e2e8 100644 --- a/module/ri-engine/src/main/java/io/trygvis/rules/engine/Engine.java +++ b/module/ri-engine/src/main/java/io/trygvis/rules/engine/Engine.java @@ -7,36 +7,39 @@ import org.kie.api.event.rule.RuleRuntimeEventListener; import org.kie.api.runtime.KieSession; import java.io.Closeable; +import java.io.File; import java.io.IOException; public class Engine implements Closeable { public final DbIo io; public final KieSession session; - public Engine(String k, String database) throws IOException { + public Engine(String name, String database, File output, String[] agendaGroups) throws IOException { var services = KieServices.Factory.get(); var container = services.getKieClasspathContainer(); - var kieBase = container.getKieBase(k); + var kieBase = container.getKieBase(name); io = new DbIo(kieBase); var objects = io.load(database); - session = container.newKieSession(k); + session = container.newKieSession(name); - session.setGlobal("te", new TemplateEngine()); + var logger = new WorkingMemoryConsoleLogger(session); + session.addEventListener((AgendaEventListener) logger); + session.addEventListener((RuleRuntimeEventListener) logger); + + session.setGlobal("te", new TemplateEngine(output)); for (var object : objects) { System.out.println("object = " + object); session.insert(object); } - var logger = new WorkingMemoryConsoleLogger(session); - session.addEventListener((AgendaEventListener) logger); - session.addEventListener((RuleRuntimeEventListener) logger); - - session.getAgenda().getAgendaGroup("init").setFocus(); - - session.fireAllRules(); + for (var agendaGroup : agendaGroups) { + System.out.println("Setting agenda: " + agendaGroup); + session.getAgenda().getAgendaGroup(agendaGroup).setFocus(); + session.fireAllRules(); + } } @Override diff --git a/module/ri-engine/src/main/java/io/trygvis/rules/engine/TemplateEngine.java b/module/ri-engine/src/main/java/io/trygvis/rules/engine/TemplateEngine.java index ace7aaf..3c7c755 100644 --- a/module/ri-engine/src/main/java/io/trygvis/rules/engine/TemplateEngine.java +++ b/module/ri-engine/src/main/java/io/trygvis/rules/engine/TemplateEngine.java @@ -12,18 +12,16 @@ import java.util.Map; public class TemplateEngine { private final Jinjava jinjava = new Jinjava(); + private final File basedir; + + public TemplateEngine(File basedir) { + this.basedir = basedir; + } + 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); + var f = new File(basedir, output); FileUtil.createMissingParentDirectories(f); Files.writeString(f.toPath(), renderedTemplate); } diff --git a/module/ri-engine/src/main/resources/io/trygvis/rules/terraform/terraform.drl b/module/ri-engine/src/main/resources/io/trygvis/rules/terraform/terraform.drl index 40b9f7c..b3d176b 100644 --- a/module/ri-engine/src/main/resources/io/trygvis/rules/terraform/terraform.drl +++ b/module/ri-engine/src/main/resources/io/trygvis/rules/terraform/terraform.drl @@ -39,6 +39,7 @@ then end rule "TF for TerraformMachine" + agenda-group "generate" when $m: Machine() $scw: ScalewayMachine(machine == $m) @@ -48,6 +49,7 @@ then end rule "Terraform for DNS" + agenda-group "generate" when $entry: DnsEntry() $tf : DnsEntryTerraformExpression(entry == $entry) diff --git a/module/ri-wireguard/src/main/resources/io/trygvis/rules/wireguard/generate.drl b/module/ri-wireguard/src/main/resources/io/trygvis/rules/wireguard/generate.drl new file mode 100644 index 0000000..3ee0583 --- /dev/null +++ b/module/ri-wireguard/src/main/resources/io/trygvis/rules/wireguard/generate.drl @@ -0,0 +1,18 @@ +package io.trygvis.rules.wireguard; + +import java.util.ArrayList +import io.trygvis.rules.dns.DnsEntry; +import io.trygvis.rules.machine.Machine; +import io.trygvis.rules.network.Ipv4Address +import io.trygvis.rules.network.Ipv4Cidr +import io.trygvis.rules.network.IpCalc + +rule "Generate base" + agenda-group "generate" + salience 10 +when + $net : WgNet() + $h : WgHost(net == $net.name) +then + System.out.println("%s : %s".formatted($net.name, $h.name)); +end |