Skip to content

Commit

Permalink
terminate-psql-sessions: Rely on the caller to set PGHOST, PGUSER.
Browse files Browse the repository at this point in the history
Signed-off-by: Anders Kaseorg <[email protected]>
  • Loading branch information
andersk authored and timabbott committed Jun 16, 2020
1 parent f2ce856 commit fa2496c
Show file tree
Hide file tree
Showing 7 changed files with 13 additions and 28 deletions.
3 changes: 2 additions & 1 deletion scripts/setup/postgres-init-db
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions scripts/setup/restore-backup
Original file line number Diff line number Diff line change
Expand Up @@ -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])

Expand Down
22 changes: 1 addition & 21 deletions scripts/setup/terminate-psql-sessions
Original file line number Diff line number Diff line change
@@ -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
Expand Down
3 changes: 2 additions & 1 deletion tools/rebuild-dev-database
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 2 additions & 1 deletion tools/rebuild-test-database
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
3 changes: 2 additions & 1 deletion tools/setup/generate-fixtures
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 2 additions & 1 deletion tools/setup/postgres-init-dev-db
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit fa2496c

Please sign in to comment.