github.com/greenboxal/deis@v1.12.1/database/bin/boot (about) 1 #!/usr/bin/env 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/database}" 16 export ETCD_TTL="${ETCD_TTL:-20}" 17 18 export BUCKET_NAME=${BUCKET_NAME:-db_wal} 19 20 # wait for etcd to be available 21 until etcdctl --no-sync -C "$ETCD" ls >/dev/null 2>&1; do 22 echo "database: waiting for etcd at $ETCD..." 23 sleep $((ETCD_TTL/2)) # sleep for half the TTL 24 done 25 26 # wait until etcd has discarded potentially stale values 27 sleep $((ETCD_TTL+1)) 28 29 function etcd_set_default { 30 set +e 31 ERROR="$(etcdctl --no-sync -C "$ETCD" mk "$ETCD_PATH/$1" "$2" 2>&1 >/dev/null)" 32 33 if [[ $? -ne 0 ]] && echo "$ERROR" | grep -iqve "key already exists"; then 34 echo "etcd_set_default: an etcd error occurred ($ERROR)" 35 echo "aborting..." 36 exit 1 37 fi 38 set -e 39 } 40 41 etcd_set_default engine postgresql_psycopg2 42 etcd_set_default adminUser "${PG_ADMIN_USER:-postgres}" 43 etcd_set_default adminPass "${PG_ADMIN_PASS:-changeme123}" 44 etcd_set_default user "${PG_USER_NAME:-deis}" 45 etcd_set_default password "${PG_USER_PASS:-changeme123}" 46 etcd_set_default name "${PG_USER_DB:-deis}" 47 etcd_set_default bucketName "${BUCKET_NAME}" 48 49 # stub out the confd reload script before it gets templated 50 echo '#!/bin/sh' > /usr/local/bin/reload 51 chmod 0755 /usr/local/bin/reload 52 53 # wait for confd to run once and install initial templates 54 until confd -onetime -node "$ETCD" -confdir /app --log-level error; do 55 echo "database: waiting for confd to write initial templates..." 56 sleep $((ETCD_TTL/2)) # sleep for half the TTL 57 done 58 59 PG_DATA_DIR=/var/lib/postgresql/9.3/main 60 61 # initialize database if one doesn't already exist 62 # for example, in the case of a data container 63 if [[ ! -d $PG_DATA_DIR ]]; then 64 chown -R postgres:postgres /var/lib/postgresql 65 sudo -u postgres /usr/bin/initdb -D $PG_DATA_DIR 66 fi 67 68 # ensure WAL log bucket exists 69 envdir /etc/wal-e.d/env /app/bin/create_bucket "${BUCKET_NAME}" 70 INIT_ID=$(etcdctl -C "$ETCD" get "$ETCD_PATH/initId" 2> /dev/null || echo none) 71 echo "database: expecting initialization id: $INIT_ID" 72 73 initial_backup=0 74 if [[ "$(cat $PG_DATA_DIR/initialized 2> /dev/null)" != "$INIT_ID" ]]; then 75 echo "database: no existing database found or it is outdated." 76 # check if there are any backups -- if so, let's restore 77 # we could probably do better than just testing number of lines -- one line is just a heading, meaning no backups 78 if [[ $(envdir /etc/wal-e.d/env wal-e --terse backup-list | wc -l) -gt "1" ]]; then 79 echo "database: restoring from backup..." 80 rm -rf $PG_DATA_DIR 81 sudo -u postgres envdir /etc/wal-e.d/env wal-e backup-fetch $PG_DATA_DIR LATEST 82 chown -R postgres:postgres $PG_DATA_DIR 83 chmod 0700 $PG_DATA_DIR 84 echo "restore_command = 'envdir /etc/wal-e.d/env wal-e wal-fetch \"%f\" \"%p\"'" | sudo -u postgres tee $PG_DATA_DIR/recovery.conf >/dev/null 85 else 86 echo "database: no backups found. Initializing a new database..." 87 initial_backup=1 88 fi 89 # either way, we mark the database as initialized 90 INIT_ID=$(cat /proc/sys/kernel/random/uuid) 91 echo "$INIT_ID" > $PG_DATA_DIR/initialized 92 etcdctl --no-sync -C "$ETCD" set "$ETCD_PATH/initId" "$INIT_ID" >/dev/null 93 else 94 echo "database: existing data directory found. Starting postgres..." 95 fi 96 97 # Explicitly correct permissions on this file. This compensates for the fact 98 # it may have been initially written by root above, but more importantly, if 99 # it's already owned by root, this will correct the permissions during upgrade. 100 chown postgres:postgres /var/lib/postgresql/9.3/main/initialized 101 102 # run the service in the background 103 sudo -i -u postgres /usr/bin/postgres \ 104 -c config-file="${PG_CONFIG:-/etc/postgresql/main/postgresql.conf}" \ 105 -c listen-addresses="${PG_LISTEN:-*}" & 106 107 SERVICE_PID=$! 108 109 # smart shutdown on SIGINT and SIGTERM 110 function on_exit() { 111 kill -TERM $SERVICE_PID 112 wait $SERVICE_PID 2>/dev/null 113 exit 0 114 } 115 trap on_exit INT TERM 116 117 # spawn confd in the background to update services based on etcd changes 118 confd -node "$ETCD" -confdir /app --log-level error --interval 5 & 119 120 # wait for the service to become available 121 until sudo -u postgres psql -l -t >/dev/null 2>&1; do sleep 1; done 122 123 # perform a one-time reload to populate database entries 124 /usr/local/bin/reload 125 126 if [[ "${initial_backup}" == "1" ]] ; then 127 echo "database: performing an initial backup..." 128 # perform an initial backup 129 sudo -u postgres envdir /etc/wal-e.d/env wal-e backup-push $PG_DATA_DIR 130 fi 131 132 sudo -Eu postgres /app/bin/backup & 133 134 echo "database: postgres is running..." 135 136 # publish the service to etcd using the injected HOST and EXTERNAL_PORT 137 if [[ ! -z $EXTERNAL_PORT ]]; then 138 # configure service discovery 139 PORT=${PORT:-5432} 140 PROTO=${PROTO:-tcp} 141 142 set +e 143 144 # wait for the service to become available on PORT 145 until sudo -u postgres psql -l -t >/dev/null 2>&1; do sleep 1; done 146 147 # while the port is listening, publish to etcd 148 while [[ ! -z $(netstat -lnt | awk "\$6 == \"LISTEN\" && \$4 ~ \".$PORT\" && \$1 ~ \"$PROTO.?\"") ]] ; do 149 etcdctl --no-sync -C "$ETCD" set "$ETCD_PATH/host" "$HOST" --ttl "$ETCD_TTL" >/dev/null 150 etcdctl --no-sync -C "$ETCD" set "$ETCD_PATH/port" "$EXTERNAL_PORT" --ttl "$ETCD_TTL" >/dev/null 151 sleep $((ETCD_TTL/2)) # sleep for half the TTL 152 done 153 154 # if the loop quits, something went wrong 155 exit 1 156 157 fi 158 159 wait