github.com/ahjdzx/deis@v1.1.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:-10}
    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 secretKey ${REGISTRY_SECRET_KEY:-`openssl rand -base64 64 | tr -d '\n'`}
    46  etcd_set_default bucketName ${BUCKET_NAME}
    47  
    48  # wait for confd to run once and install initial templates
    49  until confd -onetime -node $ETCD -config-file /app/confd.toml; do
    50  	echo "registry: waiting for confd to write initial templates..."
    51  	sleep $(($ETCD_TTL/2))  # sleep for half the TTL
    52  done
    53  
    54  # ensure registry bucket exists
    55  /app/bin/create_bucket ${BUCKET_NAME}
    56  
    57  # spawn the service in the background
    58  cd /docker-registry
    59  sudo -E -u registry docker-registry &
    60  SERVICE_PID=$!
    61  
    62  # smart shutdown on SIGINT and SIGTERM
    63  function on_exit() {
    64  	kill -TERM $SERVICE_PID
    65  	wait $SERVICE_PID 2>/dev/null
    66  	exit 0
    67  }
    68  trap on_exit INT TERM
    69  
    70  # spawn confd in the background to update services based on etcd changes
    71  confd -node $ETCD -config-file /app/confd.toml &
    72  CONFD_PID=$!
    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