scripts/deploy: implement a dry-run mode

This commit is contained in:
Colin 2024-06-19 11:24:33 +00:00
parent 294f0061bd
commit afea7fe5e7

View File

@ -9,6 +9,7 @@ usage() {
echo "usage: deploy [options] [host] [host2 ...]"
echo "options:"
echo "- --action copy|switch|test (default: 'switch')"
echo "- --dry-run: show what would be done without actually doing it"
echo "- --variant light|min|''|all (default: '')"
echo "- --pre: alias for --action copy --variant all all"
echo ""
@ -30,6 +31,7 @@ defaultHost="$SELF"
variants=()
defaultVariant=
nixArgs=()
dryRun=
addHost() {
if [ "$1" = all ]; then
# order matters:
@ -72,6 +74,9 @@ parseArgs() {
defaultVariant=all
defaultHost=all
;;
(--dry-run)
dryRun=1
;;
(*)
nixArgs+=("$arg")
;;
@ -86,6 +91,14 @@ parseArgs() {
fi
}
destructive() {
if [ -z "$dryRun" ]; then
"$@"
else
echo "dry-run: $@"
fi
}
runOnTarget() {
# run the command ($@) on the machine we're deploying to.
# if that's a remote machine, then do it via ssh, else local shell.
@ -104,7 +117,7 @@ deployOneHost() {
local variant="$2"
info "building $host$variant ..."
nix-build -A "hosts.$host$variant.toplevel" --out-link "./build/result-$host$variant" "${nixArgs[@]}" || return 1
destructive nix-build -A "hosts.$host$variant.toplevel" --out-link "./build/result-$host$variant" "${nixArgs[@]}" || return 1
storePath="$(readlink ./build/result-$host$variant)"
info "build $host$variant -> $storePath"
@ -120,20 +133,20 @@ deployOneHost() {
if [ -n "$host" ] && [ "$host" != "$SELF" ]; then
if [ -e /run/secrets/nix_signing_key ]; then
info "signing store paths ..."
sudo nix store sign -r -k /run/secrets/nix_signing_key "$storePath"
destructive sudo nix store sign -r -k /run/secrets/nix_signing_key "$storePath"
else
info "not signing store paths: /run/secrets/nix_signing_key does not exist"
fi
# add more `-v` for more verbosity (up to 5).
# builders-use-substitutes false: optimizes so that the remote machine doesn't try to get paths from its substituters.
# we already have all paths here, and the remote substitution is slow to check and SERIOUSLY flaky on moby in particular.
nix copy -vv --option builders-use-substitutes false --to "ssh-ng://$host" "$storePath" || return 1
destructive nix copy -vv --option builders-use-substitutes false --to "ssh-ng://$host" "$storePath" || return 1
fi
if [ -n "$action" ] && [ "$action" != "copy" ]; then
info "activating profile... "
runOnTarget sudo nix-env -p /nix/var/nix/profiles/system --set "$storePath" || return 1
runOnTarget sudo "$storePath/bin/switch-to-configuration" "$action" || return 1
destructive runOnTarget sudo nix-env -p /nix/var/nix/profiles/system --set "$storePath" || return 1
destructive runOnTarget sudo "$storePath/bin/switch-to-configuration" "$action" || return 1
fi
}