aboutsummaryrefslogtreecommitdiffstats
path: root/bin/docker.preinit
blob: 501878185ac86c5aba1ed904b00557a3e4369804 (plain)
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
#!/bin/sh

# Things to do before starting FixMyStreet in Docker

# Make sure that the Postgres environment is up and running.
echo "Testing connection to ${FMS_DB_HOST}."
while ! pg_isready -h $FMS_DB_HOST >/dev/null 2>&1 ; do
  echo "Still waiting for ${FMS_DB_HOST}..."
  sleep 1
done
echo "Done."

# If there's a password for the postgres user, set it up for root and see if we need
# to create an FMS user. This is intended for use when using a dedicated local postgres
# container. If this variable doesn't exist, we're going to assume that the FMS user
# has been created already so the stuff below will work.
if [ -n "$POSTGRES_PASSWORD" ]; then
  echo "${FMS_DB_HOST}:*:*:postgres:${POSTGRES_PASSWORD}" > /root/.pgpass
  chmod 0600 /root/.pgpass
  psql -h $FMS_DB_HOST -U postgres postgres -c "create user \"${FMS_DB_USER}\" with CREATEDB password '${FMS_DB_PASS}'" || true
fi

# Set up a .pgpass for the FMS user. Note that we're assuming the same name for
# both the local shell account and the DB user.
su ${FMS_DB_USER} -c "echo \"${FMS_DB_HOST}:*:*:${FMS_DB_USER}:${FMS_DB_PASS}\" > /home/${FMS_DB_USER}/.pgpass"
chmod 0600 /home/${FMS_DB_USER}/.pgpass

# If the FMS database doesn't exist, try to create it.
if ! su $FMS_DB_USER -c "psql -h $FMS_DB_HOST -U $FMS_DB_USER -l | egrep \"^ *${FMS_DB_NAME} *\|\" > /dev/null" ; then
    su $FMS_DB_USER -c "createdb -h $FMS_DB_HOST -U $FMS_DB_USER --owner \"$FMS_DB_USER\" \"$FMS_DB_NAME\""
fi

# Slot in cobrand, if one is present
su $FMS_DB_USER -c "${FMS_ROOT}/bin/docker-cobrand"

# Ensure things are up to date - schema, CSS, etc
${FMS_ROOT}/bin/install_packages docker
su $FMS_DB_USER -c "${FMS_ROOT}/script/update"

# Update reports
su $FMS_DB_USER -c "${FMS_ROOT}/bin/update-all-reports"

# If the right environment variables are present, set up a FMS superuser account.
if [ -n "$SUPERUSER_PASSWORD" ] && [ -n "$SUPERUSER_EMAIL" ]; then
  su $FMS_DB_USER -c "${FMS_ROOT}/bin/createsuperuser $SUPERUSER_EMAIL $SUPERUSER_PASSWORD"
fi