diff options
author | Trygve Laugstøl <trygvis@inamo.no> | 2021-01-25 21:29:12 +0100 |
---|---|---|
committer | Trygve Laugstøl <trygvis@inamo.no> | 2021-01-25 21:29:12 +0100 |
commit | 912bc8b903dfa6d438c2469ecdad35c181c71830 (patch) | |
tree | 1e6ee9fdcb2b9d488914b80623d68189867533bb /module/ri-base/src/main/resources | |
parent | 92b957036218c03a20ba35c19a70bdf5e2333fd3 (diff) | |
download | rules-sandbox-912bc8b903dfa6d438c2469ecdad35c181c71830.tar.gz rules-sandbox-912bc8b903dfa6d438c2469ecdad35c181c71830.tar.bz2 rules-sandbox-912bc8b903dfa6d438c2469ecdad35c181c71830.tar.xz rules-sandbox-912bc8b903dfa6d438c2469ecdad35c181c71830.zip |
Improving usability, massive refactoring.
* Moving all Drools code into their own modules. This fixes ri-engine
to acme dependency.
* Now they all inherit from their own parent, should be used by
third party code too.
* Separating acme planning code into its own module.
* Splitting rules code from ri-engine into ri-base. ri-engine is now
a pure launcher for modules.
* Dumping kogito for the most part, but it seems like the planner still
requires that.
Diffstat (limited to 'module/ri-base/src/main/resources')
5 files changed, 179 insertions, 0 deletions
diff --git a/module/ri-base/src/main/resources/META-INF/kmodule.xml b/module/ri-base/src/main/resources/META-INF/kmodule.xml new file mode 100644 index 0000000..79c5e79 --- /dev/null +++ b/module/ri-base/src/main/resources/META-INF/kmodule.xml @@ -0,0 +1,11 @@ +<?xml version="1.0" encoding="UTF-8"?> +<kmodule xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + 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 name="all" packages="io.trygvis.rules.*"/> + <kbase name="engine" packages="io.trygvis.rules.engine"/> + <kbase name="dba" packages="io.trygvis.rules.dba"/> + <kbase name="machine" packages="io.trygvis.rules.machine"/> + <kbase name="terraform" packages="io.trygvis.rules.terraform"/> +</kmodule> diff --git a/module/ri-base/src/main/resources/io/trygvis/rules/dba/dba.drl b/module/ri-base/src/main/resources/io/trygvis/rules/dba/dba.drl new file mode 100644 index 0000000..c31bc57 --- /dev/null +++ b/module/ri-base/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 machine" +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; + + List 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); + + String path = "ansible/dba/" + $cluster.name + ".yml"; + te.template("dba/cluster", path, Map.of( + "cluster", $cluster, + "containers", $containers, + "containersByMachine", containersByMachine + )); +end diff --git a/module/ri-base/src/main/resources/io/trygvis/rules/engine/init.drl b/module/ri-base/src/main/resources/io/trygvis/rules/engine/init.drl new file mode 100644 index 0000000..267cc4a --- /dev/null +++ b/module/ri-base/src/main/resources/io/trygvis/rules/engine/init.drl @@ -0,0 +1,17 @@ +package io.trygvis.rules.engine; + +import java.util.Map +import org.apache.commons.io.FileSystem +import org.apache.commons.io.FileUtils +import java.io.File + +global io.trygvis.rules.engine.TemplateEngine te; + +rule "Clean directories" + agenda-group "init" +when + not(KeyValue(key == "rm-gen")); +then + te.clean(); + insert(new KeyValue("rm-gen", null)); +end diff --git a/module/ri-base/src/main/resources/io/trygvis/rules/machine/machine.drl b/module/ri-base/src/main/resources/io/trygvis/rules/machine/machine.drl new file mode 100644 index 0000000..0250cc6 --- /dev/null +++ b/module/ri-base/src/main/resources/io/trygvis/rules/machine/machine.drl @@ -0,0 +1 @@ +package io.trygvis.rules.machine; diff --git a/module/ri-base/src/main/resources/io/trygvis/rules/terraform/terraform.drl b/module/ri-base/src/main/resources/io/trygvis/rules/terraform/terraform.drl new file mode 100644 index 0000000..5ebd082 --- /dev/null +++ b/module/ri-base/src/main/resources/io/trygvis/rules/terraform/terraform.drl @@ -0,0 +1,81 @@ +package io.trygvis.rules.terraform + +import io.trygvis.rules.dba.Cluster +import io.trygvis.rules.dba.Container +import io.trygvis.rules.machine.Machine +import io.trygvis.rules.dns.DnsEntry +import io.trygvis.rules.dns.DnsEntryTerraformExpression +import java.util.ArrayList; +import java.util.Map; + +global io.trygvis.rules.engine.TemplateEngine te; + +dialect "mvel" + +declare ScalewayMachine + machine : Machine + key : String +end + +declare GoogleManagedZoneTerraformExpression + name : String +end + +rule "Terraform for Machine" +when + $machine: Machine() +then + ScalewayMachine scw = new ScalewayMachine(); + scw.setKey($machine.name); + scw.setMachine($machine); + + insert(scw); +end + +rule "Create DNS entry for Terraform Machine" +when + $machine : Machine(fqdn != null) + not(DnsEntry(fqdn == $machine.fqdn)) +then + DnsEntry a = DnsEntry.a($machine.fqdn); + insert(a); + + String ipv4 = "scaleway_instance_ip.%s.address".formatted($machine.name); + insert(new DnsEntryTerraformExpression(a, $machine.name, ipv4)); +end + +rule "main-scaleway-machine.tf" + agenda-group "generate" +when + $managedZones : ArrayList() from collect(GoogleManagedZoneTerraformExpression()) +then + String path = "terraform/main-scaleway-machine.tf"; + te.template("terraform-main-scaleway-machine", path, Map.of( + "managedZones", $managedZones + )); +end + +rule "TF for TerraformMachine" + agenda-group "generate" +when + $m: Machine() + $scw: ScalewayMachine(machine == $m) +then + String path = "terraform/scaleway-machine-%s.tf".formatted($scw.getKey()); + te.template("terraform-machine", path, Map.of("m", $m, "scw", $scw)); +end + +rule "Terraform for DNS" + agenda-group "generate" +when + $entry: DnsEntry() + $tf : DnsEntryTerraformExpression(entry == $entry) + $managedZone : GoogleManagedZoneTerraformExpression() +then + String path = "terraform/dns-%s.tf".formatted($tf.key); + te.template("terraform-record-set", path, Map.of( + "entry", $entry, + "managedZone", $managedZone, + "tf", $tf) + ); +end |