2018-12-18 02:08:53 +01:00
|
|
|
|
#!/usr/bin/env bash
|
2015-09-28 19:45:35 +02:00
|
|
|
|
set -e
|
2018-08-21 20:36:50 +02:00
|
|
|
|
|
|
|
|
|
if [ "$EUID" -ne 0 ]; then
|
|
|
|
|
echo "Error: This script must be run as root" >&2
|
|
|
|
|
exit 1
|
|
|
|
|
fi
|
2015-09-28 19:45:35 +02:00
|
|
|
|
set -x
|
2013-02-25 21:46:36 +01:00
|
|
|
|
|
2016-04-27 00:23:37 +02:00
|
|
|
|
# What user should we use for connecting to the database
|
|
|
|
|
POSTGRES_USER="${POSTGRES_USER:-postgres}"
|
|
|
|
|
|
2021-05-25 05:45:24 +02:00
|
|
|
|
# What database name and username to use when connecting to the database
|
|
|
|
|
DATABASE_NAME=$(crudini --get /etc/zulip/zulip.conf postgresql database_name 2>/dev/null || echo zulip)
|
|
|
|
|
DATABASE_USER=$(crudini --get /etc/zulip/zulip.conf postgresql database_user 2>/dev/null || echo zulip)
|
|
|
|
|
|
2020-10-23 02:43:28 +02:00
|
|
|
|
# This psql command may fail because the Zulip database doesn’t exist,
|
2020-02-19 05:46:57 +01:00
|
|
|
|
# hence the &&.
|
|
|
|
|
if records="$(
|
2020-10-15 04:55:57 +02:00
|
|
|
|
cd / # Make sure the current working directory is readable by postgres
|
2021-05-25 05:45:24 +02:00
|
|
|
|
su "$POSTGRES_USER" -c "psql -v ON_ERROR_STOP=1 -Atc 'SELECT COUNT(*) FROM $DATABASE_NAME.zerver_message;' $DATABASE_USER"
|
2020-02-19 05:46:57 +01:00
|
|
|
|
)" && [ "$records" -gt 200 ]; then
|
2016-04-21 00:29:11 +02:00
|
|
|
|
set +x
|
|
|
|
|
echo "WARNING: This will delete your Zulip database which currently contains $records messages."
|
2019-09-13 00:14:32 +02:00
|
|
|
|
read -p "Do you want to proceed? [y/N] " -r
|
2016-04-21 00:29:11 +02:00
|
|
|
|
echo
|
2020-10-15 04:55:57 +02:00
|
|
|
|
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
|
2016-04-21 00:29:11 +02:00
|
|
|
|
exit 1
|
|
|
|
|
fi
|
|
|
|
|
set -x
|
|
|
|
|
fi
|
|
|
|
|
|
2015-10-15 04:47:35 +02:00
|
|
|
|
# Shut down all services to ensure a quiescent state.
|
2016-04-27 00:20:06 +02:00
|
|
|
|
if [ -e "/var/run/supervisor.sock" ]; then
|
2021-05-14 06:05:47 +02:00
|
|
|
|
supervisorctl stop all
|
2016-04-27 00:20:06 +02:00
|
|
|
|
fi
|
2015-10-15 04:47:35 +02:00
|
|
|
|
|
2020-02-19 05:46:57 +01:00
|
|
|
|
# Drop any open connections to any old database.
|
2020-07-02 23:00:35 +02:00
|
|
|
|
# Send the script via stdin in case the postgres user lacks permission to read it.
|
2020-06-16 01:36:02 +02:00
|
|
|
|
su -s /usr/bin/env - -- "$POSTGRES_USER" \
|
2020-10-15 04:55:57 +02:00
|
|
|
|
bash -s - zulip zulip_base <"$(dirname "$0")/terminate-psql-sessions"
|
2015-09-28 19:45:35 +02:00
|
|
|
|
|
2015-12-22 19:43:20 +01:00
|
|
|
|
(
|
2020-10-15 04:55:57 +02:00
|
|
|
|
cd / # Make sure the current working directory is readable by postgres
|
2021-05-25 05:45:24 +02:00
|
|
|
|
su "$POSTGRES_USER" -c "psql -v ON_ERROR_STOP=1 -v dbname=$DATABASE_NAME -v dbuser=$DATABASE_USER -e"
|
2020-10-15 04:55:57 +02:00
|
|
|
|
) <"$(dirname "$0")/create-db.sql"
|
2015-08-21 07:17:25 +02:00
|
|
|
|
|
2021-05-25 05:45:24 +02:00
|
|
|
|
# Set a postgres password if the postgres username is not "zulip".
|
|
|
|
|
# When the username is zulip, we rely on running as the zulip system
|
|
|
|
|
# user for authentication via postgres' peer authentication.
|
|
|
|
|
if [ "$DATABASE_USER" != "zulip" ]; then
|
|
|
|
|
PASSWORD=$(crudini --get /etc/zulip/zulip-secrets.conf secrets postgres_password)
|
|
|
|
|
su "$POSTGRES_USER" -c "psql -v ON_ERROR_STOP=1 -e postgres" <<EOF
|
|
|
|
|
ALTER ROLE $DATABASE_USER PASSWORD '$PASSWORD';
|
|
|
|
|
EOF
|
|
|
|
|
fi
|
|
|
|
|
|
2015-09-28 19:45:35 +02:00
|
|
|
|
# Clear memcached to avoid contamination from previous database state
|
2016-09-29 05:46:34 +02:00
|
|
|
|
"$(dirname "$0")/flush-memcached"
|
2015-09-28 19:57:40 +02:00
|
|
|
|
|
2013-02-25 21:46:36 +01:00
|
|
|
|
echo "Database created"
|