installer: Add run_psql_as_postgres function zulip_tools.py.

Add a helper `run_psql_as_postgres` function in
`scripts/lib/zulip_tools.py`.  This is preparatory refactoring for the
work to add custom database and user names.
This commit is contained in:
Adam Birds 2021-04-11 17:59:48 +00:00 committed by Tim Abbott
parent a9e73358bf
commit 21cc186105
2 changed files with 23 additions and 7 deletions

View File

@ -32,6 +32,7 @@ from scripts.lib.zulip_tools import (
get_config_file,
get_tornado_ports,
parse_os_release,
run_psql_as_postgres,
su_to_zulip,
)
@ -137,13 +138,8 @@ if os.path.exists("/etc/init.d/postgresql"):
if glob.glob("/usr/share/postgresql/*/extension/tsearch_extras.control"):
# Remove legacy tsearch_extras package references
subprocess.check_call(
[
"su",
"postgres",
"-c",
'psql -v ON_ERROR_STOP=1 zulip -c "DROP EXTENSION IF EXISTS tsearch_extras;"',
]
run_psql_as_postgres(
sql_query="DROP EXTENSION IF EXISTS tsearch_extras;",
)
subprocess.check_call(["apt-get", "remove", "-y", "postgresql-*-tsearch-extras"])

View File

@ -580,6 +580,26 @@ def get_deploy_options(config_file: configparser.RawConfigParser) -> List[str]:
return get_config(config_file, "deployment", "deploy_options", "").strip().split()
def run_psql_as_postgres(
sql_query: str,
) -> None:
subcmd = " ".join(
map(
shlex.quote,
[
"psql",
"-v",
"ON_ERROR_STOP=1",
# TODO: Stop hardcoding the database name.
"zulip",
"-c",
sql_query,
],
)
)
subprocess.check_call(["su", "postgres", "-c", subcmd])
def get_tornado_ports(config_file: configparser.RawConfigParser) -> List[int]:
ports = []
if config_file.has_section("tornado_sharding"):