|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from .. import internal_platform_utils as base |
| 4 | +from ... import internal_utils |
| 5 | + |
| 6 | +from testgres.operations.os_ops import OsOperations |
| 7 | +from testgres.operations.exceptions import ExecUtilException |
| 8 | + |
| 9 | +import re |
| 10 | +import shlex |
| 11 | + |
| 12 | + |
| 13 | +class InternalPlatformUtils(base.InternalPlatformUtils): |
| 14 | + C_BASH_EXE = "/bin/bash" |
| 15 | + |
| 16 | + sm_exec_env = { |
| 17 | + "LANG": "en_US.UTF-8", |
| 18 | + "LC_ALL": "en_US.UTF-8", |
| 19 | + } |
| 20 | + |
| 21 | + # -------------------------------------------------------------------- |
| 22 | + def FindPostmaster( |
| 23 | + self, |
| 24 | + os_ops: OsOperations, |
| 25 | + bin_dir: str, |
| 26 | + data_dir: str |
| 27 | + ) -> InternalPlatformUtils.FindPostmasterResult: |
| 28 | + assert isinstance(os_ops, OsOperations) |
| 29 | + assert type(bin_dir) == str # noqa: E721 |
| 30 | + assert type(data_dir) == str # noqa: E721 |
| 31 | + assert type(__class__.C_BASH_EXE) == str # noqa: E721 |
| 32 | + assert type(__class__.sm_exec_env) == dict # noqa: E721 |
| 33 | + assert len(__class__.C_BASH_EXE) > 0 |
| 34 | + assert len(bin_dir) > 0 |
| 35 | + assert len(data_dir) > 0 |
| 36 | + |
| 37 | + pg_path_e = re.escape(os_ops.build_path(bin_dir, "postgres")) |
| 38 | + data_dir_e = re.escape(data_dir) |
| 39 | + |
| 40 | + assert type(pg_path_e) == str # noqa: E721 |
| 41 | + assert type(data_dir_e) == str # noqa: E721 |
| 42 | + |
| 43 | + regexp = r"^\s*[0-9]+\s+" + pg_path_e + r"(\s+.*)?\s+\-[D]\s+" + data_dir_e + r"(\s+.*)?" |
| 44 | + |
| 45 | + cmd = [ |
| 46 | + __class__.C_BASH_EXE, |
| 47 | + "-c", |
| 48 | + "ps -ewwo \"pid=,args=\" | grep -E " + shlex.quote(regexp), |
| 49 | + ] |
| 50 | + |
| 51 | + exit_status, output_b, error_b = os_ops.exec_command( |
| 52 | + cmd=cmd, |
| 53 | + ignore_errors=True, |
| 54 | + verbose=True, |
| 55 | + exec_env=__class__.sm_exec_env, |
| 56 | + ) |
| 57 | + |
| 58 | + assert type(output_b) == bytes # noqa: E721 |
| 59 | + assert type(error_b) == bytes # noqa: E721 |
| 60 | + |
| 61 | + output = output_b.decode("utf-8") |
| 62 | + error = error_b.decode("utf-8") |
| 63 | + |
| 64 | + assert type(output) == str # noqa: E721 |
| 65 | + assert type(error) == str # noqa: E721 |
| 66 | + |
| 67 | + if exit_status == 1: |
| 68 | + return __class__.FindPostmasterResult.create_not_found() |
| 69 | + |
| 70 | + if exit_status != 0: |
| 71 | + errMsg = f"test command returned an unexpected exit code: {exit_status}" |
| 72 | + raise ExecUtilException( |
| 73 | + message=errMsg, |
| 74 | + command=cmd, |
| 75 | + exit_code=exit_status, |
| 76 | + out=output, |
| 77 | + error=error, |
| 78 | + ) |
| 79 | + |
| 80 | + lines = output.splitlines() |
| 81 | + assert type(lines) == list # noqa: E721 |
| 82 | + |
| 83 | + if len(lines) == 0: |
| 84 | + return __class__.FindPostmasterResult.create_not_found() |
| 85 | + |
| 86 | + if len(lines) > 1: |
| 87 | + msgs = [] |
| 88 | + msgs.append("Many processes like a postmaster are found: {}.".format(len(lines))) |
| 89 | + |
| 90 | + for i in range(len(lines)): |
| 91 | + assert type(lines[i]) == str # noqa: E721 |
| 92 | + lines.append("[{}] '{}'".format(i, lines[i])) |
| 93 | + continue |
| 94 | + |
| 95 | + internal_utils.send_log_debug("\n".join(lines)) |
| 96 | + return __class__.FindPostmasterResult.create_many_processes() |
| 97 | + |
| 98 | + def is_space_or_tab(ch) -> bool: |
| 99 | + assert type(ch) == str # noqa: E721 |
| 100 | + return ch == " " or ch == "\t" |
| 101 | + |
| 102 | + line = lines[0] |
| 103 | + start = 0 |
| 104 | + while start < len(line) and is_space_or_tab(line[start]): |
| 105 | + start += 1 |
| 106 | + |
| 107 | + pos = start |
| 108 | + while pos < len(line) and line[pos].isnumeric(): |
| 109 | + pos += 1 |
| 110 | + |
| 111 | + if pos == start: |
| 112 | + return __class__.FindPostmasterResult.create_has_problems() |
| 113 | + |
| 114 | + if pos != len(line) and not line[pos].isspace(): |
| 115 | + return __class__.FindPostmasterResult.create_has_problems() |
| 116 | + |
| 117 | + pid = int(line[start:pos]) |
| 118 | + assert type(pid) == int # noqa: E721 |
| 119 | + |
| 120 | + return __class__.FindPostmasterResult.create_ok(pid) |
0 commit comments