Compare commits

...

2 commits

4 changed files with 34 additions and 26 deletions

View file

@ -5,7 +5,6 @@
... ...
}@args: }@args:
let let
self = "deployment/check/data-model-ssh/data-model.nix";
inherit (sources) nixpkgs; inherit (sources) nixpkgs;
pkgs = import nixpkgs { inherit system; }; pkgs = import nixpkgs { inherit system; };
inherit (pkgs) lib; inherit (pkgs) lib;
@ -40,7 +39,7 @@ in
key-file = null; key-file = null;
inherit sshOpts; inherit sshOpts;
}; };
module = self; caller = "deployment/check/data-model-ssh/data-model.nix";
inherit args deployment-name; inherit args deployment-name;
root-path = pathToRoot; root-path = pathToRoot;
}; };

View file

@ -5,7 +5,6 @@
... ...
}@args: }@args:
let let
self = "deployment/check/data-model-tf/data-model.nix";
inherit (sources) nixpkgs; inherit (sources) nixpkgs;
pkgs = import nixpkgs { inherit system; }; pkgs = import nixpkgs { inherit system; };
inherit (pkgs) lib; inherit (pkgs) lib;
@ -40,7 +39,7 @@ in
key-file = null; key-file = null;
inherit sshOpts; inherit sshOpts;
}; };
module = self; caller = "deployment/check/data-model-tf/data-model.nix";
inherit args deployment-name httpBackend; inherit args deployment-name httpBackend;
root-path = pathToRoot; root-path = pathToRoot;
}; };

View file

