-
Notifications
You must be signed in to change notification settings - Fork 3
/
install.sh
598 lines (556 loc) · 17.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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
#!/bin/bash
if [[ $EUID -ne 0 ]]; then
echo "This script must be run as root" 1>&2
exit 1
fi
APP_MAIN_NAME="netbird"
APP_UI_NAME="netbird-ui"
REPO_USER="netbirdio"
REPO_MAIN="netbird"
# Set Default Variables
INSTALL_APP=true
INSTALL_UI=false
INSTALL_DOCKER_BASED=false
INSTALL_SERVICE=true
PRECONFIGURE=true
DISPLAY_PROMPTS=true
SETUP_KEY=""
MANAGEMENT_URL="https://api.wiretrustee.com:33073"
BASE_URL="https://github.com/${REPO_USER}/${REPO_MAIN}/releases/download"
DOCKER_NAME="netbird"
DOCKER_HOSTNAME=$(hostname)
ALREADY_INSTALLED=false
if command -v netbird >/dev/null; then
ALREADY_INSTALLED=true
fi
# Color Variables
green='\e[32m'
# blue='\e[34m'
red="\e[31m"
clear='\e[0m'
yellow='\e[33m'
getLatestRelease() {
curl --silent \
"https://api.github.com/repos/${REPO_USER}/${REPO_MAIN}/releases/latest" \
| grep tag_name \
| sed -E 's/.*"([^"]+)".*/\1/' | sed 's/v//g'
}
showHelp () {
echo "$0 - Install Netbird"
echo " "
echo "$0 [options]"
echo " "
echo "options:"
echo " -h, --help show brief help"
echo " -ia, --install-app Install Netbird Binary"
echo " -iui, --install-ui Install Netbird UI Binary"
echo " -iv, --install-version Target Install version (defaults to latest, ${VERSION})"
echo " -d, --docker Install Netbird in Docker"
echo " -ns, --no-service Don't install service"
echo " -np, --no-preconfigure Don't Preconfigure Client"
echo " -b, --base-url Base URL For downloads (For Air-Gapped Systems)"
echo " -m, --management-url Management URL (Defaults to Netbird SaaS)"
echo " -sk, --setup-key Setup Key"
echo " -q, --quiet Don't present any prompts"
echo "Docker Options:"
echo " -dn, --docker-name Set docker container name"
echo " -dh, --docker-hostname Set docker hostname"
}
VERSION=$(getLatestRelease)
# Pretty Box Functions
function prettyBoxCurrent () {
echo -e "[ ${yellow}CURRENT${clear} ] ${1}"
}
function prettyBoxComplete () {
echo -e "[ ${green}COMPLETE${clear} ] ${1}"
}
function prettyBoxFailed () {
echo -e "[ ${red}FAILED${clear} ] ${1}"
# shellcheck disable=SC2086
if [ -z ${2} ]; then
# shellcheck disable=SC2086
exit ${2}
fi
}
function prettyError () {
echo -e "${red}${1}${clear}"
}
# detect the platform
OS="$(uname)"
case $OS in
Linux)
OS='linux'
;;
Darwin)
OS='darwin'
;;
*)
echo 'OS not supported'
exit 2
;;
esac
OS_type="$(uname -m)"
case "$OS_type" in
x86_64|amd64)
OS_type='amd64'
;;
i?86|x86)
OS_type='386'
;;
aarch64|arm64)
OS_type='arm64'
;;
*)
echo "OS type ${OS_type} not supported"
exit 2
;;
esac
# Test Perams
while test $# -gt 0; do
case "$1" in
-h|--help)
showHelp
exit 0
;;
-ia|--install-app)
INSTALL_APP=true
shift
;;
-iui|--install-ui)
INSTALL_UI=true
shift
;;
-d|--docker)
INSTALL_DOCKER_BASED=true
if [ ! -x "$(command -v docker)" ]; then
prettyError "Docker not installed"
exit 1
fi
shift
;;
-dn)
shift
DOCKER_NAME=${1}
shift
;;
--docker-name*)
# shellcheck disable=SC2001
DOCKER_NAME=$(echo "${1}" | sed -e 's/^[^=]*=//g')
shift
;;
-dh)
shift
DOCKER_HOSTNAME=${1}
shift
;;
--docker-hostname*)
# shellcheck disable=SC2001
DOCKER_HOSTNAME=$(echo "${1}" | sed -e 's/^[^=]*=//g')
shift
;;
-np|--no-preconfigure)
PRECONFIGURE=false
shift
;;
-m)
shift
MANAGEMENT_URL=${1}
shift
;;
--management-url*)
# shellcheck disable=SC2001
MANAGEMENT_URL=$(echo "${1}" | sed -e 's/^[^=]*=//g')
shift
;;
-b)
shift
BASE_URL=${1}
shift
;;
--base-url*)
# shellcheck disable=SC2001
BASE_URL=$(echo "${1}" | sed -e 's/^[^=]*=//g')
shift
;;
-iv)
shift
if [ ! "${1}" == "latest" ]; then
VERSION=${1}
fi
shift
;;
--install-version*)
# shellcheck disable=SC2001
VTMP=$(echo "${1}" | sed -e 's/^[^=]*=//g')
if [ ! "${VTMP}" == "latest" ]; then
VERSION=${VTMP}
fi
VTMP=
shift
;;
-sk)
shift
SETUP_KEY=${1}
shift
;;
--setup-key*)
# shellcheck disable=SC2001
SETUP_KEY=$(echo "${1}" | sed -e 's/^[^=]*=//g')
shift
;;
-ns|--no-service)
INSTALL_SERVICE=false
shift
;;
-q|--quiet)
DISPLAY_PROMPTS=false
shift
;;
*)
break
;;
esac
done
function showInstallSummary () {
echo -e "------------------------------------------------"
echo -e "| Install Summary"
echo -e "------------------------------------------------"
echo -e "| Target Operating System: ${green}${OS}${clear}"
echo -e "| Target Arch: ${green}${OS_type}${clear}"
echo -e "| Target Version: ${green}v${VERSION}${clear}"
if ${INSTALL_APP}; then
echo -e "| Install Netbird Binary: ${green}Yes${clear}"
else
echo -e "| Install Netbird Binary: ${red}No${clear}"
fi
if ${INSTALL_UI}; then
echo -e "| Install UI Binary: ${green}Yes${clear}"
else
echo -e "| Install UI Binary: ${red}No${clear}"
fi
if ${INSTALL_DOCKER_BASED}; then
echo -e "| Install Netbird in Docker: ${green}Yes${clear}"
else
echo -e "| Install Netbird in Docker: ${red}No${clear}"
fi
if ${PRECONFIGURE}; then
echo -e "| Pre-Configure Client: ${green}Yes${clear}"
else
echo -e "| Pre-Configure Client: ${red}No${clear}"
fi
echo -e "| Base URL: ${green}${BASE_URL}${clear}"
echo -e "| Management URL: ${green}${MANAGEMENT_URL}${clear}"
echo -e "| Setup Key: ${green}${SETUP_KEY}${clear}"
echo -e "|"
if ${ALREADY_INSTALLED}; then
echo -e "| Native Binary Installed ${green}Yes${clear}"
else
echo -e "| Native Binary Installed ${red}No${clear}"
fi
echo -e "------------------------------------------------"
}
function checkContinueInstall () {
if ${DISPLAY_PROMPTS}; then
echo
read -r -p "Are you sure you want to continue? [Y/n]: " CONTINUE_INSTALL
if [[ ! ${CONTINUE_INSTALL} =~ ^[Yy]$ ]]; then
echo "Cool, See you soon!"
exit 0
fi
fi
}
function installNativeDownloadBinarys () {
# Download Binary tar files
if ${INSTALL_APP}; then
prettyBoxCurrent "Downloading ${APP_MAIN_NAME}"
if curl -OLfsS "${APP_URL}"; then
prettyBoxComplete "Downloaded ${APP_MAIN_NAME}"
else
prettyBoxFailed "Failed to download ${APP_MAIN_NAME}" 1
fi
fi
if ${INSTALL_UI}; then
prettyBoxCurrent "Downloading ${APP_UI_NAME}"
if curl -OLfsS "${UI_URL}"; then
prettyBoxComplete "Downloaded ${APP_UI_NAME}"
else
prettyBoxFailed "Failed to download ${APP_UI_NAME}" 1
fi
fi
}
function installNativeExtractBinarys () {
# Extract Binary tar files
if ${INSTALL_APP}; then
prettyBoxCurrent "Extracting ${APP_MAIN_NAME}"
if tar xf "${APP_FILENAME}.tar.gz"; then
prettyBoxComplete "Extracted ${APP_MAIN_NAME}"
else
prettyBoxFailed "Failed to extract ${APP_MAIN_NAME}" 1
fi
fi
if ${INSTALL_UI}; then
prettyBoxCurrent "Extracting ${APP_UI_NAME}"
if tar xf "${UI_FILENAME}.tar.gz"; then
prettyBoxComplete "Extracted ${APP_UI_NAME}"
else
prettyBoxFailed "Failed to extract ${APP_UI_NAME}" 1
fi
fi
}
function installNativePlaceBinarys () {
if ${INSTALL_APP}; then
case "${OS}" in
'linux')
# Copy File
prettyBoxCurrent "Copying ${APP_MAIN_NAME} to /usr/bin/${APP_MAIN_NAME}.new"
if cp "${APP_MAIN_NAME}" "/usr/bin/${APP_MAIN_NAME}.new"; then
prettyBoxComplete "Binary copied succesfully"
else
prettyBoxFailed "Failed to copy Binary" 1
fi
# Set Binary Mode
prettyBoxCurrent "Setting /usr/bin/${APP_MAIN_NAME}.new to 0755"
if chmod 775 "/usr/bin/${APP_MAIN_NAME}.new"; then
prettyBoxComplete "Binary modes set succesfully"
else
prettyBoxFailed "Failed to set Binary file modes" 1
fi
# Set owner and group
prettyBoxCurrent "Setting /usr/bin/${APP_MAIN_NAME}.new owner and group to root"
if chown root:root "/usr/bin/${APP_MAIN_NAME}.new"; then
prettyBoxComplete "Binary owner and group set succesfully"
else
prettyBoxFailed "Failed to set Binary File owner and group" 1
fi
# Overwrite /usr/bin/netbird
prettyBoxCurrent "Overwriting /usr/bin/${APP_MAIN_NAME} with /usr/bin/${APP_MAIN_NAME}.new"
if mv "/usr/bin/${APP_MAIN_NAME}.new" "/usr/bin/${APP_MAIN_NAME}"; then
prettyBoxComplete "Binary Overwritten succesfully"
else
prettyBoxFailed "Failed to overwrite /usr/bin/${APP_MAIN_NAME}" 1
fi
;;
'darwin')
# Make sure /usr/local/bin exists
if [ ! -d /usr/local/bin ]; then
prettyBoxCurrent "Create /usr/local/bin"
# shellcheck disable=SC2174
if mkdir -m 0555 -p /usr/local/bin; then
prettyBoxComplete "/usr/local/bin Created Successfully"
else
prettyBoxFailed "Failed to create /usr/local/bin" 1
fi
fi
# Copy Binary
prettyBoxCurrent "Copying ${APP_MAIN_NAME} to /usr/local/bin/${APP_MAIN_NAME}.new"
if cp "${APP_MAIN_NAME}" "/usr/local/bin/${APP_MAIN_NAME}.new"; then
prettyBoxComplete "Binary copied succesfully"
else
prettyBoxFailed "Failed to copy Binary" 1
fi
# Set Binary Mode
prettyBoxCurrent "Setting /usr/local/bin/${APP_MAIN_NAME}.new to a=x"
if chmod a=x "/usr/local/bin/${APP_MAIN_NAME}.new"; then
prettyBoxComplete "Binary File modes set succesfully"
else
prettyBoxFailed "Failed to set Binary File modes" 1
fi
# Overwrite /usr/bin/netbird
prettyBoxCurrent "Overwriting /usr/local/bin/${APP_MAIN_NAME} with /usr/local/bin/${APP_MAIN_NAME}.new"
if mv "/usr/local/bin/${APP_MAIN_NAME}.new" "/usr/local/bin/${APP_MAIN_NAME}"; then
prettyBoxComplete "Binary Overwritten succesfully"
else
prettyBoxFailed "Failed to overwrite /usr/bin/${APP_MAIN_NAME}" 1
fi
;;
esac
fi
if ${INSTALL_UI}; then
case "${OS}" in
'linux')
# Copy Binary
prettyBoxCurrent "Copying ${APP_UI_NAME} to /usr/bin/${APP_UI_NAME}.new"
if cp "${APP_UI_NAME}" "/usr/bin/${APP_UI_NAME}.new"; then
prettyBoxComplete "Binary copied succesfully"
else
prettyBoxFailed "Failed to copy Binary" 1
fi
# Set Binary Mode
prettyBoxCurrent "Setting /usr/bin/${APP_UI_NAME}.new to 0755"
if chmod 775 "/usr/bin/${APP_UI_NAME}.new"; then
prettyBoxComplete "Binary file modes set succesfully"
else
prettyBoxFailed "Failed to set Binary file modes" 1
fi
# Set owner and group
prettyBoxCurrent "Setting /usr/bin/${APP_UI_NAME}.new owner and group to root"
if chown root:root "/usr/bin/${APP_UI_NAME}.new"; then
prettyBoxComplete "Binary file owner and group set succesfully"
else
prettyBoxFailed "Failed to set Binary file owner and group" 1
fi
# Overwrite /usr/bin/netbird-ui
prettyBoxCurrent "Overwriting /usr/bin/${APP_UI_NAME} with /usr/bin/${APP_UI_NAME}.new"
if mv "/usr/bin/${APP_UI_NAME}.new" "/usr/bin/${APP_UI_NAME}"; then
prettyBoxComplete "Binary Overwritten succesfully"
else
prettyBoxFailed "Failed to overwrite /usr/bin/${APP_UI_NAME}" 1
fi
;;
'darwin')
# Make sure /usr/local/bin exists
if [ ! -d /usr/local/bin ]; then
prettyBoxCurrent "Create /usr/local/bin"
# shellcheck disable=SC2174
if mkdir -m 0555 -p /usr/local/bin; then
prettyBoxComplete "/usr/local/bin Created Successfully"
else
prettyBoxFailed "Failed to create /usr/local/bin" 1
fi
fi
# Copy Binary
prettyBoxCurrent "Copying ${APP_UI_NAME} to /usr/local/bin/${APP_UI_NAME}.new"
if cp "${APP_UI_NAME}" "/usr/local/bin/${APP_UI_NAME}.new"; then
prettyBoxComplete "Binary copied succesfully"
else
prettyBoxFailed "Failed to copy Binary" 1
fi
# Set Binary Mode
prettyBoxCurrent "Setting /usr/local/bin/${APP_UI_NAME}.new to a=x"
if chmod a=x "/usr/local/bin/${APP_UI_NAME}.new"; then
prettyBoxComplete "Binary file modes set succesfully"
else
prettyBoxFailed "Failed to set Binary file modes" 1
fi
# Overwrite /usr/bin/netbird
prettyBoxCurrent "Overwriting /usr/local/bin/${APP_UI_NAME} with /usr/local/bin/${APP_MAIN_NAME}.new"
if mv "/usr/local/bin/${APP_UI_NAME}.new" "/usr/local/bin/${APP_UI_NAME}"; then
prettyBoxComplete "Binary Overwritten succesfully"
else
prettyBoxFailed "Failed to overwrite /usr/bin/${APP_UI_NAME}" 1
fi
;;
esac
fi
}
function installNativeService () {
if ${INSTALL_APP}; then
if ${INSTALL_SERVICE}; then
case ${OS} in
'linux')
NETBIRD_BIN=/usr/bin/netbird
;;
'darwin')
NETBIRD_BIN=/usr/local/bin/netbird
;;
esac
if ${ALREADY_INSTALLED}; then
# Stop Service
prettyBoxCurrent "Stopping Service"
if ${NETBIRD_BIN} service stop >/dev/null; then
prettyBoxComplete "Service Successfully Stopped"
else
prettyBoxFailed "Failed to stop Service" 1
fi
# Uninstall Service
prettyBoxCurrent "Uninstalling Service"
if ${NETBIRD_BIN} service uninstall >/dev/null; then
prettyBoxComplete "Service Successfully Uninstalled"
else
prettyBoxFailed "Failed to uninstall service" 1
fi
fi
# Install Service
prettyBoxCurrent "Installing Service"
if ${NETBIRD_BIN} service install >/dev/null; then
prettyBoxComplete "Service Successfully Installed"
else
prettyBoxFailed "Failed to install service"
echo
prettyError "*****************************************************"
prettyError "* IF ABOVE SAYS \"INIT ALREADY EXISTS\" OR SOMETHING SIMMILAR"
prettyError "* RUN sudo ${NETBIRD_BIN} service uninstall"
prettyError "*****************************************************"
exit 1
fi
# Start Service
prettyBoxCurrent "Starting Service"
if ${NETBIRD_BIN} service start >/dev/null; then
prettyBoxComplete "Service Successfully Started"
else
prettyBoxFailed "Failed to start service"
exit 1
fi
fi
fi
}
function installNativePreconfigure () {
if ${PRECONFIGURE}; then
case ${OS} in
'linux')
NETBIRD_BIN=/usr/bin/netbird
;;
'darwin')
NETBIRD_BIN=/usr/local/bin/netbird
;;
esac
CONFIGURE_ARGS="up"
if [ ! "${MANAGEMENT_URL}" == "https://api.wiretrustee.com:33073" ]; then
CONFIGURE_ARGS+=" --management-url ${MANAGEMENT_URL}"
fi
if [ ! "${SETUP_KEY}" == "" ]; then
CONFIGURE_ARGS+=" --setup-key ${SETUP_KEY}"
fi
fi
# shellcheck disable=SC2086
${NETBIRD_BIN} ${CONFIGURE_ARGS}
}
function installNative () {
# Create Tempory Directory
tmp_dir=$(mktemp -d 2>/dev/null || mktemp -d -t 'netbird-install.XXXXXXXXXX')
cd "$tmp_dir" || exit 1
APP_FILENAME="${APP_MAIN_NAME}_${VERSION}_${OS}_${OS_type}"
UI_FILENAME="${APP_UI_NAME}-${OS}_${VERSION}_${OS}_${OS_type}"
# Generate App Binary URLS
APP_URL="${BASE_URL}/v${VERSION}/${APP_FILENAME}.tar.gz"
UI_URL="${BASE_URL}/v${VERSION}/${UI_FILENAME}.tar.gz"
installNativeDownloadBinarys
installNativeExtractBinarys
installNativePlaceBinarys
installNativeService
installNativePreconfigure
}
function installDocker () {
if ${INSTALL_DOCKER_BASED}; then
if [ "${SETUP_KEY}" == "" ]; then
prettyError "You MUST enter a Setup Key for a docker install"
exit 1
fi
prettyBoxCurrent "Pulling Container"
if docker pull "netbirdio/netbird:${VERSION}"; then
prettyBoxComplete "Pull Complete"
else
prettyBoxFailed "Failed to pull container" 1
fi
DOCKER_COMMAND="docker run --rm --cap-add=NET_ADMIN -d"
DOCKER_COMMAND+=" --name ${DOCKER_NAME}"
DOCKER_COMMAND+=" --hostname ${DOCKER_HOSTNAME}"
DOCKER_COMMAND+=" -e NB_SETUP_KEY=${SETUP_KEY}"
DOCKER_COMMAND+=" -e NB_MANAGEMENT_URL=${MANAGEMENT_URL}"
DOCKER_COMMAND+=" -v netbird-client:/etc/netbird"
DOCKER_COMMAND+=" netbirdio/netbird:${VERSION}"
prettyBoxCurrent "Starting Container"
if ${DOCKER_COMMAND}; then
prettyBoxComplete "Successfully Started"
else
prettyBoxFailed "Failed to start container" 1
fi
fi
}
showInstallSummary
checkContinueInstall
if ${INSTALL_DOCKER_BASED}; then
installDocker
else
installNative
fi