@@ -10,26 +10,29 @@ set -eo pipefail
1010[[ $DEBUG ]] && set -x
1111
1212# configure etcd
13- export ETCD_PORT=${ETCD_PORT:- 4001}
13+ export ETCD_PORT=" ${ETCD_PORT:- 4001} "
1414export ETCD=" $HOST :$ETCD_PORT "
15- export ETCD_PATH=${ETCD_PATH:-/ deis/ database}
16- export ETCD_TTL=${ETCD_TTL:- 20}
15+ export ETCD_PATH=" ${ETCD_PATH:-/ deis/ database} "
16+ export ETCD_TTL=" ${ETCD_TTL:- 20} "
1717
1818export BUCKET_NAME=${BUCKET_NAME:- db_wal}
1919
2020# 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
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
2424done
2525
2626# wait until etcd has discarded potentially stale values
27- sleep $(( $ ETCD_TTL+ 1 ))
27+ sleep $(( ETCD_TTL+ 1 ))
2828
2929function etcd_set_default {
3030 set +e
31- ERROR=" $( etcdctl --no-sync -C $ETCD mk $ETCD_PATH /$1 $2 2>&1 > /dev/null) "
32- if [[ $? -ne 0 && $( echo $ERROR | grep -ive " key already exists" ) ]]; then
31+ ERROR=" $( etcdctl --no-sync -C " $ETCD " mk " $ETCD_PATH /$1 " " $2 " 2>&1 > /dev/null) "
32+
33+ # We're not compairing output with -n so error doesn't apply
34+ # shellcheck disable=SC2143
35+ if [[ $? -ne 0 && $( echo " $ERROR " | grep -ive " key already exists" ) ]]; then
3336 echo " etcd_set_default: an etcd error occurred ($ERROR )"
3437 echo " aborting..."
3538 exit 1
@@ -38,41 +41,41 @@ function etcd_set_default {
3841}
3942
4043etcd_set_default engine postgresql_psycopg2
41- etcd_set_default adminUser ${PG_ADMIN_USER:- postgres}
42- etcd_set_default adminPass ${PG_ADMIN_PASS:- changeme123}
43- etcd_set_default user ${PG_USER_NAME:- deis}
44- etcd_set_default password ${PG_USER_PASS:- changeme123}
45- etcd_set_default name ${PG_USER_DB:- deis}
46- etcd_set_default bucketName ${BUCKET_NAME}
44+ etcd_set_default adminUser " ${PG_ADMIN_USER:- postgres} "
45+ etcd_set_default adminPass " ${PG_ADMIN_PASS:- changeme123} "
46+ etcd_set_default user " ${PG_USER_NAME:- deis} "
47+ etcd_set_default password " ${PG_USER_PASS:- changeme123} "
48+ etcd_set_default name " ${PG_USER_DB:- deis} "
49+ etcd_set_default bucketName " ${BUCKET_NAME} "
4750
4851# stub out the confd reload script before it gets templated
4952echo ' #!/bin/sh' > /usr/local/bin/reload
5053chmod 0755 /usr/local/bin/reload
5154
5255# wait for confd to run once and install initial templates
53- until confd -onetime -node $ETCD -confdir /app --log-level error; do
54- echo " database: waiting for confd to write initial templates..."
55- sleep $(( $ ETCD_TTL/ 2 )) # sleep for half the TTL
56+ until confd -onetime -node " $ETCD " -confdir /app --log-level error; do
57+ echo " database: waiting for confd to write initial templates..."
58+ sleep $(( ETCD_TTL/ 2 )) # sleep for half the TTL
5659done
5760
5861# initialize database if one doesn't already exist
5962# for example, in the case of a data container
6063if [[ ! -d /var/lib/postgresql/9.3/main ]]; then
61- chown -R postgres:postgres /var/lib/postgresql
62- sudo -u postgres /usr/bin/initdb -D /var/lib/postgresql/9.3/main
64+ chown -R postgres:postgres /var/lib/postgresql
65+ sudo -u postgres /usr/bin/initdb -D /var/lib/postgresql/9.3/main
6366fi
6467
6568# ensure WAL log bucket exists
66- envdir /etc/wal-e.d/env /app/bin/create_bucket ${BUCKET_NAME}
67- INIT_ID=$( etcdctl get $ETCD_PATH /initId 2> /dev/null || echo none)
69+ envdir /etc/wal-e.d/env /app/bin/create_bucket " ${BUCKET_NAME} "
70+ INIT_ID=$( etcdctl get " $ETCD_PATH /initId" 2> /dev/null || echo none)
6871echo " database: expecting initialization id: $INIT_ID "
6972
7073initial_backup=0
7174if [[ " $( cat /var/lib/postgresql/9.3/main/initialized 2> /dev/null) " != " $INIT_ID " ]]; then
7275 echo " database: no existing database found or it is outdated."
7376 # check if there are any backups -- if so, let's restore
7477 # we could probably do better than just testing number of lines -- one line is just a heading, meaning no backups
75- if [[ ` envdir /etc/wal-e.d/env wal-e --terse backup-list | wc -l` -gt " 1" ]]; then
78+ if [[ $( envdir /etc/wal-e.d/env wal-e --terse backup-list | wc -l) -gt " 1" ]]; then
7679 echo " database: restoring from backup..."
7780 rm -rf /var/lib/postgresql/9.3/main
7881 sudo -u postgres envdir /etc/wal-e.d/env wal-e backup-fetch /var/lib/postgresql/9.3/main LATEST
@@ -85,16 +88,16 @@ if [[ "$(cat /var/lib/postgresql/9.3/main/initialized 2> /dev/null)" != "$INIT_I
8588 fi
8689 # either way, we mark the database as initialized
8790 INIT_ID=$( cat /proc/sys/kernel/random/uuid)
88- echo $INIT_ID > /var/lib/postgresql/9.3/main/initialized
89- etcdctl --no-sync -C $ETCD set $ETCD_PATH /initId $INIT_ID > /dev/null
91+ echo " $INIT_ID " > /var/lib/postgresql/9.3/main/initialized
92+ etcdctl --no-sync -C " $ETCD " set " $ETCD_PATH /initId" " $INIT_ID " > /dev/null
9093else
9194 echo " database: existing data directory found. Starting postgres..."
9295fi
9396
9497# run the service in the background
9598sudo -i -u postgres /usr/bin/postgres \
96- -c config-file=${PG_CONFIG:-/ etc/ postgresql/ main/ postgresql.conf} \
97- -c listen-addresses=${PG_LISTEN:-* } &
99+ -c config-file=" ${PG_CONFIG:-/ etc/ postgresql/ main/ postgresql.conf} " \
100+ -c listen-addresses=" ${PG_LISTEN:-* } " &
98101
99102SERVICE_PID=$!
100103
@@ -107,8 +110,7 @@ function on_exit() {
107110trap on_exit INT TERM
108111
109112# spawn confd in the background to update services based on etcd changes
110- confd -node $ETCD -confdir /app --log-level error --interval 5 &
111- CONFD_PID=$!
113+ confd -node " $ETCD " -confdir /app --log-level error --interval 5 &
112114
113115# wait for the service to become available
114116sleep 1 && while [[ -z $( netstat -lnt | awk " \$ 6 == \" LISTEN\" && \$ 4 ~ \" .5432\" && \$ 1 ~ \" tcp.?\" " ) ]] ; do sleep 1; done
@@ -128,24 +130,24 @@ echo "database: postgres is running..."
128130
129131# publish the service to etcd using the injected HOST and EXTERNAL_PORT
130132if [[ ! -z $EXTERNAL_PORT ]]; then
131- # configure service discovery
132- PORT=${PORT:- 5432}
133- PROTO=${PROTO:- tcp}
133+ # configure service discovery
134+ PORT=${PORT:- 5432}
135+ PROTO=${PROTO:- tcp}
134136
135- set +e
137+ set +e
136138
137- # wait for the service to become available on PORT
138- sleep 1 && while [[ -z $( netstat -lnt | awk " \$ 6 == \" LISTEN\" && \$ 4 ~ \" .$PORT \" && \$ 1 ~ \" $PROTO .?\" " ) ]] ; do sleep 1; done
139+ # wait for the service to become available on PORT
140+ sleep 1 && while [[ -z $( netstat -lnt | awk " \$ 6 == \" LISTEN\" && \$ 4 ~ \" .$PORT \" && \$ 1 ~ \" $PROTO .?\" " ) ]] ; do sleep 1; done
139141
140- # while the port is listening, publish to etcd
141- while [[ ! -z $( netstat -lnt | awk " \$ 6 == \" LISTEN\" && \$ 4 ~ \" .$PORT \" && \$ 1 ~ \" $PROTO .?\" " ) ]] ; do
142- etcdctl --no-sync -C $ETCD set $ETCD_PATH /host $HOST --ttl $ETCD_TTL > /dev/null
143- etcdctl --no-sync -C $ETCD set $ETCD_PATH /port $EXTERNAL_PORT --ttl $ETCD_TTL > /dev/null
144- sleep $(( $ ETCD_TTL/ 2 )) # sleep for half the TTL
145- done
142+ # while the port is listening, publish to etcd
143+ while [[ ! -z $( netstat -lnt | awk " \$ 6 == \" LISTEN\" && \$ 4 ~ \" .$PORT \" && \$ 1 ~ \" $PROTO .?\" " ) ]] ; do
144+ etcdctl --no-sync -C " $ETCD " set " $ETCD_PATH /host" " $HOST " --ttl " $ETCD_TTL " > /dev/null
145+ etcdctl --no-sync -C " $ETCD " set " $ETCD_PATH /port" " $EXTERNAL_PORT " --ttl " $ETCD_TTL " > /dev/null
146+ sleep $(( ETCD_TTL/ 2 )) # sleep for half the TTL
147+ done
146148
147- # if the loop quits, something went wrong
148- exit 1
149+ # if the loop quits, something went wrong
150+ exit 1
149151
150152fi
151153
0 commit comments