diff options
5 files changed, 342 insertions, 367 deletions
diff --git a/module/ri-engine/src/main/java/io/trygvis/rules/dba/Cluster.java b/module/ri-engine/src/main/java/io/trygvis/rules/dba/Cluster.java index 949d9ae..0b65aaa 100644 --- a/module/ri-engine/src/main/java/io/trygvis/rules/dba/Cluster.java +++ b/module/ri-engine/src/main/java/io/trygvis/rules/dba/Cluster.java @@ -1,5 +1,9 @@ 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; 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 d852115..f6d2ba4 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,8 +1,13 @@ package io.trygvis.rules.dba; +import com.fasterxml.jackson.annotation.JsonIdentityReference; import io.trygvis.rules.machine.Machine; +//@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; @@ -12,6 +17,7 @@ public class Container { private Machine machine; public Container(Cluster cluster, String name, String machineRole, String image, String tag) { + this.id = cluster.name + "-" + name; this.cluster = cluster; this.name = name; this.machineRole = machineRole; 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 3173109..b402173 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 @@ -189,6 +189,9 @@ public class DbIo { return comparator; } + static record DbObject2(String type, Object data) { + } + public void dump(String s, Collection<FactHandle> factHandles, Function<Object, Boolean> filter) throws IOException { var yamlFile = new File("out", s + ".yaml"); @@ -214,18 +217,35 @@ public class DbIo { } } + var objects = new ArrayList<DbObject2>(facts.size()); + for (var e : facts.entrySet()) { + var name = e.getKey().getName(); + + var collection = e.getValue(); + collection.sort(); + for (var fact : collection.values) { + objects.add(new DbObject2(name, fact)); + } + } + + /* + var x = new ArrayList<DbObject2>(); + x.add(new DbObject2("io.trygvis.rules.dba.Container", null)); + x.add(new DbObject2("io.trygvis.rules.machine.Machine", null)); + x.add(new DbObject2("io.trygvis.acme.apps.AcmeMyApp", null)); + + System.out.println("xxxxxx"); + x.sort(new DbObjectComparator()); + x.forEach(System.out::println); + System.out.println("xxxxxx"); + */ + + objects.sort(new DbObjectComparator()); + var factory = mapper.getFactory(); try (var writer = new FileWriter(yamlFile); var g = factory.createGenerator(writer)) { - for (var e : facts.entrySet()) { - var name = e.getKey().getName(); - - var collection = e.getValue(); - collection.sort(); - for (var fact : collection.values) { - g.writeObject(new DbObject(name, mapper.valueToTree(fact))); - } - } + g.writeObject(objects); } } @@ -259,4 +279,43 @@ public class DbIo { } } } + + private static class DbObjectComparator implements Comparator<DbObject2> { + private final List<String> prioritizedPackages = List.of( + "io.trygvis.rules.core", + "io.trygvis.rules.machine", + "io.trygvis.rules.network", + "io.trygvis.rules.dns", + "io.trygvis.rules.dba", + "io.trygvis.rules"); + + @Override + public int compare(DbObject2 a, DbObject2 b) { + var indexA = a.type.lastIndexOf("."); + String packageA = indexA == -1 ? null : a.type.substring(0, indexA); + String classA = indexA == -1 ? a.type : a.type.substring(indexA + 1); + + var indexB = b.type.lastIndexOf("."); + String packageB = indexB == -1 ? null : b.type.substring(0, indexB); + String classB = indexB == -1 ? b.type : b.type.substring(indexB + 1); + + var priIdxA = prioritizedPackages.indexOf(packageA); + var priIdxB = prioritizedPackages.indexOf(packageB); + + if (priIdxA == -1 && priIdxB == -1) { + return classB.compareTo(classA); + } else if (priIdxA == -1) { + return 1; + } else if (priIdxB == -1) { + return -1; + } + return priIdxA - priIdxB; +// var diff = priIdxB - priIdxA; +// if (diff != 0) { +// return diff; +// } +// +// return classB.compareTo(classA); + } + } } diff --git a/module/ri-engine/src/main/java/io/trygvis/rules/machine/Machine.java b/module/ri-engine/src/main/java/io/trygvis/rules/machine/Machine.java index 52721e1..8e54d60 100644 --- a/module/ri-engine/src/main/java/io/trygvis/rules/machine/Machine.java +++ b/module/ri-engine/src/main/java/io/trygvis/rules/machine/Machine.java @@ -1,5 +1,9 @@ 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; public String fqdn; diff --git a/out/acme/apps.yaml b/out/acme/apps.yaml index d6fbe6d..3942ed5 100644 --- a/out/acme/apps.yaml +++ b/out/acme/apps.yaml @@ -1,371 +1,273 @@ --- -type: "io.trygvis.acme.AcmeServer" -data: - name: "acme-1" - machine: - name: "acme-1" +- type: "io.trygvis.rules.machine.Machine" + data: + &acme-1 name: "acme-1" fqdn: "acme-1.machine.acme.com" ---- -type: "io.trygvis.acme.AcmeServer" -data: - name: "acme-2" - machine: - name: "acme-2" - fqdn: "acme-2.machine.acme.com" ---- -type: "io.trygvis.acme.AcmeServer" -data: - name: "acme-3" - machine: - name: "acme-3" - fqdn: "acme-3.machine.acme.com" ---- -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" -data: {} ---- -type: "io.trygvis.rules.dba.Cluster" -data: - name: "acme-myapp-ci" ---- -type: "io.trygvis.rules.dba.Cluster" -data: - name: "acme-myapp-production" ---- -type: "io.trygvis.rules.dba.Cluster" -data: - name: "acme-ops" ---- -type: "io.trygvis.rules.dba.Container" -data: - cluster: - name: "acme-myapp-ci" - name: "4tune-api" - machineRole: "ci-app" - image: "4tune-api" - tag: "development" - machine: - name: "acme-1" - fqdn: "acme-1.machine.acme.com" ---- -type: "io.trygvis.rules.dba.Container" -data: - cluster: - name: "acme-myapp-production" - name: "4tune-api" - machineRole: "production-app" - image: "4tune-api" - tag: "master" - machine: - name: "acme-3" - fqdn: "acme-3.machine.acme.com" ---- -type: "io.trygvis.rules.dba.Container" -data: - cluster: - name: "acme-myapp-ci" - name: "4tune-web" - machineRole: "ci-app" - image: "4tune-web" - tag: "development" - machine: - name: "acme-1" - fqdn: "acme-1.machine.acme.com" ---- -type: "io.trygvis.rules.dba.Container" -data: - cluster: - name: "acme-myapp-production" - name: "4tune-web" - machineRole: "production-app" - image: "4tune-web" - tag: "master" - machine: - name: "acme-3" - fqdn: "acme-3.machine.acme.com" ---- -type: "io.trygvis.rules.dba.Container" -data: - cluster: - name: "acme-myapp-ci" - name: "mdb" - machineRole: "ci-db" - image: "mongodb" - tag: "3.2" - machine: - name: "acme-2" - fqdn: "acme-2.machine.acme.com" ---- -type: "io.trygvis.rules.dba.Container" -data: - cluster: - name: "acme-myapp-production" - name: "mdb" - machineRole: "production-db" - image: "mongodb" - tag: "3.2" - machine: - name: "acme-3" - fqdn: "acme-3.machine.acme.com" ---- -type: "io.trygvis.rules.dba.Container" -data: - cluster: - name: "acme-ops" - name: "n8n" - machineRole: "ops" - image: "n8n" - tag: "0.84.1" - machine: - name: "acme-2" +- type: "io.trygvis.rules.machine.Machine" + data: + &acme-2 name: "acme-2" fqdn: "acme-2.machine.acme.com" ---- -type: "io.trygvis.rules.dba.Container" -data: - cluster: - name: "acme-myapp-ci" - name: "pdb" - machineRole: "ci-db" - image: "postgresql" - tag: "13" - machine: - name: "acme-2" - fqdn: "acme-2.machine.acme.com" ---- -type: "io.trygvis.rules.dba.Container" -data: - cluster: - name: "acme-ops" - name: "pdb" - machineRole: "ops" - image: "postgresql" - tag: "11" - machine: - name: "acme-2" - fqdn: "acme-2.machine.acme.com" ---- -type: "io.trygvis.rules.dba.Container" -data: - cluster: - name: "acme-myapp-production" - name: "pdb" - machineRole: "production-db" - image: "postgresql" - tag: "13" - machine: - name: "acme-3" - fqdn: "acme-3.machine.acme.com" ---- -type: "io.trygvis.rules.dba.Container" -data: - cluster: - name: "acme-myapp-ci" - name: "statera" - machineRole: "ci-app" - image: "statera" - tag: "development" - machine: - name: "acme-1" - fqdn: "acme-1.machine.acme.com" ---- -type: "io.trygvis.rules.dba.Container" -data: - cluster: - name: "acme-myapp-production" - name: "statera" - machineRole: "production-app" - image: "statera" - tag: "master" - machine: - name: "acme-3" - fqdn: "acme-3.machine.acme.com" ---- -type: "io.trygvis.rules.dba.Container" -data: - cluster: - name: "acme-myapp-ci" - name: "statera-console" - machineRole: "ci-app" - image: "statera-console" - tag: "development" - machine: - name: "acme-1" - fqdn: "acme-1.machine.acme.com" ---- -type: "io.trygvis.rules.dba.Container" -data: - cluster: - name: "acme-myapp-production" - name: "statera-console" - machineRole: "production-app" - image: "statera-console" - tag: "master" - machine: - name: "acme-3" +- type: "io.trygvis.rules.machine.Machine" + data: + &acme-3 name: "acme-3" fqdn: "acme-3.machine.acme.com" ---- -type: "io.trygvis.rules.dba.DbaMachineRole" -data: - machine: "acme-1" - roles: - - "ci-app" ---- -type: "io.trygvis.rules.dba.DbaMachineRole" -data: - machine: "acme-2" - roles: - - "ops" - - "ci-db" ---- -type: "io.trygvis.rules.dba.DbaMachineRole" -data: - machine: "acme-3" - roles: - - "production-app" - - "production-db" ---- -type: "io.trygvis.rules.dns.DnsEntry" -data: - fqdn: "acme-1.machine.acme.com" - type: "A" ---- -type: "io.trygvis.rules.dns.DnsEntry" -data: - fqdn: "acme-2.machine.acme.com" - type: "A" ---- -type: "io.trygvis.rules.dns.DnsEntry" -data: - fqdn: "acme-3.machine.acme.com" - type: "A" ---- -type: "io.trygvis.rules.dns.DnsEntryTerraformExpression" -data: - entry: +- type: "io.trygvis.rules.machine.Machine" + data: + &ws-1 name: "ws-1" + fqdn: null +- type: "io.trygvis.rules.machine.Machine" + data: + &ws-2 name: "ws-2" + fqdn: null +- type: "io.trygvis.rules.dns.DnsEntry" + data: fqdn: "acme-1.machine.acme.com" type: "A" - key: "acme-1" - expression: "scaleway_instance_ip.acme-1.address" ---- -type: "io.trygvis.rules.dns.DnsEntryTerraformExpression" -data: - entry: +- type: "io.trygvis.rules.dns.DnsEntry" + data: fqdn: "acme-2.machine.acme.com" type: "A" - key: "acme-2" - expression: "scaleway_instance_ip.acme-2.address" ---- -type: "io.trygvis.rules.dns.DnsEntryTerraformExpression" -data: - entry: +- type: "io.trygvis.rules.dns.DnsEntry" + data: fqdn: "acme-3.machine.acme.com" type: "A" - key: "acme-3" - expression: "scaleway_instance_ip.acme-3.address" ---- -type: "io.trygvis.rules.dns.DnsZone" -data: - name: "ci.acme.com" ---- -type: "io.trygvis.rules.dns.DnsZone" -data: - name: "production.acme.com" ---- -type: "io.trygvis.rules.engine.KeyValue" -data: - key: "rm-gen" - value: null ---- -type: "io.trygvis.rules.machine.Machine" -data: - name: "acme-1" - fqdn: "acme-1.machine.acme.com" ---- -type: "io.trygvis.rules.machine.Machine" -data: - name: "acme-2" - fqdn: "acme-2.machine.acme.com" ---- -type: "io.trygvis.rules.machine.Machine" -data: - name: "acme-3" - fqdn: "acme-3.machine.acme.com" ---- -type: "io.trygvis.rules.machine.Machine" -data: - name: "ws-1" - fqdn: null ---- -type: "io.trygvis.rules.machine.Machine" -data: - name: "ws-2" - fqdn: null ---- -type: "io.trygvis.rules.terraform.GoogleManagedZoneTerraformExpression" -data: - name: "acme_zone" ---- -type: "io.trygvis.rules.terraform.ScalewayMachine" -data: - machine: - name: "acme-1" - fqdn: "acme-1.machine.acme.com" - key: "acme-1" ---- -type: "io.trygvis.rules.terraform.ScalewayMachine" -data: - machine: +- type: "io.trygvis.rules.dns.DnsEntryTerraformExpression" + data: + entry: + fqdn: "acme-1.machine.acme.com" + type: "A" + key: "acme-1" + expression: "scaleway_instance_ip.acme-1.address" +- type: "io.trygvis.rules.dns.DnsEntryTerraformExpression" + data: + entry: + fqdn: "acme-2.machine.acme.com" + type: "A" + key: "acme-2" + expression: "scaleway_instance_ip.acme-2.address" +- type: "io.trygvis.rules.dns.DnsEntryTerraformExpression" + data: + entry: + fqdn: "acme-3.machine.acme.com" + type: "A" + key: "acme-3" + expression: "scaleway_instance_ip.acme-3.address" +- type: "io.trygvis.rules.dns.DnsZone" + data: + name: "ci.acme.com" +- type: "io.trygvis.rules.dns.DnsZone" + data: + name: "production.acme.com" +- type: "io.trygvis.rules.dba.Cluster" + data: + &acme-myapp-ci name: "acme-myapp-ci" +- type: "io.trygvis.rules.dba.Cluster" + data: + &acme-myapp-production name: "acme-myapp-production" +- type: "io.trygvis.rules.dba.Cluster" + data: + &acme-ops name: "acme-ops" +- type: "io.trygvis.rules.dba.Container" + data: + id: "acme-myapp-ci-4tune-api" + cluster: *acme-myapp-ci + name: "4tune-api" + machineRole: "ci-app" + image: "4tune-api" + tag: "development" + machine: *acme-1 +- type: "io.trygvis.rules.dba.Container" + data: + id: "acme-myapp-production-4tune-api" + cluster: *acme-myapp-production + name: "4tune-api" + machineRole: "production-app" + image: "4tune-api" + tag: "master" + machine: *acme-3 +- type: "io.trygvis.rules.dba.Container" + data: + id: "acme-myapp-ci-4tune-web" + cluster: *acme-myapp-ci + name: "4tune-web" + machineRole: "ci-app" + image: "4tune-web" + tag: "development" + machine: *acme-1 +- type: "io.trygvis.rules.dba.Container" + data: + id: "acme-myapp-production-4tune-web" + cluster: *acme-myapp-production + name: "4tune-web" + machineRole: "production-app" + image: "4tune-web" + tag: "master" + machine: *acme-3 +- type: "io.trygvis.rules.dba.Container" + data: + id: "acme-myapp-ci-mdb" + cluster: *acme-myapp-ci + name: "mdb" + machineRole: "ci-db" + image: "mongodb" + tag: "3.2" + machine: *acme-2 +- type: "io.trygvis.rules.dba.Container" + data: + id: "acme-myapp-production-mdb" + cluster: *acme-myapp-production + name: "mdb" + machineRole: "production-db" + image: "mongodb" + tag: "3.2" + machine: *acme-3 +- type: "io.trygvis.rules.dba.Container" + data: + id: "acme-ops-n8n" + cluster: *acme-ops + name: "n8n" + machineRole: "ops" + image: "n8n" + tag: "0.84.1" + machine: *acme-2 +- type: "io.trygvis.rules.dba.Container" + data: + id: "acme-myapp-ci-pdb" + cluster: *acme-myapp-ci + name: "pdb" + machineRole: "ci-db" + image: "postgresql" + tag: "13" + machine: *acme-2 +- type: "io.trygvis.rules.dba.Container" + data: + id: "acme-myapp-production-pdb" + cluster: *acme-myapp-production + name: "pdb" + machineRole: "production-db" + image: "postgresql" + tag: "13" + machine: *acme-3 +- type: "io.trygvis.rules.dba.Container" + data: + id: "acme-ops-pdb" + cluster: *acme-ops + name: "pdb" + machineRole: "ops" + image: "postgresql" + tag: "11" + machine: *acme-2 +- type: "io.trygvis.rules.dba.Container" + data: + id: "acme-myapp-ci-statera" + cluster: *acme-myapp-ci + name: "statera" + machineRole: "ci-app" + image: "statera" + tag: "development" + machine: *acme-1 +- type: "io.trygvis.rules.dba.Container" + data: + id: "acme-myapp-production-statera" + cluster: *acme-myapp-production + name: "statera" + machineRole: "production-app" + image: "statera" + tag: "master" + machine: *acme-3 +- type: "io.trygvis.rules.dba.Container" + data: + id: "acme-myapp-ci-statera-console" + cluster: *acme-myapp-ci + name: "statera-console" + machineRole: "ci-app" + image: "statera-console" + tag: "development" + machine: *acme-1 +- type: "io.trygvis.rules.dba.Container" + data: + id: "acme-myapp-production-statera-console" + cluster: *acme-myapp-production + name: "statera-console" + machineRole: "production-app" + image: "statera-console" + tag: "master" + machine: *acme-3 +- type: "io.trygvis.rules.dba.DbaMachineRole" + data: + machine: "acme-1" + roles: + - "ci-app" +- type: "io.trygvis.rules.dba.DbaMachineRole" + data: + machine: "acme-2" + roles: + - "ops" + - "ci-db" +- type: "io.trygvis.rules.dba.DbaMachineRole" + data: + machine: "acme-3" + roles: + - "production-app" + - "production-db" +- type: "io.trygvis.rules.terraform.ScalewayMachine" + data: + machine: *acme-1 + key: "acme-1" +- type: "io.trygvis.rules.terraform.ScalewayMachine" + data: + machine: *acme-1 + key: "acme-1" +- type: "io.trygvis.rules.terraform.ScalewayMachine" + data: + machine: *acme-2 + key: "acme-2" +- type: "io.trygvis.rules.terraform.ScalewayMachine" + data: + machine: *acme-2 + key: "acme-2" +- type: "io.trygvis.rules.terraform.ScalewayMachine" + data: + machine: *acme-3 + key: "acme-3" +- type: "io.trygvis.rules.terraform.ScalewayMachine" + data: + machine: *acme-3 + key: "acme-3" +- type: "io.trygvis.rules.terraform.ScalewayMachine" + data: + machine: *ws-1 + key: "ws-1" +- type: "io.trygvis.rules.terraform.ScalewayMachine" + data: + machine: *ws-2 + key: "ws-2" +- type: "io.trygvis.rules.engine.KeyValue" + data: + key: "rm-gen" + value: null +- type: "io.trygvis.rules.terraform.GoogleManagedZoneTerraformExpression" + data: + name: "acme_zone" +- type: "io.trygvis.acme.AcmeServer" + data: name: "acme-1" - fqdn: "acme-1.machine.acme.com" - key: "acme-1" ---- -type: "io.trygvis.rules.terraform.ScalewayMachine" -data: - machine: + machine: *acme-1 +- type: "io.trygvis.acme.AcmeServer" + data: name: "acme-2" - fqdn: "acme-2.machine.acme.com" - key: "acme-2" ---- -type: "io.trygvis.rules.terraform.ScalewayMachine" -data: - machine: - name: "acme-2" - fqdn: "acme-2.machine.acme.com" - key: "acme-2" ---- -type: "io.trygvis.rules.terraform.ScalewayMachine" -data: - machine: - name: "acme-3" - fqdn: "acme-3.machine.acme.com" - key: "acme-3" ---- -type: "io.trygvis.rules.terraform.ScalewayMachine" -data: - machine: + machine: *acme-2 +- type: "io.trygvis.acme.AcmeServer" + data: name: "acme-3" - fqdn: "acme-3.machine.acme.com" - key: "acme-3" ---- -type: "io.trygvis.rules.terraform.ScalewayMachine" -data: - machine: - name: "ws-1" - fqdn: null - key: "ws-1" ---- -type: "io.trygvis.rules.terraform.ScalewayMachine" -data: - machine: - name: "ws-2" - fqdn: null - key: "ws-2" + machine: *acme-3 +- type: "io.trygvis.acme.apps.AcmeOps" + data: {} +- type: "io.trygvis.acme.apps.AcmeMyApp" + data: + environment: "ci" + dockerTag: "development" +- type: "io.trygvis.acme.apps.AcmeMyApp" + data: + environment: "production" + dockerTag: "master" |