From f2fc5a193743e2a66f748c8baefee949a747ea7f Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Mon, 11 Jan 2021 15:26:06 +0100 Subject: Figured out a way to not need WgNetworkAllocation. --- .../io/trygvis/rules/wireguard/wireguard.drl | 42 ++++++++++------------ 1 file changed, 18 insertions(+), 24 deletions(-) (limited to 'module/ri-wireguard/src/main/resources/io') 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 -- cgit v1.2.3