| 1 |
# Copyright (c) 2025, Dexter Le <[email protected]> |
| 2 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 3 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 4 |
|
| 5 |
# Note that this module util is **PRIVATE** to the collection. It can have breaking changes at any time. |
| 6 |
# Do not use this from other collections or standalone plugins/modules! |
| 7 |
|
| 8 |
from __future__ import annotations |
| 9 |
|
| 10 |
import re |
| 11 |
import time |
| 12 |
import typing as t |
| 13 |
|
| 14 |
from ansible_collections.community.general.plugins.module_utils._cmd_runner import CmdRunner, cmd_runner_fmt |
| 15 |
|
| 16 |
if t.TYPE_CHECKING: |
| 17 |
from ansible.module_utils.basic import AnsibleModule |
| 18 |
|
| 19 |
|
| 20 |
_state_map = { |
| 21 |
"present": "create", |
| 22 |
"absent": "remove", |
| 23 |
"cloned": "clone", |
| 24 |
"status": "status", |
| 25 |
"enabled": "enable", |
| 26 |
"disabled": "disable", |
| 27 |
"online": "start", |
| 28 |
"offline": "stop", |
| 29 |
"maintenance": "set", |
| 30 |
"config": "config", |
| 31 |
"cleanup": "cleanup", |
| 32 |
} |
| 33 |
|
| 34 |
|
| 35 |
def fmt_resource_type(value): |
| 36 |
return [ |
| 37 |
":".join( |
| 38 |
value[k] for k in ["resource_standard", "resource_provider", "resource_name"] if value.get(k) is not None |
| 39 |
) |
| 40 |
] |
| 41 |
|
| 42 |
|
| 43 |
def fmt_resource_operation(value): |
| 44 |
cmd = [] |
| 45 |
for op in value: |
| 46 |
cmd.append("op") |
| 47 |
cmd.append(op.get("operation_action")) |
| 48 |
for operation_option in op.get("operation_option"): |
| 49 |
cmd.append(operation_option) |
| 50 |
|
| 51 |
return cmd |
| 52 |
|
| 53 |
|
| 54 |
def fmt_resource_argument(value): |
| 55 |
return ["--group" if value["argument_action"] == "group" else value["argument_action"]] + value["argument_option"] |
| 56 |
|
| 57 |
|
| 58 |
def get_pacemaker_maintenance_mode(runner: CmdRunner) -> bool: |
| 59 |
with runner("cli_action config") as ctx: |
| 60 |
rc, out, err = ctx.run(cli_action="property") |
| 61 |
maint_mode_re = re.compile(r"maintenance-mode.*true", re.IGNORECASE) |
| 62 |
maintenance_mode_output = [line for line in out.splitlines() if maint_mode_re.search(line)] |
| 63 |
return bool(maintenance_mode_output) |
| 64 |
|
| 65 |
|
| 66 |
def wait_for_resource(runner: CmdRunner, cli_noun: str, name: str, wait: int, sleep_interval: int = 5) -> None: |
| 67 |
"""Poll ``pcs <cli_noun> status <name>`` until the resource reports Started or the wait budget expires. |
| 68 |
|
| 69 |
Raises an exception if the resource does not reach the Started state within *wait* seconds. |
| 70 |
""" |
| 71 |
deadline = time.monotonic() + wait |
| 72 |
while True: |
| 73 |
with runner("cli_action state name") as ctx: |
| 74 |
rc, out, err = ctx.run(cli_action=cli_noun, state="status") |
| 75 |
if out and "Started" in out: |
| 76 |
return |
| 77 |
if time.monotonic() >= deadline: |
| 78 |
raise Exception(f"Timed out waiting {wait}s for {cli_noun} resource '{name}' to start") |
| 79 |
time.sleep(sleep_interval) |
| 80 |
|
| 81 |
|
| 82 |
def pacemaker_runner(module: AnsibleModule, **kwargs) -> CmdRunner: |
| 83 |
runner_command = ["pcs"] |
| 84 |
runner = CmdRunner( |
| 85 |
module, |
| 86 |
command=runner_command, |
| 87 |
arg_formats=dict( |
| 88 |
cli_action=cmd_runner_fmt.as_list(), |
| 89 |
state=cmd_runner_fmt.as_map(_state_map), |
| 90 |
name=cmd_runner_fmt.as_list(), |
| 91 |
resource_type=cmd_runner_fmt.as_func(fmt_resource_type), |
| 92 |
resource_option=cmd_runner_fmt.as_list(), |
| 93 |
resource_operation=cmd_runner_fmt.as_func(fmt_resource_operation), |
| 94 |
resource_meta=cmd_runner_fmt.stack(cmd_runner_fmt.as_opt_val)("meta"), |
| 95 |
resource_argument=cmd_runner_fmt.as_func(fmt_resource_argument), |
| 96 |
resource_clone_ids=cmd_runner_fmt.as_list(), |
| 97 |
resource_clone_meta=cmd_runner_fmt.as_list(), |
| 98 |
apply_all=cmd_runner_fmt.as_bool("--all"), |
| 99 |
agent_validation=cmd_runner_fmt.as_bool("--agent-validation"), |
| 100 |
wait=cmd_runner_fmt.as_opt_eq_val("--wait"), |
| 101 |
config=cmd_runner_fmt.as_fixed("config"), |
| 102 |
force=cmd_runner_fmt.as_bool("--force"), |
| 103 |
version=cmd_runner_fmt.as_fixed("--version"), |
| 104 |
output_format=cmd_runner_fmt.as_opt_eq_val("--output-format"), |
| 105 |
), |
| 106 |
**kwargs, |
| 107 |
) |
| 108 |
return runner |