scripts/deploy: allow specifying multiple hosts or multiple variants

This commit is contained in:
Colin 2024-06-16 05:14:34 +00:00
parent 53139a7cdf
commit c87dab93b3

View File

@ -6,14 +6,17 @@ SELF=$(hostname)
usage() {
echo "deploy: deploy a nix config to a remote machine, possibly activating it"
echo ""
echo "usage: deploy [options] [host]"
echo "usage: deploy [options] [host] [host2 ...]"
echo "options:"
echo "- --action copy|switch|test (default: 'switch')"
echo "- --variant light|min|''|all (default: '')"
echo "- --pre: alias for --action copy --variant all all"
echo ""
echo "common idioms:"
echo "deploy all: deploy all hosts, sequentially"
echo "- deploy all: deploy all hosts, sequentially"
echo "- deploy --pre: build and copy all hosts"
exho "- deploy desko lappy: build and deploy just those hosts"
echo "- deploy: deploy the local host"
exit 1
}
@ -22,9 +25,29 @@ info() {
}
action=switch
host="$SELF"
variant=
hosts=()
defaultHost="$SELF"
variants=()
defaultVariant=
nixArgs=()
addHost() {
if [ "$1" = all ]; then
# order matters:
hosts+=(moby lappy desko servo crappy)
else
hosts+=("$1")
fi
}
addVariant() {
if [ "$1" = all ]; then
variants+=("-min" "-light" "" "-min-next" "-light-next" "-next")
elif [ -n "$1" ]; then
variants+=("-$1")
else
# "full" variant
variants+=("")
fi
}
parseArgs() {
while [ "$#" -ne 0 ]; do
local arg=$1
@ -38,26 +61,29 @@ parseArgs() {
usage
;;
(--variant)
if [ -n "$1" ]; then
variant=-$1
else
variant=
fi
addVariant "$1"
shift
;;
(crappy|desko|lappy|moby|servo)
host="$arg"
(all|crappy|desko|lappy|moby|servo)
addHost "$arg"
;;
(--pre)
action=copy
host=all
variant=all
defaultVariant=all
defaultHost=all
;;
(*)
nixArgs+=("$arg")
;;
esac
done
if [ "${#hosts[@]}" -eq 0 ] && [ -n "$defaultHost" ]; then
addHost "$defaultHost"
fi
if [ "${#variants[@]}" -eq 0 ]; then
addVariant "$defaultVariant"
fi
}
runOnTarget() {
@ -111,31 +137,16 @@ deployOneHost() {
fi
}
parseArgs "$@"
failedDeploys=()
# deployHelper is like `deployOneHost`,
# but it handles the special cases of `host=all` or `variant=all`,
# and aggregates failed deployments into the `failedDeploys` var.
deployHelper() {
local host="$1"
local variant="$2"
if [ "$variant" = "all" ]; then
for variant in -min -light "" "-min-next" "-light-next" "-next"; do
deployHelper "$host" "$variant"
done
elif [ "$host" = "all" ]; then
for host in moby lappy crappy servo desko; do
deployHelper "$host" "$variant"
done
else
deployOneHost "$host" "$variant" || \
failedDeploys+=("$host$variant")
fi
}
deployHelper "$host" "$variant"
for h in "${hosts[@]}"; do
for v in "${variants[@]}"; do
deployOneHost "$h" "$v" || \
failedDeploys+=("$h$v")
done
done
if [ "${#failedDeploys[@]}" -ne 0 ]; then
echo "FAILED DEPLOYMENT:"