Skip to content

add PGHOST variable to env files so mk_postgres can use type local co… #792

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 22 additions & 8 deletions agents/plugins/mk_postgres.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,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.
Expand All @@ -44,8 +48,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.
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -793,6 +800,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"
Expand Down Expand Up @@ -1206,10 +1215,11 @@ 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
pg_host = ""

for line in open_env_file(env_file):
line = line.strip()
Expand All @@ -1221,10 +1231,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):
Expand Down Expand Up @@ -1261,14 +1273,15 @@ 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(),
"pg_user": pg_user.strip(),
"pg_passfile": pg_passfile.strip(),
"pg_database": pg_database,
"pg_port": pg_port,
"pg_host": pg_host,
"pg_version": pg_version,
}
)
Expand Down Expand Up @@ -1329,6 +1342,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.
Expand Down