-
Notifications
You must be signed in to change notification settings - Fork 0
/
deploy.sh
executable file
·81 lines (66 loc) · 1.57 KB
/
deploy.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#!/usr/bin/env bash
function usage {
echo "Usage:"
echo " $0 OPTIONS [nixos-rebuild parameters] OPERATION"
echo
echo "Options:"
echo " -h, --host comma-separated list of hosts to deploy (can be specified multiple times)"
echo " -a, --addr, --address optional comma-separated list of addresses to deploy (matched in order to each host)"
echo " if a host does not have a matching address, its hostname is used instead"
echo " --help display this message"
}
function error {
echo -e "Error: $1\n" 1>&2
usage
exit 1
}
function missing_argument {
error "Missing argument for $1 parameter"
}
EXTRA_ARGS=()
HOSTS=()
ADDRESSES=()
while [[ $# -gt 0 ]]; do
case $1 in
-h|--host)
(( $# <= 1 )) && missing_argument "$1"
HOSTS+=("${2//,/ }")
shift 2
;;
-a|--addr|--address)
(( $# <= 1 )) && missing_argument "$1"
ADDRESSES+=("${2//,/ }")
shift 2
;;
--help)
usage
exit 0
;;
*)
EXTRA_ARGS+=("$1")
shift
;;
esac
done
echo "${EXTRA_ARGS[@]}" | grep -qvwE "boot|switch|test" && error "Missing nixos-rebuild operation (boot, switch or test)"
flake_root=$(realpath "$(dirname "$0")")
function deploy_local
{
sudo nixos-rebuild --flake "$flake_root" "${EXTRA_ARGS[@]}"
}
function deploy_remote
{
for host in "${HOSTS[@]}"; do
addr="$host"
if (( $# > 0 )); then
addr="$1"
shift
fi
nixos-rebuild --flake "$flake_root#$host" --target-host "$addr" --use-remote-sudo "${EXTRA_ARGS[@]}"
done
}
if (( ${#HOSTS[@]} <= 0 )); then
deploy_local
else
deploy_remote "${ADDRESSES[@]}"
fi