-
Notifications
You must be signed in to change notification settings - Fork 1
/
docker-entrypoint.sh
executable file
·111 lines (92 loc) · 3.18 KB
/
docker-entrypoint.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#!/usr/bin/env bash
set -e
run_sql() {
PGSSLMODE="require" \
PGPASSWORD="${FEATURES_DB_PASSWORD}" \
psql \
--tuples-only \
--username="${FEATURES_DB_USER}" \
--port="${FEATURES_DB_PORT}" \
--host="${FEATURES_DB_HOST}" \
--dbname="$1"
}
load_features_dump() {
PGSSLMODE="require" \
PGPASSWORD="${FEATURES_DB_PASSWORD}" \
pg_restore \
--username="${FEATURES_DB_USER}" \
--port="${FEATURES_DB_PORT}" \
--host="${FEATURES_DB_HOST}" \
--dbname="${FEATURES_DB_NAME}" \
--jobs="$(grep -c ^processor /proc/cpuinfo)" \
"$1"
}
features_database_exists() {
echo "SELECT 'yes' FROM pg_database WHERE datname='${FEATURES_DB_NAME}';" \
| run_sql 'postgres' \
| grep -q 'yes'
}
can_connect_to_database() {
echo "SELECT version();" \
| run_sql 'postgres'
}
log() {
echo "[$(date)] $1"
}
fail() {
echo "$1" >&2
exit 1
}
if [ -z "${FEATURES_DB_USER}" ]; then fail "Need to provide FEATURES_DB_USER environment variable"; fi
if [ -z "${FEATURES_DB_PASSWORD}" ]; then fail "Need to provide FEATURES_DB_PASSWORD environment variable"; fi
if [ -z "${FEATURES_DB_HOST}" ]; then fail "Need to provide FEATURES_DB_HOST environment variable"; fi
while ! can_connect_to_database; do
log "Unable to connect to database, retrying."
sleep 30s
done
username="${FEATURES_DB_USER%@*}"
hostname="${FEATURES_DB_USER#*@}"
if ! features_database_exists; then
dump_file="/tmp/db.fc.gz"
log "Setting up database..."
echo "CREATE DATABASE ${FEATURES_DB_NAME};" | run_sql 'postgres'
echo "CREATE USER ops WITH login password 'changeme';" | run_sql 'postgres'
echo "CREATE USER frontend WITH login password 'changeme';" | run_sql 'postgres'
echo "ALTER USER ops WITH password '${FEATURES_DB_PASSWORD}';" | run_sql 'postgres'
echo "ALTER USER frontend WITH password '${FEATURES_DB_PASSWORD}';" | run_sql 'postgres'
echo "GRANT ops TO ${username};" | run_sql 'postgres'
echo "GRANT frontend TO ${username};" | run_sql 'postgres'
log "...done, database is now set up"
log "Setting up schema..."
< /app/ddl/schema.sql run_sql "${FEATURES_DB_NAME}"
log "...done, schema is now set up"
retries=0
max_retries=5
db_is_setup=0
while [ "${retries}" -lt "${max_retries}" ]; do
log "Fetching database dump..."
curl --silent "${FEATURES_DB_DUMP_URL}" > "${dump_file}"
log "...done, database dump is now available"
log "Ingesting database dump..."
if load_features_dump "${dump_file}"; then
log "...done, database dump is now ingested"
db_is_setup=1
break
else
retries="$((retries + 1))"
log "...error ingesting database dump, retrying"
fi
done
if [ "${db_is_setup}" -ne 1 ]; then fail "Unable to setup database in ${max_retries} retries"; fi
rm "${dump_file}"
log "Setting up indices..."
< /app/ddl/indices.sql run_sql "${FEATURES_DB_NAME}"
log "...done, indices are now set up"
log "Improving query planner..."
echo "ANALYZE;" | run_sql "${FEATURES_DB_NAME}"
log "...done, query planner is now ready"
fi
export FEATURES_CONNECTION_STRING="postgres://frontend@${hostname}:${FEATURES_DB_PASSWORD}@${FEATURES_DB_HOST}:${FEATURES_DB_PORT}/${FEATURES_DB_NAME}?ssl=true"
while ! npm start; do
sleep 30s
done