diff options
Diffstat (limited to 'module/ri-engine')
26 files changed, 353 insertions, 708 deletions
diff --git a/module/ri-engine/classpath.txt b/module/ri-engine/classpath.txt index 7d0804a..580eec1 100644 --- a/module/ri-engine/classpath.txt +++ b/module/ri-engine/classpath.txt @@ -1,4 +1,5 @@ io.trygvis.rules-sandbox:ri-engine:1.0-SNAPSHOT:jar +aopalliance:aopalliance:1.0:jar ch.obermuhlner:big-math:2.0.0:jar ch.qos.logback:logback-classic:1.2.3:jar ch.qos.logback:logback-core:1.2.3:jar @@ -6,50 +7,94 @@ com.fasterxml.jackson.core:jackson-annotations:2.12.0:jar com.fasterxml.jackson.core:jackson-core:2.12.0:jar com.fasterxml.jackson.core:jackson-databind:2.12.0:jar com.fasterxml.jackson.dataformat:jackson-dataformat-yaml:2.12.0:jar +com.github.virtuald:curvesapi:1.06:jar com.google.code.findbugs:annotations:3.0.1:jar com.google.errorprone:error_prone_annotations:2.1.3:jar com.google.guava:guava:25.0-jre:jar +com.google.inject:guice:4.0:jar com.google.j2objc:j2objc-annotations:1.1:jar +com.google.protobuf:protobuf-java:3.6.1:jar com.google.re2j:re2j:1.2:jar com.googlecode.java-ipv6:java-ipv6:0.17:jar com.hubspot.jinjava:jinjava:2.5.6:jar com.thoughtworks.xstream:xstream:1.4.14:jar +com.zaxxer:SparseBitSet:1.2:jar commons-codec:commons-codec:1.14:jar commons-io:commons-io:2.8.0:jar commons-net:commons-net:3.3:jar info.picocli:picocli:4.6.1:jar -io.trygvis.rules-sandbox:ri-wireguard:1.0-SNAPSHOT:jar +javax.annotation:jsr250-api:1.0:jar +javax.enterprise:cdi-api:1.0:jar +javax.inject:javax.inject:1:jar org.antlr:antlr-runtime:3.5.2:jar +org.apache.ant:ant:1.9.15:jar +org.apache.ant:ant-launcher:1.9.15:jar +org.apache.commons:commons-collections4:4.4:jar +org.apache.commons:commons-compress:1.19:jar org.apache.commons:commons-lang3:3.10:jar +org.apache.commons:commons-math3:3.6.1:jar +org.apache.httpcomponents:httpclient:4.5.12:jar +org.apache.httpcomponents:httpcore:4.4.13:jar +org.apache.maven:maven-aether-provider:3.3.9:jar +org.apache.maven:maven-artifact:3.3.9:jar +org.apache.maven:maven-builder-support:3.3.9:jar +org.apache.maven:maven-compat:3.3.9:jar +org.apache.maven:maven-core:3.3.9:jar +org.apache.maven:maven-model:3.3.9:jar +org.apache.maven:maven-model-builder:3.3.9:jar +org.apache.maven:maven-plugin-api:3.3.9:jar +org.apache.maven:maven-repository-metadata:3.3.9:jar +org.apache.maven:maven-settings:3.3.9:jar +org.apache.maven:maven-settings-builder:3.3.9:jar +org.apache.maven.wagon:wagon-http:3.0.0:jar +org.apache.maven.wagon:wagon-http-shared:3.0.0:jar +org.apache.maven.wagon:wagon-provider-api:3.0.0:jar +org.apache.poi:poi:4.1.2:jar +org.apache.poi:poi-ooxml:4.1.2:jar +org.apache.poi:poi-ooxml-schemas:4.1.2:jar +org.apache.xmlbeans:xmlbeans:3.1.0:jar org.checkerframework:checker-compat-qual:2.0.0:jar org.codehaus.mojo:animal-sniffer-annotations:1.14:jar +org.codehaus.plexus:plexus-classworlds:2.5.2:jar +org.codehaus.plexus:plexus-component-annotations:1.6:jar +org.codehaus.plexus:plexus-interpolation:1.21:jar +org.codehaus.plexus:plexus-utils:3.0.22:jar org.drools:drools-compiler:7.48.0.Final:jar org.drools:drools-core:7.48.0.Final:jar +org.drools:drools-core-dynamic:7.48.0.Final:jar org.drools:drools-core-reflective:7.48.0.Final:jar +org.drools:drools-decisiontables:7.48.0.Final:jar org.drools:drools-ecj:7.48.0.Final:jar org.drools:drools-mvel:7.48.0.Final:jar +org.drools:drools-serialization-protobuf:7.48.0.Final:jar +org.drools:drools-templates:7.48.0.Final:jar +org.eclipse.aether:aether-api:1.1.0:jar +org.eclipse.aether:aether-connector-basic:1.1.0:jar +org.eclipse.aether:aether-impl:1.1.0:jar +org.eclipse.aether:aether-spi:1.1.0:jar +org.eclipse.aether:aether-transport-file:1.1.0:jar +org.eclipse.aether:aether-transport-http:1.1.0:jar +org.eclipse.aether:aether-transport-wagon:1.1.0:jar +org.eclipse.aether:aether-util:1.1.0:jar +org.eclipse.sisu:org.eclipse.sisu.inject:0.3.2:jar +org.eclipse.sisu:org.eclipse.sisu.plexus:0.3.2:jar org.javassist:javassist:3.24.1-GA:jar org.jsoup:jsoup:1.10.3:jar -org.kie:kie-dmn-api:7.48.0.Final:jar -org.kie:kie-dmn-model:7.48.0.Final:jar +org.kie:kie-api:7.48.0.Final:jar +org.kie:kie-ci:7.48.0.Final:jar +org.kie:kie-internal:7.48.0.Final:jar org.kie:kie-memory-compiler:7.48.0.Final:jar -org.kie:kie-pmml-api:7.48.0.Final:jar -org.kie:kie-pmml-commons:7.48.0.Final:jar -org.kie:kie-pmml-evaluator-api:7.48.0.Final:jar -org.kie.kogito:drools-core:1.1.0.Final:jar -org.kie.kogito:drools-core-static:1.1.0.Final:jar -org.kie.kogito:kogito-api:1.1.0.Final:jar -org.kie.kogito:kogito-internal:1.1.0.Final:jar -org.kie.kogito:kogito-pmml-api-dependencies:1.1.0.Final:pom -org.kie.kogito:kogito-services:1.1.0.Final:jar -org.kie.kogito:kogito-timer:1.1.0.Final:jar org.kie.soup:kie-soup-commons:7.48.0.Final:jar +org.kie.soup:kie-soup-maven-integration:7.48.0.Final:jar org.kie.soup:kie-soup-maven-support:7.48.0.Final:jar org.kie.soup:kie-soup-project-datamodel-api:7.48.0.Final:jar org.kie.soup:kie-soup-project-datamodel-commons:7.48.0.Final:jar org.kie.soup:kie-soup-xstream:7.48.0.Final:jar org.mvel:mvel2:2.4.11.Final:jar +org.slf4j:jcl-over-slf4j:1.7.30:jar org.slf4j:slf4j-api:1.7.30:jar +org.sonatype.plexus:plexus-cipher:1.7:jar +org.sonatype.plexus:plexus-sec-dispatcher:1.3:jar org.yaml:snakeyaml:1.26:jar xmlpull:xmlpull:1.1.3.1:jar xpp3:xpp3_min:1.1.4c:jar diff --git a/module/ri-engine/pom.xml b/module/ri-engine/pom.xml index 3a7243e..97fee1c 100644 --- a/module/ri-engine/pom.xml +++ b/module/ri-engine/pom.xml @@ -15,17 +15,28 @@ <dependencies> <dependency> - <groupId>${project.groupId}</groupId> - <artifactId>ri-wireguard</artifactId> - <version>${project.version}</version> + <groupId>org.drools</groupId> + <artifactId>drools-core</artifactId> </dependency> - <dependency> - <groupId>org.kie.kogito</groupId> - <artifactId>drools-core</artifactId> + <groupId>org.drools</groupId> + <artifactId>drools-compiler</artifactId> + </dependency> + <dependency> + <groupId>org.drools</groupId> + <artifactId>drools-decisiontables</artifactId> </dependency> <dependency> <groupId>org.drools</groupId> + <artifactId>drools-templates</artifactId> + </dependency> + <dependency> + <groupId>org.kie</groupId> + <artifactId>kie-ci</artifactId> + </dependency> + <!-- + <dependency> + <groupId>org.drools</groupId> <artifactId>drools-mvel</artifactId> <exclusions> <exclusion> @@ -38,6 +49,7 @@ </exclusion> </exclusions> </dependency> + --> <dependency> <groupId>ch.qos.logback</groupId> @@ -63,6 +75,11 @@ <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> </dependency> + <dependency> + <groupId>info.picocli</groupId> + <artifactId>picocli</artifactId> + <version>${version.picocli}</version> + </dependency> <dependency> <groupId>org.junit.jupiter</groupId> @@ -77,4 +94,55 @@ <scope>test</scope> </dependency> </dependencies> + + <build> + <plugins> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-compiler-plugin</artifactId> + <configuration> + <annotationProcessorPaths> + <path> + <groupId>info.picocli</groupId> + <artifactId>picocli-codegen</artifactId> + <version>${version.picocli}</version> + </path> + </annotationProcessorPaths> + <compilerArgs combine.children="append"> + <arg>-Aproject=${project.groupId}/${project.artifactId}</arg> + </compilerArgs> + </configuration> + </plugin> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-dependency-plugin</artifactId> + <executions> + <execution> + <id>copy-dependencies</id> + <phase>prepare-package</phase> + <goals> + <goal>copy-dependencies</goal> + </goals> + <configuration> + <outputDirectory>${project.build.directory}/lib</outputDirectory> + </configuration> + </execution> + </executions> + </plugin> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-jar-plugin</artifactId> + <version>3.2.0</version> + <configuration> + <archive> + <manifest> + <addClasspath>true</addClasspath> + <classpathPrefix>lib/</classpathPrefix> + <mainClass>io.trygvis.rules.engine.Main</mainClass> + </manifest> + </archive> + </configuration> + </plugin> + </plugins> + </build> </project> 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 deleted file mode 100644 index 04d1af3..0000000 --- a/module/ri-engine/src/main/java/io/trygvis/rules/core/Problem.java +++ /dev/null @@ -1,11 +0,0 @@ -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/Cluster.java b/module/ri-engine/src/main/java/io/trygvis/rules/dba/Cluster.java deleted file mode 100644 index 0b65aaa..0000000 --- a/module/ri-engine/src/main/java/io/trygvis/rules/dba/Cluster.java +++ /dev/null @@ -1,13 +0,0 @@ -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-engine/src/main/java/io/trygvis/rules/dba/Container.java b/module/ri-engine/src/main/java/io/trygvis/rules/dba/Container.java deleted file mode 100644 index 3888661..0000000 --- a/module/ri-engine/src/main/java/io/trygvis/rules/dba/Container.java +++ /dev/null @@ -1,59 +0,0 @@ -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-engine/src/main/java/io/trygvis/rules/dns/DnsEntry.java b/module/ri-engine/src/main/java/io/trygvis/rules/dns/DnsEntry.java deleted file mode 100644 index 105ef79..0000000 --- a/module/ri-engine/src/main/java/io/trygvis/rules/dns/DnsEntry.java +++ /dev/null @@ -1,27 +0,0 @@ -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-engine/src/main/java/io/trygvis/rules/dns/DnsEntryTerraformExpression.java b/module/ri-engine/src/main/java/io/trygvis/rules/dns/DnsEntryTerraformExpression.java deleted file mode 100644 index 79bf934..0000000 --- a/module/ri-engine/src/main/java/io/trygvis/rules/dns/DnsEntryTerraformExpression.java +++ /dev/null @@ -1,21 +0,0 @@ -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-engine/src/main/java/io/trygvis/rules/dns/DnsZone.java b/module/ri-engine/src/main/java/io/trygvis/rules/dns/DnsZone.java deleted file mode 100644 index 1af5c8f..0000000 --- a/module/ri-engine/src/main/java/io/trygvis/rules/dns/DnsZone.java +++ /dev/null @@ -1,9 +0,0 @@ -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/java/io/trygvis/rules/engine/DbIo.java b/module/ri-engine/src/main/java/io/trygvis/rules/engine/DbIo.java index b8ee03a..d3d309a 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 @@ -14,6 +14,8 @@ import com.fasterxml.jackson.dataformat.yaml.YAMLGenerator; import org.drools.core.common.DefaultFactHandle; import org.drools.core.factmodel.GeneratedFact; import org.kie.api.KieBase; +import org.kie.api.definition.type.FactType; +import org.kie.api.runtime.KieContainer; import org.kie.api.runtime.rule.FactHandle; import java.io.File; @@ -29,14 +31,14 @@ public class DbIo { private static final List<String> prioritizedKeys = List.of("key", "name", "fqdn"); - public DbIo(KieBase kieBase) { + public DbIo(KieContainer container, KieBase kieBase) { var factory = new YAMLFactory(); factory.enable(YAMLGenerator.Feature.USE_NATIVE_TYPE_ID); factory.enable(YAMLGenerator.Feature.USE_NATIVE_OBJECT_ID); mapper = new ObjectMapper(factory); mapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS); var typeFactory = TypeFactory.defaultInstance() - .withClassLoader(new AcmeClassLoader(kieBase)); + .withClassLoader(new DbClassLoader(container, kieBase)); mapper.setTypeFactory(typeFactory); mapper.findAndRegisterModules(); @@ -50,7 +52,7 @@ public class DbIo { for (String name : prioritizedKeys) { try { final String getter = "get" + name.substring(0, 1).toUpperCase() + name.substring(1); - var f = klass.getMethod(getter); + klass.getMethod(getter); return new ObjectIdInfo(PropertyName.construct(name), null, ObjectIdGenerators.PropertyGenerator.class, null); } catch (NoSuchMethodException ignore) { } @@ -64,8 +66,8 @@ public class DbIo { }); } - public List<Object> load(String file) throws IOException { - var parser = mapper.getFactory().createParser(new File(file)); + public List<Object> load(File file) throws IOException { + var parser = mapper.getFactory().createParser(file); var objects = mapper.readValues(parser, DbObject.class).readAll(new ArrayList<>()); @@ -79,6 +81,8 @@ public class DbIo { } items.add(x); } catch (ClassNotFoundException | NoSuchMethodException | InstantiationException | IllegalAccessException | InvocationTargetException e) { + System.out.println("e.getClass() = " + e.getClass().getName()); + System.out.println("e.getMessage() = " + e.getMessage()); // ignore } } @@ -86,8 +90,8 @@ public class DbIo { return items; } - public void dump(String s, Collection<FactHandle> factHandles) throws IOException { - dump(s, factHandles, (o) -> true); + public void dump(File file, Collection<FactHandle> factHandles) throws IOException { + dump(file, factHandles, (o) -> true); } // This should just sort by all getters instead. @@ -221,10 +225,8 @@ public class DbIo { 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"); - - FileUtil.createMissingParentDirectories(yamlFile); + public void dump(File file, Collection<FactHandle> factHandles, Function<Object, Boolean> filter) throws IOException { + FileUtil.createMissingParentDirectories(file); var facts = new TreeMap<Class<?>, FactCollection<Object>>(Comparator.comparing(Class::getName)); for (var handle : factHandles) { @@ -260,16 +262,18 @@ public class DbIo { objects.sort(new DbObjectComparator()); var factory = mapper.getFactory(); - try (var writer = new FileWriter(yamlFile); + try (var writer = new FileWriter(file); var g = factory.createGenerator(writer)) { g.writeObject(objects); } } - private static class AcmeClassLoader extends ClassLoader { + private static class DbClassLoader extends ClassLoader { private final KieBase kieBase; + private final KieContainer container; - public AcmeClassLoader(KieBase kieBase) { + public DbClassLoader(KieContainer container, KieBase kieBase) { + this.container = container; this.kieBase = kieBase; } @@ -287,7 +291,18 @@ public class DbIo { pkg = name.substring(0, i); klass = name.substring(i + 1); } - var clazz = kieBase.getFactType(pkg, klass); + + try { + return container.getClassLoader().loadClass(name); + } catch (ClassNotFoundException ignore) { + } + + FactType clazz = null; + try { + clazz = kieBase.getFactType(pkg, klass); + } catch (UnsupportedOperationException ignore) { + System.out.println("AcmeClassLoader.loadClass: " + name); + } if (clazz == null) { throw e; } diff --git a/module/ri-engine/src/main/java/io/trygvis/rules/engine/Engine.java b/module/ri-engine/src/main/java/io/trygvis/rules/engine/Engine.java index b73e2e8..3db1625 100644 --- a/module/ri-engine/src/main/java/io/trygvis/rules/engine/Engine.java +++ b/module/ri-engine/src/main/java/io/trygvis/rules/engine/Engine.java @@ -2,41 +2,83 @@ package io.trygvis.rules.engine; import org.drools.core.audit.WorkingMemoryConsoleLogger; import org.kie.api.KieServices; +import org.kie.api.builder.Message; +import org.kie.api.builder.Message.Level; +import org.kie.api.builder.ReleaseId; import org.kie.api.event.rule.AgendaEventListener; import org.kie.api.event.rule.RuleRuntimeEventListener; import org.kie.api.runtime.KieSession; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import java.io.Closeable; import java.io.File; import java.io.IOException; public class Engine implements Closeable { + private final Logger logger = LoggerFactory.getLogger(getClass()); + public final DbIo io; public final KieSession session; - public Engine(String name, String database, File output, String[] agendaGroups) throws IOException { + public Engine(String name, File database, File output, String[] agendaGroups, File[] modules) throws IOException { + logger.info("Getting KieServices"); + var services = KieServices.Factory.get(); - var container = services.getKieClasspathContainer(); - var kieBase = container.getKieBase(name); - io = new DbIo(kieBase); - var objects = io.load(database); + ReleaseId releaseId = null; + for (File path : (modules == null ? new File[0] : modules)) { + logger.info("New KieBuilder: {}", path); + + +// var kieBuilder = services.newKieBuilder(fileSystem); + var kieBuilder = services.newKieBuilder(path); + logger.info("Building module"); + kieBuilder.buildAll(); + + logger.info("Module built!"); + + var results = kieBuilder.getResults(); + + for (Message message : results.getMessages(Level.INFO, Level.WARNING, Level.ERROR)) { +// logger.info("{} {}", message.getLevel(), message.getText()); + logger.info(message.toString()); + } + + var module = kieBuilder.getKieModule(); + releaseId = module.getReleaseId(); + logger.info("module.getReleaseId() = {}", releaseId); + logger.info("module.getClass() = {}", module.getClass()); + } + + logger.info("Creating classpath container"); +// var container = services.getKieClasspathContainer(); + var container = services.newKieContainer(releaseId); + + logger.info("Creating KieBase"); + logger.info("Available kie base names: {}", container.getKieBaseNames()); + var kieBase = container.getKieBase(name); session = container.newKieSession(name); - var logger = new WorkingMemoryConsoleLogger(session); - session.addEventListener((AgendaEventListener) logger); - session.addEventListener((RuleRuntimeEventListener) logger); + var l = new WorkingMemoryConsoleLogger(session); + session.addEventListener((AgendaEventListener) l); + session.addEventListener((RuleRuntimeEventListener) l); - session.setGlobal("te", new TemplateEngine(output)); + session.setGlobal("te", new JinjavaTemplateEngine(output)); + + logger.info("Loading data"); + io = new DbIo(container, kieBase); + var objects = io.load(database); + logger.info("Loaded {} objects", objects.size()); for (var object : objects) { - System.out.println("object = " + object); + logger.info("object = " + object); session.insert(object); } for (var agendaGroup : agendaGroups) { - System.out.println("Setting agenda: " + agendaGroup); + logger.info("Setting agenda: " + agendaGroup); session.getAgenda().getAgendaGroup(agendaGroup).setFocus(); session.fireAllRules(); } diff --git a/module/ri-engine/src/main/java/io/trygvis/rules/engine/JinjavaTemplateEngine.java b/module/ri-engine/src/main/java/io/trygvis/rules/engine/JinjavaTemplateEngine.java new file mode 100644 index 0000000..42b2127 --- /dev/null +++ b/module/ri-engine/src/main/java/io/trygvis/rules/engine/JinjavaTemplateEngine.java @@ -0,0 +1,40 @@ +package io.trygvis.rules.engine; + +import ch.qos.logback.core.util.FileUtil; +import com.hubspot.jinjava.Jinjava; +import org.apache.commons.io.FileUtils; + +import java.io.File; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.Map; + +public class JinjavaTemplateEngine implements TemplateEngine { + private final Jinjava jinjava = new Jinjava(); + + private final File basedir; + + public JinjavaTemplateEngine(File basedir) { + this.basedir = basedir; + } + + @Override + public void clean() { + try { + System.out.println("Cleaning gen!"); + FileUtils.deleteDirectory(basedir); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + + @Override + public void template(String name, String output, Map<String, Object> params) throws IOException { + var template = Files.readString(Path.of("j2", name + ".j2")); + String renderedTemplate = jinjava.render(template, params); + var f = new File(basedir, output); + FileUtil.createMissingParentDirectories(f); + Files.writeString(f.toPath(), renderedTemplate); + } +} diff --git a/module/ri-engine/src/main/java/io/trygvis/rules/engine/KeyValue.java b/module/ri-engine/src/main/java/io/trygvis/rules/engine/KeyValue.java deleted file mode 100644 index 5046169..0000000 --- a/module/ri-engine/src/main/java/io/trygvis/rules/engine/KeyValue.java +++ /dev/null @@ -1,14 +0,0 @@ -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-engine/src/main/java/io/trygvis/rules/engine/Main.java b/module/ri-engine/src/main/java/io/trygvis/rules/engine/Main.java new file mode 100644 index 0000000..4f06091 --- /dev/null +++ b/module/ri-engine/src/main/java/io/trygvis/rules/engine/Main.java @@ -0,0 +1,20 @@ +package io.trygvis.rules.engine; + +import io.trygvis.rules.engine.cli.DatabaseCommand; +import io.trygvis.rules.engine.cli.RunCommand; +import picocli.CommandLine; +import picocli.CommandLine.Command; + +@Command( + name = "engine", + subcommands = {RunCommand.class, DatabaseCommand.class}, + mixinStandardHelpOptions = true, + version = "UNSPECIFIED") +class Main { + + public static void main(String... args) { + //noinspection InstantiationOfUtilityClass + int exitCode = new CommandLine(new Main()).execute(args); + System.exit(exitCode); + } +} diff --git a/module/ri-engine/src/main/java/io/trygvis/rules/engine/TemplateEngine.java b/module/ri-engine/src/main/java/io/trygvis/rules/engine/TemplateEngine.java index cc085c0..eafa6e4 100644 --- a/module/ri-engine/src/main/java/io/trygvis/rules/engine/TemplateEngine.java +++ b/module/ri-engine/src/main/java/io/trygvis/rules/engine/TemplateEngine.java @@ -1,38 +1,10 @@ package io.trygvis.rules.engine; -import ch.qos.logback.core.util.FileUtil; -import com.hubspot.jinjava.Jinjava; -import org.apache.commons.io.FileUtils; - -import java.io.File; import java.io.IOException; -import java.nio.file.Files; -import java.nio.file.Path; import java.util.Map; -public class TemplateEngine { - private final Jinjava jinjava = new Jinjava(); - - private final File basedir; - - public TemplateEngine(File basedir) { - this.basedir = basedir; - } - - public void clean() { - try { - System.out.println("Cleaning gen!"); - FileUtils.deleteDirectory(basedir); - } catch (IOException e) { - throw new RuntimeException(e); - } - } +public interface TemplateEngine { + void clean(); - public void template(String name, String output, Map<String, Object> params) throws IOException { - var template = Files.readString(Path.of("j2", name + ".j2")); - String renderedTemplate = jinjava.render(template, params); - var f = new File(basedir, output); - FileUtil.createMissingParentDirectories(f); - Files.writeString(f.toPath(), renderedTemplate); - } + void template(String name, String output, Map<String, Object> params) throws IOException; } diff --git a/module/ri-engine/src/main/java/io/trygvis/rules/engine/cli/DatabaseCommand.java b/module/ri-engine/src/main/java/io/trygvis/rules/engine/cli/DatabaseCommand.java new file mode 100644 index 0000000..03650b5 --- /dev/null +++ b/module/ri-engine/src/main/java/io/trygvis/rules/engine/cli/DatabaseCommand.java @@ -0,0 +1,20 @@ +package io.trygvis.rules.engine.cli; + +import picocli.CommandLine.Command; + +import java.util.concurrent.Callable; + +import static picocli.CommandLine.Option; + +@Command(name = "database") +public class DatabaseCommand implements Callable<Integer> { + + @Option(names = {"-v", "--verbose"}) + private boolean verbose; + + @Override + public Integer call() { + System.out.println("DatabaseCommand.call"); + return 0; + } +} diff --git a/module/ri-engine/src/main/java/io/trygvis/rules/engine/cli/RunCommand.java b/module/ri-engine/src/main/java/io/trygvis/rules/engine/cli/RunCommand.java new file mode 100644 index 0000000..345d8b8 --- /dev/null +++ b/module/ri-engine/src/main/java/io/trygvis/rules/engine/cli/RunCommand.java @@ -0,0 +1,55 @@ +package io.trygvis.rules.engine.cli; + +import io.trygvis.rules.engine.Engine; + +import java.io.File; +import java.util.concurrent.Callable; + +import static picocli.CommandLine.Command; +import static picocli.CommandLine.Option; + +@Command(name = "run") +public class RunCommand implements Callable<Integer> { + + @Option(names = {"-n", "--name"}) + public String name; + + @Option(names = {"-i", "--input"}) + public File input; + + @Option(names = {"--output-state"}) + public File outputState; + + @Option(names = {"--generated-output"}) + public File generatedOutput; + + @Option(names = {"--agenda-groups"}) + public String[] agendaGroups; + + @Option(names = {"--modules"}, split = ",") + public File[] modules; + + @Override + public Integer call() throws Exception { + System.out.println("RunCommand.call"); + + if (agendaGroups == null || agendaGroups.length == 0) { + agendaGroups = new String[]{"init", "generate"}; + } + + try (var engine = new Engine(name, input, generatedOutput, agendaGroups, modules)) { + engine.io.dump(outputState, engine.session.getFactHandles(), (Object o) -> + o.getClass().getName().contains("Wg") || + o.getClass().getSimpleName().contains("Machine") || + o.getClass().getSimpleName().contains("DnsEntry") || + o.getClass().getSimpleName().contains("Ipv4Cidr") || + o.getClass().getSimpleName().contains("Ipv4Address") + ); + System.out.println("RunCommand.call"); + } + + System.out.println("RunCommand.call"); + + return 0; + } +} 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 deleted file mode 100644 index 34c17ca..0000000 --- a/module/ri-engine/src/main/java/io/trygvis/rules/machine/Machine.java +++ /dev/null @@ -1,30 +0,0 @@ -package io.trygvis.rules.machine; - -import com.fasterxml.jackson.annotation.JsonIdentityInfo; -import com.fasterxml.jackson.annotation.ObjectIdGenerators; - -@SuppressWarnings("unused") -@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-engine/src/main/java/io/trygvis/rules/machine/MachineSpecification.java b/module/ri-engine/src/main/java/io/trygvis/rules/machine/MachineSpecification.java deleted file mode 100644 index 2e17ae5..0000000 --- a/module/ri-engine/src/main/java/io/trygvis/rules/machine/MachineSpecification.java +++ /dev/null @@ -1,19 +0,0 @@ -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-engine/src/main/java/io/trygvis/rules/network/IpCalc.java b/module/ri-engine/src/main/java/io/trygvis/rules/network/IpCalc.java deleted file mode 100644 index 7ec344c..0000000 --- a/module/ri-engine/src/main/java/io/trygvis/rules/network/IpCalc.java +++ /dev/null @@ -1,6 +0,0 @@ -package io.trygvis.rules.network; - -import java.util.regex.Pattern; - -public class IpCalc { -} diff --git a/module/ri-engine/src/main/java/io/trygvis/rules/network/Ipv4Address.java b/module/ri-engine/src/main/java/io/trygvis/rules/network/Ipv4Address.java deleted file mode 100644 index 7ca9ca5..0000000 --- a/module/ri-engine/src/main/java/io/trygvis/rules/network/Ipv4Address.java +++ /dev/null @@ -1,57 +0,0 @@ -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 other) { - 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-engine/src/main/java/io/trygvis/rules/network/Ipv4Cidr.java b/module/ri-engine/src/main/java/io/trygvis/rules/network/Ipv4Cidr.java deleted file mode 100644 index 6362107..0000000 --- a/module/ri-engine/src/main/java/io/trygvis/rules/network/Ipv4Cidr.java +++ /dev/null @@ -1,152 +0,0 @@ -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 org.checkerframework.checker.nullness.compatqual.NonNullType; - -import javax.annotation.Nonnull; -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-engine/src/main/resources/META-INF/kmodule.xml b/module/ri-engine/src/main/resources/META-INF/kmodule.xml deleted file mode 100644 index 79c5e79..0000000 --- a/module/ri-engine/src/main/resources/META-INF/kmodule.xml +++ /dev/null @@ -1,11 +0,0 @@ -<?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-engine/src/main/resources/io/trygvis/rules/dba/dba.drl b/module/ri-engine/src/main/resources/io/trygvis/rules/dba/dba.drl deleted file mode 100644 index 0bee004..0000000 --- a/module/ri-engine/src/main/resources/io/trygvis/rules/dba/dba.drl +++ /dev/null @@ -1,69 +0,0 @@ -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; - - 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 diff --git a/module/ri-engine/src/main/resources/io/trygvis/rules/engine/init.drl b/module/ri-engine/src/main/resources/io/trygvis/rules/engine/init.drl deleted file mode 100644 index 267cc4a..0000000 --- a/module/ri-engine/src/main/resources/io/trygvis/rules/engine/init.drl +++ /dev/null @@ -1,17 +0,0 @@ -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-engine/src/main/resources/io/trygvis/rules/terraform/terraform.drl b/module/ri-engine/src/main/resources/io/trygvis/rules/terraform/terraform.drl deleted file mode 100644 index 07a96e2..0000000 --- a/module/ri-engine/src/main/resources/io/trygvis/rules/terraform/terraform.drl +++ /dev/null @@ -1,81 +0,0 @@ -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 - var 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 - var a = DnsEntry.a($machine.fqdn); - insert(a); - - var 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 - var 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 - var 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 - var path = "terraform/dns-%s.tf".formatted($tf.key); - te.template("terraform-record-set", path, Map.of( - "entry", $entry, - "managedZone", $managedZone, - "tf", $tf) - ); -end diff --git a/module/ri-engine/src/test/java/io/trygvis/rules/network/Ipv4CidrTest.java b/module/ri-engine/src/test/java/io/trygvis/rules/network/Ipv4CidrTest.java deleted file mode 100644 index 0d9f33e..0000000 --- a/module/ri-engine/src/test/java/io/trygvis/rules/network/Ipv4CidrTest.java +++ /dev/null @@ -1,36 +0,0 @@ -package io.trygvis.rules.network; - -import org.junit.jupiter.api.Test; -import org.junit.jupiter.params.ParameterizedTest; -import org.junit.jupiter.params.provider.ValueSource; - -import static io.trygvis.rules.network.Ipv4Cidr.parseCidr; -import static org.junit.jupiter.api.Assertions.*; - -class Ipv4CidrTest { - - @Test - public void basic() { - assertThrows(IllegalArgumentException.class, () -> parseCidr("192.168.1.1/24").addresses()); - assertEquals(256, parseCidr("192.168.1.0/24").addresses().size()); - assertEquals(128, parseCidr("192.168.1.128/25").addresses().size()); - } - - @ParameterizedTest - @ValueSource(strings = { - "192.168.1.0/24", - "192.168.1.128/25", - }) - public void testParsing(String s) { - assertEquals(s, parseCidr(s).toString()); - } - - @Test - public void partition() { - var children = parseCidr("192.168.1.0/24").partition(26); - assertEquals(1 << 2, children.size()); - for (var cidr : children) { - assertEquals(26, cidr.bits); - } - } -} |