github.com/inflatablewoman/deis@v1.0.1-0.20141111034523-a4511c46a6ce/registry/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/registry} 16 export ETCD_TTL=${ETCD_TTL:-10} 17 18 # run.sh requires $REGISTRY_PORT 19 export REGISTRY_PORT=${PORT:-5000} 20 21 export BUCKET_NAME=${BUCKET_NAME:-registry} 22 23 # wait for etcd to be available 24 until etcdctl --no-sync -C $ETCD ls >/dev/null 2>&1; do 25 echo "waiting for etcd at $ETCD..." 26 sleep $(($ETCD_TTL/2)) # sleep for half the TTL 27 done 28 29 # wait until etcd has discarded potentially stale values 30 sleep $(($ETCD_TTL+1)) 31 32 function etcd_set_default { 33 set +e 34 etcdctl --no-sync -C $ETCD mk $ETCD_PATH/$1 $2 >/dev/null 2>&1 35 if [[ $? -ne 0 && $? -ne 4 ]]; then 36 echo "etcd_set_default: an etcd error occurred. aborting..." 37 exit 1 38 fi 39 set -e 40 } 41 42 # seed initial service configuration if necessary 43 etcd_set_default protocol http 44 etcd_set_default secretKey ${REGISTRY_SECRET_KEY:-`openssl rand -base64 64 | tr -d '\n'`} 45 etcd_set_default bucketName ${BUCKET_NAME} 46 47 # wait for confd to run once and install initial templates 48 until confd -onetime -node $ETCD -config-file /app/confd.toml; do 49 echo "registry: waiting for confd to write initial templates..." 50 sleep $(($ETCD_TTL/2)) # sleep for half the TTL 51 done 52 53 # ensure registry bucket exists 54 /app/bin/create_bucket ${BUCKET_NAME} 55 56 # spawn the service in the background 57 cd /docker-registry 58 sudo -E -u registry docker-registry & 59 SERVICE_PID=$! 60 61 # smart shutdown on SIGINT and SIGTERM 62 function on_exit() { 63 kill -TERM $SERVICE_PID 64 wait $SERVICE_PID 2>/dev/null 65 exit 0 66 } 67 trap on_exit INT TERM 68 69 # spawn confd in the background to update services based on etcd changes 70 confd -node $ETCD -config-file /app/confd.toml & 71 CONFD_PID=$! 72 73 echo deis-registry running... 74 75 # publish the service to etcd using the injected EXTERNAL_PORT 76 if [[ ! -z $EXTERNAL_PORT ]]; then 77 78 # configure service discovery 79 PORT=${PORT:-5000} 80 PROTO=${PROTO:-tcp} 81 82 set +e 83 84 # wait for the service to become available on PORT 85 sleep 1 && while [[ -z $(netstat -lnt | awk "\$6 == \"LISTEN\" && \$4 ~ \".$PORT\" && \$1 ~ \"$PROTO.?\"") ]] ; do sleep 1; done 86 87 # while the port is listening, publish to etcd 88 while [[ ! -z $(netstat -lnt | awk "\$6 == \"LISTEN\" && \$4 ~ \".$PORT\" && \$1 ~ \"$PROTO.?\"") ]] ; do 89 etcdctl --no-sync -C $ETCD set $ETCD_PATH/host $HOST --ttl $ETCD_TTL >/dev/null 90 etcdctl --no-sync -C $ETCD set $ETCD_PATH/port $EXTERNAL_PORT --ttl $ETCD_TTL >/dev/null 91 sleep $(($ETCD_TTL/2)) # sleep for half the TTL 92 done 93 94 # if the loop quits, something went wrong 95 exit 1 96 97 fi 98 99 wait