From bb92e6eaa9bca36d9f1e356a2f578d22c98af22f Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Fri, 29 Jan 2021 10:14:38 +0100 Subject: IO: Making input and output files have the same format. --- .run/AcmeAppsMain.run.xml | 15 --- .run/AcmeAppsTestMain.run.xml | 16 +++ .run/AcmeWireguardTestMain.run.xml | 16 +++ .run/VpnMain.run.xml | 15 --- acme.yaml | 139 +++++++++++---------- .../main/java/io/trygvis/rules/dba/Cluster.java | 2 + .../main/java/io/trygvis/rules/dba/Container.java | 20 +-- .../main/java/io/trygvis/rules/engine/DbIo.java | 5 +- .../main/java/io/trygvis/rules/engine/Engine.java | 6 +- .../io/trygvis/rules/engine/cli/RunCommand.java | 2 + .../io/trygvis/rules/wireguard/wireguard.drl | 16 ++- out/acme/wireguard.yaml | 17 --- 12 files changed, 138 insertions(+), 131 deletions(-) delete mode 100644 .run/AcmeAppsMain.run.xml create mode 100644 .run/AcmeAppsTestMain.run.xml create mode 100644 .run/AcmeWireguardTestMain.run.xml delete mode 100644 .run/VpnMain.run.xml diff --git a/.run/AcmeAppsMain.run.xml b/.run/AcmeAppsMain.run.xml deleted file mode 100644 index ea2e29b..0000000 --- a/.run/AcmeAppsMain.run.xml +++ /dev/null @@ -1,15 +0,0 @@ - - - - \ No newline at end of file diff --git a/.run/AcmeAppsTestMain.run.xml b/.run/AcmeAppsTestMain.run.xml new file mode 100644 index 0000000..e491ce8 --- /dev/null +++ b/.run/AcmeAppsTestMain.run.xml @@ -0,0 +1,16 @@ + + + + \ No newline at end of file diff --git a/.run/AcmeWireguardTestMain.run.xml b/.run/AcmeWireguardTestMain.run.xml new file mode 100644 index 0000000..c2fbd12 --- /dev/null +++ b/.run/AcmeWireguardTestMain.run.xml @@ -0,0 +1,16 @@ + + + + \ No newline at end of file diff --git a/.run/VpnMain.run.xml b/.run/VpnMain.run.xml deleted file mode 100644 index b9aa7db..0000000 --- a/.run/VpnMain.run.xml +++ /dev/null @@ -1,15 +0,0 @@ - - - - \ No newline at end of file diff --git a/acme.yaml b/acme.yaml index 711720e..2b4d2e3 100644 --- a/acme.yaml +++ b/acme.yaml @@ -1,76 +1,77 @@ -type: io.trygvis.acme.apps.AcmeMyApp -data: - environment: ci - dockerTag: development ---- -type: io.trygvis.acme.apps.AcmeMyApp -data: - environment: production - dockerTag: master ---- -type: io.trygvis.acme.apps.AcmeOps +- type: io.trygvis.acme.apps.AcmeMyApp + data: + environment: ci + dockerTag: development +- type: io.trygvis.acme.apps.AcmeMyApp + data: + environment: production + dockerTag: master ---- # DNS configuration -type: io.trygvis.rules.terraform.GoogleManagedZoneTerraformExpression -data: - name: "acme_zone" +- type: io.trygvis.acme.apps.AcmeOps + + + # DNS configuration +- type: io.trygvis.rules.terraform.GoogleManagedZoneTerraformExpression + data: + name: "acme_zone" + ---- -type: io.trygvis.rules.machine.Machine -data: - name: acme-1 ---- -type: io.trygvis.rules.dba.DbaMachineRole -data: - machine: acme-1 - roles: - - ci-app +- type: io.trygvis.rules.machine.Machine + data: + name: acme-1 ---- -type: io.trygvis.rules.machine.MachineSpecificaiton -data: - mCpu: 2000 - memory: 2000 ---- -type: io.trygvis.rules.machine.Machine -data: - name: acme-2 ---- -type: io.trygvis.rules.dba.DbaMachineRole -data: - machine: acme-2 - roles: - - ops - - ci-db # This also runs the DB for the non-production environments to keep the other machines stateless +- type: io.trygvis.rules.dba.DbaMachineRole + data: + machine: acme-1 + roles: + - ci-app + ---- -type: io.trygvis.rules.machine.Machine -data: - name: acme-3 ---- -type: io.trygvis.rules.dba.DbaMachineRole -data: - machine: acme-3 - roles: - - production-app - - production-db +- type: io.trygvis.rules.machine.MachineSpecificaiton + data: + mCpu: 2000 + memory: 2000 -# Workstations ---- -type: io.trygvis.rules.machine.Machine -data: - name: ws-1 ---- -type: io.trygvis.rules.machine.Machine -data: - name: ws-2 +- type: io.trygvis.rules.machine.Machine + data: + name: acme-2 ---- # Wireguard VPN network -type: io.trygvis.rules.wireguard.WgNet -data: - name: vpn0 - domain: vpn.acme.com - linkCidr: 192.168.10.0/29 - networkCidr: 10.55.0.0/16 - networkBits: 24 +- type: io.trygvis.rules.dba.DbaMachineRole + data: + machine: acme-2 + roles: + - ops + - ci-db # This also runs the DB for the non-production environments to keep the other machines stateless + + +- type: io.trygvis.rules.machine.Machine + data: + name: acme-3 + +- type: io.trygvis.rules.dba.DbaMachineRole + data: + machine: acme-3 + roles: + - production-app + - production-db + + # Workstations + +- type: io.trygvis.rules.machine.Machine + data: + name: ws-1 + +- type: io.trygvis.rules.machine.Machine + data: + name: ws-2 + + # Wireguard VPN network +- type: io.trygvis.rules.wireguard.WgNet + data: + name: vpn0 + domain: vpn.acme.com + linkCidr: 192.168.10.0/29 + networkCidr: 10.55.0.0/16 + networkBits: 24 + \ No newline at end of file diff --git a/module/ri-base/src/main/java/io/trygvis/rules/dba/Cluster.java b/module/ri-base/src/main/java/io/trygvis/rules/dba/Cluster.java index 0b65aaa..6b23cdd 100644 --- a/module/ri-base/src/main/java/io/trygvis/rules/dba/Cluster.java +++ b/module/ri-base/src/main/java/io/trygvis/rules/dba/Cluster.java @@ -10,4 +10,6 @@ public class Cluster { public Cluster(String name) { this.name = name; } + + protected Cluster() {} } diff --git a/module/ri-base/src/main/java/io/trygvis/rules/dba/Container.java b/module/ri-base/src/main/java/io/trygvis/rules/dba/Container.java index 3888661..a420671 100644 --- a/module/ri-base/src/main/java/io/trygvis/rules/dba/Container.java +++ b/module/ri-base/src/main/java/io/trygvis/rules/dba/Container.java @@ -1,22 +1,19 @@ package io.trygvis.rules.dba; -import com.fasterxml.jackson.annotation.JsonIdentityReference; import io.trygvis.rules.machine.Machine; import io.trygvis.rules.machine.MachineSpecification; //@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id") public class Container { - public final String id; - -// @JsonIdentityReference(alwaysAsId = true) - public final Cluster cluster; - public final String name; - public final String machineRole; - public final String image; - public final String tag; + public String id; + public Cluster cluster; + public String name; + public String machineRole; + public String image; + public String tag; private Machine machine; - public final MachineSpecification machineSpecification; + public MachineSpecification machineSpecification; public Container(Cluster cluster, String name, String machineRole, String image, String tag, MachineSpecification machineSpecification) { @@ -29,6 +26,9 @@ public class Container { this.machineSpecification = machineSpecification; } + protected Container() { + } + public Cluster getCluster() { return cluster; } diff --git a/module/ri-engine/src/main/java/io/trygvis/rules/engine/DbIo.java b/module/ri-engine/src/main/java/io/trygvis/rules/engine/DbIo.java index e2abd5a..7dc24ad 100644 --- a/module/ri-engine/src/main/java/io/trygvis/rules/engine/DbIo.java +++ b/module/ri-engine/src/main/java/io/trygvis/rules/engine/DbIo.java @@ -2,6 +2,7 @@ package io.trygvis.rules.engine; import ch.qos.logback.core.util.FileUtil; import com.fasterxml.jackson.annotation.ObjectIdGenerators; +import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.PropertyName; import com.fasterxml.jackson.databind.SerializationFeature; @@ -72,9 +73,9 @@ public class DbIo { public List load(File file) throws IOException { var parser = mapper.getFactory().createParser(file); - var objects = mapper.readValues(parser, DbObject.class).readAll(new ArrayList<>()); + var objects = parser.>readValueAs(new TypeReference>() {}); - List items = new ArrayList<>(objects.size()); + var items = new ArrayList<>(); for (DbObject object : objects) { try { var type = mapper.getTypeFactory().findClass(object.type); 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 fbb79aa..ef6b4b1 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 @@ -86,8 +86,10 @@ public class Engine implements Closeable { session.addEventListener((RuleRuntimeEventListener) l); var te = session.getGlobals().get("te"); - if (te != null) { + try { session.setGlobal("te", new JinjavaTemplateEngine(templateLoader, output)); + } catch (java.lang.RuntimeException ignore) { + // This happens if the rules doesn't need the template engine. } logger.info("Loading data"); @@ -102,7 +104,7 @@ public class Engine implements Closeable { return; } - logger.info("Loaded {} objects from {}", objects.size(), objects.size()); + logger.info("Loaded {} objects from {}", objects.size(), database); allObjects.addAll(objects); } logger.info("Loaded {} objects", allObjects.size()); diff --git a/module/ri-engine/src/main/java/io/trygvis/rules/engine/cli/RunCommand.java b/module/ri-engine/src/main/java/io/trygvis/rules/engine/cli/RunCommand.java index f61acbe..1f3e5ed 100644 --- a/module/ri-engine/src/main/java/io/trygvis/rules/engine/cli/RunCommand.java +++ b/module/ri-engine/src/main/java/io/trygvis/rules/engine/cli/RunCommand.java @@ -64,6 +64,8 @@ public class RunCommand implements Callable { } else if (i.endsWith("*")) { i = i.substring(0, i.length() - 2); ok = name.startsWith(i) || simpleName.startsWith(i); + } else { + ok = name.equals(i) || simpleName.equals(i); } if (ok) { diff --git a/module/ri-wireguard/src/main/resources/io/trygvis/rules/wireguard/wireguard.drl b/module/ri-wireguard/src/main/resources/io/trygvis/rules/wireguard/wireguard.drl index d971696..97d17cb 100644 --- a/module/ri-wireguard/src/main/resources/io/trygvis/rules/wireguard/wireguard.drl +++ b/module/ri-wireguard/src/main/resources/io/trygvis/rules/wireguard/wireguard.drl @@ -35,6 +35,7 @@ declare WgHost end declare WgConnection + name : String host : WgHost to : WgHost end @@ -93,7 +94,20 @@ when $other : WgHost(publicName != null, this != $h) then System.out.printf("VPN connection from %s to %s%n", $h.machine.name, $other.machine.name); - insert(new WgConnection($h, $other)) + var c = new WgConnection(); + c.host = $h; + c.to = $other; + insert(c) +end + +rule "Name connections" +when + $c : WgConnection(name == null, host != null, to != null) +then + String n = $c.host.machine.name + "_x_" + $c.to.machine.name; + modify($c) { + name = n + } end // This and the next rule needs to use .toString(), the specific objects might be generated multiple times, diff --git a/out/acme/wireguard.yaml b/out/acme/wireguard.yaml index c71536b..522d9ee 100644 --- a/out/acme/wireguard.yaml +++ b/out/acme/wireguard.yaml @@ -39,23 +39,6 @@ data: fqdn: "ws-2.vpn.acme.com" type: "A" -- type: "io.trygvis.rules.dba.DbaMachineRole" - data: - &1 machine: "acme-1" - roles: - - "ci-app" -- type: "io.trygvis.rules.dba.DbaMachineRole" - data: - &2 machine: "acme-2" - roles: - - "ops" - - "ci-db" -- type: "io.trygvis.rules.dba.DbaMachineRole" - data: - &3 machine: "acme-3" - roles: - - "production-app" - - "production-db" - type: "io.trygvis.rules.wireguard.WgNet" data: &vpn0 name: "vpn0" -- cgit v1.2.3