summaryrefslogtreecommitdiff
path: root/module/ri-engine/src
diff options
context:
space:
mode:
authorTrygve Laugstøl <trygvis@inamo.no>2021-01-12 20:57:56 +0100
committerTrygve Laugstøl <trygvis@inamo.no>2021-01-12 21:35:09 +0100
commit250cb0fb4fa9787c78b73b44425676cfec3a7944 (patch)
tree5a5c15990b15203d8e6386a9ebeb49de2bf0fea5 /module/ri-engine/src
parent5f294077a362d0127146882e6c90b863c4f54703 (diff)
downloadrules-sandbox-250cb0fb4fa9787c78b73b44425676cfec3a7944.tar.gz
rules-sandbox-250cb0fb4fa9787c78b73b44425676cfec3a7944.tar.bz2
rules-sandbox-250cb0fb4fa9787c78b73b44425676cfec3a7944.tar.xz
rules-sandbox-250cb0fb4fa9787c78b73b44425676cfec3a7944.zip
Starting on generating ansible files for docker-compose setups.
Diffstat (limited to 'module/ri-engine/src')
-rw-r--r--module/ri-engine/src/main/java/io/trygvis/rules/core/Problem.java11
-rw-r--r--module/ri-engine/src/main/java/io/trygvis/rules/dba/Container.java42
-rw-r--r--module/ri-engine/src/main/java/io/trygvis/rules/dns/DnsZone.java9
-rw-r--r--module/ri-engine/src/main/resources/io/trygvis/rules/dba/dba.drl69
4 files changed, 126 insertions, 5 deletions
diff --git a/module/ri-engine/src/main/java/io/trygvis/rules/core/Problem.java b/module/ri-engine/src/main/java/io/trygvis/rules/core/Problem.java
new file mode 100644
index 0000000..04d1af3
--- /dev/null
+++ b/module/ri-engine/src/main/java/io/trygvis/rules/core/Problem.java
@@ -0,0 +1,11 @@
+package io.trygvis.rules.core;
+
+public class Problem {
+ public final String message;
+ public final Object object;
+
+ public Problem(String message, Object object) {
+ this.message = message;
+ this.object = object;
+ }
+}
diff --git a/module/ri-engine/src/main/java/io/trygvis/rules/dba/Container.java b/module/ri-engine/src/main/java/io/trygvis/rules/dba/Container.java
index 6df939d..d852115 100644
--- a/module/ri-engine/src/main/java/io/trygvis/rules/dba/Container.java
+++ b/module/ri-engine/src/main/java/io/trygvis/rules/dba/Container.java
@@ -1,11 +1,15 @@
package io.trygvis.rules.dba;
+import io.trygvis.rules.machine.Machine;
+
public class Container {
- public Cluster cluster;
- public String name;
- public String machineRole;
- public String image;
- public String tag;
+ public final Cluster cluster;
+ public final String name;
+ public final String machineRole;
+ public final String image;
+ public final String tag;
+
+ private Machine machine;
public Container(Cluster cluster, String name, String machineRole, String image, String tag) {
this.cluster = cluster;
@@ -14,4 +18,32 @@ public class Container {
this.image = image;
this.tag = tag;
}
+
+ public Cluster getCluster() {
+ return cluster;
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public String getMachineRole() {
+ return machineRole;
+ }
+
+ public String getImage() {
+ return image;
+ }
+
+ public String getTag() {
+ return tag;
+ }
+
+ public Machine getMachine() {
+ return machine;
+ }
+
+ public void setMachine(Machine machine) {
+ this.machine = machine;
+ }
}
diff --git a/module/ri-engine/src/main/java/io/trygvis/rules/dns/DnsZone.java b/module/ri-engine/src/main/java/io/trygvis/rules/dns/DnsZone.java
new file mode 100644
index 0000000..1af5c8f
--- /dev/null
+++ b/module/ri-engine/src/main/java/io/trygvis/rules/dns/DnsZone.java
@@ -0,0 +1,9 @@
+package io.trygvis.rules.dns;
+
+public class DnsZone {
+ public final String name;
+
+ public DnsZone(String name) {
+ this.name = name;
+ }
+}
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
new file mode 100644
index 0000000..9bdc0a5
--- /dev/null
+++ b/module/ri-engine/src/main/resources/io/trygvis/rules/dba/dba.drl
@@ -0,0 +1,69 @@
+package io.trygvis.rules.dba
+
+import io.trygvis.rules.core.Problem
+import io.trygvis.rules.machine.Machine
+import java.util.ArrayList
+import java.util.Map
+import java.util.HashMap
+import java.util.List
+import java.util.stream.Collectors
+import java.util.Collections
+
+global io.trygvis.rules.engine.TemplateEngine te;
+
+dialect "mvel"
+
+declare DbaMachineRole
+ machine : String
+ roles : String[]
+end
+
+rule "Assign containers to hosts"
+when
+ $machine : Machine()
+ $machineRole : DbaMachineRole(machine == $machine.name)
+ $container : Container(machine == null, $machineRole.roles contains machineRole)
+then
+ System.out.println("Assigning container to machine: " + $machine.name);
+ modify($container) {
+ machine = $machine
+ }
+end
+
+rule "Containers without hosts"
+ agenda-group "generate"
+when
+ $container : Container(machine == null)
+then
+ insert(new Problem("No machine for container", $container))
+end
+
+rule "Generate docker-compose.yaml"
+ agenda-group "generate"
+when
+ $cluster : Cluster()
+ $containers : ArrayList(size > 0) from collect(Container(cluster == $cluster))
+then
+ System.out.println("Docker compose for cluster: " + $cluster.name + " with " + $containers.size() + " containers");
+
+ Map containersByMachine = new HashMap();
+ for (Object o : $containers) {
+ Container c = (Container) o;
+
+ var list = (List) containersByMachine.get(c.getMachine());
+ if (list == null) {
+ list = new ArrayList();
+ containersByMachine.put(c.getMachine(), list);
+ }
+ list.add(c);
+ }
+
+ System.out.println("containersByMachine = " + containersByMachine);
+
+ var path = "ansible/dba/" + $cluster.name + ".yml";
+ te.template("dba/cluster", path, Map.of(
+ "cluster", $cluster,
+ "containers", $containers,
+ "containersByMachine", containersByMachine
+ ));
+end