aboutsummaryrefslogtreecommitdiff
path: root/devices.sh
blob: 7867593e06f4e82acb28eb398a24085f15c6d0af (plain)
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
#!/usr/bin/env bash
set -eu
declare -a devices
declare -A sshmap
################################################################################
omnia_hash="bd7ac5d8c08538ec1f126d34b765f0362427fe17"
## aarch64
# Mox
devices+=( "dean" "spt-mox2" )
sshmap["spt-mox2"]="mox2.spt"
# Raspberry Pi
devices+=( "adm-mpd" )
sshmap["adm-mpd"]="mpd.adm"

## armv7
# Omnia
devices+=( "spt-omnia" )
sshmap["spt-omnia"]="omnia.spt"
# Raspberry Pi
devices+=( "spt-mpd" )
sshmap["spt-mpd"]="mpd.spt"
################################################################################

valid_device() {
	local check="$1"
	for dev in "${devices[@]}"; do
		[ "$dev" != "$check" ] \
			|| return 0
	done
	return 1
}


build() {
	local system="$1"
	echo "Building $system"
	local -a args
	local toplevel=".config.system.build.toplevel"
	args+=("--keep-going")
	args+=("--override-input" "nixturris" "/home/cynerd/projects/nixturris")
	if [[ "$system" == *omnia ]]; then
		true
		#toplevel=".config.system.build.cross.x86_64-linux${toplevel}"
		#args=( \
		#	"--override-input" "nixpkgs" "github:NixOS/nixpkgs/${omnia_hash}"
		#	"--override-input" "nixturris/nixpkgs" "github:NixOS/nixpkgs/${omnia_hash}"
		#)
	fi
	nix build \
		-o "result-${system}" \
		"${args[@]}" \
		"${0%/*}#nixosConfigurations.${system}${toplevel}"
}

build_validate() {
	local system="$1"
	[ -L "result-$system" ] && [ ! -e "result-$system" ]
}

copy() {
	local system="$1"
	if ! build_validate "$system"; then
		echo "System '$system' seems to be not build." >&2
		return 1
	fi
	local store="$(readlink -f "result-$system")"
	local host="${sshmap["$system"]:-$system}"

	local freespace="$(ssh "$host" -- df -B 1 /nix | awk 'NR == 2 { print $4 }')"
	local required="$(nix path-info -S "$store")"
	if [ "$required" -ge "$freespace" ]; then
		echo "There is not enough space to copy clousure to: $system" >&2
		return 1
	fi

	echo "Copy closure to: $system"
	nix copy -s --to "ssh://$host" "$store"
}

setenv() {
	local system="$1"
	if ! build_validate "$system"; then
		echo "System '$system' seems to be not build." >&2
		return 1
	fi
	local store="$(readlink -f "result-$system")"
	local host="${sshmap["$system"]:-$system}"

	echo "Update system: $system"
	if [ "$(ssh "$host" -- readlink -f /nix/var/nix/profiles/system)" != "$store" ]; then
		ssh -t "$host" -- \
			sudo nix-env --profile /nix/var/nix/profiles/system --set "$store"
	fi
}

boot() {
	local system="$1"
	setenv "$system" || return 1

	local store="$(readlink -f "result-$system")"
	local host="${sshmap["$system"]:-$system}"

	echo "Setting boot system: $system"
	ssh -t "$host" -- \
		sudo /nix/var/nix/profiles/system/bin/switch-to-configuration boot
}

is_current() {
	ssh "$1" -- \
		'[ "$(readlink -f /run/current-system)" != "$(readlink -f /nix/var/nix/profiles/system)" ]'
}

switch() {
	local system="$1"
	setenv "$system" || return 1

	local store="$(readlink -f "result-$system")"
	local host="${sshmap["$system"]:-$system}"

	if is_current "$host"; then
		echo "Switching: $system"
		ssh -t "$host" -- \
			sudo /nix/var/nix/profiles/system/bin/switch-to-configuration switch
	else
		echo "This system is already running: $system"
	fi
}

switch_test() {
	local system="$1"
	setenv "$system" || return 1

	local store="$(readlink -f "result-$system")"
	local host="${sshmap["$system"]:-$system}"

	if is_current "$host"; then
		echo "Testing: $system"
		ssh -t "$host" -- \
			sudo /nix/var/nix/profiles/system/bin/switch-to-configuration test
	else
		echo "This system is already running: $system"
	fi
}

for_devices() {
	for device in "${selected_devices[@]}"; do
		for op in "$@"; do
			if ! "$op" "$device"; then
				echo "Operation '$op' failed for: $device" >&2
				break
			fi
		done
	done
}


operation="${1:-}"
[ $# -gt 0 ] && shift

declare -a selected_devices
if [ $# -gt 0 ]; then
	for device in "$@"; do
		if ! valid_device "$device"; then
			echo "No such device: $device" >&2
			exit 2
		fi
		selected_devices+=("$device")
	done
else
	selected_devices=("${devices[@]}")
fi

case "$operation" in
	help|h)
		cat <<-EOF
		Usage $0 operation [device]...
		Local system builder and updater for remote devices.

		Operations:
		  build: build device system
		  copy: copy built system to the device
		  boot: set built system to be boot default on the device
		  switch: switch to the built system on the target device
		  test: test the built system on the target device
		EOF
		;;
	build|b|"")
		for_devices build
		;;
	copy|c)
		for_devices copy
		;;
	boot)
		for_devices boot
		;;
	switch|s)
		for_devices switch
		;;
	test|t)
		for_devices switch_test
		;;
	build-copy|bc)
		for_devices build copy
		;;
	build-switch|bs)
		for_devices build copy switch
		;;
	build-test|bt)
		for_devices build copy switch_test
		;;
	build-boot|bb)
		for_devices build copy boot
		;;
	default)
		echo "Unknown operation: $operation" >&2
		exit 2
		;;
esac