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 fi
# Drop any open connections to any old database. # 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 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") db_dir = os.path.join(tmp, "zulip-backup", "database")
os.setresuid(0, 0, 0) os.setresuid(0, 0, 0)
run(["chown", "-R", POSTGRES_USER, "--", tmp]) run(["chown", "-R", POSTGRES_USER, "--", tmp])
as_postgres = ["su", "-s", "/usr/bin/env", "-", "--", POSTGRES_USER]
run( run(
[ [
*as_postgres,
os.path.join( os.path.join(
settings.DEPLOY_ROOT, "scripts", "setup", "terminate-psql-sessions", settings.DEPLOY_ROOT, "scripts", "setup", "terminate-psql-sessions",
), ),
"zulip", "zulip",
"zulip",
"zulip_base", "zulip_base",
], ],
) )
as_postgres = ["su", "-s", "/usr/bin/env", "-", "--", POSTGRES_USER]
run(as_postgres + ["dropdb", "--if-exists", "--", db_name]) run(as_postgres + ["dropdb", "--if-exists", "--", db_name])
run(as_postgres + ["createdb", "-O", "zulip", "-T", "template0", "--", db_name]) run(as_postgres + ["createdb", "-O", "zulip", "-T", "template0", "--", db_name])

View File

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

View File

@ -2,7 +2,8 @@
set -e set -e
set -x 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 psql -v ON_ERROR_STOP=1 -e -h localhost postgres zulip <<EOF
DROP DATABASE IF EXISTS zulip; DROP DATABASE IF EXISTS zulip;

View File

@ -22,7 +22,8 @@ EOF
mkdir -p zerver/tests/fixtures 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 create_zulip_test

View File

@ -3,7 +3,8 @@ set -e
run() 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 psql -v ON_ERROR_STOP=1 -h localhost postgres zulip_test << EOF
DROP DATABASE IF EXISTS zulip_test; DROP DATABASE IF EXISTS zulip_test;
CREATE DATABASE zulip_test TEMPLATE zulip_test_template; CREATE DATABASE zulip_test TEMPLATE zulip_test_template;

View File

@ -93,7 +93,8 @@ else
fi fi
chmod go-rw ~/.pgpass 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 psql -v ON_ERROR_STOP=1 -e -h localhost postgres "$USERNAME" <<EOF
DROP DATABASE IF EXISTS $DBNAME; DROP DATABASE IF EXISTS $DBNAME;