summaryrefslogtreecommitdiff
path: root/module/ri-wireguard/src/main/resources
diff options
context:
space:
mode:
authorTrygve Laugstøl <trygvis@inamo.no>2021-01-06 21:01:33 +0100
committerTrygve Laugstøl <trygvis@inamo.no>2021-01-06 21:01:33 +0100
commitede2372547db659e7b0e9cfd3b531ff542018229 (patch)
tree433dcb7d2bf84c6cc0ee30d92c32c6220dc53641 /module/ri-wireguard/src/main/resources
parent149cf9efadb1da64b9ee716ac9bee1ac0c0e1329 (diff)
downloadrules-sandbox-ede2372547db659e7b0e9cfd3b531ff542018229.tar.gz
rules-sandbox-ede2372547db659e7b0e9cfd3b531ff542018229.tar.bz2
rules-sandbox-ede2372547db659e7b0e9cfd3b531ff542018229.tar.xz
rules-sandbox-ede2372547db659e7b0e9cfd3b531ff542018229.zip
Generating Wireguard files too.
Diffstat (limited to 'module/ri-wireguard/src/main/resources')
-rw-r--r--module/ri-wireguard/src/main/resources/io/trygvis/rules/wireguard/generate.drl18
-rw-r--r--module/ri-wireguard/src/main/resources/io/trygvis/rules/wireguard/wireguard.drl38
2 files changed, 38 insertions, 18 deletions
diff --git a/module/ri-wireguard/src/main/resources/io/trygvis/rules/wireguard/generate.drl b/module/ri-wireguard/src/main/resources/io/trygvis/rules/wireguard/generate.drl
deleted file mode 100644
index 3ee0583..0000000
--- a/module/ri-wireguard/src/main/resources/io/trygvis/rules/wireguard/generate.drl
+++ /dev/null
@@ -1,18 +0,0 @@
-package io.trygvis.rules.wireguard;
-
-import java.util.ArrayList
-import io.trygvis.rules.dns.DnsEntry;
-import io.trygvis.rules.machine.Machine;
-import io.trygvis.rules.network.Ipv4Address
-import io.trygvis.rules.network.Ipv4Cidr
-import io.trygvis.rules.network.IpCalc
-
-rule "Generate base"
- agenda-group "generate"
- salience 10
-when
- $net : WgNet()
- $h : WgHost(net == $net.name)
-then
- System.out.println("%s : %s".formatted($net.name, $h.name));
-end
diff --git a/module/ri-wireguard/src/main/resources/io/trygvis/rules/wireguard/wireguard.drl b/module/ri-wireguard/src/main/resources/io/trygvis/rules/wireguard/wireguard.drl
index 342cbb5..06b9bbf 100644
--- a/module/ri-wireguard/src/main/resources/io/trygvis/rules/wireguard/wireguard.drl
+++ b/module/ri-wireguard/src/main/resources/io/trygvis/rules/wireguard/wireguard.drl
@@ -6,6 +6,9 @@ import io.trygvis.rules.machine.Machine;
import io.trygvis.rules.network.Ipv4Address
import io.trygvis.rules.network.Ipv4Cidr
import io.trygvis.rules.network.IpCalc
+import java.util.Map
+
+global io.trygvis.rules.engine.TemplateEngine te;
dialect "mvel"
@@ -103,3 +106,38 @@ then
System.out.printf("IP: net=%s, pool.role=%s, host=%s, ip=%s%n", $net.name, $pool.role, $host.name, $ip);
insert(new WgIpAllocation($host.name, $pool.role, $ip))
end
+
+rule "Generate per-net files"
+ agenda-group "generate"
+ salience 10
+when
+ $net : WgNet()
+then
+ te.template("wireguard/ansible", "wireguard-" + $net.name + ".yml", Map.of(
+ "net", $net
+ ));
+
+ // TODO: Generate hosts file
+end
+
+rule "Generate per-net, per-host files"
+ agenda-group "generate"
+ salience 10
+when
+ $net : WgNet()
+ $host : WgHost(net == $net.name)
+ $link : WgIpAllocation(host == $host.name, role == "link")
+ // Needs to be a sub-cidr of the WgNet network cidr, not a specific IP
+ $network : WgIpAllocation(host == $host.name, role == "network")
+then
+ System.out.printf("%s : %s%n", $net.name, $host.name);
+
+ String output = "host_vars/%s/wireguard.yml".formatted($host.name);
+
+ te.template("wireguard/ansible-host", output, Map.of(
+ "net", $net,
+ "host", $host,
+ "link", $link.ip,
+ "network", "TODO"
+ ));
+end