@ -30,13 +30,13 @@ let
writeConfig = writeConfig =
{ {
system, system,
module, caller,
root-path, root-path,
deployment-type, deployment-type,
deployment-name, deployment-name,
args, args,
}: }:
# having a `module` location and (serializable) `args`, we know # having a `caller` location and (serializable) `args`, we know
# enough to call it again to extract different info elsewhere later. # enough to call it again to extract different info elsewhere later.
# we use this to make a deployment script using the desired nixos config, # we use this to make a deployment script using the desired nixos config,
# which would otherwise not be serializable, while nix also makes it hard to # which would otherwise not be serializable, while nix also makes it hard to
@ -46,7 +46,7 @@ let
pkgs.writers.writeText "configuration.nix" '' pkgs.writers.writeText "configuration.nix" ''
import ${root-path}/deployment/nixos.nix { import ${root-path}/deployment/nixos.nix {
system = "${system}"; system = "${system}";
configuration = (import "${root-path}/${module}" (builtins.fromJSON "${ configuration = (import "${root-path}/${caller}" (builtins.fromJSON "${
lib.replaceStrings [ "\"" ] [ "\\\"" ] (lib.strings.toJSON args) lib.replaceStrings [ "\"" ] [ "\\\"" ] (lib.strings.toJSON args)
}")).${deployment-name}.${deployment-type}.nixos-configuration; }")).${deployment-name}.${deployment-type}.nixos-configuration;
} }
@ -121,8 +121,8 @@ let
}; };
inherit nixos-configuration; inherit nixos-configuration;
ssh = host-ssh; ssh = host-ssh;
module = mkOption { caller = mkOption {
description = "The module to call to obtain the NixOS configuration from."; description = "The calling module to obtain the NixOS configuration from.";
type = types.str; type = types.str;
}; };
args = mkOption { args = mkOption {
@ -147,7 +147,7 @@ let
inherit (ssh-host.config) inherit (ssh-host.config)
system system
ssh ssh
module caller
args args
deployment-name deployment-name
root-path root-path
@ -168,7 +168,7 @@ let
nixos_conf = writeConfig { nixos_conf = writeConfig {
inherit inherit
system system
module caller
args args
deployment-name deployment-name
root-path root-path
@ -204,8 +204,8 @@ let
}; };
inherit nixos-configuration; inherit nixos-configuration;
ssh = host-ssh; ssh = host-ssh;
module = mkOption { caller = mkOption {
description = "The module to call to obtain the NixOS configuration from."; description = "The calling module to obtain the NixOS configuration from.";
type = types.str; type = types.str;
}; };
args = mkOption { args = mkOption {
@ -233,7 +233,7 @@ let
inherit (tf-host.config) inherit (tf-host.config)
system system
ssh ssh
module caller
args args
deployment-name deployment-name
root-path root-path
@ -255,7 +255,7 @@ let
nixos_conf = writeConfig { nixos_conf = writeConfig {
inherit inherit
system system
module caller
args args
deployment-name deployment-name
root-path root-path

View file

@ -1,23 +1,33 @@
#! /usr/bin/env bash #! /usr/bin/env bash
set -xeuo pipefail set -xeuo pipefail
declare username host key_file ssh_opts nixos_conf declare username host key_file ssh_opts nixos_conf
IFS=" " read -r -a ssh_opts <<< "$( (echo "$ssh_opts" | jq -r '@sh') | tr -d \'\")" readarray -t ssh_opts < <(echo "$ssh_opts" | jq -r '.[]')
# DEPLOY # DEPLOY
sshOpts=( sshOptsInit=(
-o BatchMode=yes -o BatchMode=yes
-o StrictHostKeyChecking=no -o StrictHostKeyChecking=no
) )
for ssh_opt in "${ssh_opts[@]}"; do
sshOpts+=(
-o "$ssh_opt"
)
done
if [[ -n "$key_file" ]]; then if [[ -n "$key_file" ]]; then
sshOpts+=( sshOptsInit+=(
-i "$key_file" -i "$key_file"
) )
fi fi
# [@] will quote variables containing spaces itself
sshOptsAt=("${sshOptsInit[@]}")
for ssh_opt in "${ssh_opts[@]}"; do
sshOptsAt+=(
-o "${ssh_opt}"
)
done
# [*] needs manual quoting
sshOptsAsterisk=("${sshOptsInit[@]}")
for ssh_opt in "${ssh_opts[@]}"; do
sshOptsAsterisk+=(
-o "\"${ssh_opt}\""
)
done
destination="$username@$host" destination="$username@$host"
command=(nix-instantiate --show-trace "${nixos_conf}") command=(nix-instantiate --show-trace "${nixos_conf}")
@ -32,9 +42,9 @@ command=(nix-instantiate --show-trace "${nixos_conf}")
# FIXME explore import/readFile as ways to instantiate the derivation, potentially allowing to realize the store path up-front from Nix? # FIXME explore import/readFile as ways to instantiate the derivation, potentially allowing to realize the store path up-front from Nix?
outPath=$(nix-store --realize "$("${command[@]}" -A config.system.build.toplevel.drvPath --eval --strict --json | jq -r '.')") outPath=$(nix-store --realize "$("${command[@]}" -A config.system.build.toplevel.drvPath --eval --strict --json | jq -r '.')")
# deploy the config by nix-copy-closure # deploy the config by nix-copy-closure
NIX_SSHOPTS="${sshOpts[*]}" nix-copy-closure --to "$destination" "$outPath" --gzip --use-substitutes NIX_SSHOPTS="${sshOptsAsterisk[*]}" nix-copy-closure --to "$destination" "$outPath" --gzip --use-substitutes
# switch the remote host to the config # switch the remote host to the config
# shellcheck disable=SC2029 # shellcheck disable=SC2029
ssh "${sshOpts[@]}" "$destination" "nix-env --profile /nix/var/nix/profiles/system --set $outPath" ssh "${sshOptsAt[@]}" "$destination" "nix-env --profile /nix/var/nix/profiles/system --set $outPath"
# shellcheck disable=SC2029 # shellcheck disable=SC2029
ssh -o "ConnectTimeout=1" -o "ServerAliveInterval=1" "${sshOpts[@]}" "$destination" "nohup $outPath/bin/switch-to-configuration switch &" 2>&1 ssh -o "ConnectTimeout=5" -o "ServerAliveInterval=1" "${sshOptsAt[@]}" "$destination" "nohup env $outPath/bin/switch-to-configuration switch &" 2>&1