|
| 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/store/gateway} |
| 16 | +export ETCD_TTL=${ETCD_TTL:-10} |
| 17 | + |
| 18 | +export EXTERNAL_PORT=${EXTERNAL_PORT:-8888} |
| 19 | +export STORAGE_DIRECTORY=${STORAGE_DIRECTORY:-/app/storage} |
| 20 | + |
| 21 | +# wait for etcd to be available |
| 22 | +until etcdctl --no-sync -C $ETCD ls >/dev/null 2>&1; do |
| 23 | + echo "waiting for etcd at $ETCD..." |
| 24 | + sleep $(($ETCD_TTL/2)) # sleep for half the TTL |
| 25 | +done |
| 26 | + |
| 27 | +# wait until etcd has discarded potentially stale values |
| 28 | +sleep $(($ETCD_TTL+1)) |
| 29 | + |
| 30 | +function etcd_safe_mkdir { |
| 31 | + set +e |
| 32 | + etcdctl --no-sync -C $ETCD mkdir $1 >/dev/null 2>&1 |
| 33 | + if [[ $? -ne 0 && $? -ne 4 ]]; then |
| 34 | + echo "etcd_safe_mkdir: an etcd error occurred. aborting..." |
| 35 | + exit 1 |
| 36 | + fi |
| 37 | + set -e |
| 38 | +} |
| 39 | + |
| 40 | +etcd_safe_mkdir $ETCD_PATH |
| 41 | + |
| 42 | +# store the access key and secret key for consumption by other services |
| 43 | +ACCESS_KEY=`etcdctl --no-sync -C $ETCD get $ETCD_PATH/accessKey 2>/dev/null || openssl rand -base64 21 | tr -d '\n'` |
| 44 | +SECRET_KEY=`etcdctl --no-sync -C $ETCD get $ETCD_PATH/secretKey 2>/dev/null || openssl rand -base64 64 | tr -d '\n'` |
| 45 | +etcdctl --no-sync -C $ETCD set $ETCD_PATH/accessKey ${ACCESS_KEY} >/dev/null |
| 46 | +etcdctl --no-sync -C $ETCD set $ETCD_PATH/secretKey ${SECRET_KEY} >/dev/null |
| 47 | + |
| 48 | +mkdir -p /app/storage |
| 49 | + |
| 50 | +mock_s3 --hostname 0.0.0.0 --port $EXTERNAL_PORT --root $STORAGE_DIRECTORY & |
| 51 | + |
| 52 | +echo deis-store-gateway running... |
| 53 | + |
| 54 | +# configure service discovery |
| 55 | +PROTO=${PROTO:-tcp} |
| 56 | + |
| 57 | +set +e |
| 58 | + |
| 59 | +# wait for the service to become available on PUBLISH port |
| 60 | +sleep 1 && while [[ -z $(netstat -lnt | awk "\$6 == \"LISTEN\" && \$4 ~ \".$PUBLISH\" && \$1 ~ \"$PROTO.?\"") ]] ; do sleep 1; done |
| 61 | + |
| 62 | +# while the port is listening, publish to etcd |
| 63 | +while [[ ! -z $(netstat -lnt | awk "\$6 == \"LISTEN\" && \$4 ~ \".$PUBLISH\" && \$1 ~ \"$PROTO.?\"") ]] ; do |
| 64 | + etcdctl --no-sync -C $ETCD set $ETCD_PATH/host $HOST --ttl $ETCD_TTL >/dev/null |
| 65 | + etcdctl --no-sync -C $ETCD set $ETCD_PATH/port $EXTERNAL_PORT --ttl $ETCD_TTL >/dev/null |
| 66 | + sleep $(($ETCD_TTL/2)) # sleep for half the TTL |
| 67 | +done |
| 68 | + |
| 69 | +# if the loop quits, something went wrong |
| 70 | +exit 1 |
0 commit comments