|
| 1 | +#!/bin/bash |
| 2 | +# |
| 3 | +# This script is designed to be run inside the container |
| 4 | +# |
| 5 | + |
| 6 | +# configure etcd |
| 7 | +export ETCD=${ETCD:-127.0.0.1:4001} |
| 8 | +export ETCD_PATH=${ETCD_PATH:-/deis/controller} |
| 9 | +export ETCD_TTL=${ETCD_TTL:-10} |
| 10 | + |
| 11 | +# configure service discovery |
| 12 | +export HOST=${HOST:-localhost} |
| 13 | +export PORT=${PORT:-8000} |
| 14 | +export PROTO=${PROTO:-tcp} |
| 15 | + |
| 16 | +# wait for etcd to be available |
| 17 | +until etcdctl -C $ETCD ls >/dev/null; do |
| 18 | + echo "waiting for etcd at $ETCD..." |
| 19 | + sleep $(($ETCD_TTL/2)) # sleep for half the TTL |
| 20 | +done |
| 21 | + |
| 22 | +# wait until etcd has discarded potentially stale values |
| 23 | +sleep $(($ETCD_TTL+1)) |
| 24 | + |
| 25 | +# seed initial service configuration if necessary |
| 26 | +$(dirname ${BASH_SOURCE[0]})/seed >/dev/null 2>&1 |
| 27 | + |
| 28 | +# wait for confd to run once and install initial templates |
| 29 | +until confd -onetime -node $ETCD -config-file /app/confd.toml 2>/dev/null; do |
| 30 | + echo "waiting for confd to write initial templates..." |
| 31 | + sleep $(($ETCD_TTL/2)) # sleep for half the TTL |
| 32 | +done |
| 33 | + |
| 34 | +# wait for confd to populate all values |
| 35 | +while grep -q '<no value>' /app/confd_settings.py; do |
| 36 | + echo "waiting for confd to write all values..." |
| 37 | + confd -onetime -node $ETCD -config-file /app/confd.toml 2>/dev/null |
| 38 | + sleep $(($ETCD_TTL/2)) # sleep for half the TTL |
| 39 | +done |
| 40 | + |
| 41 | +# perform a one-time reload to install check config |
| 42 | +/usr/local/bin/reload |
| 43 | + |
| 44 | +# spawn the service in the background |
| 45 | +$(dirname ${BASH_SOURCE[0]})/start & |
| 46 | +SERVICE_PID=$! |
| 47 | + |
| 48 | +# smart shutdown on SIGINT and SIGTERM |
| 49 | +function on_exit() { |
| 50 | + kill -TERM $SERVICE_PID |
| 51 | + wait $SERVICE_PID 2>/dev/null |
| 52 | +} |
| 53 | +trap on_exit INT TERM |
| 54 | + |
| 55 | +# spawn confd in the background to update services based on etcd changes |
| 56 | +confd -node $ETCD -config-file /app/confd.toml & |
| 57 | +CONFD_PID=$! |
| 58 | + |
| 59 | +# wait for the service to become available |
| 60 | +sleep 1 && while [[ -z $(netstat -lnt | awk "\$6 == \"LISTEN\" && \$4 ~ \".$PORT\" && \$1 ~ \"$PROTO.?\"") ]] ; do sleep 1; done |
| 61 | + |
| 62 | +# as long as the service remains up, keep publishing to etcd with a TTL |
| 63 | +$(dirname ${BASH_SOURCE[0]})/publish & |
| 64 | + |
| 65 | +wait |
0 commit comments