From 15d800b89c31ab55d97e8379f23aca4666c4f73f Mon Sep 17 00:00:00 2001 From: Robert Dahlem Date: Mon, 17 Feb 2025 18:13:07 +0100 Subject: [PATCH 1/2] add PGHOST variable to env files so mk_postgres can use type local connections --- agents/plugins/mk_postgres.py | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/agents/plugins/mk_postgres.py b/agents/plugins/mk_postgres.py index 0a40a6483b6..195ebbf7980 100755 --- a/agents/plugins/mk_postgres.py +++ b/agents/plugins/mk_postgres.py @@ -26,9 +26,13 @@ Example of an environment file: -----/home/postgres/db1.env----------------------------------------- -export PGDATABASE="data" -export PGPORT="5432" -export PGVERSION="14" +PGDATABASE="data" +PGPORT="5432" +PGVERSION="14" +# optional: +# PGHOST="hostname.my.domain" +# or (for access through socket): +# PGHOST="/tmp" ---------------------------------------------------------- Inside of the environment file, only `PGPORT` is mandatory. @@ -49,8 +53,8 @@ ---------------------------------------------------------- -----/home/postgres/does-not-exist.env-------------------- -export PGDATABASE="main" -export PGPORT="5432" +PGDATABASE="main" +PGPORT="5432" ---------------------------------------------------------- The only difference being `/home/postgres/does-not-exist.env` does not exist in the first setup. @@ -167,6 +171,7 @@ def __init__(self, db_user, pg_binary_path, instance, process_match_patterns): self.name = instance["name"] self.pg_user = instance["pg_user"] self.pg_port = instance["pg_port"] + self.pg_host = instance["pg_host"] self.pg_database = instance["pg_database"] self.pg_passfile = instance.get("pg_passfile", "") self.pg_version = instance.get("pg_version") @@ -463,6 +468,8 @@ def run_sql_as_db_user( extra_args += " -U %s" % self.pg_user extra_args += " -d %s" % self.pg_database extra_args += " -p %s" % self.pg_port + if self.pg_host != "": + extra_args += " -h %s" % self.pg_host if quiet: extra_args += " -q" @@ -792,6 +799,8 @@ def _run_sql_as_db_user( extra_args += " -U %s" % self.pg_user extra_args += " -d %s" % self.pg_database extra_args += " -p %s" % self.pg_port + if self.pg_host != "": + extra_args += " -h %s" % self.pg_host if quiet: extra_args += " -q" @@ -1209,6 +1218,7 @@ def parse_env_file(env_file): pg_port = None # mandatory in env_file pg_database = "postgres" # default value pg_version = None + pg_host = "" for line in open_env_file(env_file): line = line.strip() @@ -1220,10 +1230,12 @@ def parse_env_file(env_file): pg_port = re.sub(re.compile("#.*"), "", line.split("=")[-1]).strip() elif "PGVERSION=" in line: pg_version = re.sub(re.compile("#.*"), "", line.split("=")[-1]).strip() + elif "PGHOST=" in line: + pg_host = re.sub(re.compile("#.*"), "", line.split("=")[-1]).strip() if pg_port is None: raise ValueError("PGPORT is not specified in %s" % env_file) - return pg_database, pg_port, pg_version + return pg_database, pg_port, pg_version, pg_host def _parse_INSTANCE_value(value, config_separator): @@ -1260,7 +1272,7 @@ def parse_postgres_cfg(postgres_cfg, config_separator): env_file, pg_user, pg_passfile, instance_name = _parse_INSTANCE_value( value, config_separator ) - pg_database, pg_port, pg_version = parse_env_file(env_file) + pg_database, pg_port, pg_version, pg_host = parse_env_file(env_file) instances.append( { "name": instance_name.strip(), @@ -1268,6 +1280,7 @@ def parse_postgres_cfg(postgres_cfg, config_separator): "pg_passfile": pg_passfile.strip(), "pg_database": pg_database, "pg_port": pg_port, + "pg_host": pg_host, "pg_version": pg_version, } ) @@ -1328,6 +1341,7 @@ def main(argv=None): "pg_user": "postgres", "pg_database": "postgres", "pg_port": "5432", + "pg_host": "", # Assumption: if no pg_passfile is specified no password will be required. # If a password is required but no pg_passfile is specified the process will # interactivly prompt for a password. From 87aa190601ed5b92f3473a24c43646e7750f905d Mon Sep 17 00:00:00 2001 From: Robert Dahlem Date: Thu, 20 Feb 2025 18:40:56 +0100 Subject: [PATCH 2/2] adjusted type hint of parse_env_file() because of new return value --- agents/plugins/mk_postgres.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/agents/plugins/mk_postgres.py b/agents/plugins/mk_postgres.py index 195ebbf7980..4983cc549d5 100755 --- a/agents/plugins/mk_postgres.py +++ b/agents/plugins/mk_postgres.py @@ -1214,7 +1214,7 @@ def open_env_file(file_to_open): def parse_env_file(env_file): - # type: (str) -> tuple[str, str, str | None] + # type: (str) -> tuple[str, str, str | None, str] pg_port = None # mandatory in env_file pg_database = "postgres" # default value pg_version = None