-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnixos-rebuild.sh
executable file
·129 lines (115 loc) · 2.45 KB
/
nixos-rebuild.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#!/usr/bin/env bash
set -eEuo pipefail
trap 'echo "Error when executing $BASH_COMMAND at line $LINENO!" >&2' ERR
cd "${BASH_SOURCE[0]%/*}"
info() { echo "[$(date -Iseconds)]" "$@" >&2; }
info STARTING
trap 'info FINISHED' EXIT
test -z "${DEBUG:-}" || set -x
is_available() {
local hostname="$1"
nc -vz -w 1 "${hostname}" 22 >&2
}
discover_hostname() {
local host="$1" hostname
for domain in "${check_domains[@]}"; do
hostname="${host}.${domain}"
if is_available "${hostname}"; then
printf "%s" "${hostname}"
return
fi
done
if is_available "${host}"; then
printf "%s" "${host}"
return
fi
echo "Failed to discover ${host}." >&2
return 1
}
check_domains=(
lan.etra.net.int.kdn.im.
lan.drek.net.int.kdn.im.
priv.nb.net.int.kdn.im.
netbird.cloud.
)
pre_cmd=(
)
pre_args=(
)
post_args=(
# below 2 do not work with `--fast` flag due to carrying over to raw `nix` commands
#--print-build-logs --show-trace
# required for `nom` handling, replaces above 2
--log-format internal-json -v
)
name="$(hostname -s)"
cmd="${1}"
remote=""
shift 1
if [[ "${1:-}" == remote=* ]]; then
# remote="etra=kdn@kdn.im@etra.netbird.cloud"
# remote="kdn.im@etra.netbird.cloud"
# remote="etra.netbird.cloud"
# remote="kdn@etra"
# remote="etra"
remote="${1#remote=}"
shift 1
if [[ "${remote}" == *=* ]]; then
name="${remote%=*}"
remote="${remote#*=}"
else
name="${remote}"
fi
addr="${remote}"
if [[ "${addr}" == *@* ]]; then
user="${addr%@*}"
addr="${addr##*@}"
fi
if [[ "${addr}" != *.* ]]; then
addr="$(discover_hostname "${addr}")"
fi
if test -z "${name:-}"; then
name="${addr%%.*}"
fi
if test -n "${user:-}"; then
pre_args+=(
--target-host "${user}@${addr}"
)
else
pre_args+=(
--target-host "${addr}"
)
fi
pre_args+=(
--use-remote-sudo
)
elif [[ -n "${1:-}" && "${1}" != -* ]]; then
name="${1}"
shift 1
fi
if [[ "${1:-}" == --build-on-remote ]]; then
if test -n "${user:-}"; then
pre_args+=(
--build-host "${user}@${addr}"
)
else
pre_args+=(
--build-host "${addr}" --fast
)
fi
shift 1
fi
post_args+=(
--flake ".#${name}"
)
case "$cmd" in
switch | boot | test)
if test "${remote}" == ""; then
pre_cmd=(sudo DEBUG="${DEBUG:-}")
fi
;;
esac
if test "${DRY_RUN:-0}" == 1; then
pre_cmd=(echo "${pre_cmd[@]}")
fi
"${pre_cmd[@]}" nixos-rebuild "${pre_args[@]}" "${cmd}" "${post_args[@]}" "${@}" |& nom --json