github.com/amrnt/deis@v1.3.1/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 # wait for etcd to be available 19 until etcdctl --no-sync -C $ETCD ls >/dev/null 2>&1; do 20 echo "waiting for etcd at $ETCD..." 21 sleep $(($ETCD_TTL/2)) # sleep for half the TTL 22 done 23 24 # wait until etcd has discarded potentially stale values 25 sleep $(($ETCD_TTL+1)) 26 27 function etcd_set_default { 28 set +e 29 etcdctl --no-sync -C $ETCD mk $ETCD_PATH/$1 $2 >/dev/null 2>&1 30 if [[ $? -ne 0 && $? -ne 4 ]]; then 31 echo "etcd_set_default: an etcd error occurred. aborting..." 32 exit 1 33 fi 34 set -e 35 } 36 37 function etcd_safe_mkdir { 38 set +e 39 etcdctl --no-sync -C $ETCD mkdir $1 >/dev/null 2>&1 40 if [[ $? -ne 0 && $? -ne 4 ]]; then 41 echo "etcd_safe_mkdir: an etcd error occurred. aborting..." 42 exit 1 43 fi 44 set -e 45 } 46 47 etcd_set_default protocol ${DEIS_PROTOCOL:-http} 48 etcd_set_default secretKey ${DEIS_SECRET_KEY:-`openssl rand -base64 64 | tr -d '\n'`} 49 etcd_set_default builderKey ${DEIS_BUILDER_KEY:-`openssl rand -base64 64 | tr -d '\n'`} 50 etcd_set_default registrationEnabled 1 51 etcd_set_default webEnabled 0 52 etcd_set_default unitHostname default 53 54 # safely create required keyspaces 55 etcd_safe_mkdir /deis/services 56 etcd_safe_mkdir /deis/domains 57 etcd_safe_mkdir /deis/platform 58 59 # wait for confd to run once and install initial templates 60 until confd -onetime -node $ETCD -config-file /app/confd.toml 2>/dev/null; do 61 echo "controller: waiting for confd to write initial templates..." 62 sleep $(($ETCD_TTL/2)) # sleep for half the TTL 63 done 64 65 cd /app 66 67 mkdir -p /data/logs 68 chmod 777 /data/logs 69 70 # run an idempotent database migration 71 sudo -E -u deis ./manage.py syncdb --migrate --noinput 72 73 # spawn a gunicorn server in the background 74 sudo -E -u deis gunicorn deis.wsgi -b 0.0.0.0 -w 8 -n deis --timeout=1200 --pid=/tmp/gunicorn.pid \ 75 --log-level info --error-logfile - --access-logfile - \ 76 --access-logformat '%(h)s "%(r)s" %(s)s %(b)s "%(a)s"' & 77 78 # smart shutdown on SIGINT and SIGTERM 79 function on_exit() { 80 GUNICORN_PID=$(cat /tmp/gunicorn.pid) 81 kill -TERM $GUNICORN_PID 2>/dev/null 82 wait $GUNICORN_PID 2>/dev/null 83 exit 0 84 } 85 trap on_exit INT TERM 86 87 # spawn confd in the background to update services based on etcd changes 88 confd -node $ETCD -config-file /app/confd.toml & 89 CONFD_PID=$! 90 91 echo deis-controller running... 92 93 # publish the service to etcd using the injected EXTERNAL_PORT 94 if [[ ! -z $EXTERNAL_PORT ]]; then 95 96 # configure service discovery 97 PORT=${PORT:-8000} 98 PROTO=${PROTO:-tcp} 99 100 set +e 101 102 # wait for the service to become available on PORT 103 sleep 1 && while [[ -z $(netstat -lnt | awk "\$6 == \"LISTEN\" && \$4 ~ \".$PORT\" && \$1 ~ \"$PROTO.?\"") ]] ; do sleep 1; done 104 105 # while the port is listening, publish to etcd 106 while [[ ! -z $(netstat -lnt | awk "\$6 == \"LISTEN\" && \$4 ~ \".$PORT\" && \$1 ~ \"$PROTO.?\"") ]] ; do 107 etcdctl --no-sync -C $ETCD set $ETCD_PATH/host $HOST --ttl $ETCD_TTL >/dev/null 108 etcdctl --no-sync -C $ETCD set $ETCD_PATH/port $EXTERNAL_PORT --ttl $ETCD_TTL >/dev/null 109 sleep $(($ETCD_TTL/2)) # sleep for half the TTL 110 done 111 112 # if the loop quits, something went wrong 113 exit 1 114 115 fi 116 117 wait