2024-07-18 00:23:04 +03:00
|
|
|
{ testers, nixosModules, consul, cluster }:
|
2023-08-29 01:55:34 +03:00
|
|
|
|
|
|
|
let
|
|
|
|
dataDir = {
|
|
|
|
node1 = "/data/before";
|
|
|
|
node2 = "/data/nested/after";
|
|
|
|
};
|
|
|
|
kvPath = {
|
|
|
|
node1 = "before";
|
|
|
|
node2 = "after";
|
|
|
|
};
|
|
|
|
in
|
|
|
|
|
2023-09-03 04:15:45 +03:00
|
|
|
testers.runNixOSTest {
|
2023-08-29 01:55:34 +03:00
|
|
|
name = "ascensions";
|
2023-09-03 04:15:45 +03:00
|
|
|
|
|
|
|
imports = [
|
|
|
|
./modules/consul.nix
|
|
|
|
];
|
|
|
|
|
2024-06-04 15:52:15 +03:00
|
|
|
extraBaseModules.services.consul.package = consul;
|
|
|
|
|
2023-08-29 01:55:34 +03:00
|
|
|
nodes = let
|
|
|
|
common = { config, lib, ... }: let
|
|
|
|
inherit (config.networking) hostName;
|
|
|
|
in {
|
|
|
|
imports = [
|
|
|
|
nixosModules.ascensions
|
2024-07-18 00:50:18 +03:00
|
|
|
nixosModules.port-magic
|
2023-08-29 01:55:34 +03:00
|
|
|
nixosModules.systemd-extras
|
|
|
|
nixosModules.consul-distributed-services
|
2024-07-18 00:23:04 +03:00
|
|
|
cluster.config.services.consul.nixos.ready
|
2023-08-29 01:55:34 +03:00
|
|
|
];
|
2024-07-18 00:50:18 +03:00
|
|
|
links.consulAgent = {
|
|
|
|
protocol = "http";
|
|
|
|
hostname = "consul";
|
|
|
|
port = 8500;
|
|
|
|
};
|
2023-08-29 01:55:34 +03:00
|
|
|
systemd.services = {
|
|
|
|
create-file = {
|
|
|
|
serviceConfig.Type = "oneshot";
|
|
|
|
script = ''
|
|
|
|
if ! test -e ${dataDir.${hostName}}/file.txt; then
|
|
|
|
mkdir -p ${dataDir.${hostName}}
|
|
|
|
echo "${hostName}" > ${dataDir.${hostName}}/file.txt
|
|
|
|
fi
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
create-kv = {
|
|
|
|
serviceConfig.Type = "oneshot";
|
|
|
|
path = [ config.services.consul.package ];
|
|
|
|
script = ''
|
|
|
|
if ! consul kv get ${kvPath.${hostName}}; then
|
|
|
|
consul kv put ${kvPath.${hostName}} ${hostName}
|
|
|
|
fi
|
|
|
|
'';
|
2024-07-18 00:50:18 +03:00
|
|
|
environment.CONSUL_HTTP_ADDR = config.links.consulAgent.tuple;
|
2023-08-29 01:55:34 +03:00
|
|
|
};
|
|
|
|
ascend-create-kv = {
|
2024-07-18 00:50:18 +03:00
|
|
|
environment.CONSUL_HTTP_ADDR = config.links.consulAgent.tuple;
|
2023-08-29 01:55:34 +03:00
|
|
|
};
|
|
|
|
};
|
|
|
|
system.ascensions = {
|
|
|
|
create-file = {
|
|
|
|
before = [ "create-file.service" ];
|
|
|
|
incantations = m: with m; lib.optionals (hostName == "node2") [
|
2023-09-03 04:21:43 +03:00
|
|
|
(move dataDir.node1 "/data/somewhere/intermediate1")
|
|
|
|
(move "/data/somewhere/intermediate1" "/var/lib/intermediate2")
|
|
|
|
(move "/var/lib/intermediate2" dataDir.node2)
|
2023-08-29 01:55:34 +03:00
|
|
|
];
|
|
|
|
};
|
|
|
|
create-kv = {
|
|
|
|
distributed = true;
|
|
|
|
before = [ "create-kv.service" ];
|
|
|
|
incantations = m: with m; lib.optionals (hostName == "node2") [
|
|
|
|
(execShellWith [ config.services.consul.package ] ''
|
2023-09-03 04:21:43 +03:00
|
|
|
consul kv put intermediate/data $(consul kv get ${kvPath.node1})
|
2023-08-29 01:55:34 +03:00
|
|
|
consul kv delete ${kvPath.node1}
|
|
|
|
'')
|
2023-09-03 04:21:43 +03:00
|
|
|
(execShellWith [ config.services.consul.package ] ''
|
|
|
|
consul kv put ${kvPath.node2} $(consul kv get intermediate/data)
|
|
|
|
consul kv delete intermediate/data
|
|
|
|
'')
|
2023-08-29 01:55:34 +03:00
|
|
|
];
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
in {
|
2023-09-03 04:15:45 +03:00
|
|
|
node1.imports = [ common ];
|
|
|
|
node2.imports = [ common ];
|
2023-08-29 01:55:34 +03:00
|
|
|
};
|
|
|
|
testScript = /*python*/ ''
|
2023-09-07 14:21:49 +03:00
|
|
|
node1.start()
|
2023-08-29 01:55:34 +03:00
|
|
|
consul.wait_for_unit("consul.service")
|
2023-09-03 04:15:45 +03:00
|
|
|
consul.wait_until_succeeds("CONSUL_HTTP_ADDR=consul:8500 consul members")
|
2023-08-29 01:55:34 +03:00
|
|
|
node1.wait_for_unit("multi-user.target")
|
|
|
|
node1.succeed("systemctl start create-file create-kv")
|
|
|
|
node1.succeed("tar cvf /tmp/shared/data.tar /data /var/lib/ascensions")
|
|
|
|
|
|
|
|
node2.wait_for_unit("multi-user.target")
|
2023-09-07 14:21:49 +03:00
|
|
|
node2.succeed("rm -rf /data")
|
2023-08-29 01:55:34 +03:00
|
|
|
node2.succeed("tar xvf /tmp/shared/data.tar -C /")
|
|
|
|
node2.succeed("systemctl start create-file create-kv")
|
|
|
|
|
|
|
|
assert "node1" in node2.succeed("cat ${dataDir.node2}/file.txt")
|
2023-09-03 04:15:45 +03:00
|
|
|
assert "node1" in consul.succeed("CONSUL_HTTP_ADDR=consul:8500 consul kv get ${kvPath.node2}")
|
2023-08-29 01:55:34 +03:00
|
|
|
'';
|
|
|
|
}
|