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