github.com/sbuss/deis@v1.6.1/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 HOST_ETCD_PATH=${HOST_ETCD_PATH:-/deis/registry/hosts/$HOST} 17 export ETCD_TTL=${ETCD_TTL:-20} 18 19 # run.sh requires $REGISTRY_PORT 20 export REGISTRY_PORT=${PORT:-5000} 21 22 export BUCKET_NAME=${BUCKET_NAME:-registry} 23 24 # wait for etcd to be available 25 until etcdctl --no-sync -C $ETCD ls >/dev/null 2>&1; do 26 echo "waiting for etcd at $ETCD..." 27 sleep $(($ETCD_TTL/2)) # sleep for half the TTL 28 done 29 30 # wait until etcd has discarded potentially stale values 31 sleep $(($ETCD_TTL+1)) 32 33 function etcd_set_default { 34 set +e 35 etcdctl --no-sync -C $ETCD mk $ETCD_PATH/$1 $2 >/dev/null 2>&1 36 if [[ $? -ne 0 && $? -ne 4 ]]; then 37 echo "etcd_set_default: an etcd error occurred. aborting..." 38 exit 1 39 fi 40 set -e 41 } 42 43 # seed initial service configuration if necessary 44 etcd_set_default protocol http 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 --confdir /app --log-level error; 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 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 --confdir /app --log-level error --interval 5 & 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 if etcdctl --no-sync -C $ETCD mk ${ETCD_PATH}/masterLock $HOSTNAME --ttl $ETCD_TTL >/dev/null 2>&1 \ 90 || [[ `etcdctl --no-sync -C $ETCD get ${ETCD_PATH}/masterLock` == "$HOSTNAME" ]] ; then 91 etcdctl --no-sync -C $ETCD set $ETCD_PATH/host $HOST --ttl $ETCD_TTL >/dev/null 92 etcdctl --no-sync -C $ETCD set $ETCD_PATH/port $EXTERNAL_PORT --ttl $ETCD_TTL >/dev/null 93 etcdctl --no-sync -C $ETCD update ${ETCD_PATH}/masterLock $HOSTNAME --ttl $ETCD_TTL >/dev/null 94 fi 95 etcdctl --no-sync -C $ETCD set $HOST_ETCD_PATH/host $HOST --ttl $ETCD_TTL >/dev/null 96 etcdctl --no-sync -C $ETCD set $HOST_ETCD_PATH/port $EXTERNAL_PORT --ttl $ETCD_TTL >/dev/null 97 98 sleep $(($ETCD_TTL/2)) # sleep for half the TTL 99 done 100 101 # if the loop quits, something went wrong 102 exit 1 103 104 fi 105 106 wait