Skip to content

Commit eebe555

Browse files
Gabriel MonroyMatthew Fisher
authored andcommitted
refactor(bin/boot) consolidate into a single script with debug and optional publishing
1 parent 721035f commit eebe555

4 files changed

Lines changed: 46 additions & 58 deletions

File tree

bin/boot

Lines changed: 46 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,17 @@
33
# This script is designed to be run inside the container
44
#
55

6+
# fail hard and fast even on pipelines
7+
set -eo pipefail
8+
9+
# set debug based on envvar
10+
[[ $DEBUG ]] && set -x
11+
612
# configure etcd
713
export ETCD=${ETCD:-172.17.42.1:4001}
814
export ETCD_PATH=${ETCD_PATH:-/deis/controller}
915
export ETCD_TTL=${ETCD_TTL:-10}
1016

11-
# configure service discovery
12-
export HOST=${HOST:-localhost}
13-
export PORT=${PORT:-8000}
14-
export PROTO=${PROTO:-tcp}
15-
1617
# wait for etcd to be available
1718
until etcdctl -C $ETCD ls >/dev/null; do
1819
echo "waiting for etcd at $ETCD..."
@@ -23,7 +24,11 @@ done
2324
sleep $(($ETCD_TTL+1))
2425

2526
# seed initial service configuration if necessary
26-
$(dirname ${BASH_SOURCE[0]})/seed >/dev/null 2>&1
27+
if ! etcdctl -C $ETCD ls $ETCD_PATH >/dev/null 2>/dev/null; then
28+
etcdctl -C $ETCD set $ETCD_PATH/protocol ${DEIS_PROTOCOL:-http}
29+
etcdctl -C $ETCD set $ETCD_PATH/secretKey ${DEIS_SECRET_KEY:-`openssl rand -base64 64 | tr -d '\n'`}
30+
etcdctl -C $ETCD set $ETCD_PATH/builderKey ${DEIS_BUILDER_KEY:-`openssl rand -base64 64 | tr -d '\n'`}
31+
fi
2732

2833
# wait for confd to run once and install initial templates
2934
until confd -onetime -node $ETCD -config-file /app/confd.toml 2>/dev/null; do
@@ -38,25 +43,51 @@ while grep -q '<no value>' /templates/confd_settings.py; do
3843
sleep $(($ETCD_TTL/2)) # sleep for half the TTL
3944
done
4045

41-
# spawn the service in the background
42-
$(dirname ${BASH_SOURCE[0]})/start &
43-
SERVICE_PID=$!
46+
cd /app
47+
48+
# run an idempotent database migration
49+
sudo -E -u deis ./manage.py syncdb --migrate --noinput
50+
51+
# spawn celery worker in the background
52+
sudo -E -u deis celery worker --app=deis --loglevel=INFO --workdir=/app --pidfile=/tmp/celery.pid &
53+
CELERY_PID=$!
54+
55+
# spawn a gunicorn server in the foreground
56+
sudo -E -u deis ./manage.py run_gunicorn -b 0.0.0.0 -w 8 -t 600 -n deis --log-level debug --pid=/tmp/gunicorn.pid --preload & # -k gevent
57+
GUNICORN_PID=$!
4458

4559
# smart shutdown on SIGINT and SIGTERM
4660
function on_exit() {
47-
kill -TERM $SERVICE_PID
48-
wait $SERVICE_PID 2>/dev/null
61+
kill -TERM $CELERY_PID $GUNICORN_PID
62+
wait $CELERY_PID $GUNICORN_PID 2>/dev/null
4963
}
5064
trap on_exit INT TERM
5165

5266
# spawn confd in the background to update services based on etcd changes
5367
confd -node $ETCD -config-file /app/confd.toml &
5468
CONFD_PID=$!
5569

56-
# wait for the service to become available
57-
sleep 1 && while [[ -z $(netstat -lnt | awk "\$6 == \"LISTEN\" && \$4 ~ \".$PORT\" && \$1 ~ \"$PROTO.?\"") ]] ; do sleep 1; done
70+
# publish the service to etcd using the injected HOST and PORT
71+
if [[ ! -z $PUBLISH ]]; then
72+
73+
# configure service discovery
74+
HOST=${HOST:-localhost}
75+
PORT=${PORT:-8000}
76+
PROTO=${PROTO:-tcp}
77+
78+
# wait for the service to become available on PUBLISH port
79+
sleep 1 && while [[ -z $(netstat -lnt | awk "\$6 == \"LISTEN\" && \$4 ~ \".$PUBLISH\" && \$1 ~ \"$PROTO.?\"") ]] ; do sleep 1; done
80+
81+
# while the port is listening, publish to etcd
82+
while [[ ! -z $(netstat -lnt | awk "\$6 == \"LISTEN\" && \$4 ~ \".$PUBLISH\" && \$1 ~ \"$PROTO.?\"") ]] ; do
83+
etcdctl -C $ETCD set $ETCD_PATH/host $HOST --ttl $ETCD_TTL >/dev/null
84+
etcdctl -C $ETCD set $ETCD_PATH/port $PORT --ttl $ETCD_TTL >/dev/null
85+
sleep $(($ETCD_TTL/2)) # sleep for half the TTL
86+
done
87+
88+
# if the loop quits, something went wrong
89+
exit 1
5890

59-
# as long as the service remains up, keep publishing to etcd with a TTL
60-
$(dirname ${BASH_SOURCE[0]})/publish &
91+
fi
6192

6293
wait

bin/publish

Lines changed: 0 additions & 16 deletions
This file was deleted.

bin/seed

Lines changed: 0 additions & 13 deletions
This file was deleted.

bin/start

Lines changed: 0 additions & 14 deletions
This file was deleted.

0 commit comments

Comments
 (0)