github.com/technosophos/deis@v1.7.1-0.20150915173815-f9005256004b/controller/bin/boot (about) 1 #!/usr/bin/env 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:-20} 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 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 32 echo "etcd_set_default: an etcd error occurred ($ERROR)" 33 echo "aborting..." 34 exit 1 35 fi 36 set -e 37 } 38 39 function etcd_safe_mkdir { 40 set +e 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 44 echo "etcd_safe_mkdir: an etcd error occurred ($ERROR)" 45 echo "aborting..." 46 exit 1 47 fi 48 set -e 49 } 50 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')}" 54 etcd_set_default registrationMode "enabled" 55 etcd_set_default webEnabled 0 56 etcd_set_default unitHostname default 57 58 # safely create required keyspaces 59 etcd_safe_mkdir /deis/domains 60 etcd_safe_mkdir /deis/platform 61 etcd_safe_mkdir /deis/scheduler 62 etcd_safe_mkdir /deis/services 63 64 # run etcd data migrations 65 echo "controller: running etcd data migrations..." 66 for script in /app/migrations/data/*.sh; 67 do 68 . "$script"; 69 done 70 echo "controller: done running etcd data migrations." 71 72 # wait for confd to run once and install initial templates 73 until confd -onetime -node "$ETCD" --confdir /app --log-level error; do 74 echo "controller: waiting for confd to write initial templates..." 75 sleep $((ETCD_TTL/2)) # sleep for half the TTL 76 done 77 78 cd /app 79 80 mkdir -p /data/logs 81 chmod 777 /data/logs 82 83 # run an idempotent database migration 84 sudo -E -u deis ./manage.py syncdb --migrate --noinput 85 86 # spawn a gunicorn server in the background 87 sudo -E -u deis gunicorn -c deis/gconf.py deis.wsgi & 88 89 # smart shutdown on SIGTERM (SIGINT is handled by gunicorn) 90 function on_exit() { 91 GUNICORN_PID=$(cat /tmp/gunicorn.pid) 92 kill -TERM "$GUNICORN_PID" 2>/dev/null 93 wait "$GUNICORN_PID" 2>/dev/null 94 exit 0 95 } 96 trap on_exit TERM 97 98 # spawn confd in the background to update services based on etcd changes 99 confd -node "$ETCD" --confdir /app --log-level error --interval 5 & 100 101 echo deis-controller running... 102 103 # publish the service to etcd using the injected EXTERNAL_PORT 104 if [[ ! -z $EXTERNAL_PORT ]]; then 105 106 # configure service discovery 107 PORT=${PORT:-8000} 108 PROTO=${PROTO:-tcp} 109 110 set +e 111 112 # wait for the service to become available on PORT 113 sleep 1 && while [[ -z $(netstat -lnt | awk "\$6 == \"LISTEN\" && \$4 ~ \".$PORT\" && \$1 ~ \"$PROTO.?\"") ]] ; do sleep 1; done 114 115 # while the port is listening, publish to etcd 116 while [[ ! -z $(netstat -lnt | awk "\$6 == \"LISTEN\" && \$4 ~ \".$PORT\" && \$1 ~ \"$PROTO.?\"") ]] ; do 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 120 done 121 122 # if the loop quits, something went wrong 123 exit 1 124 125 fi 126 127 wait