github.com/blystad/deis@v0.11.0/controller/bin/boot (about) 1 #!/bin/bash 2 # 3 # This script is designed to be run inside the container 4 # 5 6 # fail hard and fast even on pipelines 7 set -eo pipefail 8 9 # set debug based on envvar 10 [[ $DEBUG ]] && set -x 11 12 # configure etcd 13 export ETCD_PORT=${ETCD_PORT:-4001} 14 export ETCD="$HOST:$ETCD_PORT" 15 export ETCD_PATH=${ETCD_PATH:-/deis/controller} 16 export ETCD_TTL=${ETCD_TTL:-10} 17 18 export CELERY_LOG_LEVEL=${CELERY_LOG_LEVEL:-INFO} 19 20 # wait for etcd to be available 21 until etcdctl --no-sync -C $ETCD ls >/dev/null 2>&1; do 22 echo "waiting for etcd at $ETCD..." 23 sleep $(($ETCD_TTL/2)) # sleep for half the TTL 24 done 25 26 # wait until etcd has discarded potentially stale values 27 sleep $(($ETCD_TTL+1)) 28 29 function etcd_set_default { 30 etcdctl --no-sync -C $ETCD mk $ETCD_PATH/$1 $2 >/dev/null 2>&1 || true 31 } 32 33 function etcd_safe_mkdir { 34 etcdctl --no-sync -C $ETCD mkdir $1 >/dev/null 2>&1 || true 35 } 36 37 etcd_set_default protocol ${DEIS_PROTOCOL:-http} 38 etcd_set_default secretKey ${DEIS_SECRET_KEY:-`openssl rand -base64 64 | tr -d '\n'`} 39 etcd_set_default builderKey ${DEIS_BUILDER_KEY:-`openssl rand -base64 64 | tr -d '\n'`} 40 etcd_set_default registrationEnabled 1 41 etcd_set_default webEnabled 0 42 # create path for application metadata 43 etcd_safe_mkdir /deis/services 44 45 # wait for confd to run once and install initial templates 46 until confd -onetime -node $ETCD -config-file /app/confd.toml 2>/dev/null; do 47 echo "controller: waiting for confd to write initial templates..." 48 sleep $(($ETCD_TTL/2)) # sleep for half the TTL 49 done 50 51 cd /app 52 53 # give the deis user permissions to write to the volume mount 54 chown -R deis:deis /var/log/deis 55 56 # run an idempotent database migration 57 sudo -E -u deis ./manage.py syncdb --migrate --noinput 58 59 # spawn celery workers in the background 60 sudo -E -u deis celery worker --app=deis --loglevel=$CELERY_LOG_LEVEL --workdir=/app --pidfile=/tmp/celery.pid & 61 62 # spawn a gunicorn server in the background 63 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 & 64 65 # smart shutdown on SIGINT and SIGTERM 66 function on_exit() { 67 CELERY_PID=$(cat /tmp/celery.pid) 68 GUNICORN_PID=$(cat /tmp/gunicorn.pid) 69 kill -TERM $CELERY_PID $GUNICORN_PID 70 wait $CELERY_PID $GUNICORN_PID 2>/dev/null 71 exit 0 72 } 73 trap on_exit INT TERM 74 75 # spawn confd in the background to update services based on etcd changes 76 confd -node $ETCD -config-file /app/confd.toml & 77 CONFD_PID=$! 78 79 echo deis-controller running... 80 81 # publish the service to etcd using the injected PORT 82 if [[ ! -z $PUBLISH ]]; then 83 84 # configure service discovery 85 PORT=${PORT:-8000} 86 PROTO=${PROTO:-tcp} 87 88 set +e 89 90 # wait for the service to become available on PUBLISH port 91 sleep 1 && while [[ -z $(netstat -lnt | awk "\$6 == \"LISTEN\" && \$4 ~ \".$PUBLISH\" && \$1 ~ \"$PROTO.?\"") ]] ; do sleep 1; done 92 93 # while the port is listening, publish to etcd 94 while [[ ! -z $(netstat -lnt | awk "\$6 == \"LISTEN\" && \$4 ~ \".$PUBLISH\" && \$1 ~ \"$PROTO.?\"") ]] ; do 95 etcdctl --no-sync -C $ETCD set $ETCD_PATH/host $HOST --ttl $ETCD_TTL >/dev/null 96 etcdctl --no-sync -C $ETCD set $ETCD_PATH/port $PORT --ttl $ETCD_TTL >/dev/null 97 sleep $(($ETCD_TTL/2)) # sleep for half the TTL 98 done 99 100 # if the loop quits, something went wrong 101 exit 1 102 103 fi 104 105 wait