github.com/spg/deis@v1.7.3/store/gateway/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/store/gateway} 16 export HOST_ETCD_PATH=${HOST_ETCD_PATH:-/deis/store/gateway/hosts/$HOST} 17 export ETCD_TTL=${ETCD_TTL:-20} 18 19 # wait for etcd to be available 20 until etcdctl --no-sync -C $ETCD ls >/dev/null 2>&1; do 21 echo "waiting for etcd at $ETCD..." 22 sleep $(($ETCD_TTL/2)) # sleep for half the TTL 23 done 24 25 # wait until etcd has discarded potentially stale values 26 sleep $(($ETCD_TTL+1)) 27 28 # wait for confd to run once and install initial templates 29 until confd -onetime -node $ETCD --confdir /app --log-level error; do 30 echo "store-gateway: waiting for confd to write initial templates..." 31 sleep $(($ETCD_TTL/2)) # sleep for half the TTL 32 done 33 34 # we generate a key for the gateway. we can do this because we have the client key templated out 35 if ! etcdctl --no-sync -C $ETCD get /deis/store/gatewayKeyring >/dev/null 2>&1 ; then 36 ceph-authtool --create-keyring /etc/ceph/ceph.client.radosgw.keyring 37 chmod +r /etc/ceph/ceph.client.radosgw.keyring 38 ceph-authtool /etc/ceph/ceph.client.radosgw.keyring -n client.radosgw.gateway --gen-key 39 ceph-authtool -n client.radosgw.gateway --cap osd 'allow rwx' --cap mon 'allow rwx' /etc/ceph/ceph.client.radosgw.keyring 40 ceph -k /etc/ceph/ceph.client.admin.keyring auth add client.radosgw.gateway -i /etc/ceph/ceph.client.radosgw.keyring 41 etcdctl --no-sync -C $ETCD set /deis/store/gatewayKeyring < /etc/ceph/ceph.client.radosgw.keyring >/dev/null 42 else 43 etcdctl --no-sync -C $ETCD get /deis/store/gatewayKeyring > /etc/ceph/ceph.client.radosgw.keyring 44 chmod +r /etc/ceph/ceph.client.radosgw.keyring 45 fi 46 47 if ! radosgw-admin user info --uid=deis >/dev/null 2>&1 ; then 48 radosgw-admin user create --uid=deis --display-name="Deis" >/dev/null 49 fi 50 51 radosgw-admin user info --uid=deis >/etc/ceph/user.json 52 # store the access key and secret key for consumption by other services 53 ACCESS_KEY=`cat /etc/ceph/user.json | python -c 'import json,sys;obj=json.load(sys.stdin);print json.dumps(obj["keys"][0]["access_key"]);' | tr -d '"'` 54 SECRET_KEY=`cat /etc/ceph/user.json | python -c 'import json,sys;obj=json.load(sys.stdin);print json.dumps(obj["keys"][0]["secret_key"]);' | tr -d '"'` 55 etcdctl --no-sync -C $ETCD set $ETCD_PATH/accessKey ${ACCESS_KEY} >/dev/null 56 etcdctl --no-sync -C $ETCD set $ETCD_PATH/secretKey ${SECRET_KEY} >/dev/null 57 58 # spawn the service in the background 59 echo "Starting RADOS gateway..." 60 /etc/init.d/radosgw start 61 62 # smart shutdown on SIGINT and SIGTERM 63 function on_exit() { 64 /etc/init.d/radosgw stop 65 exit 0 66 } 67 trap on_exit INT TERM 68 69 # spawn confd in the background to update services based on etcd changes 70 confd -node $ETCD --confdir /app --log-level error --interval 5 & 71 CONFD_PID=$! 72 73 echo deis-store-gateway running... 74 75 # publish the service to etcd using the injected EXTERNAL_PORT 76 if [[ ! -z $EXTERNAL_PORT ]]; then 77 78 # configure service discovery 79 PORT=${PORT:-8888} 80 PROTO=${PROTO:-tcp} 81 82 set +e 83 84 # wait for the service to become available on PUBLISH port 85 sleep 1 && while [[ -z $(netstat -lnt | awk "\$6 == \"LISTEN\" && \$4 ~ \".$PUBLISH\" && \$1 ~ \"$PROTO.?\"") ]] ; do sleep 1; done 86 87 # while the port is listening, publish to etcd 88 while [[ ! -z $(netstat -lnt | awk "\$6 == \"LISTEN\" && \$4 ~ \".$PUBLISH\" && \$1 ~ \"$PROTO.?\"") ]] ; do 89 if etcdctl --no-sync -C $ETCD mk ${ETCD_PATH}/masterLock $HOST --ttl $ETCD_TTL >/dev/null 2>&1 \ 90 || [[ `etcdctl --no-sync -C $ETCD get ${ETCD_PATH}/masterLock` == "$HOST" ]] ; then 91 etcdctl --no-sync -C $ETCD set $ETCD_PATH/host $HOST --ttl $ETCD_TTL >/dev/null 92 etcdctl --no-sync -C $ETCD set $ETCD_PATH/port $EXTERNAL_PORT --ttl $ETCD_TTL >/dev/null 93 etcdctl --no-sync -C $ETCD update ${ETCD_PATH}/masterLock $HOST --ttl $ETCD_TTL >/dev/null 94 fi 95 etcdctl --no-sync -C $ETCD set $HOST_ETCD_PATH/host $HOST --ttl $ETCD_TTL >/dev/null 96 etcdctl --no-sync -C $ETCD set $HOST_ETCD_PATH/port $EXTERNAL_PORT --ttl $ETCD_TTL >/dev/null 97 sleep $(($ETCD_TTL/2)) # sleep for half the TTL 98 done 99 100 # if the loop quits, something went wrong 101 exit 1 102 103 fi 104 105 wait