diff options
author | Trumeet <yuuta@yuuta.moe> | 2021-04-04 14:51:45 -0700 |
---|---|---|
committer | Trumeet <yuuta@yuuta.moe> | 2021-04-04 14:51:45 -0700 |
commit | 02c2f523606f81c8dcd452fe5ab54408c3aac8f1 (patch) | |
tree | d1093e4e7e647a95d6e828a373c22037f7834385 | |
parent | e8a187d5caa8ec8bda572fbf54c16fefbe9642e6 (diff) | |
download | dn42peering-02c2f523606f81c8dcd452fe5ab54408c3aac8f1.tar dn42peering-02c2f523606f81c8dcd452fe5ab54408c3aac8f1.tar.gz dn42peering-02c2f523606f81c8dcd452fe5ab54408c3aac8f1.tar.bz2 dn42peering-02c2f523606f81c8dcd452fe5ab54408c3aac8f1.zip |
fix(agent): disable legacy services before calculating netlink changesv1.13.1
Disabling wg-quick services will remove interfaces, making the predict of netlink changes outdated.
3 files changed, 49 insertions, 31 deletions
diff --git a/agent/src/main/java/moe/yuuta/dn42peering/agent/Deploy.java b/agent/src/main/java/moe/yuuta/dn42peering/agent/Deploy.java index 94f614b..6af8159 100644 --- a/agent/src/main/java/moe/yuuta/dn42peering/agent/Deploy.java +++ b/agent/src/main/java/moe/yuuta/dn42peering/agent/Deploy.java @@ -6,10 +6,7 @@ import io.vertx.core.impl.logging.Logger; import io.vertx.core.impl.logging.LoggerFactory; import moe.yuuta.dn42peering.agent.proto.DeployResult; import moe.yuuta.dn42peering.agent.proto.NodeConfig; -import moe.yuuta.dn42peering.agent.provision.BGPProvisioner; -import moe.yuuta.dn42peering.agent.provision.Change; -import moe.yuuta.dn42peering.agent.provision.WireGuardCleanupProvisioner; -import moe.yuuta.dn42peering.agent.provision.WireGuardProvisioner; +import moe.yuuta.dn42peering.agent.provision.*; import javax.annotation.Nonnull; import java.util.List; @@ -31,6 +28,7 @@ public class Deploy { public static Future<DeployResult> deploy(@Nonnull Vertx vertx, @Nonnull NodeConfig config) { logger.info("Deployment started"); final BGPProvisioner bgpProvisioner = new BGPProvisioner(vertx); + final WireGuardLegacyCleanupProvisioner wireGuardLegacyCleanupProvisioner = new WireGuardLegacyCleanupProvisioner(vertx); final WireGuardProvisioner wireGuardProvisioner = new WireGuardProvisioner(vertx); final WireGuardCleanupProvisioner wireGuardCleanupProvisioner = new WireGuardCleanupProvisioner(vertx); @@ -38,8 +36,10 @@ public class Deploy { // TODO: if one operation fails, the following will fail. This may be changed in later. // Changes in each provisioners are executed in sequence. // Two provisioners are executed in sequence. - return wireGuardProvisioner.calculateChanges(config.getNode(), config.getWgsList()) - .compose(changes -> chainChanges(vertx, changes)) + return wireGuardLegacyCleanupProvisioner.calculateChanges(config.getNode(), config.getWgsList()) + .compose(changes -> chainChanges(vertx, changes)) + .compose(_v -> wireGuardProvisioner.calculateChanges(config.getNode(), config.getWgsList()) + .compose(changes -> chainChanges(vertx, changes))) .compose(_v -> bgpProvisioner.calculateChanges(config.getNode(), config.getBgpsList()) .compose(changes -> chainChanges(vertx, changes))) .compose(_v -> wireGuardCleanupProvisioner.calculateChanges(config.getNode(), config.getWgsList()) diff --git a/agent/src/main/java/moe/yuuta/dn42peering/agent/provision/WireGuardLegacyCleanupProvisioner.java b/agent/src/main/java/moe/yuuta/dn42peering/agent/provision/WireGuardLegacyCleanupProvisioner.java new file mode 100644 index 0000000..e349cbf --- /dev/null +++ b/agent/src/main/java/moe/yuuta/dn42peering/agent/provision/WireGuardLegacyCleanupProvisioner.java @@ -0,0 +1,37 @@ +package moe.yuuta.dn42peering.agent.provision; + +import io.vertx.core.Future; +import io.vertx.core.Vertx; +import moe.yuuta.dn42peering.agent.proto.Node; +import moe.yuuta.dn42peering.agent.proto.WireGuardConfig; + +import javax.annotation.Nonnull; +import java.io.File; +import java.util.Arrays; +import java.util.List; +import java.util.stream.Collectors; + +public class WireGuardLegacyCleanupProvisioner implements IProvisioner<WireGuardConfig> { + private final Vertx vertx; + + public WireGuardLegacyCleanupProvisioner(@Nonnull Vertx vertx) { + this.vertx = vertx; + } + + @Nonnull + @Override + public Future<List<Change>> calculateChanges(@Nonnull Node node, @Nonnull List<WireGuardConfig> allDesired) { + final String[] actualNamesRaw = new File("/etc/wireguard/").list((dir, name) -> name.matches("wg_.*\\.conf")); + final List<String> actualNames = Arrays.stream(actualNamesRaw == null ? new String[]{} : actualNamesRaw) + .sorted() + .collect(Collectors.toList()); + return Future.succeededFuture(actualNames.stream() + .flatMap(string -> { + return Arrays.stream(new Change[]{ + new CommandChange(new String[]{"systemctl", "disable", "--now", "-q", "wg-quick@" + string.replace(".conf", ".service")}), + new FileChange("/etc/wireguard/" + string, null, FileChange.Action.DELETE.toString()) + }); + }) + .collect(Collectors.toList())); + } +} diff --git a/agent/src/main/java/moe/yuuta/dn42peering/agent/provision/WireGuardProvisioner.java b/agent/src/main/java/moe/yuuta/dn42peering/agent/provision/WireGuardProvisioner.java index fee8917..66f48db 100644 --- a/agent/src/main/java/moe/yuuta/dn42peering/agent/provision/WireGuardProvisioner.java +++ b/agent/src/main/java/moe/yuuta/dn42peering/agent/provision/WireGuardProvisioner.java @@ -17,10 +17,12 @@ import moe.yuuta.dn42peering.agent.proto.WireGuardConfig; import javax.annotation.Nonnull; import javax.annotation.Nullable; -import java.io.File; import java.io.IOException; import java.net.Inet6Address; -import java.util.*; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; import java.util.stream.Collectors; public class WireGuardProvisioner implements IProvisioner<WireGuardConfig> { @@ -40,22 +42,6 @@ public class WireGuardProvisioner implements IProvisioner<WireGuardConfig> { } @Nonnull - private Future<List<Change>> calculateDeleteChanges(@Nonnull List<WireGuardConfig> allDesired) { - final String[] actualNamesRaw = new File("/etc/wireguard/").list((dir, name) -> name.matches("wg_.*\\.conf")); - final List<String> actualNames = Arrays.stream(actualNamesRaw == null ? new String[]{} : actualNamesRaw) - .sorted() - .collect(Collectors.toList()); - return Future.succeededFuture(actualNames.stream() - .flatMap(string -> { - return Arrays.stream(new Change[]{ - new CommandChange(new String[]{"systemctl", "disable", "--now", "-q", "wg-quick@" + string.replace(".conf", ".service")}), - new FileChange("/etc/wireguard/" + string, null, FileChange.Action.DELETE.toString()) - }); - }) - .collect(Collectors.toList())); - } - - @Nonnull private Future<Buffer> renderConfig(@Nonnull WireGuardConfig config) { final Map<String, Object> params = new HashMap<>(5); params.put("listen_port", config.getListenPort()); @@ -231,13 +217,8 @@ public class WireGuardProvisioner implements IProvisioner<WireGuardConfig> { @Nonnull @Override public Future<List<Change>> calculateChanges(@Nonnull Node node, @Nonnull List<WireGuardConfig> allDesired) { - return calculateDeleteChanges(allDesired).compose(changes -> { - return calculateTotalNetlinkChanges(node, allDesired) - .compose(netlinkChanges -> { - changes.addAll(netlinkChanges); - return Future.succeededFuture(changes); - }); - }).compose(changes -> { + return calculateTotalNetlinkChanges(node, allDesired) + .compose(changes -> { return calculateTotalWireGuardChanges(node, allDesired) .compose(wireguardChanges -> { changes.addAll(wireguardChanges); |