github.com/jiasir/deis@v1.12.2/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 # shellcheck disable=SC1090 69 . "$script"; 70 done 71 echo "controller: done running etcd data migrations." 72 73 # wait for confd to run once and install initial templates 74 until confd -onetime -node "$ETCD" --confdir /app --log-level error; do 75 echo "controller: waiting for confd to write initial templates..." 76 sleep $((ETCD_TTL/2)) # sleep for half the TTL 77 done 78 79 cd /app 80 81 mkdir -p /data/logs 82 chmod 777 /data/logs 83 84 # allow deis user permission to Docker 85 if addgroup -g "$(stat -c "%g" /var/run/docker.sock)" docker; then 86 addgroup deis docker 87 fi 88 89 # run an idempotent database migration 90 sudo -E -u deis ./manage.py syncdb --migrate --noinput 91 92 # spawn a gunicorn server in the background 93 sudo -E -u deis gunicorn -c deis/gconf.py deis.wsgi & 94 95 ./manage.py load_db_state_to_etcd 96 97 # smart shutdown on SIGTERM (SIGINT is handled by gunicorn) 98 function on_exit() { 99 GUNICORN_PID=$(cat /tmp/gunicorn.pid) 100 kill -TERM "$GUNICORN_PID" 2>/dev/null 101 wait "$GUNICORN_PID" 2>/dev/null 102 exit 0 103 } 104 trap on_exit TERM 105 106 # spawn confd in the background to update services based on etcd changes 107 confd -node "$ETCD" --confdir /app --log-level error --interval 5 & 108 109 echo deis-controller running... 110 111 # publish the service to etcd using the injected EXTERNAL_PORT 112 if [[ ! -z $EXTERNAL_PORT ]]; then 113 114 # configure service discovery 115 PORT=${PORT:-8000} 116 PROTO=${PROTO:-tcp} 117 118 set +e 119 120 # wait for the service to become available on PORT 121 sleep 1 && while [[ -z $(netstat -lnt | awk "\$6 == \"LISTEN\" && \$4 ~ \".$PORT\" && \$1 ~ \"$PROTO.?\"") ]] ; do sleep 1; done 122 123 # while the port is listening, publish to etcd 124 while [[ ! -z $(netstat -lnt | awk "\$6 == \"LISTEN\" && \$4 ~ \".$PORT\" && \$1 ~ \"$PROTO.?\"") ]] ; do 125 etcdctl --no-sync -C "$ETCD" set "$ETCD_PATH/host" "$HOST" --ttl "$ETCD_TTL" >/dev/null 126 etcdctl --no-sync -C "$ETCD" set "$ETCD_PATH/port" "$EXTERNAL_PORT" --ttl "$ETCD_TTL" >/dev/null 127 sleep $((ETCD_TTL/2)) # sleep for half the TTL 128 done 129 130 # if the loop quits, something went wrong 131 exit 1 132 133 fi 134 135 wait