github.com/technosophos/deis@v1.7.1-0.20150915173815-f9005256004b/registry/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/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 ERROR="$(etcdctl --no-sync -C "$ETCD" mk "$ETCD_PATH/$1" "$2" 2>&1 >/dev/null)" 36 37 if [[ $? -ne 0 ]] && echo "$ERROR" | grep -iqve "key already exists"; then 38 echo "etcd_set_default: an etcd error occurred ($ERROR)" 39 echo "aborting..." 40 exit 1 41 fi 42 set -e 43 } 44 45 # seed initial service configuration if necessary 46 etcd_set_default protocol http 47 etcd_set_default bucketName "${BUCKET_NAME}" 48 49 # wait for confd to run once and install initial templates 50 until confd -onetime -node "$ETCD" --confdir /app --log-level error; do 51 echo "registry: waiting for confd to write initial templates..." 52 sleep $((ETCD_TTL/2)) # sleep for half the TTL 53 done 54 55 # ensure registry bucket exists 56 /app/bin/create_bucket 57 58 # spawn the service in the background 59 cd /docker-registry 60 sudo -E -u registry docker-registry & 61 SERVICE_PID=$! 62 63 # smart shutdown on SIGINT and SIGTERM 64 function on_exit() { 65 kill -TERM $SERVICE_PID 66 wait $SERVICE_PID 2>/dev/null 67 exit 0 68 } 69 trap on_exit INT TERM 70 71 # spawn confd in the background to update services based on etcd changes 72 confd -node "$ETCD" --confdir /app --log-level error --interval 5 & 73 74 echo deis-registry running... 75 76 # publish the service to etcd using the injected EXTERNAL_PORT 77 if [[ ! -z $EXTERNAL_PORT ]]; then 78 79 # configure service discovery 80 PORT=${PORT:-5000} 81 PROTO=${PROTO:-tcp} 82 83 set +e 84 85 # wait for the service to become available on PORT 86 sleep 1 && while [[ -z $(netstat -lnt | awk "\$6 == \"LISTEN\" && \$4 ~ \".$PORT\" && \$1 ~ \"$PROTO.?\"") ]] ; do sleep 1; done 87 88 # while the port is listening, publish to etcd 89 while [[ ! -z $(netstat -lnt | awk "\$6 == \"LISTEN\" && \$4 ~ \".$PORT\" && \$1 ~ \"$PROTO.?\"") ]] ; do 90 if etcdctl --no-sync -C "$ETCD" mk "${ETCD_PATH}/masterLock" "$HOSTNAME" --ttl "$ETCD_TTL" >/dev/null 2>&1 \ 91 || [[ $(etcdctl --no-sync -C "$ETCD" get "${ETCD_PATH}/masterLock") == "$HOSTNAME" ]] ; then 92 etcdctl --no-sync -C "$ETCD" set "$ETCD_PATH/host" "$HOST" --ttl "$ETCD_TTL" >/dev/null 93 etcdctl --no-sync -C "$ETCD" set "$ETCD_PATH/port" "$EXTERNAL_PORT" --ttl "$ETCD_TTL" >/dev/null 94 etcdctl --no-sync -C "$ETCD" update "${ETCD_PATH}/masterLock" "$HOSTNAME" --ttl "$ETCD_TTL" >/dev/null 95 fi 96 etcdctl --no-sync -C "$ETCD" set "$HOST_ETCD_PATH/host" "$HOST" --ttl "$ETCD_TTL" >/dev/null 97 etcdctl --no-sync -C "$ETCD" set "$HOST_ETCD_PATH/port" "$EXTERNAL_PORT" --ttl "$ETCD_TTL" >/dev/null 98 99 sleep $((ETCD_TTL/2)) # sleep for half the TTL 100 done 101 102 # if the loop quits, something went wrong 103 exit 1 104 105 fi 106 107 wait