github.com/spg/deis@v1.7.3/controller/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/controller}
    16  export ETCD_TTL=${ETCD_TTL:-20}
    17  
    18  # wait for etcd to be available
    19  until etcdctl --no-sync -C $ETCD ls >/dev/null 2>&1; do
    20  	echo "waiting for etcd at $ETCD..."
    21  	sleep $(($ETCD_TTL/2))  # sleep for half the TTL
    22  done
    23  
    24  # wait until etcd has discarded potentially stale values
    25  sleep $(($ETCD_TTL+1))
    26  
    27  function etcd_set_default {
    28  	set +e
    29  	ERROR="$(etcdctl --no-sync -C $ETCD mk $ETCD_PATH/$1 $2 2>&1 >/dev/null)"
    30  	if [[ $? -ne 0 && $(echo $ERROR | grep -ive "key already exists") ]]; then
    31  		echo "etcd_set_default: an etcd error occurred ($ERROR)"
    32  		echo "aborting..."
    33  		exit 1
    34  	fi
    35  	set -e
    36  }
    37  
    38  function etcd_safe_mkdir {
    39  	set +e
    40  	ERROR="$(etcdctl --no-sync -C $ETCD mkdir $1 2>&1 >/dev/null)"
    41  	if [[ $? -ne 0 && $(echo $ERROR | grep -ive "key already exists") ]]; then
    42  		echo "etcd_safe_mkdir: an etcd error occurred ($ERROR)"
    43  		echo "aborting..."
    44  		exit 1
    45  	fi
    46  	set -e
    47  }
    48  
    49  etcd_set_default protocol ${DEIS_PROTOCOL:-http}
    50  etcd_set_default secretKey ${DEIS_SECRET_KEY:-`openssl rand -base64 64 | tr -d '\n'`}
    51  etcd_set_default builderKey ${DEIS_BUILDER_KEY:-`openssl rand -base64 64 | tr -d '\n'`}
    52  etcd_set_default registrationMode "enabled"
    53  etcd_set_default webEnabled 0
    54  etcd_set_default unitHostname default
    55  
    56  # safely create required keyspaces
    57  etcd_safe_mkdir /deis/domains
    58  etcd_safe_mkdir /deis/platform
    59  etcd_safe_mkdir /deis/scheduler
    60  etcd_safe_mkdir /deis/services
    61  
    62  # run etcd data migrations
    63  echo "controller: running etcd data migrations..."
    64  for script in $(ls /app/migrations/data/*.sh);
    65  do
    66      . $script;
    67  done
    68  echo "controller: done running etcd data migrations."
    69  
    70  # wait for confd to run once and install initial templates
    71  until confd -onetime -node $ETCD --confdir /app --log-level error; do
    72  	echo "controller: waiting for confd to write initial templates..."
    73  	sleep $(($ETCD_TTL/2))  # sleep for half the TTL
    74  done
    75  
    76  cd /app
    77  
    78  mkdir -p /data/logs
    79  chmod 777 /data/logs
    80  
    81  # run an idempotent database migration
    82  sudo -E -u deis ./manage.py syncdb --migrate --noinput
    83  
    84  # spawn a gunicorn server in the background
    85  sudo -E -u deis gunicorn -c deis/gconf.py deis.wsgi &
    86  
    87  # smart shutdown on SIGTERM (SIGINT is handled by gunicorn)
    88  function on_exit() {
    89  	GUNICORN_PID=$(cat /tmp/gunicorn.pid)
    90  	kill -TERM $GUNICORN_PID 2>/dev/null
    91  	wait $GUNICORN_PID 2>/dev/null
    92  	exit 0
    93  }
    94  trap on_exit TERM
    95  
    96  # spawn confd in the background to update services based on etcd changes
    97  confd -node $ETCD --confdir /app --log-level error --interval 5 &
    98  CONFD_PID=$!
    99  
   100  echo deis-controller running...
   101  
   102  # publish the service to etcd using the injected EXTERNAL_PORT
   103  if [[ ! -z $EXTERNAL_PORT ]]; then
   104  
   105  	# configure service discovery
   106  	PORT=${PORT:-8000}
   107  	PROTO=${PROTO:-tcp}
   108  
   109  	set +e
   110  
   111  	# wait for the service to become available on PORT
   112  	sleep 1 && while [[ -z $(netstat -lnt | awk "\$6 == \"LISTEN\" && \$4 ~ \".$PORT\" && \$1 ~ \"$PROTO.?\"") ]] ; do sleep 1; done
   113  
   114  	# while the port is listening, publish to etcd
   115  	while [[ ! -z $(netstat -lnt | awk "\$6 == \"LISTEN\" && \$4 ~ \".$PORT\" && \$1 ~ \"$PROTO.?\"") ]] ; do
   116  		etcdctl --no-sync -C $ETCD set $ETCD_PATH/host $HOST --ttl $ETCD_TTL >/dev/null
   117  		etcdctl --no-sync -C $ETCD set $ETCD_PATH/port $EXTERNAL_PORT --ttl $ETCD_TTL >/dev/null
   118  		sleep $(($ETCD_TTL/2)) # sleep for half the TTL
   119  	done
   120  
   121  	# if the loop quits, something went wrong
   122  	exit 1
   123  
   124  fi
   125  
   126  wait