summaryrefslogtreecommitdiff
path: root/module/ri-wireguard/src/main/resources/io/trygvis/rules/wireguard/wireguard.drl
diff options
context:
space:
mode:
Diffstat (limited to 'module/ri-wireguard/src/main/resources/io/trygvis/rules/wireguard/wireguard.drl')
-rw-r--r--module/ri-wireguard/src/main/resources/io/trygvis/rules/wireguard/wireguard.drl100
1 files changed, 48 insertions, 52 deletions
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 2e4498f..d971696 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
@@ -21,54 +21,48 @@ declare WgNet
end
declare WgIpPool
- net : String
+ net : WgNet
role : String
cidr : Ipv4Cidr
end
declare WgHost
- name : String
- net : String
- publicName : String
- netToNetIp : String
- networkIp : String
+ machine : Machine
+ net : WgNet
+ publicName : String
+ ip : String // This host's IP
+ networkCidr : String
end
declare WgConnection
- host : String
- to : String
+ host : WgHost
+ to : WgHost
end
declare WgIpAllocation
- host : String
+ host : WgHost
role : String
ip : Ipv4Address
end
-declare WgNetworkAllocation
- host : String
- role : String
- cidr : Ipv4Cidr
-end
-
rule "Create IP pools" when
$net : WgNet()
// not(Ipv4Cidr(network == Ipv4Cidr.parseCidr($net.linkCidr).network))
then
System.out.println("Creating main IP pools");
- insert(new WgIpPool($net.name, "link", Ipv4Cidr.parseCidr($net.linkCidr)))
- insert(new WgIpPool($net.name, "networks", Ipv4Cidr.parseCidr($net.networkCidr)))
+ insert(new WgIpPool($net, "link", Ipv4Cidr.parseCidr($net.linkCidr)))
+ insert(new WgIpPool($net, "networks", Ipv4Cidr.parseCidr($net.networkCidr)))
end
rule "WgHost VPN machines"
when
$machine : Machine()
$wgNet : WgNet(name == "vpn0")
- not(WgHost(name == $machine.name))
+ not(WgHost(machine == $machine))
then
var wgHost = new WgHost();
- wgHost.name = $machine.name;
- wgHost.net = $wgNet.name;
+ wgHost.machine = $machine;
+ wgHost.net = $wgNet;
wgHost.publicName = $machine.fqdn;
insert(wgHost)
end
@@ -76,7 +70,7 @@ end
rule "Set public name of WgHost"
when
$host : WgHost(publicName == null)
- $m : Machine(name == $host.name, fqdn != null)
+ $m : Machine(this == $host.machine, fqdn != null)
then
modify($host) {
publicName = $m.fqdn
@@ -86,10 +80,9 @@ end
rule "Make DNS entries for all VPN hosts"
when
$h : WgHost()
- $net : WgNet(name == $h.net)
- not(DnsEntry(fqdn == "%s.%s".formatted($h.name, $net.domain), type == "A"))
+ not(DnsEntry(fqdn == "%s.%s".formatted($h.machine.name, $h.net.domain), type == "A"))
then
- var fqdn = "%s.%s".formatted($h.name, $net.domain);
+ var fqdn = "%s.%s".formatted($h.machine.name, $h.net.domain);
insert(DnsEntry.a(fqdn))
end
@@ -97,35 +90,38 @@ rule "Connect VPN nodes"
salience -1
when
$h : WgHost()
- $other : WgHost(publicName != null, name != $h.name)
+ $other : WgHost(publicName != null, this != $h)
then
- System.out.printf("VPN connection from %s to %s%n", $h.name, $other.name);
- insert(new WgConnection($h.name, $other.name))
+ System.out.printf("VPN connection from %s to %s%n", $h.machine.name, $other.machine.name);
+ insert(new WgConnection($h, $other))
end
-rule "Assign link IP"
+// This and the next rule needs to use .toString(), the specific objects might be generated multiple times,
+// but Drools use identityHashCode() to find equal objects, not equals().
+rule "Assign IP"
when
- $net : WgNet()
- $host : WgHost(net == $net.name)
- $pool : WgIpPool(net == $net.name, role == "link")
- not(WgIpAllocation(host == $host.name, role == $pool.role))
+ $pool : WgIpPool(role == "link")
$ip : Ipv4Address() from $pool.cidr.addresses()
- not(WgIpAllocation(ip == $ip))
+ not(WgHost(net == $pool.net, ip == $ip.toString()))
+ $host : WgHost(net == $pool.net, ip == null)
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))
+ System.out.printf("IP: net=%s, pool.role=%s, host=%s, ip=%s%n", $pool.net.name, $pool.role, $host.machine.name, $ip);
+ modify($host) {
+ ip = $ip.toString()
+ }
end
rule "Assign network CIDR"
when
$net : WgNet()
- $host : WgHost(net == $net.name)
$network : Ipv4Cidr() from Ipv4Cidr.parseCidr($net.networkCidr).partition($net.networkBits)
- not(WgNetworkAllocation(host == $host.name, role == "network"))
- not(WgNetworkAllocation(cidr == $network))
+ $host : WgHost(net == $net, networkCidr == null)
+ not(WgHost(net == $net, networkCidr == $network.toString()))
then
- System.out.printf("Network CIDR: net=%s, host=%s, network=%s%n", $net.name, $host.name, $network);
- insert(new WgNetworkAllocation($host.name, "network", $network))
+ System.out.printf("Network CIDR: net=%s, host=%s, network=%s%n", $net.name, $host.machine.name, $network);
+ modify($host) {
+ networkCidr = $network.toString()
+ }
end
rule "Generate per-net files"
@@ -133,15 +129,20 @@ rule "Generate per-net files"
salience 10
when
$net : WgNet()
- $names : ArrayList() from accumulate(WgHost(net == $net.name, $name: name), collectList($name))
- $hosts : ArrayList() from accumulate(Machine($names contains name, $m: this), collectList($m))
+ $hosts : ArrayList() from collect(WgHost(net == $net))
then
te.template("wireguard/ansible", "wireguard-" + $net.name + ".yml", Map.of(
"net", $net
));
+ var machines = new ArrayList();
+ for (Object o : $hosts) {
+ WgHost m = (WgHost) o;
+ machines.add(m.machine);
+ }
+
te.template("wireguard/inventory", "inventory.yml", Map.of(
- "hosts", $hosts
+ "hosts", machines
));
end
@@ -150,21 +151,16 @@ rule "Generate per-net, per-host files"
salience 10
when
$net : WgNet()
- $host : WgHost(net == $net.name)
- $link : WgIpAllocation(host == $host.name, role == "link")
- $network : WgNetworkAllocation(host == $host.name, role == "network")
- $peerMachines : ArrayList() from accumulate(WgConnection(host == $host.name, $to: to), collectList($to))
- $peers : ArrayList() from accumulate(Machine($peerMachines contains name, $fqdn: fqdn), collectList($fqdn))
+ $host : WgHost(net == $net)
+ $peers : ArrayList() from accumulate(WgConnection(host == $host, $to: to), collectList($to.machine))
then
- System.out.printf("Generating per-host files: net=%s, host=%s%n", $net.name, $host.name);
+ System.out.printf("Generating per-host files: net=%s, host=%s%n", $net.name, $host.machine.name);
- String output = "host_vars/%s/wireguard.yml".formatted($host.name);
+ String output = "host_vars/%s/wireguard.yml".formatted($host.machine.name);
te.template("wireguard/ansible-host", output, Map.of(
"net", $net,
"host", $host,
- "link", $link.ip,
- "network", $network.cidr,
"peers", $peers
));
end