Skip to content

Commit a54575d

Browse files
author
Matthew Fisher
committed
Merge pull request #4126 from Joshua-Anderson/add-shellcheck-controller
test(controller): run shell scripts through shellcheck
2 parents 9752e08 + 7125280 commit a54575d

4 files changed

Lines changed: 29 additions & 25 deletions

File tree

controller/Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ include ../includes.mk
44

55
all: build run
66

7+
SHELL_SCRIPTS = $(wildcard bin/*) $(shell find "." -name '*.sh')
8+
79
COMPONENT = controller
810
IMAGE = $(IMAGE_PREFIX)$(COMPONENT):$(BUILD_TAG)
911
DEV_IMAGE = $(REGISTRY)$(IMAGE)
@@ -73,6 +75,7 @@ setup-venv:
7375

7476
test-style: setup-venv
7577
venv/bin/flake8
78+
shellcheck $(SHELL_SCRIPTS)
7679

7780
test-unit: setup-venv test-style
7881
venv/bin/coverage run manage.py test --noinput api

controller/bin/boot

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,19 @@ export ETCD_PATH=${ETCD_PATH:-/deis/controller}
1616
export ETCD_TTL=${ETCD_TTL:-20}
1717

1818
# wait for etcd to be available
19-
until etcdctl --no-sync -C $ETCD ls >/dev/null 2>&1; do
19+
until etcdctl --no-sync -C "$ETCD" ls >/dev/null 2>&1; do
2020
echo "waiting for etcd at $ETCD..."
21-
sleep $(($ETCD_TTL/2)) # sleep for half the TTL
21+
sleep $((ETCD_TTL/2)) # sleep for half the TTL
2222
done
2323

2424
# wait until etcd has discarded potentially stale values
25-
sleep $(($ETCD_TTL+1))
25+
sleep $((ETCD_TTL+1))
2626

2727
function etcd_set_default {
2828
set +e
29-
ERROR="$(etcdctl --no-sync -C $ETCD mk $ETCD_PATH/$1 $2 2>&1 >/dev/null)"
30-
if [[ $? -ne 0 && $(echo $ERROR | grep -ive "key already exists") ]]; then
29+
ERROR="$(etcdctl --no-sync -C "$ETCD" mk "$ETCD_PATH/$1" "$2" 2>&1 >/dev/null)"
30+
31+
if [[ $? -ne 0 ]] && echo "$ERROR" | grep -iqve "key already exists" ]]; then
3132
echo "etcd_set_default: an etcd error occurred ($ERROR)"
3233
echo "aborting..."
3334
exit 1
@@ -37,18 +38,19 @@ function etcd_set_default {
3738

3839
function etcd_safe_mkdir {
3940
set +e
40-
ERROR="$(etcdctl --no-sync -C $ETCD mkdir $1 2>&1 >/dev/null)"
41-
if [[ $? -ne 0 && $(echo $ERROR | grep -ive "key already exists") ]]; then
41+
ERROR="$(etcdctl --no-sync -C "$ETCD" mkdir "$1" 2>&1 >/dev/null)"
42+
43+
if [[ $? -ne 0 ]] && echo "$ERROR" | grep -iqve "key already exists"; then
4244
echo "etcd_safe_mkdir: an etcd error occurred ($ERROR)"
4345
echo "aborting..."
4446
exit 1
4547
fi
4648
set -e
4749
}
4850

49-
etcd_set_default protocol ${DEIS_PROTOCOL:-http}
50-
etcd_set_default secretKey ${DEIS_SECRET_KEY:-`openssl rand -base64 64 | tr -d '\n'`}
51-
etcd_set_default builderKey ${DEIS_BUILDER_KEY:-`openssl rand -base64 64 | tr -d '\n'`}
51+
etcd_set_default protocol "${DEIS_PROTOCOL:-http}"
52+
etcd_set_default secretKey "${DEIS_SECRET_KEY:-$(openssl rand -base64 64 | tr -d '\n')}"
53+
etcd_set_default builderKey "${DEIS_BUILDER_KEY:-$(openssl rand -base64 64 | tr -d '\n')}"
5254
etcd_set_default registrationMode "enabled"
5355
etcd_set_default webEnabled 0
5456
etcd_set_default unitHostname default
@@ -61,16 +63,16 @@ etcd_safe_mkdir /deis/services
6163

6264
# run etcd data migrations
6365
echo "controller: running etcd data migrations..."
64-
for script in $(ls /app/migrations/data/*.sh);
66+
for script in /app/migrations/data/*.sh;
6567
do
66-
. $script;
68+
. "$script";
6769
done
6870
echo "controller: done running etcd data migrations."
6971

7072
# wait for confd to run once and install initial templates
71-
until confd -onetime -node $ETCD --confdir /app --log-level error; do
73+
until confd -onetime -node "$ETCD" --confdir /app --log-level error; do
7274
echo "controller: waiting for confd to write initial templates..."
73-
sleep $(($ETCD_TTL/2)) # sleep for half the TTL
75+
sleep $((ETCD_TTL/2)) # sleep for half the TTL
7476
done
7577

7678
cd /app
@@ -87,15 +89,14 @@ sudo -E -u deis gunicorn -c deis/gconf.py deis.wsgi &
8789
# smart shutdown on SIGTERM (SIGINT is handled by gunicorn)
8890
function on_exit() {
8991
GUNICORN_PID=$(cat /tmp/gunicorn.pid)
90-
kill -TERM $GUNICORN_PID 2>/dev/null
91-
wait $GUNICORN_PID 2>/dev/null
92+
kill -TERM "$GUNICORN_PID" 2>/dev/null
93+
wait "$GUNICORN_PID" 2>/dev/null
9294
exit 0
9395
}
9496
trap on_exit TERM
9597

9698
# spawn confd in the background to update services based on etcd changes
97-
confd -node $ETCD --confdir /app --log-level error --interval 5 &
98-
CONFD_PID=$!
99+
confd -node "$ETCD" --confdir /app --log-level error --interval 5 &
99100

100101
echo deis-controller running...
101102

@@ -113,9 +114,9 @@ if [[ ! -z $EXTERNAL_PORT ]]; then
113114

114115
# while the port is listening, publish to etcd
115116
while [[ ! -z $(netstat -lnt | awk "\$6 == \"LISTEN\" && \$4 ~ \".$PORT\" && \$1 ~ \"$PROTO.?\"") ]] ; do
116-
etcdctl --no-sync -C $ETCD set $ETCD_PATH/host $HOST --ttl $ETCD_TTL >/dev/null
117-
etcdctl --no-sync -C $ETCD set $ETCD_PATH/port $EXTERNAL_PORT --ttl $ETCD_TTL >/dev/null
118-
sleep $(($ETCD_TTL/2)) # sleep for half the TTL
117+
etcdctl --no-sync -C "$ETCD" set "$ETCD_PATH/host" "$HOST" --ttl "$ETCD_TTL" >/dev/null
118+
etcdctl --no-sync -C "$ETCD" set "$ETCD_PATH/port" "$EXTERNAL_PORT" --ttl "$ETCD_TTL" >/dev/null
119+
sleep $((ETCD_TTL/2)) # sleep for half the TTL
119120
done
120121

121122
# if the loop quits, something went wrong

controller/bin/reload

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/bin/bash
22

33
# gracefully reload gunicorn
4-
kill -HUP $(cat /tmp/gunicorn.pid)
4+
kill -HUP "$(cat /tmp/gunicorn.pid)"
55

66
exit 0

controller/migrations/data/0001.sh

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ if [[ "$($ETCDCTL get /deis/migrations/data/0001 2> /dev/null)" != "done" ]];
88
then
99
for i in $($ETCDCTL ls /deis/domains 2> /dev/null);
1010
do
11-
for j in $($ETCDCTL get $i);
11+
for j in $($ETCDCTL get "$i");
1212
do
13-
$ETCDCTL set /deis/domains/$j "$(basename $i)" 1> /dev/null;
13+
$ETCDCTL set "/deis/domains/$j" "$(basename "$i")" 1> /dev/null;
1414
echo "migrated $j"
1515
done;
16-
$ETCDCTL rm $i;
16+
$ETCDCTL rm "$i";
1717
done
1818
$ETCDCTL set /deis/migrations/data/0001 "done"
1919
fi

0 commit comments

Comments
 (0)