forked from Fediversity/Fediversity
123 lines
3.6 KiB
Nix
123 lines
3.6 KiB
Nix
{
|
|
lib,
|
|
config,
|
|
pkgs,
|
|
inputs,
|
|
...
|
|
}:
|
|
let
|
|
inherit (import ./constants.nix) pathToRoot;
|
|
escapedJson = v: lib.replaceStrings [ "\"" ] [ "\\\\\"" ] (lib.strings.toJSON v);
|
|
deployment-config = {
|
|
inherit (import ./constants.nix) pathToRoot pathFromRoot;
|
|
inherit (config) enableAcme;
|
|
acmeNodeIP = if config.enableAcme then config.nodes.acme.networking.primaryIPAddress else null;
|
|
nodeName = "ssh";
|
|
};
|
|
inherit
|
|
((import ../common/data-model.nix {
|
|
inherit (pkgs) system;
|
|
inherit inputs;
|
|
config = deployment-config;
|
|
})."ssh-deployment".ssh-host.ssh
|
|
)
|
|
host
|
|
username
|
|
key-file
|
|
;
|
|
in
|
|
{
|
|
_class = "nixosTest";
|
|
imports = [
|
|
../common/data-model-options.nix
|
|
];
|
|
|
|
name = "deployment-model";
|
|
sourceFileset = lib.fileset.unions [
|
|
../../data-model.nix
|
|
../../function.nix
|
|
../common/data-model.nix
|
|
../common/data-model-options.nix
|
|
./constants.nix
|
|
(config.pathToCwd + "/flake-under-test.nix")
|
|
];
|
|
|
|
nodes.deployer =
|
|
{ pkgs, ... }:
|
|
{
|
|
environment.systemPackages = with pkgs; [
|
|
inputs.nixops4.packages.${system}.default
|
|
jq
|
|
];
|
|
|
|
# FIXME: sad times
|
|
system.extraDependencies = with pkgs; [
|
|
jq
|
|
jq.inputDerivation
|
|
];
|
|
|
|
system.extraDependenciesFromModule =
|
|
{ pkgs, ... }:
|
|
{
|
|
environment.systemPackages = with pkgs; [
|
|
hello
|
|
];
|
|
};
|
|
};
|
|
|
|
extraTestScript = ''
|
|
with subtest("nixops4"):
|
|
nixops4.fail("hello 1>&2")
|
|
deployer.succeed("nixops4 apply check-deployment-model --show-trace --verbose --no-interactive 1>&2")
|
|
nixops4.succeed("su - operator -c hello 1>&2")
|
|
|
|
with subtest("ssh: Check the status before deployment"):
|
|
ssh.fail("hello 1>&2")
|
|
|
|
with subtest("ssh: Run the deployment"):
|
|
deployer.succeed("""
|
|
set -euo pipefail
|
|
|
|
# INSTANTIATE
|
|
command=(nix-instantiate --show-trace --expr '
|
|
let
|
|
system = "${pkgs.system}"; # FIXME: what system are we deploying to?
|
|
in
|
|
import ${pathToRoot}/deployment/nixos.nix {
|
|
inherit system;
|
|
configuration = (
|
|
import ${pathToRoot}/deployment/check/common/data-model.nix {
|
|
inherit system;
|
|
config = builtins.fromJSON "${escapedJson deployment-config}";
|
|
}
|
|
)."ssh-deployment".ssh-host.nixos-configuration;
|
|
}
|
|
')
|
|
# DEPLOY
|
|
host="${lib.defaultTo "root" username}@${host}"
|
|
sshOpts=(
|
|
${if key-file == null then "" else "-i ${key-file}"}
|
|
-o StrictHostKeyChecking=no
|
|
-o "ConnectTimeout=1"
|
|
-o "ServerAliveInterval=1"
|
|
)
|
|
# instantiate the config in /nix/store
|
|
"''${command[@]}" --show-trace -A out_path
|
|
# get the realized derivation to deploy
|
|
outPath=$(nix-store --realize "$("''${command[@]}" --show-trace --eval --strict --json | jq -r '.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 ssh not responding"* ]]; then
|
|
echo "non-timeout error: $output"
|
|
exit 1
|
|
else
|
|
exit 0
|
|
fi
|
|
""")
|
|
ssh.wait_for_unit("multi-user.target")
|
|
ssh.succeed("su - operator -c hello 1>&2")
|
|
'';
|
|
}
|