-
Notifications
You must be signed in to change notification settings - Fork 3
/
install.sh
executable file
·310 lines (258 loc) · 10.1 KB
/
install.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
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
#!/bin/sh
# This script installs viam-agent. It is intended to be run directly from a download, with a command such as:
# sudo /bin/sh -c "$(curl -fsSL https://storage.googleapis.com/packages.viam.com/apps/viam-agent/install.sh)"
# The preferred command should be copy/pasted from the robot's setup tab on app.viam.com
# It should auto-generate a command in the following format (with key/id info filled in)
# sudo /bin/sh -c "VIAM_API_KEY_ID=<KEYID> VIAM_API_KEY=<KEY> VIAM_PART_ID=<PARTID>; $(curl -fsSL https://storage.googleapis.com/packages.viam.com/apps/viam-agent/install.sh)"
ARCH=$(uname -m)
URL="https://storage.googleapis.com/packages.viam.com/apps/viam-agent/viam-agent-stable-$ARCH"
# Force will bypass all prompts by treating them as yes. May also be set as an environment variable when running as download.
# sudo /bin/sh -c "FORCE=1; $(curl -fsSL https://storage.googleapis.com/packages.viam.com/apps/viam-agent/install.sh)"
if [ "$1" = "--force" ] || [ "$1" = "-f" ]; then
FORCE=1
fi
# Removes previous viam-server service as installed from AppImage.
uninstall_old_service() {
if ! [ -f /etc/systemd/system/viam-server.service ]; then
return
fi
echo
echo "A previous install of viam-server has been detected. It needs to be removed before proceeding."
echo
echo "Note: This will only remove the installed binary and service files. Your /etc/viam.json config will be left in place."
if [ -z "$FORCE" ]; then
echo && echo
read -p "Remove previous viam-server service? (y/n): " REMOVE_OLD
if [ "$REMOVE_OLD" != "y" ]; then
echo "Installation cancelled."
exit 1
fi
fi
systemctl disable --now viam-server || (echo "Error disabling previous service" && exit 2)
rm -f /etc/systemd/system/viam-server.service /usr/local/bin/viam-server
systemctl daemon-reload
return
}
# Uses API keys and ID provided as env vars to fetch and install /etc/viam.sjon
fetch_config() {
if [ "$VIAM_API_KEY_ID" != "" ] && [ "$VIAM_API_KEY" != "" ] && [ "$VIAM_PART_ID" != "" ]; then
if [ -f /etc/viam.json ] && ! [ -z $FORCE ]; then
echo
echo "/etc/viam.json already exists."
echo
echo "Do you wish to overwrite it with an updated version fetched using the VIAM_PART_ID provided?"
echo && echo
read -p "Overwrite /etc/viam.json ? (y/n): " OVERWRITE_CREDS
if [ "$OVERWRITE_CREDS" != "y" ]; then
return
fi
fi
echo "Writing machine credentials to /etc/viam.json"
curl -fsSL \
-H "key_id:$VIAM_API_KEY_ID" \
-H "key:$VIAM_API_KEY" \
"https://app.viam.com/api/json1/config?client=true&id=$VIAM_PART_ID" \
-o /etc/viam.json
fi
}
# Verifies that NetworkManager is 1.30 or newer.
check_nm_version() {
which nmcli >/dev/null 2>&1 || return 1
NM_VERSION=$(nmcli --version | grep -Eo '[0-9]+.[0-9]+.[0-9]+')
NM_VERSION_MAJOR=$(echo $NM_VERSION | cut -d. -f1)
NM_VERSION_MINOR=$(echo $NM_VERSION | cut -d. -f2)
if [ $NM_VERSION_MAJOR -ge 1 ] && [ $NM_VERSION_MINOR -ge 30 ]; then
return 0
fi
return 1
}
is_bullseye() {
grep -q VERSION_CODENAME=bullseye /etc/os-release
}
add_network() {
if [ "$SSID" != "" ]; then
echo "Migrating $SSID connection settings"
FILENAME=
if [ "$PSK" != "" ]; then
nmcli --offline con add connection.id "$SSID" connection.type 802-11-wireless 802-11-wireless.ssid "$SSID" 802-11-wireless-security.key-mgmt wpa-psk 802-11-wireless-security.psk "$PSK" > /etc/NetworkManager/system-connections/"$SSID.nmconnection"
else
nmcli --offline con add connection.id "$SSID" connection.type 802-11-wireless 802-11-wireless.ssid "$SSID" > /etc/NetworkManager/system-connections/"$SSID.nmconnection"
fi
fi
}
migrate_wpa_conf() {
NETWORK=0
SSID=""
PSK=""
while read -r line; do
if echo $line | grep -qE 'network[[:space:]]?='; then
NETWORK=$(( NETWORK + 1 ))
add_network
SSID=""
PSK=""
elif echo $line | grep -qE 'ssid[[:space:]]?='; then
SSID=$(echo $line | cut -d= -f2 | tr -d \")
elif echo $line | grep -qE 'psk[[:space:]]?='; then
PSK=$(echo $line | cut -d= -f2 | tr -d \")
fi
done < /etc/wpa_supplicant/wpa_supplicant.conf
add_network
chmod 600 /etc/NetworkManager/system-connections/*.nmconnection
}
warn_nonm() {
echo
echo "Please manually install/activate NetworkManager to use network/provisioning services. Until then, you may notice errors in your logs regarding this."
echo
echo "You may disable the \"agent-provisioning\" subsystem in your device's config to avoid these."
echo
echo "To do so, click the \"Raw Json\" on the \"Config\" tab for your device at https://app.viam.com/ and set \"disable_subsystem\" to \"true\" and save"
echo
echo "This should not affect other Viam services, nor viam-server itself."
}
# Attempts to enable NetworkManager (only tested on Raspberry PiOS/Bullseye)
enable_networkmanager() {
systemctl is-enabled NetworkManager && check_nm_version && return
echo
echo "Viam provides a wifi management and device provisioning service. To use it, NetworkManager 1.30 (or newer) must be installed and active."
if check_nm_version || is_bullseye; then
# We can automate this.
echo
echo "This script can attempt to upgrade/activate NetworkManager for you, but may potentially break your existing network configuration."
echo
echo "It will attempt to migrate any existing wifi connections from wpa_supplicant, but may not always work."
echo
echo "If you are connected through SSH via WiFi, you may be disconnected. If this happens, please wait several minutes to see if the connection resumes."
echo
echo "If after 5 minutes, you remain disconnected, please look for a provisioning hotspot to join. You may need to reboot for this to appear."
if [ -z "$FORCE" ]; then
echo && echo
read -p "Proceed with NetworkManager upgrade/activation? (y/n): " DO_NM_INSTALL
if [ "$DO_NM_INSTALL" != "y" ]; then
echo "NetworkManager upgrade/activation skipped."
warn_nonm
return 1
fi
fi
else
# We can't automate this.
warn_nonm
return 1
fi
if is_bullseye; then
echo 'deb http://deb.debian.org/debian/ bullseye-backports main' > /etc/apt/sources.list.d/backports.list && \
apt update && apt install -y network-manager/bullseye-backports || (echo "Failed to upgrade NetworkManager" && return 1)
fi
if [ -f "/etc/wpa_supplicant/wpa_supplicant.conf" ]; then
migrate_wpa_conf
fi
if systemctl cat NetworkManager >/dev/null; then
systemctl enable --now NetworkManager || (echo "Failed to activate NetworkManager" && return 1)
systemctl disable dhcpcd
else
return 1
fi
n=1
while [ "$n" -le 30 ]; do
systemctl is-enabled NetworkManager && break
n=$(( n + 1 ))
sleep 1
done
if ! systemctl is-enabled NetworkManager; then
echo
echo "Error: Was unable to activate NetworkManager."
return 1
fi
nmcli g reload
return 1
}
# Main
main() {
if [ "$(uname -s)" != "Linux" ] || ! [ "$ARCH" = "x86_64" -o "$ARCH" = "aarch64" ]; then
echo
echo "Viam Agent is currently only available for Linux on x86_64 (amd64) and aarch64 (arm64)."
echo
echo "Please see https://docs.viam.com/get-started/installation/ to install on other platforms."
exit 1
fi
if [ "$ARCH" = "aarch64" ] && ! [ -e /lib/ld-linux-aarch64.so.1 ]; then
echo
echo "Your kernel reports as aarch64 (arm64), but userspace is missing /lib/ld-linux-aarch64.so.1"
echo "Please ensure that you've installed a fully 64-bit version of your distro, including userspace, then retry this install."
exit 1
elif [ "$ARCH" = "x86_64" ] && ! [ -e /lib64/ld-linux-x86-64.so.2 ]; then
echo
echo "Your kernel reports as x86_64 (amd64), but userspace is missing /lib/ld-linux-x86-64.so.2"
echo "Please ensure that you've installed a fully 64-bit version of your distro, including userspace, then retry this install."
exit 1
fi
if ! [ -d /etc/systemd/system ]; then
echo
echo "Viam Agent is only supported on systems using systemd."
exit 1
fi
if [ "$(id -u)" -ne 0 ]; then
echo
echo "This install script must be run as root. Try running via sudo."
exit 1
fi
# Remove old AppImage based install
uninstall_old_service
# Attempt to fetch the config using API keys (if set)
fetch_config
if ! [ -f /etc/viam.json ]; then
echo
echo "WARNING: No configuration file found at /etc/viam.json and no (valid) API keys were provided to automatically download one."
echo
echo "Viam agent may fail to fully start, or may immediately enter provisioning mode after installation, which will disconnect wifi."
echo
echo "It is recommended that you re-run this installer with the exact command (including API keys) provided on the \"Setup\" tab for your robot at https://app.viam.com/"
echo
echo "Alternately, manually install /etc/viam.json, then re-run this installation."
if [ -z "$FORCE" ]; then
echo && echo
read -p "Continue anyway (not recommended)? (y/n): " CONTINUE
if [ "$CONTINUE" != "y" ]; then
echo "Installation aborted."
exit 1
fi
fi
fi
if [ -f /etc/systemd/system/viam-agent.service ] || [ -f /usr/local/lib/systemd/system/viam-agent.service ]; then
echo
echo "It appears viam-agent is already installed. You can restart it with 'systemctl restart viam-agent' if it's not running."
if [ -z "$FORCE" ]; then
echo && echo
read -p "Force reinstall anyway? (y/n): " DO_REINSTALL
if [ "$DO_REINSTALL" != "y" ]; then
echo "Installation aborted."
exit 1
fi
fi
systemctl stop viam-agent
if [ $? -ne 0 ]; then
echo
echo "Error stopping existing viam-agent service for reinstall."
exit 2
fi
fi
mkdir -p /opt/viam/tmp && cd /opt/viam/tmp && curl -fL -o viam-agent-temp-$ARCH "$URL" && chmod 755 viam-agent-temp-$ARCH
if [ $? -ne 0 ]; then
echo
echo "Error downloading agent binary. Please correct any errors mentioned above and try again."
exit 2
fi
./viam-agent-temp-$ARCH --install
if [ $? -ne 0 ]; then
echo
echo "Error installing viam-agent. Please correct any errors mentioned above and try again."
exit 2
fi
enable_networkmanager
systemctl restart viam-agent
echo && echo && echo
echo "Viam Agent installed successfully. You may start/stop/restart it via systemd's 'systemctl' command."
echo "Example: 'systemctl restart viam-agent'"
echo
echo "It has already been started for you and set to start automatically at boot time."
}
main