diff options
author | Trygve Laugstøl <trygvis@inamo.no> | 2021-02-03 16:35:48 +0100 |
---|---|---|
committer | Trygve Laugstøl <trygvis@inamo.no> | 2021-02-03 16:35:48 +0100 |
commit | bafe762ac01d16904c18404283027e426e19bc73 (patch) | |
tree | c22688ad186f5ce635f525704c5035f91b983b68 /example/modules | |
parent | 91abd0b04320a9c9d932df195aca38c1cacfcc5a (diff) | |
download | rules-sandbox-bafe762ac01d16904c18404283027e426e19bc73.tar.gz rules-sandbox-bafe762ac01d16904c18404283027e426e19bc73.tar.bz2 rules-sandbox-bafe762ac01d16904c18404283027e426e19bc73.tar.xz rules-sandbox-bafe762ac01d16904c18404283027e426e19bc73.zip |
Code reorganization.
Moving main code to src, keeping modules in modules/
Diffstat (limited to 'example/modules')
8 files changed, 216 insertions, 0 deletions
diff --git a/example/modules/acme/pom.xml b/example/modules/acme/pom.xml new file mode 100644 index 0000000..01b9ab0 --- /dev/null +++ b/example/modules/acme/pom.xml @@ -0,0 +1,29 @@ +<?xml version="1.0" encoding="UTF-8"?> +<project xmlns="http://maven.apache.org/POM/4.0.0" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + <modelVersion>4.0.0</modelVersion> + + <parent> + <groupId>io.trygvis.rules-sandbox.module</groupId> + <artifactId>ri-module-parent</artifactId> + <version>1.0-SNAPSHOT</version> + <relativePath>../../../modules/ri-module-parent/pom.xml</relativePath> + </parent> + + <artifactId>acme</artifactId> + + <dependencies> + <dependency> + <groupId>${project.groupId}</groupId> + <artifactId>ri-base</artifactId> + <version>${project.version}</version> + </dependency> + <dependency> + <groupId>${project.groupId}</groupId> + <artifactId>ri-wireguard</artifactId> + <version>${project.version}</version> + </dependency> + </dependencies> + +</project> diff --git a/example/modules/acme/src/main/java/io/trygvis/acme/AcmeAppsMain.java b/example/modules/acme/src/main/java/io/trygvis/acme/AcmeAppsMain.java new file mode 100644 index 0000000..c65a3b2 --- /dev/null +++ b/example/modules/acme/src/main/java/io/trygvis/acme/AcmeAppsMain.java @@ -0,0 +1,14 @@ +package io.trygvis.acme; + +public class AcmeAppsMain { +// public static void main(String[] args) throws IOException { +// try (var engine = new Engine( +// "acme-apps", +// new File("acme.yaml"), +// new File("acme-apps"), +// new String[]{"init", "generate"}, +// new File[0])) { +// engine.io.dump(new File("out", "acme/apps.yaml"), engine.session.getFactHandles()); +// } +// } +} diff --git a/example/modules/acme/src/main/java/io/trygvis/acme/VpnMain.java b/example/modules/acme/src/main/java/io/trygvis/acme/VpnMain.java new file mode 100644 index 0000000..cbf962c --- /dev/null +++ b/example/modules/acme/src/main/java/io/trygvis/acme/VpnMain.java @@ -0,0 +1,21 @@ +package io.trygvis.acme; + +public class VpnMain { +// public static void main(String[] args) throws IOException { +// try (var engine = new Engine( +// "acme-wireguard", +// new File("acme.yaml"), +// new File("acme-wireguard"), +// new String[]{"init", "generate"}, +// new File[0])) { +// var outputFile = new File("out", "acme/wireguard.yaml"); +// engine.io.dump(outputFile, engine.session.getFactHandles(), (Object o) -> +// o.getClass().getName().contains("Wg") +// || o instanceof Machine +// || o instanceof DnsEntry +// || o instanceof Ipv4Cidr +// || o instanceof Ipv4Address +// ); +// } +// } +} diff --git a/example/modules/acme/src/main/java/io/trygvis/acme/apps/AcmeMyApp.java b/example/modules/acme/src/main/java/io/trygvis/acme/apps/AcmeMyApp.java new file mode 100644 index 0000000..9371af2 --- /dev/null +++ b/example/modules/acme/src/main/java/io/trygvis/acme/apps/AcmeMyApp.java @@ -0,0 +1,6 @@ +package io.trygvis.acme.apps; + +public class AcmeMyApp { + public String environment; + public String dockerTag; +} diff --git a/example/modules/acme/src/main/java/io/trygvis/acme/apps/AcmeOps.java b/example/modules/acme/src/main/java/io/trygvis/acme/apps/AcmeOps.java new file mode 100644 index 0000000..be9d82b --- /dev/null +++ b/example/modules/acme/src/main/java/io/trygvis/acme/apps/AcmeOps.java @@ -0,0 +1,4 @@ +package io.trygvis.acme.apps; + +public class AcmeOps { +} diff --git a/example/modules/acme/src/main/resources/META-INF/kmodule.xml b/example/modules/acme/src/main/resources/META-INF/kmodule.xml new file mode 100644 index 0000000..da435d6 --- /dev/null +++ b/example/modules/acme/src/main/resources/META-INF/kmodule.xml @@ -0,0 +1,17 @@ +<?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="acme-apps" packages="io.trygvis.acme.apps" includes="dba,engine,machine,terraform, acme-shared"> + <ksession name="acme-apps" default="true"/> + </kbase> + + <kbase name="acme-wireguard" packages="io.trygvis.acme.wireguard" includes="engine,wireguard, acme-shared"> + <ksession name="acme-wireguard" default="true"/> + </kbase> + + <kbase name="acme-shared" packages="io.trygvis.acme"> + <ksession name="acme-shared" default="true"/> + </kbase> +</kmodule> diff --git a/example/modules/acme/src/main/resources/io/trygvis/acme/acme.drl b/example/modules/acme/src/main/resources/io/trygvis/acme/acme.drl new file mode 100644 index 0000000..6a2f33b --- /dev/null +++ b/example/modules/acme/src/main/resources/io/trygvis/acme/acme.drl @@ -0,0 +1,81 @@ +package io.trygvis.acme; + +import io.trygvis.rules.machine.Machine; +import io.trygvis.rules.dba.Cluster; +import io.trygvis.rules.dba.Container +import io.trygvis.rules.terraform.TerraformResource; +import io.trygvis.rules.terraform.TerraformMain +import io.trygvis.rules.terraform.TerraformProvider; + +dialect "mvel" + +declare AcmeServer + name : String + machine : Machine +end + +rule "Create Acme servers" +when + $m : Machine(name.startsWith("acme-")) + not(AcmeServer(name == $m.name)) +then + var s = new AcmeServer(); + s.name = $m.name; + s.machine = $m; + insert(s); +end + +rule "Set public domain for ACME servers" +when + $m : Machine(fqdn == null) + $s : AcmeServer(machine == $m) +then + var fqdn = "%s.machine.acme.com".formatted($m.name); + modify ($m) { + fqdn = fqdn + } +end + +rule "Make Terraform module for machines" +when + not TerraformMain(module == "terraform") +then + var module = new TerraformMain("terraform"); + + var scaleway = new TerraformProvider("scaleway", "2.0.0-rc1"); + module.getProviders().put("scaleway", scaleway); + + insert(module) +end + +rule "Make Scaleway Terraform resources for Machine" +when + $m : Machine() + $s : AcmeServer(machine == $m) +then + var ip = new TerraformResource("terraform/" + $m.name + "-ip.tf", "scaleway_instance_ip", $m.name); + var instance = new TerraformResource("terraform/" + $m.name + "-instance.tf", "scaleway_server_instance", $m.name) + .set("name", "acme-1") + .set("type", "DEV1-S") + .set("image", "b3042271-d2b1-4f87-b407-aedd3bbd1663") + .setExpression("ip_id", ip.name + ".ip") + .set("enable_dynamic_ip", false) + .set("enable_ipv6", true); + + + var rev = new TerraformResource("terraform/" + $m.name + "-ip.tf", "scaleway_instance_reverse_dns", $m.name) + .set("reverse", $m.fqdn + ".") + .setExpression("ip_id", ip.name + ".id"); + var dns = new TerraformResource("terraform/" + $m.name + "-dns.tf", "google_dns_record_set", $m.name) + .set("name", $m.fqdn) + .setExpression("managed_zone", "var.dns_zone") + .set("type", "A") + .set("ttl", 300) + .array("rrdatas") + .addExpression(ip.name + ".id"); + + insert(instance) + insert(ip) + insert(rev) + insert(dns) +end diff --git a/example/modules/acme/src/main/resources/io/trygvis/acme/apps/apps.drl b/example/modules/acme/src/main/resources/io/trygvis/acme/apps/apps.drl new file mode 100644 index 0000000..670079f --- /dev/null +++ b/example/modules/acme/src/main/resources/io/trygvis/acme/apps/apps.drl @@ -0,0 +1,44 @@ +package io.trygvis.acme.apps + +import io.trygvis.rules.machine.Machine; +import io.trygvis.rules.dba.Cluster; +import io.trygvis.rules.dba.Container +import io.trygvis.rules.dns.DnsZone; +import io.trygvis.rules.machine.Machine +import io.trygvis.rules.machine.MachineSpecification +import io.trygvis.rules.dba.Cluster +import io.trygvis.rules.dba.Container + +dialect "mvel" + +rule "Ops" +when + $ops: AcmeOps() +then + var cluster = new Cluster("acme-ops"); + insert(cluster); + insert(new Container(cluster, "pdb", "ops", "postgresql", "11", null)); + insert(new Container(cluster, "n8n", "ops", "n8n", "0.84.1", null)); +end + +rule "MyApp" +when + $app: AcmeMyApp() +then + var zone = new DnsZone($app.environment + ".acme.com"); + insert(zone) + + var cluster = new Cluster("acme-myapp-" + $app.environment); + insert(cluster); + + var app = $app.environment + "-app"; + var db = $app.environment + "-db"; + + var tag = $app.dockerTag; + insert(new Container(cluster, "statera", app, "statera", tag, new MachineSpecification(200, 1000))); + insert(new Container(cluster, "statera-console", app, "statera-console", tag, new MachineSpecification(100, 50))); + insert(new Container(cluster, "4tune-web", app, "4tune-web", tag, new MachineSpecification(100, 50))); + insert(new Container(cluster, "4tune-api", app, "4tune-api", tag, new MachineSpecification(200, 200))); + insert(new Container(cluster, "pdb", db, "postgresql", "13", new MachineSpecification(500, 500))); + insert(new Container(cluster, "mdb", db, "mongodb", "3.2", new MachineSpecification(500, 200))); +end |