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
713export ETCD=${ETCD:- 172.17.42.1: 4001}
814export ETCD_PATH=${ETCD_PATH:-/ deis/ controller}
915export 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
1718until etcdctl -C $ETCD ls > /dev/null; do
1819 echo " waiting for etcd at $ETCD ..."
2324sleep $(( $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
2934until 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
3944done
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
4660function 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}
5064trap on_exit INT TERM
5165
5266# spawn confd in the background to update services based on etcd changes
5367confd -node $ETCD -config-file /app/confd.toml &
5468CONFD_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
6293wait
0 commit comments