github.com/misfo/deis@v1.0.1-0.20141111224634-e0eee0392b8a/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 ETCD_TTL=${ETCD_TTL:-10} 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 # wait for confd to run once and install initial templates 28 until confd -onetime -node $ETCD -config-file /app/confd.toml >/dev/null 2>/dev/null; do 29 echo "store-gateway: waiting for confd to write initial templates..." 30 sleep $(($ETCD_TTL/2)) # sleep for half the TTL 31 done 32 33 # set the number of placement groups for the default pools - they come up with defaults that are too low 34 if ! etcdctl --no-sync -C $ETCD get /deis/store/defaultPoolsConfigured >/dev/null 2>&1 ; then 35 echo "store-gateway: setting pg_num values for default pools..." 36 function set_until_success { 37 set +e 38 39 echo "store-gateway: checking pool $1..." 40 if ! ceph osd pool get $1 pg_num | grep "pg_num: $2" ; then 41 ceph osd pool set $1 pg_num $2 2>/dev/null 42 PG_SET=$? 43 until [[ $PG_SET -eq 0 ]]; do 44 sleep 5 45 ceph osd pool set $1 pg_num $2 2>/dev/null 46 PG_SET=$? 47 done 48 fi 49 50 if ! ceph osd pool get $1 pgp_num | grep "pgp_num: $2" ; then 51 ceph osd pool set $1 pgp_num $2 2>/dev/null 52 PGP_SET=$? 53 until [[ $PGP_SET -eq 0 ]]; do 54 sleep 5 55 ceph osd pool set $1 pgp_num $2 2>/dev/null 56 PGP_SET=$? 57 done 58 fi 59 60 set -e 61 } 62 63 PG_NUM=`etcdctl --no-sync -C $ETCD get /deis/store/pgNum` 64 65 set_until_success data ${PG_NUM} 66 set_until_success rbd ${PG_NUM} 67 set_until_success metadata ${PG_NUM} 68 69 etcdctl --no-sync -C $ETCD set /deis/store/defaultPoolsConfigured youBetcha >/dev/null 70 fi 71 72 # we generate a key for the gateway. we can do this because we have the client key templated out 73 if ! etcdctl --no-sync -C $ETCD get /deis/store/gatewayKeyring >/dev/null 2>&1 ; then 74 ceph-authtool --create-keyring /etc/ceph/ceph.client.radosgw.keyring 75 chmod +r /etc/ceph/ceph.client.radosgw.keyring 76 ceph-authtool /etc/ceph/ceph.client.radosgw.keyring -n client.radosgw.gateway --gen-key 77 ceph-authtool -n client.radosgw.gateway --cap osd 'allow rwx' --cap mon 'allow rwx' /etc/ceph/ceph.client.radosgw.keyring 78 ceph -k /etc/ceph/ceph.client.admin.keyring auth add client.radosgw.gateway -i /etc/ceph/ceph.client.radosgw.keyring 79 etcdctl --no-sync -C $ETCD set /deis/store/gatewayKeyring < /etc/ceph/ceph.client.radosgw.keyring >/dev/null 80 else 81 etcdctl --no-sync -C $ETCD get /deis/store/gatewayKeyring > /etc/ceph/ceph.client.radosgw.keyring 82 chmod +r /etc/ceph/ceph.client.radosgw.keyring 83 fi 84 85 if ! radosgw-admin user info --uid=deis >/dev/null 2>&1 ; then 86 radosgw-admin user create --uid=deis --display-name="Deis" >/etc/ceph/user.json 87 # store the access key and secret key for consumption by other services 88 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 '"'` 89 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 '"'` 90 etcdctl --no-sync -C $ETCD set $ETCD_PATH/accessKey ${ACCESS_KEY} >/dev/null 91 etcdctl --no-sync -C $ETCD set $ETCD_PATH/secretKey ${SECRET_KEY} >/dev/null 92 fi 93 94 # spawn the service in the background 95 echo "Starting RADOS gateway..." 96 /etc/init.d/radosgw start 97 98 echo "Starting Apache..." 99 /usr/sbin/apache2ctl start 100 101 # smart shutdown on SIGINT and SIGTERM 102 function on_exit() { 103 /usr/sbin/apache2ctl stop 104 /etc/init.d/radosgw stop 105 exit 0 106 } 107 trap on_exit INT TERM 108 109 # spawn confd in the background to update services based on etcd changes 110 confd -node $ETCD -config-file /app/confd.toml & 111 CONFD_PID=$! 112 113 echo deis-store-gateway running... 114 115 # publish the service to etcd using the injected EXTERNAL_PORT 116 if [[ ! -z $EXTERNAL_PORT ]]; then 117 118 # configure service discovery 119 PORT=${PORT:-8888} 120 PROTO=${PROTO:-tcp} 121 122 set +e 123 124 # wait for the service to become available on PUBLISH port 125 sleep 1 && while [[ -z $(netstat -lnt | awk "\$6 == \"LISTEN\" && \$4 ~ \".$PUBLISH\" && \$1 ~ \"$PROTO.?\"") ]] ; do sleep 1; done 126 127 # while the port is listening, publish to etcd 128 while [[ ! -z $(netstat -lnt | awk "\$6 == \"LISTEN\" && \$4 ~ \".$PUBLISH\" && \$1 ~ \"$PROTO.?\"") ]] ; do 129 etcdctl --no-sync -C $ETCD set $ETCD_PATH/host $HOST --ttl $ETCD_TTL >/dev/null 130 etcdctl --no-sync -C $ETCD set $ETCD_PATH/port $EXTERNAL_PORT --ttl $ETCD_TTL >/dev/null 131 sleep $(($ETCD_TTL/2)) # sleep for half the TTL 132 done 133 134 # if the loop quits, something went wrong 135 exit 1 136 137 fi 138 139 wait