diff options
Diffstat (limited to 'module/ri-engine/src')
10 files changed, 25 insertions, 110 deletions
diff --git a/module/ri-engine/src/main/java/io/trygvis/rules/acme/AcmeMyApp.java b/module/ri-engine/src/main/java/io/trygvis/rules/acme/AcmeMyApp.java deleted file mode 100644 index a1340e7..0000000 --- a/module/ri-engine/src/main/java/io/trygvis/rules/acme/AcmeMyApp.java +++ /dev/null @@ -1,6 +0,0 @@ -package io.trygvis.rules.acme; - -public class AcmeMyApp { - public String environment; - public String dockerTag; -} diff --git a/module/ri-engine/src/main/java/io/trygvis/rules/acme/AcmeOps.java b/module/ri-engine/src/main/java/io/trygvis/rules/acme/AcmeOps.java deleted file mode 100644 index 147fcfa..0000000 --- a/module/ri-engine/src/main/java/io/trygvis/rules/acme/AcmeOps.java +++ /dev/null @@ -1,4 +0,0 @@ -package io.trygvis.rules.acme; - -public class AcmeOps { -} diff --git a/module/ri-engine/src/main/java/io/trygvis/rules/acme/AcmeIo.java b/module/ri-engine/src/main/java/io/trygvis/rules/engine/DbIo.java index 456195d..b835da5 100644 --- a/module/ri-engine/src/main/java/io/trygvis/rules/acme/AcmeIo.java +++ b/module/ri-engine/src/main/java/io/trygvis/rules/engine/DbIo.java @@ -1,10 +1,9 @@ -package io.trygvis.rules.acme; +package io.trygvis.rules.engine; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.type.TypeFactory; import com.fasterxml.jackson.dataformat.yaml.YAMLFactory; import com.fasterxml.jackson.dataformat.yaml.YAMLGenerator; -import org.apache.commons.collections4.OrderedMap; import org.drools.core.common.DefaultFactHandle; import org.kie.api.KieBase; import org.kie.api.runtime.rule.FactHandle; @@ -16,10 +15,10 @@ import java.util.*; import java.util.function.Function; @SuppressWarnings("unchecked") -public class AcmeIo { +public class DbIo { private final ObjectMapper mapper; - public AcmeIo(KieBase kieBase) { + public DbIo(KieBase kieBase) { var factory = new YAMLFactory(); factory.enable(YAMLGenerator.Feature.USE_NATIVE_TYPE_ID); factory.enable(YAMLGenerator.Feature.USE_NATIVE_OBJECT_ID); @@ -33,10 +32,10 @@ public class AcmeIo { public List<Object> load(String file) throws IOException { var parser = mapper.getFactory().createParser(new File(file)); - var objects = mapper.readValues(parser, AcmeObject.class).readAll(new ArrayList<>()); + var objects = mapper.readValues(parser, DbObject.class).readAll(new ArrayList<>()); List<Object> items = new ArrayList<>(objects.size()); - for (AcmeObject object : objects) { + for (DbObject object : objects) { try { var type = mapper.getTypeFactory().findClass(object.type); var x = mapper.treeToValue(object.data, type); @@ -221,7 +220,7 @@ public class AcmeIo { var collection = e.getValue(); collection.sort(); for (var fact : collection.values) { - g.writeObject(new AcmeObject(name, mapper.valueToTree(fact))); + g.writeObject(new DbObject(name, mapper.valueToTree(fact))); } } } diff --git a/module/ri-engine/src/main/java/io/trygvis/rules/acme/AcmeObject.java b/module/ri-engine/src/main/java/io/trygvis/rules/engine/DbObject.java index a75c4ba..6b9817e 100644 --- a/module/ri-engine/src/main/java/io/trygvis/rules/acme/AcmeObject.java +++ b/module/ri-engine/src/main/java/io/trygvis/rules/engine/DbObject.java @@ -1,15 +1,15 @@ -package io.trygvis.rules.acme; +package io.trygvis.rules.engine; import com.fasterxml.jackson.databind.node.ObjectNode; -public final class AcmeObject { +public final class DbObject { public String type; public ObjectNode data; - public AcmeObject() { + public DbObject() { } - public AcmeObject(String type, ObjectNode data) { + public DbObject(String type, ObjectNode data) { this.type = type; this.data = data; } diff --git a/module/ri-engine/src/main/java/io/trygvis/rules/engine/Main.java b/module/ri-engine/src/main/java/io/trygvis/rules/engine/Engine.java index 7c96be6..2565112 100644 --- a/module/ri-engine/src/main/java/io/trygvis/rules/engine/Main.java +++ b/module/ri-engine/src/main/java/io/trygvis/rules/engine/Engine.java @@ -1,27 +1,27 @@ package io.trygvis.rules.engine; -import io.trygvis.rules.acme.AcmeIo; -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 org.drools.core.audit.WorkingMemoryConsoleLogger; import org.kie.api.KieServices; import org.kie.api.event.rule.AgendaEventListener; import org.kie.api.event.rule.RuleRuntimeEventListener; +import org.kie.api.runtime.KieSession; +import java.io.Closeable; import java.io.IOException; -public class Main { - public static void main(String[] args) throws IOException { +public class Engine implements Closeable { + public final DbIo io; + public final KieSession session; + + public Engine(String database) throws IOException { var services = KieServices.Factory.get(); var container = services.getKieClasspathContainer(); var kieBase = container.getKieBase(); - var io = new AcmeIo(kieBase); - var objects = io.load("acme.yaml"); + io = new DbIo(kieBase); + var objects = io.load(database); - var session = container.newKieSession(); + session = container.newKieSession(); session.setGlobal("te", new TemplateEngine()); @@ -37,17 +37,10 @@ public class Main { session.getAgenda().getAgendaGroup("init").setFocus(); session.fireAllRules(); + } - io.dump("phase-1", session.getFactHandles()); - - io.dump("vpn0", session.getFactHandles(), (Object o) -> - o.getClass().getName().contains("Wg") - || o instanceof Machine - || o instanceof DnsEntry - || o instanceof Ipv4Cidr - || o instanceof Ipv4Address - ); - + @Override + public void close() { session.dispose(); } } diff --git a/module/ri-engine/src/main/resources/META-INF/kmodule.xml b/module/ri-engine/src/main/resources/META-INF/kmodule.xml index 7a46b1a..ffba357 100644 --- a/module/ri-engine/src/main/resources/META-INF/kmodule.xml +++ b/module/ri-engine/src/main/resources/META-INF/kmodule.xml @@ -3,9 +3,6 @@ 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.engine,io.trygvis.rules.machine,io.trygvis.rules.terraform" - default="true" includes="wireguard"> - <ksession name="Default" default="true"> - </ksession> + <kbase name="engine" packages="io.trygvis.rules.dba,io.trygvis.rules.engine,io.trygvis.rules.machine,io.trygvis.rules.terraform"> </kbase> </kmodule> diff --git a/module/ri-engine/src/main/resources/io/trygvis/rules/acme/acme.drl b/module/ri-engine/src/main/resources/io/trygvis/rules/acme/acme.drl deleted file mode 100644 index 7d53763..0000000 --- a/module/ri-engine/src/main/resources/io/trygvis/rules/acme/acme.drl +++ /dev/null @@ -1,58 +0,0 @@ -package io.trygvis.rules.acme; - -import io.trygvis.rules.machine.Machine; -import io.trygvis.rules.dba.Cluster; -import io.trygvis.rules.dba.Container; - -dialect "mvel" - -declare AcmeServer - name : String - machine : Machine -end - -rule "Ops" -when - $ops: AcmeOps() -then - var cluster = new Cluster("acme-ops"); - insert(cluster); - insert(new Container(cluster, "app", "pdb", "postgresql", "11")); - insert(new Container(cluster, "app", "n8n", "n8n", "0.84.1")); -end - -rule "MyApp" -when - $app: AcmeMyApp() -then - var cluster = new Cluster("acme-" + $app.environment); - insert(cluster); - - var tag = $app.dockerTag; - insert(new Container(cluster, "app", "statera", "statera", tag)); - insert(new Container(cluster, "app", "statera-console", "statera-console", tag)); - insert(new Container(cluster, "app", "4tune-web", "4tune-web", tag)); - insert(new Container(cluster, "app", "4tune-api", "4tune-api", tag)); - insert(new Container(cluster, "db", "pdb", "postgresql", "13")); - insert(new Container(cluster, "db", "mdb", "mongodb", "3.2")); -end - -rule "Create Acme servers" -when - $m : Machine(name.startsWith("acme-")) - not(AcmeServer(name == $m.name)) -then - var s = new AcmeServer(); - s.name = $m.name; - s.machine = $m; - insert(s); -end - -rule "Set public domain for ACME servers" -when - $m : Machine(fqdn == null) - $s : AcmeServer(machine == $m) -then - $s.machine.fqdn = "%s.machine.acme.com".formatted($s.machine.name); - update($s.machine) -end diff --git a/module/ri-engine/src/main/resources/io/trygvis/rules/dba/dba.drl b/module/ri-engine/src/main/resources/io/trygvis/rules/dba/dba.drl deleted file mode 100644 index 7beceef..0000000 --- a/module/ri-engine/src/main/resources/io/trygvis/rules/dba/dba.drl +++ /dev/null @@ -1 +0,0 @@ -package io.trygvis.rules.dba; diff --git a/module/ri-engine/src/main/resources/io/trygvis/rules/engine/default.drl b/module/ri-engine/src/main/resources/io/trygvis/rules/engine/default.drl index 090fddf..a933bd9 100644 --- a/module/ri-engine/src/main/resources/io/trygvis/rules/engine/default.drl +++ b/module/ri-engine/src/main/resources/io/trygvis/rules/engine/default.drl @@ -1,4 +1,5 @@ package io.trygvis.rules.engine; + import java.util.Map import org.apache.commons.io.FileSystem import org.apache.commons.io.FileUtils diff --git a/module/ri-engine/src/main/resources/io/trygvis/rules/machine/machine.drl b/module/ri-engine/src/main/resources/io/trygvis/rules/machine/machine.drl deleted file mode 100644 index a9a379f..0000000 --- a/module/ri-engine/src/main/resources/io/trygvis/rules/machine/machine.drl +++ /dev/null @@ -1,6 +0,0 @@ -package io.trygvis.rules.machine; - -import io.trygvis.rules.dba.Cluster; -import io.trygvis.rules.dba.Container; -import io.trygvis.rules.machine.Machine; -import io.trygvis.rules.dns.DnsEntry; |