terminate-psql-sessions: Rely on the caller to set PGHOST, PGUSER.

Signed-off-by: Anders Kaseorg <anders@zulip.com>
This commit is contained in:
Anders Kaseorg 2020-06-15 16:36:02 -07:00 committed by Tim Abbott
parent f2ce856b8f
commit fa2496c229
7 changed files with 13 additions and 28 deletions

View File

@ -33,7 +33,8 @@ if [ -e "/var/run/supervisor.sock" ]; then
fi
# Drop any open connections to any old database.
"$(dirname "$0")/terminate-psql-sessions" postgres zulip zulip_base
su -s /usr/bin/env - -- "$POSTGRES_USER" \
"$(dirname "$0")/terminate-psql-sessions" zulip zulip_base
(
cd / # Make sure the current working directory is readable by postgres

View File

@ -84,17 +84,17 @@ def restore_backup(tarball_file: IO[bytes]) -> None:
db_dir = os.path.join(tmp, "zulip-backup", "database")
os.setresuid(0, 0, 0)
run(["chown", "-R", POSTGRES_USER, "--", tmp])
as_postgres = ["su", "-s", "/usr/bin/env", "-", "--", POSTGRES_USER]
run(
[
*as_postgres,
os.path.join(
settings.DEPLOY_ROOT, "scripts", "setup", "terminate-psql-sessions",
),
"zulip",
"zulip",
"zulip_base",
],
)
as_postgres = ["su", "-s", "/usr/bin/env", "-", "--", POSTGRES_USER]
run(as_postgres + ["dropdb", "--if-exists", "--", db_name])
run(as_postgres + ["createdb", "-O", "zulip", "-T", "template0", "--", db_name])

View File

@ -1,32 +1,12 @@
#!/usr/bin/env bash
set -e
POSTGRES_USER="postgres"
if [ "$(uname)" = "OpenBSD" ]; then
POSTGRES_USER="_postgresql"
fi
cd /
username=$1
shift
tables="$(printf "'%s'," "${@//\'/\'\'}")"
tables="${tables%,}"
case "$(id -un)" in
root)
psql=(su -s /usr/bin/env - -- "$POSTGRES_USER" psql postgres "$POSTGRES_USER")
;;
"$POSTGRES_USER")
psql=(psql postgres "$POSTGRES_USER")
;;
*)
psql=(psql -h localhost postgres "$username")
;;
esac
"${psql[@]}" -v ON_ERROR_STOP=1 <<EOF
psql postgres -v ON_ERROR_STOP=1 <<EOF
SELECT pg_terminate_backend(s.pid)
FROM pg_stat_activity s, pg_roles r
WHERE

View File

@ -2,7 +2,8 @@
set -e
set -x
"$(dirname "$0")/../scripts/setup/terminate-psql-sessions" zulip zulip zulip_base
PGHOST=localhost PGUSER=zulip \
"$(dirname "$0")/../scripts/setup/terminate-psql-sessions" zulip zulip_base
psql -v ON_ERROR_STOP=1 -e -h localhost postgres zulip <<EOF
DROP DATABASE IF EXISTS zulip;

View File

@ -22,7 +22,8 @@ EOF
mkdir -p zerver/tests/fixtures
"$(dirname "$0")/../scripts/setup/terminate-psql-sessions" zulip zulip_test zulip_test_base zulip_test_template
PGHOST=localhost PGUSER=zulip \
"$(dirname "$0")/../scripts/setup/terminate-psql-sessions" zulip_test zulip_test_base zulip_test_template
create_zulip_test

View File

@ -3,7 +3,8 @@ set -e
run()
{
"$(dirname "$0")/../../scripts/setup/terminate-psql-sessions" zulip zulip_test zulip_test_base zulip_test_template
PGHOST=localhost PGUSER=zulip \
"$(dirname "$0")/../../scripts/setup/terminate-psql-sessions" zulip_test zulip_test_base zulip_test_template
psql -v ON_ERROR_STOP=1 -h localhost postgres zulip_test << EOF
DROP DATABASE IF EXISTS zulip_test;
CREATE DATABASE zulip_test TEMPLATE zulip_test_template;

View File

@ -93,7 +93,8 @@ else
fi
chmod go-rw ~/.pgpass
"$(dirname "$0")/../../scripts/setup/terminate-psql-sessions" "$USERNAME" "$DBNAME" "$DBNAME_BASE"
PGHOST=localhost PGUSER="$USERNAME" \
"$(dirname "$0")/../../scripts/setup/terminate-psql-sessions" "$DBNAME" "$DBNAME_BASE"
psql -v ON_ERROR_STOP=1 -e -h localhost postgres "$USERNAME" <<EOF
DROP DATABASE IF EXISTS $DBNAME;