summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTrygve Laugstøl <trygvis@inamo.no>2021-01-29 10:14:38 +0100
committerTrygve Laugstøl <trygvis@inamo.no>2021-01-29 10:14:38 +0100
commitbb92e6eaa9bca36d9f1e356a2f578d22c98af22f (patch)
tree6507fa866d5a4ee0f45500e8143c12638aafc07a
parentd167fc9b40a83187c657f3a048f19ac56f529af3 (diff)
downloadrules-sandbox-bb92e6eaa9bca36d9f1e356a2f578d22c98af22f.tar.gz
rules-sandbox-bb92e6eaa9bca36d9f1e356a2f578d22c98af22f.tar.bz2
rules-sandbox-bb92e6eaa9bca36d9f1e356a2f578d22c98af22f.tar.xz
rules-sandbox-bb92e6eaa9bca36d9f1e356a2f578d22c98af22f.zip
IO: Making input and output files have the same format.
-rw-r--r--.run/AcmeAppsMain.run.xml15
-rw-r--r--.run/AcmeAppsTestMain.run.xml16
-rw-r--r--.run/AcmeWireguardTestMain.run.xml16
-rw-r--r--.run/VpnMain.run.xml15
-rw-r--r--acme.yaml139
-rw-r--r--module/ri-base/src/main/java/io/trygvis/rules/dba/Cluster.java2
-rw-r--r--module/ri-base/src/main/java/io/trygvis/rules/dba/Container.java20
-rw-r--r--module/ri-engine/src/main/java/io/trygvis/rules/engine/DbIo.java5
-rw-r--r--module/ri-engine/src/main/java/io/trygvis/rules/engine/Engine.java6
-rw-r--r--module/ri-engine/src/main/java/io/trygvis/rules/engine/cli/RunCommand.java2
-rw-r--r--module/ri-wireguard/src/main/resources/io/trygvis/rules/wireguard/wireguard.drl16
-rw-r--r--out/acme/wireguard.yaml17
12 files changed, 138 insertions, 131 deletions
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 @@
-<component name="ProjectRunConfigurationManager">
- <configuration default="false" name="AcmeAppsMain" type="Application" factoryName="Application" nameIsGenerated="true">
- <option name="MAIN_CLASS_NAME" value="io.trygvis.acme.AcmeAppsMain" />
- <module name="acme" />
- <extension name="coverage">
- <pattern>
- <option name="PATTERN" value="io.trygvis.acme.*" />
- <option name="ENABLED" value="true" />
- </pattern>
- </extension>
- <method v="2">
- <option name="Make" enabled="true" />
- </method>
- </configuration>
-</component> \ 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 @@
+<component name="ProjectRunConfigurationManager">
+ <configuration default="false" name="AcmeAppsTestMain" type="Application" factoryName="Application" nameIsGenerated="true">
+ <option name="MAIN_CLASS_NAME" value="io.trygvis.rules.engine.AcmeAppsTestMain" />
+ <module name="ri-engine" />
+ <option name="VM_PARAMETERS" value="--add-opens java.base/java.lang=ALL-UNNAMED" />
+ <extension name="coverage">
+ <pattern>
+ <option name="PATTERN" value="io.trygvis.rules.engine.*" />
+ <option name="ENABLED" value="true" />
+ </pattern>
+ </extension>
+ <method v="2">
+ <option name="Make" enabled="true" />
+ </method>
+ </configuration>
+</component> \ 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 @@
+<component name="ProjectRunConfigurationManager">
+ <configuration default="false" name="AcmeWireguardTestMain" type="Application" factoryName="Application" nameIsGenerated="true">
+ <option name="MAIN_CLASS_NAME" value="io.trygvis.rules.engine.AcmeWireguardTestMain" />
+ <module name="ri-engine" />
+ <option name="VM_PARAMETERS" value="--add-opens java.base/java.lang=ALL-UNNAMED" />
+ <extension name="coverage">
+ <pattern>
+ <option name="PATTERN" value="io.trygvis.rules.engine.*" />
+ <option name="ENABLED" value="true" />
+ </pattern>
+ </extension>
+ <method v="2">
+ <option name="Make" enabled="true" />
+ </method>
+ </configuration>
+</component> \ 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 @@
-<component name="ProjectRunConfigurationManager">
- <configuration default="false" name="VpnMain" type="Application" factoryName="Application" nameIsGenerated="true">
- <option name="MAIN_CLASS_NAME" value="io.trygvis.acme.VpnMain" />
- <module name="acme" />
- <extension name="coverage">
- <pattern>
- <option name="PATTERN" value="io.trygvis.acme.*" />
- <option name="ENABLED" value="true" />
- </pattern>
- </extension>
- <method v="2">
- <option name="Make" enabled="true" />
- </method>
- </configuration>
-</component> \ 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<Object> load(File file) throws IOException {
var parser = mapper.getFactory().createParser(file);
- var objects = mapper.readValues(parser, DbObject.class).readAll(new ArrayList<>());
+ var objects = parser.<List<DbObject>>readValueAs(new TypeReference<List<DbObject>>() {});
- List<Object> 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<Integer> {
} 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"