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 | |
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')
19 files changed, 701 insertions, 0 deletions
diff --git a/module/ri-base/src/main/java/io/trygvis/rules/core/Problem.java b/module/ri-base/src/main/java/io/trygvis/rules/core/Problem.java new file mode 100644 index 0000000..04d1af3 --- /dev/null +++ b/module/ri-base/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-base/src/main/java/io/trygvis/rules/dba/Cluster.java b/module/ri-base/src/main/java/io/trygvis/rules/dba/Cluster.java new file mode 100644 index 0000000..0b65aaa --- /dev/null +++ b/module/ri-base/src/main/java/io/trygvis/rules/dba/Cluster.java @@ -0,0 +1,13 @@ +package io.trygvis.rules.dba; + +import com.fasterxml.jackson.annotation.JsonIdentityInfo; +import com.fasterxml.jackson.annotation.ObjectIdGenerators; + +@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "name") +public class Cluster { + public String name; + + public Cluster(String name) { + this.name = name; + } +} 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 new file mode 100644 index 0000000..3888661 --- /dev/null +++ b/module/ri-base/src/main/java/io/trygvis/rules/dba/Container.java @@ -0,0 +1,59 @@ +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; + + private Machine machine; + public final MachineSpecification machineSpecification; + + public Container(Cluster cluster, String name, String machineRole, String image, String tag, + MachineSpecification machineSpecification) { + this.id = cluster.name + "-" + name; + this.cluster = cluster; + this.name = name; + this.machineRole = machineRole; + this.image = image; + this.tag = tag; + this.machineSpecification = machineSpecification; + } + + 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-base/src/main/java/io/trygvis/rules/dns/DnsEntry.java b/module/ri-base/src/main/java/io/trygvis/rules/dns/DnsEntry.java new file mode 100644 index 0000000..105ef79 --- /dev/null +++ b/module/ri-base/src/main/java/io/trygvis/rules/dns/DnsEntry.java @@ -0,0 +1,27 @@ +package io.trygvis.rules.dns; + +public class DnsEntry { + public String fqdn; + public String type; + + public DnsEntry(String fqdn, String type) { + this.fqdn = fqdn; + this.type = type; + } + + public static DnsEntry a(String fqdn) { + return new DnsEntry(fqdn, "A"); + } + + public static DnsEntry aaaa(String fqdn) { + return new DnsEntry(fqdn, "AAAA"); + } + + public String getFqdn() { + return fqdn; + } + + public String getType() { + return type; + } +} diff --git a/module/ri-base/src/main/java/io/trygvis/rules/dns/DnsEntryTerraformExpression.java b/module/ri-base/src/main/java/io/trygvis/rules/dns/DnsEntryTerraformExpression.java new file mode 100644 index 0000000..79bf934 --- /dev/null +++ b/module/ri-base/src/main/java/io/trygvis/rules/dns/DnsEntryTerraformExpression.java @@ -0,0 +1,21 @@ +package io.trygvis.rules.dns; + +public class DnsEntryTerraformExpression { + public DnsEntry entry; + public String key; + public String expression; + + public DnsEntryTerraformExpression(DnsEntry entry, String key, String expression) { + this.entry = entry; + this.key = key; + this.expression = expression; + } + + public String getKey() { + return key; + } + + public String getExpression() { + return expression; + } +} diff --git a/module/ri-base/src/main/java/io/trygvis/rules/dns/DnsZone.java b/module/ri-base/src/main/java/io/trygvis/rules/dns/DnsZone.java new file mode 100644 index 0000000..1af5c8f --- /dev/null +++ b/module/ri-base/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-base/src/main/java/io/trygvis/rules/engine/KeyValue.java b/module/ri-base/src/main/java/io/trygvis/rules/engine/KeyValue.java new file mode 100644 index 0000000..5046169 --- /dev/null +++ b/module/ri-base/src/main/java/io/trygvis/rules/engine/KeyValue.java @@ -0,0 +1,14 @@ +package io.trygvis.rules.engine; + +public class KeyValue { + public String key; + public String value; + + public KeyValue() { + } + + public KeyValue(String key, String value) { + this.key = key; + this.value = value; + } +} diff --git a/module/ri-base/src/main/java/io/trygvis/rules/engine/TemplateEngine.java b/module/ri-base/src/main/java/io/trygvis/rules/engine/TemplateEngine.java new file mode 100644 index 0000000..eafa6e4 --- /dev/null +++ b/module/ri-base/src/main/java/io/trygvis/rules/engine/TemplateEngine.java @@ -0,0 +1,10 @@ +package io.trygvis.rules.engine; + +import java.io.IOException; +import java.util.Map; + +public interface TemplateEngine { + void clean(); + + void template(String name, String output, Map<String, Object> params) throws IOException; +} diff --git a/module/ri-base/src/main/java/io/trygvis/rules/machine/Machine.java b/module/ri-base/src/main/java/io/trygvis/rules/machine/Machine.java new file mode 100644 index 0000000..8f162c6 --- /dev/null +++ b/module/ri-base/src/main/java/io/trygvis/rules/machine/Machine.java @@ -0,0 +1,29 @@ +package io.trygvis.rules.machine; + +import com.fasterxml.jackson.annotation.JsonIdentityInfo; +import com.fasterxml.jackson.annotation.ObjectIdGenerators; + +@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "name") +public class Machine { + public String name; + private String fqdn; + + public Machine() { + } + + public Machine(String name) { + this.name = name; + } + + public String getName() { + return name; + } + + public String getFqdn() { + return fqdn; + } + + public void setFqdn(String fqdn) { + this.fqdn = fqdn; + } +} diff --git a/module/ri-base/src/main/java/io/trygvis/rules/machine/MachineSpecification.java b/module/ri-base/src/main/java/io/trygvis/rules/machine/MachineSpecification.java new file mode 100644 index 0000000..2e17ae5 --- /dev/null +++ b/module/ri-base/src/main/java/io/trygvis/rules/machine/MachineSpecification.java @@ -0,0 +1,19 @@ +package io.trygvis.rules.machine; + +public class MachineSpecification { + public final int cpu; + public final int memory; + + public MachineSpecification(int cpu, int memory) { + this.cpu = cpu; + this.memory = memory; + } + + public int getCpu() { + return cpu; + } + + public int getMemory() { + return memory; + } +} diff --git a/module/ri-base/src/main/java/io/trygvis/rules/network/IpCalc.java b/module/ri-base/src/main/java/io/trygvis/rules/network/IpCalc.java new file mode 100644 index 0000000..7ec344c --- /dev/null +++ b/module/ri-base/src/main/java/io/trygvis/rules/network/IpCalc.java @@ -0,0 +1,6 @@ +package io.trygvis.rules.network; + +import java.util.regex.Pattern; + +public class IpCalc { +} diff --git a/module/ri-base/src/main/java/io/trygvis/rules/network/Ipv4Address.java b/module/ri-base/src/main/java/io/trygvis/rules/network/Ipv4Address.java new file mode 100644 index 0000000..9021198 --- /dev/null +++ b/module/ri-base/src/main/java/io/trygvis/rules/network/Ipv4Address.java @@ -0,0 +1,58 @@ +package io.trygvis.rules.network; + +import com.fasterxml.jackson.core.JsonGenerator; +import com.fasterxml.jackson.databind.JsonSerializer; +import com.fasterxml.jackson.databind.SerializerProvider; +import com.fasterxml.jackson.databind.annotation.JsonSerialize; + +import java.io.IOException; +import java.util.Objects; + +@JsonSerialize(using = Ipv4Address.Serializer.class) +public class Ipv4Address implements Comparable<Ipv4Address> { + public final int address; + + public Ipv4Address(int address) { + this.address = address; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + + if (o instanceof Ipv4Address) { + Ipv4Address other = (Ipv4Address) o; + return address == other.address; + } + + return false; + } + + @Override + public int hashCode() { + return Objects.hash(address); + } + + @Override + public int compareTo(Ipv4Address o) { + return address - o.address; + } + + @Override + public String toString() { + return "%d.%d.%d.%d".formatted( + address >> 24 & 0xff, + address >> 16 & 0xff, + address >> 8 & 0xff, + address & 0xff); + } + + public static class Serializer extends JsonSerializer<Ipv4Address> { + @Override + public void serialize(Ipv4Address value, JsonGenerator gen, SerializerProvider serializers) throws IOException { + gen.writeStartObject(); + gen.writeObjectField("value", value.toString()); + gen.writeEndObject(); + } + } +} diff --git a/module/ri-base/src/main/java/io/trygvis/rules/network/Ipv4Cidr.java b/module/ri-base/src/main/java/io/trygvis/rules/network/Ipv4Cidr.java new file mode 100644 index 0000000..851af95 --- /dev/null +++ b/module/ri-base/src/main/java/io/trygvis/rules/network/Ipv4Cidr.java @@ -0,0 +1,150 @@ +package io.trygvis.rules.network; + +import com.fasterxml.jackson.core.JsonGenerator; +import com.fasterxml.jackson.databind.JsonSerializer; +import com.fasterxml.jackson.databind.SerializerProvider; +import com.fasterxml.jackson.databind.annotation.JsonSerialize; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; +import java.util.Objects; +import java.util.regex.Pattern; + +@JsonSerialize(using = Ipv4Cidr.Serializer.class) +public class Ipv4Cidr implements Comparable<Ipv4Cidr> { + public final int network; + public final int bits; + + public Ipv4Cidr(int network, int bits) { + if (bits < 0 || bits > 32) { + throw new IllegalArgumentException("bits must be [0, 32]"); + } + + int hostBits = 32 - bits; + int netmask = (-1 >> hostBits) << hostBits; + + int x = network & ~netmask; + + if (x != 0) { + throw new IllegalArgumentException("The host part of the address must be 0."); + } + + this.network = network; + this.bits = bits; + } + + @Override + public String toString() { + return "%d.%d.%d.%d/%d".formatted( + network >> 24 & 0xff, + network >> 16 & 0xff, + network >> 8 & 0xff, + network & 0xff, + bits); + } + + private String formatIpv4(int address) { + return "%d.%d.%d.%d".formatted(address >> 24 & 0xff, address >> 16 & 0xff, address >> 8 & 0xff, address & 0xff); + } + + public List<Ipv4Cidr> partition(int bits) { + if (bits <= 0 || bits <= this.bits || bits > 32) { + throw new IllegalArgumentException("Invalid new network size"); + } + + var list = new ArrayList<Ipv4Cidr>(); + + int count = 1 << (bits - this.bits); + for (int i = 0; i < count; i++) { + var network = this.network | (i << (32 - bits)); + + list.add(new Ipv4Cidr(network, bits)); + } + + return list; + } + + public List<Ipv4Address> addresses() { + int size = 1 << 32 - bits; + var end = network + size; + var addresses = new ArrayList<Ipv4Address>(size); + for (int address = network; address < end; address++) { + addresses.add(new Ipv4Address(address)); + } + + return addresses; + } + + @Override + public int compareTo(Ipv4Cidr o) { + if (this == o) { + return 0; + } + + var ret = network - o.network; + if (ret != 0) { + return ret; + } + + return bits - o.bits; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + Ipv4Cidr ipv4Cidr = (Ipv4Cidr) o; + return network == ipv4Cidr.network && bits == ipv4Cidr.bits; + } + + @Override + public int hashCode() { + return Objects.hash(network, bits); + } + + private static final Pattern pattern = Pattern.compile("([0-9]{1,3})\\.([0-9]{1,3})\\.([0-9]{1,3})\\.([0-9]{1,3})/([0-9]{1,3})"); + + public static Ipv4Cidr parseCidr(String cidr) { + var matcher = pattern.matcher(cidr); + if (!matcher.matches()) { + throw new IllegalArgumentException("Not a CIDR: " + cidr); + } + + var b1 = matcher.group(1); + var b2 = matcher.group(2); + var b3 = matcher.group(3); + var b4 = matcher.group(4); + + int network = parse(b1) << 24 | + parse(b2) << 16 | + parse(b3) << 8 | + parse(b4); + +// System.out.printf("network = %x%n", network); + + var l = matcher.group(5); + var bits = Integer.parseInt(l); +// System.out.printf("netmask = %08x%n", netmask); + + return new Ipv4Cidr(network, bits); + } + + private static int parse(String s) { + var i = Integer.parseInt(s); + if (i > 255) { + throw new IllegalArgumentException("Not a CIDR"); + } + + return i; + } + + public static class Serializer extends JsonSerializer<Ipv4Cidr> { + @Override + public void serialize(Ipv4Cidr value, JsonGenerator gen, SerializerProvider serializers) throws IOException { + gen.writeStartObject(); + gen.writeObjectField("value", value.toString()); + gen.writeEndObject(); + } + } +} diff --git a/module/ri-base/src/main/java/io/trygvis/rules/terraform/TerraformResource.java b/module/ri-base/src/main/java/io/trygvis/rules/terraform/TerraformResource.java new file mode 100644 index 0000000..41bd6ae --- /dev/null +++ b/module/ri-base/src/main/java/io/trygvis/rules/terraform/TerraformResource.java @@ -0,0 +1,96 @@ +package io.trygvis.rules.terraform; + +import java.util.HashMap; +import java.util.Map; + +public class TerraformResource { + private static final String EOL = System.getProperty("line.separator"); + private final String output; + private final String kind; + private final String instance; + private final Map<String, TerraformValue> values = new HashMap<>(); + + public TerraformResource(String output, String kind, String instance) { + this.output = output; + this.kind = kind; + this.instance = instance; + } + + public String getOutput() { + return output; + } + + public void putString(String key, String value) { + values.put(key, new StringTerraformValue(value)); + } + + public String asString() { + var buf = new StringBuilder(); + + buf.append(quote(kind)); + buf.append(" "); + buf.append(quote(instance)); + buf.append(" {"); + buf.append(EOL); + + for (var entry : values.entrySet()) { + buf.append(" "); + buf.append(entry.getKey()); + buf.append(" = "); + buf.append(entry.getValue().asString()); + buf.append(EOL); + } + + buf.append("}"); + buf.append(EOL); + + return buf.toString(); + } + + public interface TerraformValue { + String asString(); + } + + public static class StringTerraformValue implements TerraformValue { + private final String value; + + public StringTerraformValue(String value) { + this.value = value; + } + + @Override + public String asString() { + return quote(value); + } + } + + public static class BooleanTerraformValue implements TerraformValue { + private final boolean value; + + public BooleanTerraformValue(boolean value) { + this.value = value; + } + + @Override + public String asString() { + return String.valueOf(value); + } + } + + public static class ExpressionTerraformValue implements TerraformValue { + private final String value; + + public ExpressionTerraformValue(String value) { + this.value = value; + } + + @Override + public String asString() { + return value; + } + } + + private static String quote(String value) { + return "\"%s\"".formatted(value); + } +} 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 |