This repository has been archived by the owner on Aug 16, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
dbinit.sh
72 lines (60 loc) · 1.68 KB
/
dbinit.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#!/bin/bash
#
# This script prepares application database via the following steps:
#
# 1. Create user/database if needed
# 2. Run /home/app/.ondbcreate if it exists and db was just created
# 3. Run /home/app/.ondbready if it exists
# 4. Start supervisord services listed in $DBINIT_START
#
# See project home for details: https://github.com/LeKovr/consup
#
ver="1.1"
# strict mode http://redsymbol.net/articles/unofficial-bash-strict-mode/
set -euo pipefail
IFS=$'\n\t'
# logging func
log() {
D=$(date "+%F %T")
echo "$D $1"
}
# onExit handler
function finish {
# Special formatted message, do not change
log "** Init done **"
}
trap finish EXIT
# ------------------------------------------------------------------------------
log "***** Run dbinit.sh v$ver for database $DB_NAME ***"
log "Wait for consul and postgresql startup..."
while true; do sleep 1 && ping -c1 $PG_HOST > /dev/null 2>&1 && break; done
log "DB started"
if [ -z ${DB_TEMPLATE+x} ]; then
tmpl=""
note=""
else
log "Will use template $DB_TEMPLATE"
tmpl="&tmpl=$DB_TEMPLATE"
note=" with template $DB_TEMPLATE"
fi
# Try to create user & database. Get result
log "Check db"
curl -s "http://$PG_HOST:$DBCC_PORT/?key=$DBCC_KEY&name=$DB_NAME&pass=$DB_PASS$tmpl" | grep "OK: 1" && {
log "Created database $DB_NAME$note"
if [ -e /home/app/.ondbcreate ] ; then
log "Run onDBCreate script"
. /home/app/.ondbcreate
fi
}
if [ -e /home/app/.ondbready ] ; then
log "Run onDBReady script"
. /home/app/.ondbready
fi
if [ ! -z ${DBINIT_START+x} ]; then
log "Start app"
for s in $DBINIT_START ; do
supervisorctl -c /etc/supervisor/supervisord.conf start $s
done
fi
# Say all is Ok to supervisor
exit 0