summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTrygve Laugstøl <trygvis@inamo.no>2021-01-06 16:04:22 +0100
committerTrygve Laugstøl <trygvis@inamo.no>2021-01-06 16:04:22 +0100
commit149cf9efadb1da64b9ee716ac9bee1ac0c0e1329 (patch)
tree4e4de8f953d1d3ae96911eb190c281476f08b363
parente84ef1adb8ff0d3b121ea12b2d3e15fa52eb2f00 (diff)
downloadrules-sandbox-149cf9efadb1da64b9ee716ac9bee1ac0c0e1329.tar.gz
rules-sandbox-149cf9efadb1da64b9ee716ac9bee1ac0c0e1329.tar.bz2
rules-sandbox-149cf9efadb1da64b9ee716ac9bee1ac0c0e1329.tar.xz
rules-sandbox-149cf9efadb1da64b9ee716ac9bee1ac0c0e1329.zip
Using agenda groups to sequence phases.
-rw-r--r--.gitignore1
-rw-r--r--acme-apps/dns/acme-1.tf (renamed from gen/dns/acme-1.tf)0
-rw-r--r--acme-apps/dns/acme-2.tf (renamed from gen/dns/acme-2.tf)0
-rw-r--r--acme-apps/dns/acme-3.tf (renamed from gen/dns/acme-3.tf)0
-rw-r--r--acme-apps/platform/terraform/acme-1.tf (renamed from gen/platform/terraform/acme-1.tf)0
-rw-r--r--acme-apps/platform/terraform/acme-2.tf (renamed from gen/platform/terraform/acme-2.tf)0
-rw-r--r--acme-apps/platform/terraform/acme-3.tf (renamed from gen/platform/terraform/acme-3.tf)0
-rw-r--r--module/acme/src/main/java/io/trygvis/acme/AcmeAppsMain.java7
-rw-r--r--module/acme/src/main/java/io/trygvis/acme/VpnMain.java7
-rw-r--r--module/ri-engine/src/main/java/io/trygvis/rules/engine/Engine.java25
-rw-r--r--module/ri-engine/src/main/java/io/trygvis/rules/engine/TemplateEngine.java16
-rw-r--r--module/ri-engine/src/main/resources/io/trygvis/rules/terraform/terraform.drl2
-rw-r--r--module/ri-wireguard/src/main/resources/io/trygvis/rules/wireguard/generate.drl18
13 files changed, 54 insertions, 22 deletions
diff --git a/.gitignore b/.gitignore
index 55c9d4a..93a2aa4 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,3 +1,4 @@
*.iml
target
*.log
+.idea
diff --git a/gen/dns/acme-1.tf b/acme-apps/dns/acme-1.tf
index 2252410..2252410 100644
--- a/gen/dns/acme-1.tf
+++ b/acme-apps/dns/acme-1.tf
diff --git a/gen/dns/acme-2.tf b/acme-apps/dns/acme-2.tf
index 04b79a2..04b79a2 100644
--- a/gen/dns/acme-2.tf
+++ b/acme-apps/dns/acme-2.tf
diff --git a/gen/dns/acme-3.tf b/acme-apps/dns/acme-3.tf
index 57d9f45..57d9f45 100644
--- a/gen/dns/acme-3.tf
+++ b/acme-apps/dns/acme-3.tf
diff --git a/gen/platform/terraform/acme-1.tf b/acme-apps/platform/terraform/acme-1.tf
index 8100533..8100533 100644
--- a/gen/platform/terraform/acme-1.tf
+++ b/acme-apps/platform/terraform/acme-1.tf
diff --git a/gen/platform/terraform/acme-2.tf b/acme-apps/platform/terraform/acme-2.tf
index 07d5e8b..07d5e8b 100644
--- a/gen/platform/terraform/acme-2.tf
+++ b/acme-apps/platform/terraform/acme-2.tf
diff --git a/gen/platform/terraform/acme-3.tf b/acme-apps/platform/terraform/acme-3.tf
index a2246bc..a2246bc 100644
--- a/gen/platform/terraform/acme-3.tf
+++ b/acme-apps/platform/terraform/acme-3.tf
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