forked from Fediversity/Fediversity
151 lines
4.6 KiB
Nix
151 lines
4.6 KiB
Nix
{
|
|
lib,
|
|
config,
|
|
sources,
|
|
...
|
|
}:
|
|
let
|
|
inherit (import ./constants.nix) targetMachines pathToRoot;
|
|
in
|
|
{
|
|
_class = "nixosTest";
|
|
|
|
name = "deployment-model";
|
|
|
|
sourceFileset = lib.fileset.unions [
|
|
../../data-model.nix
|
|
../../function.nix
|
|
./constants.nix
|
|
./deployment.nix
|
|
];
|
|
|
|
nodes.deployer =
|
|
{ pkgs, ... }:
|
|
{
|
|
|
|
environment.systemPackages = with pkgs; [
|
|
jq
|
|
automake
|
|
autoconf
|
|
];
|
|
|
|
# FIXME: sad times
|
|
system.extraDependencies = with pkgs; [
|
|
jq
|
|
jq.inputDerivation
|
|
automake
|
|
autoconf
|
|
];
|
|
|
|
system.extraDependenciesFromModule =
|
|
{ pkgs, ... }:
|
|
{
|
|
environment.systemPackages = with pkgs; [
|
|
hello
|
|
cowsay
|
|
automake
|
|
autoconf
|
|
];
|
|
};
|
|
};
|
|
|
|
extraTestScript = ''
|
|
with subtest("Check the status before deployment"):
|
|
hello.fail("hello 1>&2")
|
|
|
|
${lib.concatStringsSep "\n" (
|
|
lib.lists.map (nodeName: ''
|
|
with subtest("Run the deployment for ${nodeName}"):
|
|
deployer.succeed("""
|
|
set -euo pipefail
|
|
|
|
# INSTANTIATE
|
|
command=(
|
|
nix-instantiate
|
|
--expr
|
|
|
|
'
|
|
let
|
|
args = builtins.fromJSON "${
|
|
lib.replaceStrings [ "\"" ] [ "\\\\\"" ] (
|
|
lib.strings.toJSON {
|
|
inherit sources;
|
|
}
|
|
)
|
|
}";
|
|
inherit (args) sources;
|
|
configuration = { pkgs, config, ... }: {
|
|
imports = [
|
|
${pathToRoot}/deployment/check/common/sharedOptions.nix
|
|
${pathToRoot}/deployment/check/common/targetNode.nix
|
|
${sources.nixpkgs}/nixos/modules/profiles/qemu-guest.nix
|
|
];
|
|
|
|
enableAcme = ${lib.strings.toJSON config.enableAcme};
|
|
acmeNodeIP = if config.enableAcme then config.nodes.acme.networking.primaryIPAddress else null;
|
|
|
|
environment.systemPackages = with pkgs; [
|
|
hello
|
|
automake
|
|
autoconf
|
|
];
|
|
};
|
|
eval = import "${sources.nixpkgs}/nixos/lib/eval-config.nix" {
|
|
system = builtins.currentSystem;
|
|
specialArgs = {
|
|
inherit sources;
|
|
};
|
|
modules = [ configuration ];
|
|
};
|
|
os = {
|
|
inherit (eval) pkgs config options;
|
|
system = eval.config.system.build.toplevel;
|
|
inherit (eval.config.system.build) vm vmWithBootLoader;
|
|
};
|
|
in
|
|
# import "${pathToRoot}/deployment/nixos.nix" {}
|
|
{
|
|
drv_path = os.config.system.build.toplevel.drvPath;
|
|
out_path = os.config.system.build.toplevel;
|
|
}
|
|
'
|
|
)
|
|
# instantiate the config in /nix/store
|
|
"''${command[@]}" -A out_path
|
|
# get the other info
|
|
json="$("''${command[@]}" --eval --strict --json)"
|
|
|
|
# DEPLOY
|
|
declare drv_path
|
|
# set our variables using the json object
|
|
eval "export $(echo $json | jq -r 'to_entries | map("\(.key)=\(.value)") | @sh')"
|
|
host="root@${nodeName}"
|
|
sshOpts=(
|
|
-o BatchMode=yes
|
|
-o StrictHostKeyChecking=no
|
|
-o "ConnectionAttempts=1"
|
|
-o "ConnectTimeout=1"
|
|
-o "ServerAliveCountMax=1"
|
|
-o "ServerAliveInterval=1"
|
|
)
|
|
# get the realized derivation to deploy
|
|
outPath=$(nix-store --realize "$drv_path")
|
|
# deploy the config by nix-copy-closure
|
|
NIX_SSHOPTS="''${sshOpts[*]}" nix-copy-closure --to "$host" "$outPath" --gzip --use-substitutes
|
|
# switch the remote host to the config
|
|
output=$(ssh "''${sshOpts[@]}" "$host" "nix-env --profile /nix/var/nix/profiles/system --set $outPath; nohup $outPath/bin/switch-to-configuration switch &" 2>&1) || echo "status code: $?"
|
|
echo "output: $output"
|
|
if [[ $output != *"Timeout, server ${nodeName} not responding"* ]]; then
|
|
echo "non-timeout error: $output"
|
|
exit 1
|
|
else
|
|
exit 0
|
|
fi
|
|
""")
|
|
${nodeName}.wait_for_unit("multi-user.target")
|
|
${nodeName}.succeed("systemctl is-active sshd")
|
|
${nodeName}.succeed("${nodeName} 1>&2")
|
|
'') targetMachines
|
|
)}
|
|
'';
|
|
}
|