summaryrefslogtreecommitdiff
path: root/module/ri-wireguard
diff options
context:
space:
mode:
authorTrygve Laugstøl <trygvis@inamo.no>2021-01-11 15:26:06 +0100
committerTrygve Laugstøl <trygvis@inamo.no>2021-01-11 15:26:06 +0100
commitf2fc5a193743e2a66f748c8baefee949a747ea7f (patch)
tree987968bf8d3c3dbf732d4b21106909e6db4d6051 /module/ri-wireguard
parent26769b6059e299103e2ee5427ac71b30533c204f (diff)
downloadrules-sandbox-f2fc5a193743e2a66f748c8baefee949a747ea7f.tar.gz
rules-sandbox-f2fc5a193743e2a66f748c8baefee949a747ea7f.tar.bz2
rules-sandbox-f2fc5a193743e2a66f748c8baefee949a747ea7f.tar.xz
rules-sandbox-f2fc5a193743e2a66f748c8baefee949a747ea7f.zip
Figured out a way to not need WgNetworkAllocation.
Diffstat (limited to 'module/ri-wireguard')
-rw-r--r--module/ri-wireguard/src/main/resources/io/trygvis/rules/wireguard/wireguard.drl42
1 files changed, 18 insertions, 24 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..261374a 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
@@ -27,11 +27,11 @@ declare WgIpPool
end
declare WgHost
- name : String
- net : String
- publicName : String
- netToNetIp : String
- networkIp : String
+ name : String // TODO: rename to machine
+ net : String
+ publicName : String
+ ip : String // This host's IP
+ networkCidr : String
end
declare WgConnection
@@ -45,12 +45,6 @@ declare WgIpAllocation
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))
@@ -103,29 +97,33 @@ then
insert(new WgConnection($h.name, $other.name))
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))
$ip : Ipv4Address() from $pool.cidr.addresses()
- not(WgIpAllocation(ip == $ip))
+ not(WgHost(net == $net.name, ip == $ip.toString()))
+ $host : WgHost(net == $net.name, 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))
+ 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.name, networkCidr == null)
+ not(WgHost(net == $net.name, 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))
+ modify($host) {
+ networkCidr = $network.toString()
+ }
end
rule "Generate per-net files"
@@ -151,8 +149,6 @@ rule "Generate per-net, per-host files"
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))
then
@@ -163,8 +159,6 @@ then
te.template("wireguard/ansible-host", output, Map.of(
"net", $net,
"host", $host,
- "link", $link.ip,
- "network", $network.cidr,
"peers", $peers
));
end