1
1
#! /usr/bin/env bash
2
2
3
3
# stops the execution if a command or pipeline has an error
4
- set -eu
4
+ set -euxo pipefail
5
5
6
6
# Tinkerbell stack Linux setup script
7
7
#
@@ -38,7 +38,7 @@ NEXT="${GREEN:-}NEXT:${RESET:-}"
38
38
get_distribution () (
39
39
local lsb_dist=" "
40
40
# Every system that we officially support has /etc/os-release
41
- if [ -r /etc/os-release ]; then
41
+ if [[ -r /etc/os-release ] ]; then
42
42
# shellcheck disable=SC1091
43
43
lsb_dist=" $( . /etc/os-release && echo " $ID " ) "
44
44
fi
@@ -50,7 +50,7 @@ get_distribution() (
50
50
get_distro_version () (
51
51
local lsb_version=" 0"
52
52
# Every system that we officially support has /etc/os-release
53
- if [ -r /etc/os-release ]; then
53
+ if [[ -r /etc/os-release ] ]; then
54
54
# shellcheck disable=SC1091
55
55
lsb_version=" $( . /etc/os-release && echo " $VERSION_ID " ) "
56
56
fi
@@ -112,10 +112,10 @@ setup_networking() (
112
112
fi
113
113
114
114
NAT_INTERFACE=" "
115
- if [ -r .nat_interface ]; then
115
+ if [[ -r .nat_interface ] ]; then
116
116
NAT_INTERFACE=$( cat .nat_interface)
117
117
fi
118
- if [ -n " $NAT_INTERFACE " ] && ip addr show " $NAT_INTERFACE " & > /dev/null; then
118
+ if [[ -n $NAT_INTERFACE ] ] && ip addr show " $NAT_INTERFACE " & > /dev/null; then
119
119
# TODO(nshalman) the terraform code would just run these commands as-is once
120
120
# but it would be nice to make these more persistent based on OS
121
121
iptables -A FORWARD -i " $TINKERBELL_NETWORK_INTERFACE " -o " $NAT_INTERFACE " -j ACCEPT
@@ -135,10 +135,10 @@ setup_networking_manually() (
135
135
136
136
setup_network_forwarding () (
137
137
# enable IP forwarding for docker
138
- if [ " $( sysctl -n net.ipv4.ip_forward) " != " 1 " ] ; then
139
- if [ -d /etc/sysctl.d ]; then
138
+ if (( $(sysctl - n net.ipv4 .ip_forward) != 1 )) ; then
139
+ if [[ -d /etc/sysctl.d ] ]; then
140
140
echo " net.ipv4.ip_forward=1" > /etc/sysctl.d/99-tinkerbell.conf
141
- elif [ -f /etc/sysctl.conf ]; then
141
+ elif [[ -f /etc/sysctl.conf ] ]; then
142
142
echo " net.ipv4.ip_forward=1" >> /etc/sysctl.conf
143
143
fi
144
144
@@ -171,7 +171,7 @@ setup_networking_netplan() (
171
171
)
172
172
173
173
setup_networking_ubuntu_legacy () (
174
- if [ ! -f /etc/network/interfaces ]; then
174
+ if ! [[ -f /etc/network/interfaces ] ]; then
175
175
echo " $ERR file /etc/network/interfaces not found"
176
176
exit 1
177
177
fi
224
224
225
225
local cfgfile=" /etc/sysconfig/network-scripts/ifcfg-$TINKERBELL_NETWORK_INTERFACE "
226
226
227
- if [ -f " $cfgfile " ]; then
227
+ if [[ -f $cfgfile ] ]; then
228
228
echo " $ERR network config already exists: $cfgfile "
229
229
echo " $BLANK Please update it to match this configuration:"
230
230
echo " $content "
@@ -245,12 +245,12 @@ setup_osie() (
245
245
246
246
local osie_current=$STATEDIR /webroot/misc/osie/current
247
247
local tink_workflow=$STATEDIR /webroot/workflow/
248
- if [ ! -d " $osie_current " ] || [ ! -d " $tink_workflow " ]; then
248
+ if [[ ! -d $osie_current ]] || [[ ! -d $tink_workflow ] ]; then
249
249
mkdir -p " $osie_current "
250
250
mkdir -p " $tink_workflow "
251
251
pushd " $SCRATCH "
252
252
253
- if [ -z " ${TB_OSIE_TAR:- } " ]; then
253
+ if [[ -z ${TB_OSIE_TAR:- } ] ]; then
254
254
curl " ${OSIE_DOWNLOAD_LINK} " -o ./osie.tar.gz
255
255
tar -zxf osie.tar.gz
256
256
else
@@ -305,7 +305,7 @@ check_container_status() (
305
305
--filter " event=health_status" \
306
306
--format ' {{.Status}}' )
307
307
308
- if [ " $status " != " health_status: healthy" ]; then
308
+ if [[ $status != " health_status: healthy" ] ]; then
309
309
echo " $ERR $container_name is not healthy. status: $status "
310
310
exit 1
311
311
fi
@@ -314,7 +314,7 @@ check_container_status() (
314
314
generate_certificates () (
315
315
mkdir -p " $STATEDIR /certs"
316
316
317
- if [ ! -f " $STATEDIR /certs/ca.json" ]; then
317
+ if ! [[ -f " $STATEDIR /certs/ca.json" ] ]; then
318
318
jq \
319
319
' .
320
320
| .names[0].L = $facility
@@ -325,7 +325,7 @@ generate_certificates() (
325
325
> " $STATEDIR /certs/ca.json"
326
326
fi
327
327
328
- if [ ! -f " $STATEDIR /certs/server-csr.json" ]; then
328
+ if ! [[ -f " $STATEDIR /certs/server-csr.json" ] ]; then
329
329
jq \
330
330
' .
331
331
| .hosts += [ $ip, "tinkerbell.\($facility).packet.net" ]
@@ -347,13 +347,13 @@ generate_certificates() (
347
347
local certs_dir=" /etc/docker/certs.d/$TINKERBELL_HOST_IP "
348
348
349
349
# copy public key to NGINX for workers
350
- if ! cmp --quiet " $STATEDIR " /certs/ca.pem " $STATEDIR /webroot/workflow/ca.pem" ; then
351
- cp " $STATEDIR " /certs/ca.pem " $STATEDIR /webroot/workflow/ca.pem"
350
+ if ! cmp --quiet " $STATEDIR /certs/ca.pem" " $STATEDIR /webroot/workflow/ca.pem" ; then
351
+ cp " $STATEDIR /certs/ca.pem" " $STATEDIR /webroot/workflow/ca.pem"
352
352
fi
353
353
354
354
# update host to trust registry certificate
355
355
if ! cmp --quiet " $STATEDIR /certs/ca.pem" " $certs_dir /tinkerbell.crt" ; then
356
- if [ ! -d " $certs_dir /tinkerbell.crt " ]; then
356
+ if ! [[ -d " $certs_dir /" ] ]; then
357
357
# The user will be told to create the directory
358
358
# in the next block, if copying the certs there
359
359
# fails.
@@ -363,7 +363,7 @@ generate_certificates() (
363
363
echo " $ERR please copy $STATEDIR /certs/ca.pem to $certs_dir /tinkerbell.crt"
364
364
echo " $BLANK and run $0 again:"
365
365
366
- if [ ! -d " $certs_dir " ]; then
366
+ if ! [[ -d $certs_dir ] ]; then
367
367
echo " sudo mkdir -p '$certs_dir '"
368
368
fi
369
369
echo " sudo cp '$STATEDIR /certs/ca.pem' '$certs_dir /tinkerbell.crt'"
@@ -406,7 +406,7 @@ bootstrap_docker_registry() (
406
406
407
407
setup_docker_registry () (
408
408
local registry_images=" $STATEDIR /registry"
409
- if [ ! -d " $registry_images " ]; then
409
+ if ! [[ -d $registry_images ] ]; then
410
410
mkdir -p " $registry_images "
411
411
fi
412
412
start_registry
@@ -427,13 +427,15 @@ command_exists() (
427
427
)
428
428
429
429
check_command () (
430
- if command_exists " $1 " ; then
431
- echo " $BLANK Found prerequisite: $1 "
432
- return 0
433
- else
434
- echo " $ERR Prerequisite command not installed: $1 "
430
+ if ! command_exists " $1 " ; then
431
+ echo " $ERR Prerequisite executable command not found: $1 "
435
432
return 1
436
433
fi
434
+ if ! [[ -s " $( which " $1 " ) " ]]; then
435
+ echo " $ERR Prerequisite command is an empty file: $1 "
436
+ fi
437
+ echo " $BLANK Found prerequisite: $1 "
438
+ return 0
437
439
)
438
440
439
441
check_prerequisites () (
@@ -469,15 +471,15 @@ check_prerequisites() (
469
471
;;
470
472
esac
471
473
472
- if [ $ failed -eq 1 ] ; then
474
+ if (( failed == 1 )) ; then
473
475
echo " $ERR Prerequisites not met. Please install the missing commands and re-run $0 ."
474
476
exit 1
475
477
fi
476
478
)
477
479
478
480
whats_next () (
479
481
echo " $NEXT 1. Enter /vagrant/deploy and run: source ../.env; docker-compose up -d"
480
- echo " $BLANK 2. Try executing your fist workflow."
482
+ echo " $BLANK 2. Try executing your first workflow."
481
483
echo " $BLANK Follow the steps described in https://tinkerbell.org/examples/hello-world/ to say 'Hello World!' with a workflow."
482
484
)
483
485
@@ -489,7 +491,7 @@ do_setup() (
489
491
echo " $INFO starting tinkerbell stack setup"
490
492
check_prerequisites " $lsb_dist " " $lsb_version "
491
493
492
- if [ ! -f " $ENV_FILE " ]; then
494
+ if ! [[ -f $ENV_FILE ] ]; then
493
495
echo " $ERR Run './generate-env.sh network-interface > \" $ENV_FILE \" ' before continuing."
494
496
exit 1
495
497
fi
@@ -503,7 +505,7 @@ do_setup() (
503
505
setup_docker_registry
504
506
505
507
echo " $INFO tinkerbell stack setup completed successfully on $lsb_dist server"
506
- whats_next
508
+ whats_next | tee /tmp/post-setup-message
507
509
)
508
510
509
511
# wrapped up in a function so that we have some protection against only getting
0 commit comments