1    	import base64
2    	import getpass
3    	import json
4    	import logging
5    	import os
6    	import re
7    	import signal
8    	import subprocess
9    	import sys
10   	import tarfile
11   	import tempfile
12   	import threading
13   	import time
14   	import xml.dom.minidom
15   	from functools import lru_cache
16   	from io import BytesIO
17   	from textwrap import dedent
18   	from typing import Any, cast
19   	from urllib.parse import urlencode
20   	from xml.dom.minidom import Document as DomDocument
21   	from xml.dom.minidom import parseString
22   	
23   	import pcs.lib.corosync.config_parser as corosync_conf_parser
24   	from pcs import settings, usage
25   	from pcs.cli.cluster_property.output import PropertyConfigurationFacade
26   	from pcs.cli.common import middleware
27   	from pcs.cli.common.env_cli import Env
28   	from pcs.cli.common.errors import CmdLineInputError
29   	from pcs.cli.common.lib_wrapper import Library
30   	from pcs.cli.common.parse_args import InputModifiers
31   	from pcs.cli.common.tools import print_to_stderr, timeout_to_seconds_legacy
32   	from pcs.cli.file import metadata as cli_file_metadata
33   	from pcs.cli.reports import ReportProcessorToConsole
34   	from pcs.cli.reports import output as reports_output
35   	from pcs.cli.reports.messages import report_item_msg_from_dto
36   	from pcs.common import const, file_type_codes
37   	from pcs.common import file as pcs_file
38   	from pcs.common import pacemaker as common_pacemaker
39   	from pcs.common import pcs_pycurl as pycurl
40   	from pcs.common.host import PcsKnownHost
41   	from pcs.common.pacemaker.resource.operations import (
42   	    OCF_CHECK_LEVEL_INSTANCE_ATTRIBUTE_NAME,
43   	)
44   	from pcs.common.reports import ReportItemList, ReportItemSeverity
45   	from pcs.common.reports.messages import CibUpgradeFailedToMinimalRequiredVersion
46   	from pcs.common.reports.utils import add_context_to_message
47   	from pcs.common.services.errors import ManageServiceError
48   	from pcs.common.services.interfaces import ServiceManagerInterface
49   	from pcs.common.str_tools import format_list
50   	from pcs.common.tools import Version, timeout_to_seconds
51   	from pcs.common.types import StringSequence
52   	from pcs.lib.corosync.config_facade import ConfigFacade as corosync_conf_facade
53   	from pcs.lib.env import LibraryEnvironment
54   	from pcs.lib.errors import LibraryError
55   	from pcs.lib.external import CommandRunner, is_proxy_set
56   	from pcs.lib.file.instance import FileInstance as LibFileInstance
57   	from pcs.lib.host.config.facade import Facade as KnownHostsFacade
58   	from pcs.lib.interface.config import ParserErrorException
59   	from pcs.lib.pacemaker.values import validate_id
60   	from pcs.lib.services import get_service_manager as _get_service_manager
61   	from pcs.lib.services import service_exception_to_report
62   	
63   	# usefile & filename variables are set in pcs module
64   	usefile = False
65   	filename = ""
66   	# Note: not properly typed
67   	pcs_options: dict[Any, Any] = {}
68   	
69   	
70   	def _getValidateWithVersion(dom) -> Version:
71   	    """
72   	    Commandline options: no options
73   	    """
74   	    cib = dom.getElementsByTagName("cib")
75   	    if len(cib) != 1:
76   	        err("Bad cib")
77   	
78   	    cib = cib[0]
79   	
80   	    version = cib.getAttribute("validate-with")
81   	    r = re.compile(r"pacemaker-(\d+)\.(\d+)\.?(\d+)?")
82   	    m = r.match(version)
83   	    if m is None:
84   	        raise AssertionError()
85   	    major = int(m.group(1))
86   	    minor = int(m.group(2))
87   	    rev = int(m.group(3) or 0)
88   	    return Version(major, minor, rev)
89   	
90   	
91   	def isCibVersionSatisfied(cib_dom, required_version: Version) -> bool:
92   	    if not isinstance(cib_dom, DomDocument):
93   	        cib_dom = cib_dom.ownerDocument
94   	    return _getValidateWithVersion(cib_dom) >= required_version
95   	
96   	
97   	# Check the current pacemaker version in cib and upgrade it if necessary
98   	# Returns False if not upgraded and True if upgraded
99   	def _checkAndUpgradeCIB(required_version: Version) -> bool:
100  	    """
101  	    Commandline options:
102  	      * -f - CIB file
103  	    """
104  	    if isCibVersionSatisfied(get_cib_dom(), required_version):
105  	        return False
106  	    cluster_upgrade()
107  	    return True
108  	
109  	
110  	def cluster_upgrade():
111  	    """
112  	    Commandline options:
113  	      * -f - CIB file
114  	    """
115  	    output, retval = run(["cibadmin", "--upgrade", "--force"])
116  	    if retval != 0:
117  	        err("unable to upgrade cluster: %s" % output)
118  	    if (
119  	        output.strip()
120  	        == "Upgrade unnecessary: Schema is already the latest available"
121  	    ):
122  	        return
123  	    print_to_stderr("Cluster CIB has been upgraded to latest version")
124  	
125  	
126  	def cluster_upgrade_to_version(required_version: Version) -> Any:
127  	    """
128  	    Commandline options:
129  	      * -f - CIB file
130  	    """
131  	    _checkAndUpgradeCIB(required_version)
132  	    dom = get_cib_dom()
133  	    current_version = _getValidateWithVersion(dom)
134  	    if current_version < required_version:
135  	        err(
136  	            CibUpgradeFailedToMinimalRequiredVersion(
137  	                str(current_version),
138  	                str(required_version),
139  	            ).message
140  	        )
141  	    return dom
142  	
143  	
144  	# Check status of node
145  	def checkStatus(node):
146  	    """
147  	    Commandline options:
148  	      * --request-timeout - timeout for HTTP requests
149  	    """
150  	    return sendHTTPRequest(
151  	        node, "remote/status", urlencode({"version": "2"}), False, False
152  	    )
153  	
154  	
155  	# Check and see if we're authorized (faster than a status check)
156  	def checkAuthorization(node):
157  	    """
158  	    Commandline options:
159  	      * --request-timeout - timeout for HTTP requests
160  	    """
161  	    return sendHTTPRequest(node, "remote/check_auth", None, False, False)
162  	
163  	
164  	def get_uid_gid_file_name(uid, gid):
165  	    """
166  	    Commandline options: no options
167  	    """
168  	    return "pcs-uidgid-%s-%s" % (uid, gid)
169  	
170  	
171  	# Reads in uid file and returns dict of values {'uid':'theuid', 'gid':'thegid'}
172  	def read_uid_gid_file(uidgid_filename):
173  	    """
174  	    Commandline options: no options
175  	    """
176  	    uidgid = {}
177  	    with open(
178  	        os.path.join(settings.corosync_uidgid_dir, uidgid_filename), "r"
179  	    ) as myfile:
180  	        data = myfile.read().split("\n")
181  	    in_uidgid = False
182  	    for data_line in data:
183  	        line = re.sub(r"#.*", "", data_line)
184  	        if not in_uidgid:
185  	            if re.search(r"uidgid.*{", line):
186  	                in_uidgid = True
187  	            else:
188  	                continue
189  	        matches = re.search(r"uid:\s*(\S+)", line)
190  	        if matches:
191  	            uidgid["uid"] = matches.group(1)
192  	
193  	        matches = re.search(r"gid:\s*(\S+)", line)
194  	        if matches:
195  	            uidgid["gid"] = matches.group(1)
196  	
197  	    return uidgid
198  	
199  	
200  	def get_uidgid_file_content(
201  	    uid: str | None = None, gid: str | None = None
202  	) -> str | None:
203  	    if not uid and not gid:
204  	        return None
205  	    uid_gid_lines = []
206  	    if uid:
207  	        uid_gid_lines.append(f"  uid: {uid}")
208  	    if gid:
209  	        uid_gid_lines.append(f"  gid: {gid}")
210  	    return dedent(
211  	        """\
212  	        uidgid {{
213  	        {uid_gid_keys}
214  	        }}
215  	        """
216  	    ).format(uid_gid_keys="\n".join(uid_gid_lines))
217  	
218  	
219  	def write_uid_gid_file(uid, gid):
220  	    """
221  	    Commandline options: no options
222  	    """
223  	    orig_filename = get_uid_gid_file_name(uid, gid)
224  	    uidgid_filename = orig_filename
225  	    counter = 0
226  	    if find_uid_gid_files(uid, gid):
227  	        err("uidgid file with uid=%s and gid=%s already exists" % (uid, gid))
228  	
229  	    while os.path.exists(
230  	        os.path.join(settings.corosync_uidgid_dir, uidgid_filename)
231  	    ):
232  	        counter = counter + 1
233  	        uidgid_filename = orig_filename + "-" + str(counter)
234  	
235  	    data = get_uidgid_file_content(uid, gid)
236  	    if data:
237  	        with open(
238  	            os.path.join(settings.corosync_uidgid_dir, uidgid_filename), "w"
239  	        ) as uidgid_file:
240  	            uidgid_file.write(data)
241  	
242  	
243  	def find_uid_gid_files(uid, gid):
244  	    """
245  	    Commandline options: no options
246  	    """
247  	    if uid == "" and gid == "":
248  	        return []
249  	
250  	    found_files = []
251  	    uid_gid_files = os.listdir(settings.corosync_uidgid_dir)
252  	    for uidgid_file in uid_gid_files:
253  	        uid_gid_dict = read_uid_gid_file(uidgid_file)
254  	        if ("uid" in uid_gid_dict and uid == "") or (
255  	            "uid" not in uid_gid_dict and uid != ""
256  	        ):
257  	            continue
258  	        if ("gid" in uid_gid_dict and gid == "") or (
259  	            "gid" not in uid_gid_dict and gid != ""
260  	        ):
261  	            continue
262  	        if "uid" in uid_gid_dict and uid != uid_gid_dict["uid"]:
263  	            continue
264  	        if "gid" in uid_gid_dict and gid != uid_gid_dict["gid"]:
265  	            continue
266  	
267  	        found_files.append(uidgid_file)
268  	
269  	    return found_files
270  	
271  	
272  	# Removes all uid/gid files with the specified uid/gid, returns false if we
273  	# couldn't find one
274  	def remove_uid_gid_file(uid, gid):
275  	    """
276  	    Commandline options: no options
277  	    """
278  	    if uid == "" and gid == "":
279  	        return False
280  	
281  	    file_removed = False
282  	    for uidgid_file in find_uid_gid_files(uid, gid):
283  	        os.remove(os.path.join(settings.corosync_uidgid_dir, uidgid_file))
284  	        file_removed = True
285  	
286  	    return file_removed
287  	
288  	
289  	# DEPRECATED
290  	# This function has been replaced in all pcs code and has been kept solely for
291  	# the purpose of read_known_hosts_file_not_cached function in this module. Once
292  	# that function is cleaned up, this function should be removed.
293  	def _process_library_reports(
294  	    report_item_list: ReportItemList,
295  	    exit_on_error: bool = True,
296  	    include_debug: bool = True,
297  	) -> None:
298  	    if not report_item_list:
299  	        raise reports_output.error(
300  	            "Errors have occurred, therefore pcs is unable to continue"
301  	        )
302  	
303  	    critical_error = False
304  	    for report_item in report_item_list:
305  	        report_dto = report_item.to_dto()
306  	        severity = report_dto.severity.level
307  	
308  	        if severity == ReportItemSeverity.DEBUG and not include_debug:
309  	            continue
310  	
311  	        cli_report_msg = report_item_msg_from_dto(report_dto.message)
312  	        msg = add_context_to_message(cli_report_msg.message, report_dto.context)
313  	
314  	        if severity == ReportItemSeverity.WARNING:
315  	            reports_output.warn(msg)
316  	            continue
317  	
318  	        if severity == ReportItemSeverity.DEPRECATION:
319  	            reports_output.deprecation_warning(msg)
320  	            continue
321  	
322  	        if severity != ReportItemSeverity.ERROR:
323  	            reports_output.print_to_stderr(msg)
324  	            continue
325  	
326  	        reports_output.error(
327  	            add_context_to_message(
328  	                cli_report_msg.get_message_with_force_text(
329  	                    report_item.severity.force_code
330  	                ),
331  	                report_dto.context,
332  	            )
333  	        )
334  	        critical_error = True
335  	
336  	    if critical_error and exit_on_error:
337  	        sys.exit(1)
338  	
339  	
340  	@lru_cache
341  	def read_known_hosts_file() -> dict[str, PcsKnownHost]:
342  	    return read_known_hosts_file_not_cached()
343  	
344  	
345  	# TODO
346  	# This is the last function where _process_library_reports was not replaced
347  	# with pcs.cli.reports.processor.report_and_exit_on_error. The problem is, that
348  	# this function is meant to be used in CLI layer only, and despite that it is
349  	# being used in pcs.daemon.async_tasks.worker.executor. Replacing
350  	# _process_library_reports with report_and_exit_on_error cannot be done, as
351  	# they both use different report processors and that would break the daemon.
352  	# Steps:
353  	# 1. Write known hosts file getter designed specifically for the daemon context.
354  	#    This should have been done in the first place.
355  	# 2. Replace _process_library_reports with report_and_exit_on_error.
356  	# 3. Drop _process_library_reports.
357  	def read_known_hosts_file_not_cached() -> dict[str, PcsKnownHost]:
358  	    """
359  	    Commandline options: no options
360  	    """
361  	    try:
362  	        if os.getuid() != 0:
363  	            known_hosts_raw_file = pcs_file.RawFile(
364  	                cli_file_metadata.for_file_type(file_type_codes.PCS_KNOWN_HOSTS)
365  	            )
366  	            # json.loads handles bytes, it expects utf-8, 16 or 32 encoding
367  	            known_hosts_struct = json.loads(known_hosts_raw_file.read())
368  	            # TODO use known hosts facade for getting info from json struct once the
369  	            # facade exists
370  	            return {
371  	                name: PcsKnownHost.from_known_host_file_dict(name, host)
372  	                for name, host in known_hosts_struct["known_hosts"].items()
373  	            }
374  	        # TODO remove
375  	        # This is here to provide known-hosts to functions not yet
376  	        # overhauled to pcs.lib. Cli should never read known hosts from
377  	        # /var/lib/pcsd/.
378  	        known_hosts_instance = LibFileInstance.for_known_hosts()
379  	        known_hosts_facade = cast(
380  	            KnownHostsFacade, known_hosts_instance.read_to_facade()
381  	        )
382  	        return known_hosts_facade.known_hosts
383  	
384  	    except LibraryError as e:
385  	        # TODO remove
386  	        # This is here to provide known-hosts to functions not yet
387  	        # overhauled to pcs.lib. Cli should never read known hosts from
388  	        # /var/lib/pcsd/.
389  	        _process_library_reports(list(e.args))
390  	    except ParserErrorException as e:
391  	        # TODO remove
392  	        # This is here to provide known-hosts to functions not yet
393  	        # overhauled to pcs.lib. Cli should never read known hosts from
394  	        # /var/lib/pcsd/.
395  	        _process_library_reports(
396  	            known_hosts_instance.parser_exception_to_report_list(e)
397  	        )
398  	    except pcs_file.RawFileError as e:
399  	        reports_output.warn("Unable to read the known-hosts file: " + e.reason)
400  	    except json.JSONDecodeError as e:
401  	        reports_output.warn(f"Unable to parse the known-hosts file: {e}")
402  	    except (TypeError, KeyError):
403  	        reports_output.warn("Warning: Unable to parse the known-hosts file.")
404  	    return {}
405  	
406  	
407  	def repeat_if_timeout(send_http_request_function, repeat_count=15):
408  	    """
409  	    Commandline options: no options
410  	    NOTE: callback send_http_request_function may use --request-timeout
411  	    """
412  	
413  	    def repeater(node, *args, **kwargs):
414  	        repeats_left = repeat_count
415  	        while True:
416  	            retval, output = send_http_request_function(node, *args, **kwargs)
417  	            if (
418  	                retval != 2
419  	                or "Operation timed out" not in output
420  	                or repeats_left < 1
421  	            ):
422  	                # did not timed out OR repeat limit exceeded
423  	                return retval, output
424  	            repeats_left = repeats_left - 1
425  	            if "--debug" in pcs_options:
426  	                print_to_stderr(f"{node}: {output}, trying again...")
427  	
428  	    return repeater
429  	
430  	
431  	def setCorosyncConfig(node, config):
432  	    """
433  	    Commandline options:
434  	      * --request-timeout - timeout for HTTP requests
435  	    """
436  	    data = urlencode({"corosync_conf": config})
437  	    (status, data) = sendHTTPRequest(node, "remote/set_corosync_conf", data)
438  	    if status != 0:
439  	        err("Unable to set corosync config: {0}".format(data))
440  	
441  	
442  	def getPacemakerNodeStatus(node):
443  	    """
444  	    Commandline options:
445  	      * --request-timeout - timeout for HTTP requests
446  	    """
447  	    return sendHTTPRequest(
448  	        node, "remote/pacemaker_node_status", None, False, False
449  	    )
450  	
451  	
452  	def startCluster(node, quiet=False, timeout=None):
453  	    """
454  	    Commandline options:
455  	      * --request-timeout - timeout for HTTP requests
456  	    """
457  	    return sendHTTPRequest(
458  	        node,
459  	        "remote/cluster_start",
460  	        printResult=False,
461  	        printSuccess=not quiet,
462  	        timeout=timeout,
463  	    )
464  	
465  	
466  	def stopPacemaker(node, quiet=False, force=True):
467  	    """
468  	    Commandline options:
469  	      * --request-timeout - timeout for HTTP requests
470  	    """
471  	    return stopCluster(
472  	        node, pacemaker=True, corosync=False, quiet=quiet, force=force
473  	    )
474  	
475  	
476  	def stopCorosync(node, quiet=False, force=True):
477  	    """
478  	    Commandline options:
479  	      * --request-timeout - timeout for HTTP requests
480  	    """
481  	    return stopCluster(
482  	        node, pacemaker=False, corosync=True, quiet=quiet, force=force
483  	    )
484  	
485  	
486  	def stopCluster(node, quiet=False, pacemaker=True, corosync=True, force=True):
487  	    """
488  	    Commandline options:
489  	      * --request-timeout - timeout for HTTP requests
490  	    """
491  	    data = {}
492  	    timeout = None
493  	    if pacemaker and not corosync:
494  	        data["component"] = "pacemaker"
495  	        timeout = 2 * 60
496  	    elif corosync and not pacemaker:
497  	        data["component"] = "corosync"
498  	    if force:
499  	        data["force"] = 1
500  	    data = urlencode(data)
501  	    return sendHTTPRequest(
502  	        node,
503  	        "remote/cluster_stop",
504  	        data,
505  	        printResult=False,
506  	        printSuccess=not quiet,
507  	        timeout=timeout,
508  	    )
509  	
510  	
511  	def enableCluster(node):
512  	    """
513  	    Commandline options:
514  	      * --request-timeout - timeout for HTTP requests
515  	    """
516  	    return sendHTTPRequest(node, "remote/cluster_enable", None, False, True)
517  	
518  	
519  	def disableCluster(node):
520  	    """
521  	    Commandline options:
522  	      * --request-timeout - timeout for HTTP requests
523  	    """
524  	    return sendHTTPRequest(node, "remote/cluster_disable", None, False, True)
525  	
526  	
527  	def destroyCluster(node, quiet=False):
528  	    """
529  	    Commandline options:
530  	      * --request-timeout - timeout for HTTP requests
531  	    """
532  	    return sendHTTPRequest(
533  	        node, "remote/cluster_destroy", None, not quiet, not quiet
534  	    )
535  	
536  	
537  	def restoreConfig(node, tarball_data):
538  	    """
539  	    Commandline options:
540  	      * --request-timeout - timeout for HTTP requests
541  	    """
542  	    data = urlencode({"tarball": tarball_data})
543  	    return sendHTTPRequest(node, "remote/config_restore", data, False, True)
544  	
545  	
546  	def pauseConfigSyncing(node, delay_seconds=300):
547  	    """
548  	    Commandline options:
549  	      * --request-timeout - timeout for HTTP requests
550  	    """
551  	    data = urlencode({"sync_thread_pause": delay_seconds})
552  	    return sendHTTPRequest(node, "remote/set_sync_options", data, False, False)
553  	
554  	
555  	# Send an HTTP request to a node return a tuple with status, data
556  	# If status is 0 then data contains server response
557  	# Otherwise if non-zero then data contains error message
558  	# Returns a tuple (error, error message)
559  	# 0 = Success,
560  	# 1 = HTTP Error
561  	# 2 = No response,
562  	# 3 = Auth Error
563  	# 4 = Permission denied
564  	def sendHTTPRequest(  # noqa: PLR0912, PLR0915
565  	    host, request, data=None, printResult=True, printSuccess=True, timeout=None
566  	):
567  	    """
568  	    Commandline options:
569  	      * --request-timeout - timeout for HTTP requests
570  	      * --debug
571  	    """
572  	    port = None
573  	    addr = host
574  	    token = None
575  	    known_host = read_known_hosts_file().get(host, None)
576  	    # TODO: do not allow communication with unknown host
577  	    if known_host:
578  	        port = known_host.dest.port
579  	        addr = known_host.dest.addr
580  	        token = known_host.token
581  	    if port is None:
582  	        port = settings.pcsd_default_port
583  	    url = "https://{host}:{port}/{request}".format(
584  	        host="[{0}]".format(addr) if ":" in addr else addr,
585  	        request=request,
586  	        port=port,
587  	    )
588  	    if "--debug" in pcs_options:
589  	        print_to_stderr(f"Sending HTTP Request to: {url}\nData: {data}")
590  	
591  	    def __debug_callback(data_type, debug_data):
592  	        prefixes = {
593  	            pycurl.DEBUG_TEXT: b"* ",
594  	            pycurl.DEBUG_HEADER_IN: b"< ",
595  	            pycurl.DEBUG_HEADER_OUT: b"> ",
596  	            pycurl.DEBUG_DATA_IN: b"<< ",
597  	            pycurl.DEBUG_DATA_OUT: b">> ",
598  	        }
599  	        if data_type in prefixes:
600  	            debug_output.write(prefixes[data_type])
601  	            debug_output.write(debug_data)
602  	            if not debug_data.endswith(b"\n"):
603  	                debug_output.write(b"\n")
604  	
605  	    output = BytesIO()
606  	    debug_output = BytesIO()
607  	    cookies = __get_cookie_list(token)
608  	    if not timeout:
609  	        timeout = settings.default_request_timeout
610  	    timeout = pcs_options.get("--request-timeout", timeout)
611  	
612  	    handler = pycurl.Curl()
613  	    handler.setopt(pycurl.PROTOCOLS, pycurl.PROTO_HTTPS)
614  	    handler.setopt(pycurl.URL, url.encode("utf-8"))
615  	    handler.setopt(pycurl.WRITEFUNCTION, output.write)
616  	    handler.setopt(pycurl.VERBOSE, 1)
617  	    handler.setopt(pycurl.NOSIGNAL, 1)  # required for multi-threading
618  	    handler.setopt(pycurl.DEBUGFUNCTION, __debug_callback)
619  	    handler.setopt(pycurl.TIMEOUT_MS, int(timeout * 1000))
620  	    handler.setopt(pycurl.SSL_VERIFYHOST, 0)
621  	    handler.setopt(pycurl.SSL_VERIFYPEER, 0)
622  	    handler.setopt(pycurl.HTTPHEADER, ["Expect: "])
623  	    if cookies:
624  	        handler.setopt(pycurl.COOKIE, ";".join(cookies).encode("utf-8"))
625  	    if data:
626  	        handler.setopt(pycurl.COPYPOSTFIELDS, data.encode("utf-8"))
627  	    try:
628  	        handler.perform()
629  	        response_data = output.getvalue().decode("utf-8")
630  	        response_code = handler.getinfo(pycurl.RESPONSE_CODE)
631  	        if printResult or printSuccess:
632  	            print_to_stderr(host + ": " + response_data.strip())
633  	        if "--debug" in pcs_options:
634  	            print_to_stderr(
635  	                "Response Code: {response_code}\n"
636  	                "--Debug Response Start--\n"
637  	                "{response_data}\n"
638  	                "--Debug Response End--\n"
639  	                "Communication debug info for calling: {url}\n"
640  	                "--Debug Communication Output Start--\n"
641  	                "{debug_comm_output}\n"
642  	                "--Debug Communication Output End--".format(
643  	                    response_code=response_code,
644  	                    response_data=response_data,
645  	                    url=url,
646  	                    debug_comm_output=debug_output.getvalue().decode(
647  	                        "utf-8", "ignore"
648  	                    ),
649  	                )
650  	            )
651  	
652  	        if response_code == 401:
653  	            output = (
654  	                3,
655  	                (
656  	                    "Unable to authenticate to {node} - (HTTP error: {code}), "
657  	                    "try running 'pcs host auth {node}'"
658  	                ).format(node=host, code=response_code),
659  	            )
660  	        elif response_code == 403:
661  	            output = (
662  	                4,
663  	                "{node}: Permission denied - (HTTP error: {code})".format(
664  	                    node=host, code=response_code
665  	                ),
666  	            )
667  	        elif response_code >= 400:
668  	            output = (
669  	                1,
670  	                "Error connecting to {node} - (HTTP error: {code})".format(
671  	                    node=host, code=response_code
672  	                ),
673  	            )
674  	        else:
675  	            output = (0, response_data)
676  	
677  	        if printResult and output[0] != 0:
678  	            print_to_stderr(output[1])
679  	
680  	        return output
681  	    except pycurl.error as e:
682  	        if is_proxy_set(os.environ):
683  	            reports_output.warn(
684  	                "Proxy is set in environment variables, try disabling it"
685  	            )
686  	        dummy_errno, reason = e.args
687  	        if "--debug" in pcs_options:
688  	            print_to_stderr(f"Response Reason: {reason}")
689  	        msg = (
690  	            "Unable to connect to {host}, check if pcsd is running there or try "
691  	            "setting higher timeout with --request-timeout option ({reason})"
692  	        ).format(host=host, reason=reason)
693  	        if printResult:
694  	            print_to_stderr(msg)
695  	        return (2, msg)
696  	
697  	
698  	def __get_cookie_list(token):
699  	    """
700  	    Commandline options: no options
701  	    """
702  	    cookies = []
703  	    if token:
704  	        cookies.append("token=" + token)
705  	    if os.geteuid() == 0:
706  	        for name in ("CIB_user", "CIB_user_groups"):
707  	            if name in os.environ and os.environ[name].strip():
708  	                value = os.environ[name].strip()
709  	                # Let's be safe about characters in env variables and do base64.
710  	                # We cannot do it for CIB_user however to be backward compatible
711  	                # so we at least remove disallowed characters.
712  	                if name == "CIB_user":
713  	                    value = re.sub(r"[^!-~]", "", value).replace(";", "")
714  	                else:
715  	                    # python3 requires the value to be bytes not str
716  	                    value = base64.b64encode(value.encode("utf8")).decode(
717  	                        "utf-8"
718  	                    )
719  	                cookies.append("{0}={1}".format(name, value))
720  	    return cookies
721  	
722  	
723  	def get_corosync_conf_facade(conf_text=None):
724  	    """
725  	    Commandline options:
726  	      * --corosync_conf - path to a mocked corosync.conf is set directly to
727  	        settings
728  	    """
729  	    try:
730  	        return corosync_conf_facade(
731  	            corosync_conf_parser.Parser.parse(
732  	                (getCorosyncConf() if conf_text is None else conf_text).encode(
733  	                    "utf-8"
734  	                )
735  	            )
736  	        )
737  	    except corosync_conf_parser.CorosyncConfParserException as e:
738  	        return err("Unable to parse corosync.conf: %s" % e)
739  	
740  	
741  	def hasCorosyncConf():
742  	    """
743  	    Commandline options:
744  	      * --corosync_conf - path to a mocked corosync.conf is set directly to
745  	        settings
746  	    """
747  	    return os.path.isfile(settings.corosync_conf_file)
748  	
749  	
750  	def getCorosyncConf():
751  	    """
752  	    Commandline options:
753  	      * --corosync_conf - path to a mocked corosync.conf is set directly to
754  	        settings
755  	    """
756  	    corosync_conf_content = None
757  	    try:
758  	        with open(
759  	            settings.corosync_conf_file, "r", encoding="utf-8"
760  	        ) as corosync_conf_file:
761  	            corosync_conf_content = corosync_conf_file.read()
762  	    except OSError as e:
763  	        err("Unable to read %s: %s" % (settings.corosync_conf_file, e.strerror))
764  	    return corosync_conf_content
765  	
766  	
767  	def getCorosyncActiveNodes():
768  	    """
769  	    Commandline options: no options
770  	    """
771  	    output, retval = run(["corosync-cmapctl"])
772  	    if retval != 0:
773  	        return []
774  	
775  	    nodename_re = re.compile(r"^nodelist\.node\.(\d+)\.name .*= (.*)", re.M)
776  	    nodestatus_re = re.compile(
777  	        r"^runtime\.members\.(\d+).status .*= (.*)", re.M
778  	    )
779  	    nodenameid_mapping_re = re.compile(
780  	        r"nodelist\.node\.(\d+)\.nodeid .*= (\d+)", re.M
781  	    )
782  	
783  	    node_names = nodename_re.findall(output)
784  	
785  	    index_to_id = dict(nodenameid_mapping_re.findall(output))
786  	    id_to_status = dict(nodestatus_re.findall(output))
787  	
788  	    node_status = {}
789  	    for index, node_name in node_names:
790  	        if index in index_to_id:
791  	            nodeid = index_to_id[index]
792  	            if nodeid in id_to_status:
793  	                node_status[node_name] = id_to_status[nodeid]
794  	        else:
795  	            print_to_stderr(f"Error mapping {node_name}")
796  	
797  	    nodes_active = []
798  	    for node, status in node_status.items():
799  	        if status == "joined":
800  	            nodes_active.append(node)
801  	
802  	    return nodes_active
803  	
804  	
805  	# is it needed to handle corosync-qdevice service when managing cluster services
806  	def need_to_handle_qdevice_service():
807  	    """
808  	    Commandline options: no options
809  	      * --corosync_conf - path to a mocked corosync.conf is set directly to
810  	        settings but it doesn't make sense for contexts in which this function
811  	        is used
812  	    """
813  	    try:
814  	        with open(settings.corosync_conf_file, "rb") as corosync_conf_file:
815  	            return (
816  	                corosync_conf_facade(
817  	                    corosync_conf_parser.Parser.parse(corosync_conf_file.read())
818  	                ).get_quorum_device_model()
819  	                is not None
820  	            )
821  	    except (OSError, corosync_conf_parser.CorosyncConfParserException):
822  	        # corosync.conf not present or not valid => no qdevice specified
823  	        return False
824  	
825  	
826  	# Restore default behavior before starting subprocesses
827  	def subprocess_setup():
828  	    signal.signal(signal.SIGPIPE, signal.SIG_DFL)
829  	
830  	
831  	def touch_cib_file(cib_filename):
832  	    if not os.path.isfile(cib_filename):
833  	        try:
834  	            write_empty_cib(cib_filename)
835  	        except OSError as e:
836  	            err(
837  	                "Unable to write to file: '{0}': '{1}'".format(
838  	                    cib_filename, str(e)
839  	                )
840  	            )
841  	
842  	
843  	# Run command, with environment and return (output, retval)
844  	# DEPRECATED, please use lib.external.CommandRunner via utils.cmd_runner()
845  	def run(
846  	    args,
847  	    ignore_stderr=False,
848  	    string_for_stdin=None,
849  	    env_extend=None,
850  	    binary_output=False,
851  	):
852  	    """
853  	    Commandline options:
854  	      * -f - CIB file (effective only for some pacemaker tools)
855  	      * --debug
856  	    """
857  	    if not env_extend:
858  	        env_extend = {}
859  	    env_var = env_extend
860  	    env_var.update(dict(os.environ))
861  	    env_var["LC_ALL"] = "C"
862  	    if usefile:
863  	        env_var["CIB_file"] = filename
864  	        touch_cib_file(filename)
865  	
866  	    command = args[0]
867  	    if command[0:3] == "crm" or command in [
868  	        "cibadmin",
869  	        "stonith_admin",
870  	    ]:
871  	        args[0] = os.path.join(settings.pacemaker_execs, command)
872  	    elif command[0:8] == "corosync":
873  	        args[0] = os.path.join(settings.corosync_execs, command)
874  	
875  	    try:
876  	        if "--debug" in pcs_options:
877  	            print_to_stderr("Running: " + " ".join(args))
878  	            if string_for_stdin:
879  	                print_to_stderr(
880  	                    f"--Debug Input Start--\n"
881  	                    f"{string_for_stdin}\n"
882  	                    f"--Debug Input End--"
883  	                )
884  	
885  	        # Some commands react differently if you give them anything via stdin
886  	        if string_for_stdin is not None:
887  	            stdin_pipe = subprocess.PIPE
888  	        else:
889  	            stdin_pipe = subprocess.DEVNULL
890  	
891  	        p = subprocess.Popen(
892  	            args,
893  	            stdin=stdin_pipe,
894  	            stdout=subprocess.PIPE,
895  	            stderr=(subprocess.PIPE if ignore_stderr else subprocess.STDOUT),
896  	            preexec_fn=subprocess_setup,  # noqa: PLW1509
897  	            close_fds=True,
898  	            env=env_var,
899  	            # decodes newlines and in python3 also converts bytes to str
900  	            universal_newlines=(not binary_output),
901  	        )
902  	        output, dummy_stderror = p.communicate(string_for_stdin)
903  	        retval = p.returncode
904  	        if "--debug" in pcs_options:
905  	            print_to_stderr(
906  	                "Return Value: {retval}\n"
907  	                "--Debug Output Start--\n"
908  	                "{debug_output}\n"
909  	                "--Debug Output End--".format(
910  	                    retval=retval,
911  	                    debug_output=output.rstrip(),
912  	                )
913  	            )
914  	    except OSError as e:
915  	        print_to_stderr(e.strerror)
916  	        err("unable to locate command: " + args[0])
917  	
918  	    return output, retval
919  	
920  	
921  	def cmd_runner(cib_file_override=None):
922  	    """
923  	    Commandline options:
924  	      * -f - CIB file
925  	    """
926  	    env_vars = {}
927  	    if usefile:
928  	        env_vars["CIB_file"] = filename
929  	    if cib_file_override:
930  	        env_vars["CIB_file"] = cib_file_override
931  	    env_vars.update(os.environ)
932  	    env_vars["LC_ALL"] = "C"
933  	    return CommandRunner(
934  	        logging.getLogger("pcs"), get_report_processor(), env_vars
935  	    )
936  	
937  	
938  	def run_pcsdcli(command, data=None):
939  	    """
940  	    Commandline options:
941  	      * --request-timeout - timeout for HTTP request, applicable for commands:
942  	        * remove_known_hosts - only when running on cluster node (sync will
943  	            be initiated)
944  	        * auth
945  	        * send_local_configs
946  	    """
947  	    if not data:
948  	        data = {}
949  	    env_var = {}
950  	    if "--debug" in pcs_options:
951  	        env_var["PCSD_DEBUG"] = "true"
952  	    if "--request-timeout" in pcs_options:
953  	        env_var["PCSD_NETWORK_TIMEOUT"] = str(pcs_options["--request-timeout"])
954  	    else:
955  	        env_var["PCSD_NETWORK_TIMEOUT"] = str(settings.default_request_timeout)
956  	    pcsd_dir_path = settings.pcsd_exec_location
957  	    pcsdcli_path = os.path.join(pcsd_dir_path, "pcsd-cli.rb")
958  	    if settings.pcsd_gem_path is not None:
959  	        env_var["GEM_HOME"] = settings.pcsd_gem_path
960  	    stdout, dummy_stderr, retval = cmd_runner().run(
961  	        [settings.ruby_exec, "-I" + pcsd_dir_path, pcsdcli_path, command],
962  	        json.dumps(data),
963  	        env_var,
964  	    )
965  	    try:
966  	        output_json = json.loads(stdout)
967  	        for key in ["status", "text", "data"]:
968  	            if key not in output_json:
969  	                output_json[key] = None
970  	
971  	        output = "".join(output_json["log"])
972  	        # check if some requests timed out, if so print message about it
973  	        if "error: operation_timedout" in output:
974  	            print_to_stderr("Error: Operation timed out")
975  	        # check if there are any connection failures due to proxy in pcsd and
976  	        # print warning if so
977  	        proxy_msg = "Proxy is set in environment variables, try disabling it"
978  	        if proxy_msg in output:
979  	            reports_output.warn(proxy_msg)
980  	
981  	    except ValueError:
982  	        output_json = {
983  	            "status": "bad_json_output",
984  	            "text": stdout,
985  	            "data": None,
986  	        }
987  	    return output_json, retval
988  	
989  	
990  	def call_local_pcsd(argv, options, std_in=None):  # noqa: PLR0911
991  	    """
992  	    Commandline options:
993  	      * --request-timeout - timeout of call to local pcsd
994  	    """
995  	    # some commands cannot be run under a non-root account
996  	    # so we pass those commands to locally running pcsd to execute them
997  	    # returns [list_of_errors, exit_code, stdout, stderr]
998  	    data = {
999  	        "command": json.dumps(argv),
1000 	        "options": json.dumps(options),
1001 	    }
1002 	    if std_in:
1003 	        data["stdin"] = std_in
1004 	    data_send = urlencode(data)
1005 	    code, output = sendHTTPRequest(
1006 	        "localhost", "run_pcs", data_send, False, False
1007 	    )
1008 	
1009 	    if code == 3:  # not authenticated
1010 	        return [
1011 	            [
1012 	                "Unable to authenticate against the local pcsd. Run the same "
1013 	                "command as root or authenticate yourself to the local pcsd "
1014 	                "using command 'pcs client local-auth'"
1015 	            ],
1016 	            1,
1017 	            "",
1018 	            "",
1019 	        ]
1020 	    if code != 0:  # http error connecting to localhost
1021 	        return [[output], 1, "", ""]
1022 	
1023 	    try:
1024 	        output_json = json.loads(output)
1025 	        for key in ["status", "data"]:
1026 	            if key not in output_json:
1027 	                output_json[key] = None
1028 	    except ValueError:
1029 	        return [["Unable to communicate with pcsd"], 1, "", ""]
1030 	    if output_json["status"] == "bad_command":
1031 	        return [["Command not allowed"], 1, "", ""]
1032 	    if output_json["status"] == "access_denied":
1033 	        return [["Access denied"], 1, "", ""]
1034 	    if output_json["status"] != "ok" or not output_json["data"]:
1035 	        return [["Unable to communicate with pcsd"], 1, "", ""]
1036 	    try:
1037 	        exitcode = output_json["data"]["code"]
1038 	        std_out = output_json["data"]["stdout"]
1039 	        std_err = output_json["data"]["stderr"]
1040 	        return [[], exitcode, std_out, std_err]
1041 	    except KeyError:
1042 	        return [["Unable to communicate with pcsd"], 1, "", ""]
1043 	
1044 	
1045 	def map_for_error_list(callab, iterab):
1046 	    """
1047 	    Commandline options: no options
1048 	    NOTE: callback 'callab' may use some options
1049 	    """
1050 	    error_list = []
1051 	    for item in iterab:
1052 	        retval, error = callab(item)
1053 	        if retval != 0:
1054 	            error_list.append(error)
1055 	    return error_list
1056 	
1057 	
1058 	def run_parallel(worker_list, wait_seconds=1):
1059 	    """
1060 	    Commandline options: no options
1061 	    """
1062 	    thread_list = set()
1063 	    for worker in worker_list:
1064 	        thread = threading.Thread(target=worker)
1065 	        thread.daemon = True
1066 	        thread.start()
1067 	        thread_list.add(thread)
1068 	
1069 	    while thread_list:
1070 	        thread = thread_list.pop()
1071 	        thread.join(wait_seconds)
1072 	        if thread.is_alive():
1073 	            thread_list.add(thread)
1074 	
1075 	
1076 	def create_task(report, action, node, *args, **kwargs):
1077 	    """
1078 	    Commandline options: no options
1079 	    """
1080 	
1081 	    def worker():
1082 	        returncode, output = action(node, *args, **kwargs)
1083 	        report(node, returncode, output)
1084 	
1085 	    return worker
1086 	
1087 	
1088 	def create_task_list(report, action, node_list, *args, **kwargs):
1089 	    """
1090 	    Commandline options: no options
1091 	    """
1092 	    return [
1093 	        create_task(report, action, node, *args, **kwargs) for node in node_list
1094 	    ]
1095 	
1096 	
1097 	def parallel_for_nodes(action, node_list, *args, **kwargs):
1098 	    """
1099 	    Commandline options: no options
1100 	    NOTE: callback 'action' may use some cmd options
1101 	    """
1102 	    node_errors = {}
1103 	
1104 	    def report(node, returncode, output):
1105 	        message = "{0}: {1}".format(node, output.strip())
1106 	        print_to_stderr(message)
1107 	        if returncode != 0:
1108 	            node_errors[node] = message
1109 	
1110 	    run_parallel(create_task_list(report, action, node_list, *args, **kwargs))
1111 	    return node_errors
1112 	
1113 	
1114 	def get_group_children(group_id):
1115 	    """
1116 	    Commandline options: no options
1117 	    """
1118 	    return dom_get_group_children(get_cib_dom(), group_id)
1119 	
1120 	
1121 	def dom_get_group_children(dom, group_id):
1122 	    groups = dom.getElementsByTagName("group")
1123 	    for g in groups:
1124 	        if g.getAttribute("id") == group_id:
1125 	            return [
1126 	                child_el.getAttribute("id")
1127 	                for child_el in get_group_children_el_from_el(g)
1128 	            ]
1129 	    return []
1130 	
1131 	
1132 	def get_group_children_el_from_el(group_el):
1133 	    child_resources = []
1134 	    for child in group_el.childNodes:
1135 	        if child.nodeType != xml.dom.minidom.Node.ELEMENT_NODE:
1136 	            continue
1137 	        if child.tagName == "primitive":
1138 	            child_resources.append(child)
1139 	    return child_resources
1140 	
1141 	
1142 	def dom_get_clone_ms_resource(dom, clone_ms_id):
1143 	    """
1144 	    Commandline options: no options
1145 	    """
1146 	    clone_ms = dom_get_clone(dom, clone_ms_id) or dom_get_master(
1147 	        dom, clone_ms_id
1148 	    )
1149 	    if clone_ms:
1150 	        return dom_elem_get_clone_ms_resource(clone_ms)
1151 	    return None
1152 	
1153 	
1154 	def dom_elem_get_clone_ms_resource(clone_ms):
1155 	    """
1156 	    Commandline options: no options
1157 	    """
1158 	    for child in clone_ms.childNodes:
1159 	        if (
1160 	            child.nodeType == xml.dom.minidom.Node.ELEMENT_NODE
1161 	            and child.tagName in ["group", "primitive"]
1162 	        ):
1163 	            return child
1164 	    return None
1165 	
1166 	
1167 	def dom_get_resource_clone_ms_parent(dom, resource_id):
1168 	    """
1169 	    Commandline options: no options
1170 	    """
1171 	    resource = dom_get_resource(dom, resource_id) or dom_get_group(
1172 	        dom, resource_id
1173 	    )
1174 	    if resource:
1175 	        return dom_get_parent_by_tag_names(resource, ["clone", "master"])
1176 	    return None
1177 	
1178 	
1179 	def dom_get_resource_bundle_parent(dom, resource_id):
1180 	    """
1181 	    Commandline options: no options
1182 	    """
1183 	    resource = dom_get_resource(dom, resource_id)
1184 	    if resource:
1185 	        return dom_get_parent_by_tag_names(resource, ["bundle"])
1186 	    return None
1187 	
1188 	
1189 	def dom_get_master(dom, master_id):
1190 	    """
1191 	    Commandline options: no options
1192 	    """
1193 	    for master in dom.getElementsByTagName("master"):
1194 	        if master.getAttribute("id") == master_id:
1195 	            return master
1196 	    return None
1197 	
1198 	
1199 	def dom_get_clone(dom, clone_id):
1200 	    """
1201 	    Commandline options: no options
1202 	    """
1203 	    for clone in dom.getElementsByTagName("clone"):
1204 	        if clone.getAttribute("id") == clone_id:
1205 	            return clone
1206 	    return None
1207 	
1208 	
1209 	def dom_get_group(dom, group_id):
1210 	    """
1211 	    Commandline options: no options
1212 	    """
1213 	    for group in dom.getElementsByTagName("group"):
1214 	        if group.getAttribute("id") == group_id:
1215 	            return group
1216 	    return None
1217 	
1218 	
1219 	def dom_get_bundle(dom, bundle_id):
1220 	    """
1221 	    Commandline options: no options
1222 	    """
1223 	    for bundle in dom.getElementsByTagName("bundle"):
1224 	        if bundle.getAttribute("id") == bundle_id:
1225 	            return bundle
1226 	    return None
1227 	
1228 	
1229 	def dom_get_resource_bundle(bundle_el):
1230 	    """
1231 	    Commandline options: no options
1232 	    """
1233 	    for child in bundle_el.childNodes:
1234 	        if (
1235 	            child.nodeType == xml.dom.minidom.Node.ELEMENT_NODE
1236 	            and child.tagName == "primitive"
1237 	        ):
1238 	            return child
1239 	    return None
1240 	
1241 	
1242 	def dom_get_group_clone(dom, group_id):
1243 	    """
1244 	    Commandline options: no options
1245 	    """
1246 	    for clone in dom.getElementsByTagName("clone"):
1247 	        group = dom_get_group(clone, group_id)
1248 	        if group:
1249 	            return group
1250 	    return None
1251 	
1252 	
1253 	def dom_get_group_masterslave(dom, group_id):
1254 	    """
1255 	    Commandline options: no options
1256 	    """
1257 	    for master in dom.getElementsByTagName("master"):
1258 	        group = dom_get_group(master, group_id)
1259 	        if group:
1260 	            return group
1261 	    return None
1262 	
1263 	
1264 	def dom_get_resource(dom, resource_id):
1265 	    """
1266 	    Commandline options: no options
1267 	    """
1268 	    for primitive in dom.getElementsByTagName("primitive"):
1269 	        if primitive.getAttribute("id") == resource_id:
1270 	            return primitive
1271 	    return None
1272 	
1273 	
1274 	def dom_get_any_resource(dom, resource_id):
1275 	    """
1276 	    Commandline options: no options
1277 	    """
1278 	    return (
1279 	        dom_get_resource(dom, resource_id)
1280 	        or dom_get_group(dom, resource_id)
1281 	        or dom_get_clone(dom, resource_id)
1282 	        or dom_get_master(dom, resource_id)
1283 	    )
1284 	
1285 	
1286 	def dom_get_resource_clone(dom, resource_id):
1287 	    """
1288 	    Commandline options: no options
1289 	    """
1290 	    for clone in dom.getElementsByTagName("clone"):
1291 	        resource = dom_get_resource(clone, resource_id)
1292 	        if resource:
1293 	            return resource
1294 	    return None
1295 	
1296 	
1297 	def dom_get_resource_masterslave(dom, resource_id):
1298 	    """
1299 	    Commandline options: no options
1300 	    """
1301 	    for master in dom.getElementsByTagName("master"):
1302 	        resource = dom_get_resource(master, resource_id)
1303 	        if resource:
1304 	            return resource
1305 	    return None
1306 	
1307 	
1308 	# returns tuple (is_valid, error_message, correct_resource_id_if_exists)
1309 	# there is a duplicate code in pcs/lib/cib/constraint/constraint.py
1310 	# please use function in pcs/lib/cib/constraint/constraint.py
1311 	def validate_constraint_resource(dom, resource_id):  # noqa: PLR0911
1312 	    """
1313 	    Commandline options:
1314 	      * --force - allow constraint on any resource
1315 	    """
1316 	    resource_el = (
1317 	        dom_get_clone(dom, resource_id)
1318 	        or dom_get_master(dom, resource_id)
1319 	        or dom_get_bundle(dom, resource_id)
1320 	    )
1321 	    if resource_el:
1322 	        # clones, masters and bundles are always valid
1323 	        return True, "", resource_id
1324 	
1325 	    resource_el = dom_get_resource(dom, resource_id) or dom_get_group(
1326 	        dom, resource_id
1327 	    )
1328 	    if not resource_el:
1329 	        return False, "Resource '%s' does not exist" % resource_id, None
1330 	
1331 	    clone_el = dom_get_resource_clone_ms_parent(
1332 	        dom, resource_id
1333 	    ) or dom_get_resource_bundle_parent(dom, resource_id)
1334 	    if not clone_el:
1335 	        # a primitive and a group is valid if not in a clone nor a master nor a
1336 	        # bundle
1337 	        return True, "", resource_id
1338 	
1339 	    if "--force" in pcs_options:
1340 	        return True, "", clone_el.getAttribute("id")
1341 	
1342 	    if clone_el.tagName in ["clone", "master"]:
1343 	        return (
1344 	            False,
1345 	            "%s is a clone resource, you should use the clone id: %s "
1346 	            "when adding constraints. Use --force to override."
1347 	            % (resource_id, clone_el.getAttribute("id")),
1348 	            clone_el.getAttribute("id"),
1349 	        )
1350 	    if clone_el.tagName == "bundle":
1351 	        return (
1352 	            False,
1353 	            "%s is a bundle resource, you should use the bundle id: %s "
1354 	            "when adding constraints. Use --force to override."
1355 	            % (resource_id, clone_el.getAttribute("id")),
1356 	            clone_el.getAttribute("id"),
1357 	        )
1358 	    return True, "", resource_id
1359 	
1360 	
1361 	def validate_resources_not_in_same_group(dom, resource_id1, resource_id2):
1362 	    resource_el1 = dom_get_resource(dom, resource_id1)
1363 	    resource_el2 = dom_get_resource(dom, resource_id2)
1364 	    if not resource_el1 or not resource_el2:
1365 	        # Only primitive resources can be in a group. If at least one of the
1366 	        # resources is not a primitive (resource_el is None), then the
1367 	        # resources are not in the same group.
1368 	        return True
1369 	    group1 = dom_get_parent_by_tag_names(resource_el1, ["group"])
1370 	    group2 = dom_get_parent_by_tag_names(resource_el2, ["group"])
1371 	    if not group1 or not group2:
1372 	        return True
1373 	    return group1 != group2
1374 	
1375 	
1376 	def dom_get_resource_remote_node_name(dom_resource):
1377 	    """
1378 	    Commandline options: no options
1379 	    """
1380 	    if dom_resource.tagName != "primitive":
1381 	        return None
1382 	    if (
1383 	        dom_resource.getAttribute("class").lower() == "ocf"
1384 	        and dom_resource.getAttribute("provider").lower() == "pacemaker"
1385 	        and dom_resource.getAttribute("type").lower() == "remote"
1386 	    ):
1387 	        return dom_resource.getAttribute("id")
1388 	    return dom_get_meta_attr_value(dom_resource, "remote-node")
1389 	
1390 	
1391 	def dom_get_meta_attr_value(dom_resource, meta_name):
1392 	    """
1393 	    Commandline options: no options
1394 	    """
1395 	    for meta in dom_resource.getElementsByTagName("meta_attributes"):
1396 	        for nvpair in meta.getElementsByTagName("nvpair"):
1397 	            if nvpair.getAttribute("name") == meta_name:
1398 	                return nvpair.getAttribute("value")
1399 	    return None
1400 	
1401 	
1402 	def dom_get_node(dom, node_name):
1403 	    """
1404 	    Commandline options: no options
1405 	    """
1406 	    for e in dom.getElementsByTagName("node"):
1407 	        if e.hasAttribute("uname") and e.getAttribute("uname") == node_name:
1408 	            return e
1409 	    return None
1410 	
1411 	
1412 	def _dom_get_children_by_tag_name(dom_el, tag_name):
1413 	    """
1414 	    Commandline options: no options
1415 	    """
1416 	    return [
1417 	        node
1418 	        for node in dom_el.childNodes
1419 	        if node.nodeType == xml.dom.minidom.Node.ELEMENT_NODE
1420 	        and node.tagName == tag_name
1421 	    ]
1422 	
1423 	
1424 	def dom_get_parent_by_tag_names(dom_el, tag_names):
1425 	    """
1426 	    Commandline options: no options
1427 	    """
1428 	    parent = dom_el.parentNode
1429 	    while parent:
1430 	        if not isinstance(parent, xml.dom.minidom.Element):
1431 	            return None
1432 	        if parent.tagName in tag_names:
1433 	            return parent
1434 	        parent = parent.parentNode
1435 	    return None
1436 	
1437 	
1438 	# moved to pcs.lib.pacemaker.state
1439 	def get_resource_for_running_check(cluster_state, resource_id, stopped=False):
1440 	    """
1441 	    Commandline options: no options
1442 	    """
1443 	
1444 	    def _isnum(value):
1445 	        return all(char in list("0123456789") for char in value)
1446 	
1447 	    for clone in cluster_state.getElementsByTagName("clone"):
1448 	        if clone.getAttribute("id") == resource_id:
1449 	            for child in clone.childNodes:
1450 	                if child.nodeType == child.ELEMENT_NODE and child.tagName in [
1451 	                    "resource",
1452 	                    "group",
1453 	                ]:
1454 	                    resource_id = child.getAttribute("id")
1455 	                    # in a clone, a resource can have an id of '<name>:N'
1456 	                    if ":" in resource_id:
1457 	                        parts = resource_id.rsplit(":", 1)
1458 	                        if _isnum(parts[1]):
1459 	                            resource_id = parts[0]
1460 	                    break
1461 	    for group in cluster_state.getElementsByTagName("group"):
1462 	        # If resource is a clone it can have an id of '<resource name>:N'
1463 	        if group.getAttribute("id") == resource_id or group.getAttribute(
1464 	            "id"
1465 	        ).startswith(resource_id + ":"):
1466 	            if stopped:
1467 	                elem = group.getElementsByTagName("resource")[0]
1468 	            else:
1469 	                elem = group.getElementsByTagName("resource")[-1]
1470 	            resource_id = elem.getAttribute("id")
1471 	    return resource_id
1472 	
1473 	
1474 	# moved to pcs.lib.pacemaker.state
1475 	# see pcs.lib.commands.resource for usage
1476 	def resource_running_on(resource, passed_state=None, stopped=False):
1477 	    """
1478 	    Commandline options:
1479 	      * -f - has effect but doesn't make sense to check state of resource
1480 	    """
1481 	    nodes_started = []
1482 	    nodes_promoted = []
1483 	    nodes_unpromoted = []
1484 	    state = passed_state if passed_state else getClusterState()
1485 	    resource_original = resource
1486 	    resource = get_resource_for_running_check(state, resource, stopped)
1487 	    resources = state.getElementsByTagName("resource")
1488 	    for res in resources:
1489 	        # If resource is a clone it can have an id of '<resource name>:N'
1490 	        # If resource is a clone it will be found more than once - cannot break
1491 	        if (
1492 	            res.getAttribute("id") == resource
1493 	            or res.getAttribute("id").startswith(resource + ":")
1494 	        ) and res.getAttribute("failed") != "true":
1495 	            for node in res.getElementsByTagName("node"):
1496 	                node_name = node.getAttribute("name")
1497 	                role = res.getAttribute("role")
1498 	                if role == const.PCMK_ROLE_STARTED:
1499 	                    nodes_started.append(node_name)
1500 	                elif role in (
1501 	                    const.PCMK_ROLE_PROMOTED,
1502 	                    const.PCMK_ROLE_PROMOTED_LEGACY,
1503 	                ):
1504 	                    nodes_promoted.append(node_name)
1505 	                elif role in (
1506 	                    const.PCMK_ROLE_UNPROMOTED,
1507 	                    const.PCMK_ROLE_UNPROMOTED_LEGACY,
1508 	                ):
1509 	                    nodes_unpromoted.append(node_name)
1510 	    if not nodes_started and not nodes_promoted and not nodes_unpromoted:
1511 	        message = "Resource '%s' is not running on any node" % resource_original
1512 	    else:
1513 	        message_parts = []
1514 	        for alist, label in (
1515 	            (nodes_started, "running"),
1516 	            (nodes_promoted, str(const.PCMK_ROLE_PROMOTED).lower()),
1517 	            (nodes_unpromoted, str(const.PCMK_ROLE_UNPROMOTED).lower()),
1518 	        ):
1519 	            if alist:
1520 	                alist.sort()
1521 	                message_parts.append(
1522 	                    "%s on node%s %s"
1523 	                    % (label, "s" if len(alist) > 1 else "", ", ".join(alist))
1524 	                )
1525 	        message = "Resource '%s' is %s." % (
1526 	            resource_original,
1527 	            "; ".join(message_parts),
1528 	        )
1529 	    return {
1530 	        "message": message,
1531 	        "is_running": bool(nodes_started or nodes_promoted or nodes_unpromoted),
1532 	    }
1533 	
1534 	
1535 	def validate_wait_get_timeout(need_cib_support=True):
1536 	    """
1537 	    Commandline options:
1538 	      * --wait
1539 	      * -f - to check if -f and --wait are not used simultaneously
1540 	    """
1541 	    if need_cib_support and usefile:
1542 	        err("Cannot use '-f' together with '--wait'")
1543 	    wait_timeout = pcs_options["--wait"]
1544 	    if wait_timeout is None:
1545 	        return wait_timeout
1546 	    wait_timeout = timeout_to_seconds(wait_timeout)
1547 	    if wait_timeout is None:
1548 	        err(
1549 	            "%s is not a valid number of seconds to wait"
1550 	            % pcs_options["--wait"]
1551 	        )
1552 	    return wait_timeout
1553 	
1554 	
1555 	# Return matches from the CIB with the xpath_query
1556 	def get_cib_xpath(xpath_query):
1557 	    """
1558 	    Commandline options:
1559 	      * -f - CIB file
1560 	    """
1561 	    args = ["cibadmin", "-Q", "--xpath", xpath_query]
1562 	    output, retval = run(args)
1563 	    if retval != 0:
1564 	        return ""
1565 	    return output
1566 	
1567 	
1568 	def get_cib(scope=None):
1569 	    """
1570 	    Commandline options:
1571 	      * -f - CIB file
1572 	    """
1573 	    command = ["cibadmin", "-l", "-Q"]
1574 	    if scope:
1575 	        command.append("--scope=%s" % scope)
1576 	    output, retval = run(command)
1577 	    if retval != 0:
1578 	        if retval == 105 and scope:
1579 	            err("unable to get cib, scope '%s' not present in cib" % scope)
1580 	        else:
1581 	            err("unable to get cib")
1582 	    return output
1583 	
1584 	
1585 	def get_cib_dom(cib_xml=None):
1586 	    """
1587 	    Commandline options:
1588 	      * -f - CIB file
1589 	    """
1590 	    if cib_xml is None:
1591 	        cib_xml = get_cib()
1592 	    try:
1593 	        return parseString(cib_xml)
1594 	    except xml.parsers.expat.ExpatError:
1595 	        return err("unable to get cib")
1596 	
1597 	
1598 	# Replace only configuration section of cib with dom passed
1599 	def replace_cib_configuration(dom):
1600 	    """
1601 	    Commandline options:
1602 	      * -f - CIB file
1603 	    """
1604 	    new_dom = dom.toxml() if hasattr(dom, "toxml") else dom
1605 	    cmd = ["cibadmin", "--replace", "-V", "--xml-pipe", "-o", "configuration"]
1606 	    output, retval = run(cmd, False, new_dom)
1607 	    if retval != 0:
1608 	        err("Unable to update cib\n" + output)
1609 	
1610 	
1611 	def is_valid_cib_scope(scope):
1612 	    """
1613 	    Commandline options: no options
1614 	    """
1615 	    return scope in [
1616 	        "acls",
1617 	        "alerts",
1618 	        "configuration",
1619 	        "constraints",
1620 	        "crm_config",
1621 	        "fencing-topology",
1622 	        "nodes",
1623 	        "op_defaults",
1624 	        "resources",
1625 	        "rsc_defaults",
1626 	        "tags",
1627 	    ]
1628 	
1629 	
1630 	# Checks to see if id exists in the xml dom passed
1631 	# DEPRECATED use lxml version available in pcs.lib.cib.tools
1632 	def does_id_exist(dom, check_id):
1633 	    """
1634 	    Commandline options: no options
1635 	    """
1636 	    # do not search in /cib/status, it may contain references to previously
1637 	    # existing and deleted resources and thus preventing creating them again
1638 	    document = (
1639 	        dom if isinstance(dom, xml.dom.minidom.Document) else dom.ownerDocument
1640 	    )
1641 	    cib_found = False
1642 	    for cib in _dom_get_children_by_tag_name(document, "cib"):
1643 	        cib_found = True
1644 	        for section in cib.childNodes:
1645 	            if section.nodeType != xml.dom.minidom.Node.ELEMENT_NODE:
1646 	                continue
1647 	            if section.tagName == "status":
1648 	                continue
1649 	            for elem in section.getElementsByTagName("*"):
1650 	                if elem.getAttribute("id") == check_id:
1651 	                    return True
1652 	    if not cib_found:
1653 	        for elem in document.getElementsByTagName("*"):
1654 	            if elem.getAttribute("id") == check_id:
1655 	                return True
1656 	    return False
1657 	
1658 	
1659 	# Returns check_id if it doesn't exist in the dom, otherwise it adds an integer
1660 	# to the end of the id and increments it until a unique id is found
1661 	# DEPRECATED use lxml version available in pcs.lib.cib.tools
1662 	def find_unique_id(dom, check_id):
1663 	    """
1664 	    Commandline options: no options
1665 	    """
1666 	    counter = 1
1667 	    temp_id = check_id
1668 	    while does_id_exist(dom, temp_id):
1669 	        temp_id = check_id + "-" + str(counter)
1670 	        counter += 1
1671 	    return temp_id
1672 	
1673 	
1674 	# Checks to see if the specified operation already exists in passed set of
1675 	# operations
1676 	# pacemaker differentiates between operations only by name and interval
1677 	def operation_exists(operations_el, op_el):
1678 	    """
1679 	    Commandline options: no options
1680 	    """
1681 	    op_name = op_el.getAttribute("name")
1682 	    op_interval = timeout_to_seconds_legacy(op_el.getAttribute("interval"))
1683 	    return [
1684 	        op
1685 	        for op in operations_el.getElementsByTagName("op")
1686 	        if (
1687 	            op.getAttribute("name") == op_name
1688 	            and timeout_to_seconds_legacy(op.getAttribute("interval"))
1689 	            == op_interval
1690 	        )
1691 	    ]
1692 	
1693 	
1694 	def operation_exists_by_name(operations_el, op_el):
1695 	    """
1696 	    Commandline options: no options
1697 	    """
1698 	
1699 	    def get_role(_el, new_roles_supported):
1700 	        return common_pacemaker.role.get_value_for_cib(
1701 	            _el.getAttribute("role") or const.PCMK_ROLE_STARTED,
1702 	            new_roles_supported,
1703 	        )
1704 	
1705 	    new_roles_supported = isCibVersionSatisfied(
1706 	        operations_el, const.PCMK_NEW_ROLES_CIB_VERSION
1707 	    )
1708 	    existing = []
1709 	    op_name = op_el.getAttribute("name")
1710 	    op_role = get_role(op_el, new_roles_supported)
1711 	    ocf_check_level = None
1712 	    if op_name == "monitor":
1713 	        ocf_check_level = get_operation_ocf_check_level(op_el)
1714 	
1715 	    for op in operations_el.getElementsByTagName("op"):
1716 	        if op.getAttribute("name") == op_name:
1717 	            if (
1718 	                op_name != "monitor"
1719 	                or get_role(op, new_roles_supported) == op_role
1720 	                and ocf_check_level == get_operation_ocf_check_level(op)
1721 	            ):
1722 	                existing.append(op)
1723 	    return existing
1724 	
1725 	
1726 	def get_operation_ocf_check_level(operation_el):
1727 	    """
1728 	    Commandline options: no options
1729 	    """
1730 	    for attr_el in operation_el.getElementsByTagName("instance_attributes"):
1731 	        for nvpair_el in attr_el.getElementsByTagName("nvpair"):
1732 	            if (
1733 	                nvpair_el.getAttribute("name")
1734 	                == OCF_CHECK_LEVEL_INSTANCE_ATTRIBUTE_NAME
1735 	            ):
1736 	                return nvpair_el.getAttribute("value")
1737 	    return None
1738 	
1739 	
1740 	def set_node_attribute(prop, value, node):
1741 	    """
1742 	    Commandline options:
1743 	      * -f - CIB file
1744 	      * --force - no error if attribute to delete doesn't exist
1745 	    """
1746 	    if value == "":
1747 	        o, r = run(
1748 	            [
1749 	                "crm_attribute",
1750 	                "-t",
1751 	                "nodes",
1752 	                "--node",
1753 	                node,
1754 	                "--name",
1755 	                prop,
1756 	                "--query",
1757 	            ]
1758 	        )
1759 	        if r != 0 and "--force" not in pcs_options:
1760 	            err(
1761 	                "attribute: '%s' doesn't exist for node: '%s'" % (prop, node),
1762 	                False,
1763 	            )
1764 	            # This return code is used by pcsd
1765 	            sys.exit(2)
1766 	        o, r = run(
1767 	            [
1768 	                "crm_attribute",
1769 	                "-t",
1770 	                "nodes",
1771 	                "--node",
1772 	                node,
1773 	                "--name",
1774 	                prop,
1775 	                "--delete",
1776 	            ]
1777 	        )
1778 	    else:
1779 	        o, r = run(
1780 	            [
1781 	                "crm_attribute",
1782 	                "-t",
1783 	                "nodes",
1784 	                "--node",
1785 	                node,
1786 	                "--name",
1787 	                prop,
1788 	                "--update",
1789 	                value,
1790 	            ]
1791 	        )
1792 	
1793 	    if r != 0:
1794 	        err("unable to set attribute %s\n%s" % (prop, o))
1795 	
1796 	
1797 	def get_terminal_input(message=None):
1798 	    """
1799 	    Commandline options: no options
1800 	    """
1801 	    if message:
1802 	        sys.stdout.write(message)
1803 	        sys.stdout.flush()
1804 	    try:
1805 	        return input("")
1806 	    except EOFError:
1807 	        return ""
1808 	    except KeyboardInterrupt:
1809 	        print("Interrupted")
1810 	        sys.exit(1)
1811 	
1812 	
1813 	def _get_continue_confirmation_interactive(warning_text: str) -> bool:
1814 	    """
1815 	    Warns user and asks for permission to continue. Returns True if user wishes
1816 	    to continue, False otherwise.
1817 	
1818 	    This function is mostly intended for prompting user to confirm destructive
1819 	    operations - that's why WARNING is in all caps here and user is asked to
1820 	    explicitly type 'yes' or 'y' to continue.
1821 	
1822 	    warning_text -- describes action that we want the user to confirm
1823 	    """
1824 	    print(f"WARNING: {warning_text}")
1825 	    print("Type 'yes' or 'y' to proceed, anything else to cancel: ", end="")
1826 	    response = get_terminal_input()
1827 	    if response in ["yes", "y"]:
1828 	        return True
1829 	    print("Canceled")
1830 	    return False
1831 	
1832 	
1833 	def is_run_interactive() -> bool:
1834 	    """
1835 	    Return True if pcs is running in an interactive environment, False otherwise
1836 	    """
1837 	    return (
1838 	        sys.stdin is not None
1839 	        and sys.stdout is not None
1840 	        and sys.stdin.isatty()
1841 	        and sys.stdout.isatty()
1842 	    )
1843 	
1844 	
1845 	def get_continue_confirmation(warning_text: str, yes: bool) -> bool:
1846 	    """
1847 	    Either asks user to confirm continuation interactively or use --yes to
1848 	    override when running from a script. Returns True if user wants to continue.
1849 	    Returns False if user cancels the action. If a non-interactive environment
1850 	    is detected, pcs exits with an error formed from warning_text.
1851 	
1852 	    warning_text -- describes action that we want the user to confirm
1853 	    yes -- was --yes flag provided?
1854 	    """
1855 	    if yes:
1856 	        reports_output.warn(warning_text)
1857 	        return True
1858 	    if not is_run_interactive():
1859 	        err(f"{warning_text}, use --yes to override")
1860 	        return False
1861 	    return _get_continue_confirmation_interactive(warning_text)
1862 	
1863 	
1864 	def get_terminal_password(message="Password: "):
1865 	    """
1866 	    Commandline options: no options
1867 	    """
1868 	    if sys.stdin is not None and sys.stdin.isatty():
1869 	        try:
1870 	            return getpass.getpass(message)
1871 	        except KeyboardInterrupt:
1872 	            print("Interrupted")
1873 	            sys.exit(1)
1874 	    else:
1875 	        return get_terminal_input(message)
1876 	
1877 	
1878 	# Returns an xml dom containing the current status of the cluster
1879 	# DEPRECATED, please use
1880 	# ClusterState(lib.pacemaker.live.get_cluster_status_dom()) instead
1881 	def getClusterState():
1882 	    """
1883 	    Commandline options:
1884 	      * -f - CIB file
1885 	    """
1886 	    xml_string, returncode = run(
1887 	        ["crm_mon", "--one-shot", "--output-as=xml", "--inactive"],
1888 	        ignore_stderr=True,
1889 	    )
1890 	    if returncode != 0:
1891 	        err("error running crm_mon, is pacemaker running?")
1892 	    return parseString(xml_string)
1893 	
1894 	
1895 	def write_empty_cib(cibfile):
1896 	    """
1897 	    Commandline options: no options
1898 	    """
1899 	    empty_xml = """
1900 	        <cib admin_epoch="0" epoch="1" num_updates="1" validate-with="pacemaker-3.1">
1901 	          <configuration>
1902 	            <crm_config/>
1903 	            <nodes/>
1904 	            <resources/>
1905 	            <constraints/>
1906 	          </configuration>
1907 	          <status/>
1908 	        </cib>
1909 	    """
1910 	    with open(cibfile, "w") as f:
1911 	        f.write(empty_xml)
1912 	
1913 	
1914 	def validate_xml_id(var: str, description: str = "id") -> tuple[bool, str]:
1915 	    """
1916 	    Commandline options: no options
1917 	    """
1918 	    report_list: ReportItemList = []
1919 	    validate_id(var, description, report_list)
1920 	    if report_list:
1921 	        return False, report_list[0].message.message
1922 	    return True, ""
1923 	
1924 	
1925 	def err(errorText: str, exit_after_error: bool = True) -> None:
1926 	    retval = reports_output.error(errorText)
1927 	    if exit_after_error:
1928 	        raise retval
1929 	
1930 	
1931 	@lru_cache(typed=True)
1932 	def get_service_manager() -> ServiceManagerInterface:
1933 	    return _get_service_manager(cmd_runner(), get_report_processor())
1934 	
1935 	
1936 	def enableServices():
1937 	    """
1938 	    Commandline options: no options
1939 	    """
1940 	    # do NOT handle SBD in here, it is started by pacemaker not systemd or init
1941 	    service_list = ["corosync", "pacemaker"]
1942 	    if need_to_handle_qdevice_service():
1943 	        service_list.append("corosync-qdevice")
1944 	    service_manager = get_service_manager()
1945 	
1946 	    report_item_list = []
1947 	    for service in service_list:
1948 	        try:
1949 	            service_manager.enable(service)
1950 	        except ManageServiceError as e:
1951 	            report_item_list.append(service_exception_to_report(e))
1952 	    if report_item_list:
1953 	        raise LibraryError(*report_item_list)
1954 	
1955 	
1956 	def disableServices():
1957 	    """
1958 	    Commandline options: no options
1959 	    """
1960 	    # do NOT handle SBD in here, it is started by pacemaker not systemd or init
1961 	    service_list = ["corosync", "pacemaker"]
1962 	    if need_to_handle_qdevice_service():
1963 	        service_list.append("corosync-qdevice")
1964 	    service_manager = get_service_manager()
1965 	
1966 	    report_item_list = []
1967 	    for service in service_list:
1968 	        try:
1969 	            service_manager.disable(service)
1970 	        except ManageServiceError as e:
1971 	            report_item_list.append(service_exception_to_report(e))
1972 	    if report_item_list:
1973 	        raise LibraryError(*report_item_list)
1974 	
1975 	
1976 	def start_service(service):
1977 	    """
1978 	    Commandline options: no options
1979 	    """
1980 	    service_manager = get_service_manager()
1981 	
1982 	    try:
1983 	        service_manager.start(service)
1984 	    except ManageServiceError as e:
1985 	        raise LibraryError(service_exception_to_report(e)) from e
1986 	
1987 	
1988 	def stop_service(service):
1989 	    """
1990 	    Commandline options: no options
1991 	    """
1992 	    service_manager = get_service_manager()
1993 	
1994 	    try:
1995 	        service_manager.stop(service)
1996 	    except ManageServiceError as e:
1997 	        raise LibraryError(service_exception_to_report(e)) from e
1998 	
1999 	
2000 	def write_file(path, data, permissions=0o644, binary=False):
2001 	    """
2002 	    Commandline options:
2003 	      * --force - overwrite a file if it already exists
2004 	    """
2005 	    if os.path.exists(path):
2006 	        if "--force" not in pcs_options:
2007 	            return False, "'%s' already exists, use --force to overwrite" % path
2008 	        try:
2009 	            os.remove(path)
2010 	        except OSError as e:
2011 	            return False, "unable to remove '%s': %s" % (path, e)
2012 	    mode = "wb" if binary else "w"
2013 	    try:
2014 	        with os.fdopen(
2015 	            os.open(path, os.O_WRONLY | os.O_CREAT, permissions), mode
2016 	        ) as outfile:
2017 	            outfile.write(data)
2018 	    except OSError as e:
2019 	        return False, "unable to write to '%s': %s" % (path, e)
2020 	    return True, ""
2021 	
2022 	
2023 	def tar_add_file_data(  # noqa: PLR0913
2024 	    tarball,
2025 	    data,
2026 	    name,
2027 	    *,
2028 	    mode=None,
2029 	    uid=None,
2030 	    gid=None,
2031 	    uname=None,
2032 	    gname=None,
2033 	    mtime=None,
2034 	):
2035 	    """
2036 	    Commandline options: no options
2037 	    """
2038 	    info = tarfile.TarInfo(name)
2039 	    info.size = len(data)
2040 	    info.type = tarfile.REGTYPE
2041 	    info.mtime = int(time.time()) if mtime is None else mtime
2042 	    if mode is not None:
2043 	        info.mode = mode
2044 	    if uid is not None:
2045 	        info.uid = uid
2046 	    if gid is not None:
2047 	        info.gid = gid
2048 	    if uname is not None:
2049 	        info.uname = uname
2050 	    if gname is not None:
2051 	        info.gname = gname
2052 	    data_io = BytesIO(data)
2053 	    tarball.addfile(info, data_io)
2054 	    data_io.close()
2055 	
2056 	
2057 	# DEPRECATED, please use pcs.lib.pacemaker.live.simulate_cib
2058 	def simulate_cib(cib_dom):
2059 	    """
2060 	    Commandline options: no options
2061 	    """
2062 	    try:
2063 	        with (
2064 	            tempfile.NamedTemporaryFile(
2065 	                mode="w+", suffix=".pcs"
2066 	            ) as new_cib_file,
2067 	            tempfile.NamedTemporaryFile(
2068 	                mode="w+", suffix=".pcs"
2069 	            ) as transitions_file,
2070 	        ):
2071 	            output, retval = run(
2072 	                [
2073 	                    "crm_simulate",
2074 	                    "--simulate",
2075 	                    "--save-output",
2076 	                    new_cib_file.name,
2077 	                    "--save-graph",
2078 	                    transitions_file.name,
2079 	                    "--xml-pipe",
2080 	                ],
2081 	                string_for_stdin=cib_dom.toxml(),
2082 	            )
2083 	            if retval != 0:
2084 	                return err("Unable to run crm_simulate:\n%s" % output)
2085 	            new_cib_file.seek(0)
2086 	            transitions_file.seek(0)
2087 	            return (
2088 	                output,
CID (unavailable; MK=c30004330dc7a1580afdb00f63baac8c) (#1 of 2): XML external entity processing enabled (SIGMA.xml_external_entity_enabled):
(1) Event Sigma main event: The application uses Python's built in `xml` module which does not properly handle erroneous or maliciously constructed data, making the application vulnerable to one or more types of XML attacks.
(2) Event remediation: Avoid using the `xml` module. Consider using the `defusedxml` module or similar which safely prevents all XML entity attacks.
2089 	                parseString(transitions_file.read()),
2090 	                parseString(new_cib_file.read()),
2091 	            )
2092 	    except (OSError, xml.parsers.expat.ExpatError) as e:
2093 	        return err("Unable to run crm_simulate:\n%s" % e)
2094 	    except xml.etree.ElementTree.ParseError as e:
2095 	        return err("Unable to run crm_simulate:\n%s" % e)
2096 	
2097 	
2098 	# DEPRECATED
2099 	# please use pcs.lib.pacemaker.simulate.get_operations_from_transitions
2100 	def get_operations_from_transitions(transitions_dom):
2101 	    """
2102 	    Commandline options: no options
2103 	    """
2104 	    operation_list = []
2105 	    watched_operations = (
2106 	        "start",
2107 	        "stop",
2108 	        "promote",
2109 	        "demote",
2110 	        "migrate_from",
2111 	        "migrate_to",
2112 	    )
2113 	    for rsc_op in transitions_dom.getElementsByTagName("rsc_op"):
2114 	        primitives = rsc_op.getElementsByTagName("primitive")
2115 	        if not primitives:
2116 	            continue
2117 	        if rsc_op.getAttribute("operation").lower() not in watched_operations:
2118 	            continue
2119 	        for prim in primitives:
2120 	            prim_id = prim.getAttribute("id")
2121 	            operation_list.append(
2122 	                (
2123 	                    int(rsc_op.getAttribute("id")),
2124 	                    {
2125 	                        "id": prim_id,
2126 	                        "long_id": prim.getAttribute("long-id") or prim_id,
2127 	                        "operation": rsc_op.getAttribute("operation").lower(),
2128 	                        "on_node": rsc_op.getAttribute("on_node"),
2129 	                    },
2130 	                )
2131 	            )
2132 	    operation_list.sort(key=lambda x: x[0])
2133 	    return [op[1] for op in operation_list]
2134 	
2135 	
2136 	def get_resources_location_from_operations(cib_dom, resources_operations):
2137 	    """
2138 	    Commandline options:
2139 	      * --force - allow constraints on any resource, may not have any effect as
2140 	        an invalid constraint is ignored anyway
2141 	    """
2142 	    locations = {}
2143 	    for res_op in resources_operations:
2144 	        operation = res_op["operation"]
2145 	        if operation not in ("start", "promote", "migrate_from"):
2146 	            continue
2147 	        long_id = res_op["long_id"]
2148 	        if long_id not in locations:
2149 	            # Move clone instances as if they were non-cloned resources, it
2150 	            # really works with current pacemaker (1.1.13-6). Otherwise there
2151 	            # is probably no way to move them other then setting their
2152 	            # stickiness to 0.
2153 	            res_id = res_op["id"]
2154 	            if ":" in res_id:
2155 	                res_id = res_id.split(":")[0]
2156 	            id_for_constraint = validate_constraint_resource(cib_dom, res_id)[2]
2157 	            if not id_for_constraint:
2158 	                continue
2159 	            locations[long_id] = {
2160 	                "id": res_op["id"],
2161 	                "long_id": long_id,
2162 	                "id_for_constraint": id_for_constraint,
2163 	            }
2164 	        if operation in ("start", "migrate_from"):
2165 	            locations[long_id]["start_on_node"] = res_op["on_node"]
2166 	        if operation == "promote":
2167 	            locations[long_id]["promote_on_node"] = res_op["on_node"]
2168 	    return {
2169 	        key: val
2170 	        for key, val in locations.items()
2171 	        if "start_on_node" in val or "promote_on_node" in val
2172 	    }
2173 	
2174 	
2175 	def get_remote_quorumtool_output(node):
2176 	    """
2177 	    Commandline options:
2178 	      * --request-timeout - timeout for HTTP requests
2179 	    """
2180 	    return sendHTTPRequest(node, "remote/get_quorum_info", None, False, False)
2181 	
2182 	
2183 	# return True if quorumtool_output is a string returned when the node is off
2184 	def is_node_offline_by_quorumtool_output(quorum_info):
2185 	    """
2186 	    Commandline options: no options
2187 	    """
2188 	    return quorum_info.strip() == "Cannot initialize CMAP service"
2189 	
2190 	
2191 	def dom_prepare_child_element(dom_element, tag_name, id_candidate):
2192 	    """
2193 	    Commandline options: no options
2194 	    """
2195 	    child_elements = [
2196 	        child
2197 	        for child in dom_element.childNodes
2198 	        if child.nodeType == child.ELEMENT_NODE and child.tagName == tag_name
2199 	    ]
2200 	
2201 	    if not child_elements:
2202 	        dom = dom_element.ownerDocument
2203 	        child_element = dom.createElement(tag_name)
2204 	        child_element.setAttribute("id", find_unique_id(dom, id_candidate))
2205 	        dom_element.appendChild(child_element)
2206 	    else:
2207 	        child_element = child_elements[0]
2208 	    return child_element
2209 	
2210 	
2211 	def dom_update_nvset(dom_element, nvpair_tuples, tag_name, id_candidate):
2212 	    """
2213 	    Commandline options: no options
2214 	    """
2215 	    # Already ported to pcs.libcib.nvpair
2216 	
2217 	    # Do not ever remove the nvset element, even if it is empty. There may be
2218 	    # ACLs set in pacemaker which allow "write" for nvpairs (adding, changing
2219 	    # and removing) but not nvsets. In such a case, removing the nvset would
2220 	    # cause the whole change to be rejected by pacemaker with a "permission
2221 	    # denied" message.
2222 	    # https://bugzilla.redhat.com/show_bug.cgi?id=1642514
2223 	    if not nvpair_tuples:
2224 	        return
2225 	
2226 	    only_removing = True
2227 	    for _, value in nvpair_tuples:
2228 	        if value != "":
2229 	            only_removing = False
2230 	            break
2231 	
2232 	    # Do not use dom.getElementsByTagName, that would get elements we do not
2233 	    # want to. For example if dom_element is a clone, we would get the clones's
2234 	    # as well as clone's primitive's attributes.
2235 	    nvset_element_list = _dom_get_children_by_tag_name(dom_element, tag_name)
2236 	
2237 	    # Do not create new nvset if we are only removing values from it.
2238 	    if not nvset_element_list and only_removing:
2239 	        return
2240 	
2241 	    if not nvset_element_list:
2242 	        dom = dom_element.ownerDocument
2243 	        nvset_element = dom.createElement(tag_name)
2244 	        nvset_element.setAttribute("id", find_unique_id(dom, id_candidate))
2245 	        dom_element.appendChild(nvset_element)
2246 	    else:
2247 	        nvset_element = nvset_element_list[0]
2248 	
2249 	    for name, value in nvpair_tuples:
2250 	        dom_update_nv_pair(
2251 	            nvset_element, name, value, nvset_element.getAttribute("id") + "-"
2252 	        )
2253 	
2254 	
2255 	def dom_update_nv_pair(dom_element, name, value, id_prefix=""):
2256 	    """
2257 	    Commandline options: no options
2258 	    """
2259 	    # Do not ever remove the nvset element, even if it is empty. There may be
2260 	    # ACLs set in pacemaker which allow "write" for nvpairs (adding, changing
2261 	    # and removing) but not nvsets. In such a case, removing the nvset would
2262 	    # cause the whole change to be rejected by pacemaker with a "permission
2263 	    # denied" message.
2264 	    # https://bugzilla.redhat.com/show_bug.cgi?id=1642514
2265 	
2266 	    dom = dom_element.ownerDocument
2267 	    element_found = False
2268 	    for el in dom_element.getElementsByTagName("nvpair"):
2269 	        if el.getAttribute("name") == name:
2270 	            element_found = True
2271 	            if value == "":
2272 	                dom_element.removeChild(el)
2273 	            else:
2274 	                el.setAttribute("value", value)
2275 	            break
2276 	    if not element_found and value != "":
2277 	        el = dom.createElement("nvpair")
2278 	        el.setAttribute("id", id_prefix + name)
2279 	        el.setAttribute("name", name)
2280 	        el.setAttribute("value", value)
2281 	        dom_element.appendChild(el)
2282 	    return dom_element
2283 	
2284 	
2285 	# Passed an array of strings ["a=b","c=d"], return array of tuples
2286 	# [("a","b"),("c","d")]
2287 	def convert_args_to_tuples(ra_values):
2288 	    """
2289 	    Commandline options: no options
2290 	    """
2291 	    ret = []
2292 	    for ra_val in ra_values:
2293 	        if ra_val.count("=") != 0:
2294 	            split_val = ra_val.split("=", 1)
2295 	            ret.append((split_val[0], split_val[1]))
2296 	    return ret
2297 	
2298 	
2299 	def is_int(val):
2300 	    try:
2301 	        int(val)
2302 	        return True
2303 	    except ValueError:
2304 	        return False
2305 	
2306 	
2307 	def dom_update_utilization(dom_element, attributes, id_prefix=""):
2308 	    """
2309 	    Commandline options: no options
2310 	    """
2311 	    attr_tuples = []
2312 	    for name, value in sorted(attributes.items()):
2313 	        if value != "" and not is_int(value):
2314 	            err(
2315 	                "Value of utilization attribute must be integer: "
2316 	                "'{0}={1}'".format(name, value)
2317 	            )
2318 	        attr_tuples.append((name, value))
2319 	    dom_update_nvset(
2320 	        dom_element,
2321 	        attr_tuples,
2322 	        "utilization",
2323 	        id_prefix + dom_element.getAttribute("id") + "-utilization",
2324 	    )
2325 	
2326 	
2327 	def dom_update_meta_attr(dom_element, attributes):
2328 	    """
2329 	    Commandline options: no options
2330 	    """
2331 	    dom_update_nvset(
2332 	        dom_element,
2333 	        attributes,
2334 	        "meta_attributes",
2335 	        dom_element.getAttribute("id") + "-meta_attributes",
2336 	    )
2337 	
2338 	
2339 	def dom_update_instance_attr(dom_element, attributes):
2340 	    """
2341 	    Commandline options: no options
2342 	    """
2343 	    dom_update_nvset(
2344 	        dom_element,
2345 	        attributes,
2346 	        "instance_attributes",
2347 	        dom_element.getAttribute("id") + "-instance_attributes",
2348 	    )
2349 	
2350 	
2351 	def get_utilization(element, filter_name=None):
2352 	    """
2353 	    Commandline options: no options
2354 	    """
2355 	    utilization = {}
2356 	    for e in element.getElementsByTagName("utilization"):
2357 	        for u in e.getElementsByTagName("nvpair"):
2358 	            name = u.getAttribute("name")
2359 	            if filter_name is not None and name != filter_name:
2360 	                continue
2361 	            utilization[name] = u.getAttribute("value")
2362 	        # Use just first element of utilization attributes. We don't support
2363 	        # utilization with rules just yet.
2364 	        break
2365 	    return utilization
2366 	
2367 	
2368 	def get_utilization_str(element, filter_name=None):
2369 	    """
2370 	    Commandline options: no options
2371 	    """
2372 	    output = []
2373 	    for name, value in sorted(get_utilization(element, filter_name).items()):
2374 	        output.append(name + "=" + value)
2375 	    return " ".join(output)
2376 	
2377 	
2378 	def get_lib_env() -> LibraryEnvironment:
2379 	    """
2380 	    Commandline options:
2381 	      * -f - CIB file
2382 	      * --corosync_conf - corosync.conf file
2383 	      * --request-timeout - timeout of HTTP requests
2384 	    """
2385 	    user = None
2386 	    groups = None
2387 	    if os.geteuid() == 0:
2388 	        for name in ("CIB_user", "CIB_user_groups"):
2389 	            if name in os.environ and os.environ[name].strip():
2390 	                value = os.environ[name].strip()
2391 	                if name == "CIB_user":
2392 	                    user = value
2393 	                else:
2394 	                    groups = value.split(" ")
2395 	
2396 	    cib_data = None
2397 	    if usefile:
2398 	        cib_data = get_cib()
2399 	
2400 	    corosync_conf_data = None
2401 	    if "--corosync_conf" in pcs_options:
2402 	        conf = pcs_options["--corosync_conf"]
2403 	        try:
2404 	            with open(conf) as corosync_conf_file:
2405 	                corosync_conf_data = corosync_conf_file.read()
2406 	        except OSError as e:
2407 	            err("Unable to read %s: %s" % (conf, e.strerror))
2408 	
2409 	    return LibraryEnvironment(
2410 	        logging.getLogger("pcs"),
2411 	        get_report_processor(),
2412 	        user,
2413 	        groups,
2414 	        cib_data,
2415 	        corosync_conf_data,
2416 	        known_hosts_getter=read_known_hosts_file,
2417 	        request_timeout=pcs_options.get("--request-timeout"),
2418 	    )
2419 	
2420 	
2421 	def get_cib_user_groups():
2422 	    """
2423 	    Commandline options: no options
2424 	    """
2425 	    user = None
2426 	    groups = None
2427 	    if os.geteuid() == 0:
2428 	        for name in ("CIB_user", "CIB_user_groups"):
2429 	            if name in os.environ and os.environ[name].strip():
2430 	                value = os.environ[name].strip()
2431 	                if name == "CIB_user":
2432 	                    user = value
2433 	                else:
2434 	                    groups = value.split(" ")
2435 	    return user, groups
2436 	
2437 	
2438 	def get_cli_env() -> Env:
2439 	    """
2440 	    Commandline options:
2441 	      * --debug
2442 	      * --request-timeout
2443 	    """
2444 	    env = Env()
2445 	    env.user, env.groups = get_cib_user_groups()
2446 	    env.known_hosts_getter = read_known_hosts_file
2447 	    env.report_processor = get_report_processor()
2448 	    env.request_timeout = pcs_options.get("--request-timeout")
2449 	    return env
2450 	
2451 	
2452 	def get_middleware_factory():
2453 	    """
2454 	    Commandline options:
2455 	      * --corosync_conf
2456 	      * --name
2457 	      * -f
2458 	    """
2459 	    return middleware.create_middleware_factory(
2460 	        cib=middleware.cib(filename if usefile else None, touch_cib_file),
2461 	        corosync_conf_existing=middleware.corosync_conf_existing(
2462 	            pcs_options.get("--corosync_conf")
2463 	        ),
2464 	    )
2465 	
2466 	
2467 	def get_library_wrapper() -> Library:
2468 	    """
2469 	    Commandline options:
2470 	      * --debug
2471 	      * --request-timeout
2472 	      * --corosync_conf
2473 	      * --name
2474 	      * -f
2475 	    NOTE: usage of options may depend on used middleware for particular command
2476 	    """
2477 	    return Library(get_cli_env(), get_middleware_factory())
2478 	
2479 	
2480 	def exit_on_cmdline_input_error(
2481 	    error: CmdLineInputError, main_name: str, usage_name: StringSequence
2482 	) -> None:
2483 	    if error and error.message:
2484 	        reports_output.error(error.message)
2485 	    if error and error.hint:
2486 	        print_to_stderr(f"Hint: {error.hint}")
2487 	    if not error or (not error.message or error.show_both_usage_and_message):
2488 	        usage.show(main_name, list(usage_name))
2489 	    sys.exit(1)
2490 	
2491 	
2492 	def get_report_processor() -> ReportProcessorToConsole:
2493 	    return ReportProcessorToConsole(debug="--debug" in pcs_options)
2494 	
2495 	
2496 	def get_user_and_pass() -> tuple[str, str]:
2497 	    """
2498 	    Commandline options:
2499 	      * -u - username
2500 	      * -p - password
2501 	    """
2502 	    username = (
2503 	        pcs_options["-u"]
2504 	        if "-u" in pcs_options
2505 	        else get_terminal_input("Username: ")
2506 	    )
2507 	    password = (
2508 	        pcs_options["-p"] if "-p" in pcs_options else get_terminal_password()
2509 	    )
2510 	    return username, password
2511 	
2512 	
2513 	def get_input_modifiers() -> InputModifiers:
2514 	    return InputModifiers(pcs_options)
2515 	
2516 	
2517 	def get_token_from_file(file_name: str) -> str:
2518 	    try:
2519 	        with open(file_name, "rb") as file:
2520 	            # 256 to stay backwards compatible
2521 	            max_size = 256
2522 	            value_bytes = file.read(max_size + 1)
2523 	            if len(value_bytes) > max_size:
2524 	                err(f"Maximal token size of {max_size} bytes exceeded")
2525 	            if not value_bytes:
2526 	                err(f"File '{file_name}' is empty")
2527 	            return base64.b64encode(value_bytes).decode("utf-8")
2528 	    except OSError as e:
2529 	        err(f"Unable to read file '{file_name}': {e}", exit_after_error=False)
2530 	        raise SystemExit(1) from e
2531 	
2532 	
2533 	def print_warning_if_utilization_attrs_has_no_effect(
2534 	    properties_facade: PropertyConfigurationFacade,
2535 	) -> None:
2536 	    PLACEMENT_STRATEGIES_USING_UTILIZATION_ATTRS = [
2537 	        "balanced",
2538 	        "minimal",
2539 	        "utilization",
2540 	    ]
2541 	    value = properties_facade.get_property_value_or_default(
2542 	        "placement-strategy"
2543 	    )
2544 	    if value not in PLACEMENT_STRATEGIES_USING_UTILIZATION_ATTRS:
2545 	        reports_output.warn(
2546 	            "Utilization attributes configuration has no effect until cluster "
2547 	            "property option 'placement-strategy' is set to one of the "
2548 	            "values: "
2549 	            f"{format_list(PLACEMENT_STRATEGIES_USING_UTILIZATION_ATTRS)}"
2550 	        )
2551