# Path: crmsh/utils.py
# def rmdir_r(d):
#     # TODO: Make sure we're not deleting something we shouldn't!
#     if d and os.path.isdir(d):
#         shutil.rmtree(d)
# 
# def quote(s):
#     """Return a shell-escaped version of the string *s*."""
#     if not s:
#         return "''"
#     if _find_unsafe(s) is None:
#         return s
# 
#     # use single quotes, and put single quotes into double quotes
#     # the string $'b is then quoted as '$'"'"'b'
#     return "'" + s.replace("'", "'\"'\"'") + "'"
# 
# @memoize
# def this_node():
#     'returns name of this node (hostname)'
#     return os.uname()[1]
# 
# def ext_cmd(cmd, shell=True):
#     cmd = add_sudo(cmd)
#     if options.regression_tests:
#         print(".EXT", cmd)
#     logger.debug("invoke: %s", cmd)
#     return subprocess.call(cmd, shell=shell)
# 
# Path: crmsh/xmlutil.py
# def get_topmost_rsc(node):
#     '''
#     Return a topmost node which is a resource and contains this resource
#     '''
#     if is_container(node.getparent()):
#         return get_topmost_rsc(node.getparent())
#     return node
# 
# def get_op_timeout(rsc_node, op, default_timeout):
#     interval = (op == "monitor" and "non-0" or "0")
#     op_n = find_operation(rsc_node, op == "probe" and "monitor" or op, interval)
#     timeout = op_n is not None and op_n.get("timeout") or default_timeout
#     return crm_msec(timeout)
# 
# def get_child_nvset_node(node, attr_set="meta_attributes"):
#     if node is None:
#         return None
#     for c in node.iterchildren():
#         if c.tag != attr_set:
#             continue
#         return c
#     return None
# 
# def is_ms_or_promotable_clone(self, ident):
#     '''
#     Test if the resource is master-slave.
#     '''
#     rsc_node = self.rsc2node(ident)
#     if rsc_node is None:
#         return False
#     return is_ms_or_promotable_clone(rsc_node)
# 
# def is_cloned(node):
#     return (node.getparent().tag in constants.clonems_tags or
#             (node.getparent().tag == "group" and
#              node.getparent().getparent().tag in constants.clonems_tags))


import os
import sys
from .utils import rmdir_r, quote, this_node, ext_cmd
from .xmlutil import get_topmost_rsc, get_op_timeout, get_child_nvset_node, is_ms_or_promotable_clone, is_cloned
from . import log
        from tempfile import mkdtemp
            from .crm_pssh import show_output
                from .crm_pssh import do_pssh_cmd
    from . import ra
        show_output(self.errdir, (host,), "stderr")
        show_output(self.outdir, (host,), "stdout")

    def run_on_all(self, op):
        '''
        In case of cloned resources, it doesn't make sense to run
        (certain) operations on just one node. So, we run them
        everywhere instead.
        For instance, some clones require quorum.
        '''
        return is_cloned(self.rscdef_node) and op in ("start", "stop")

    def exec_cmd(self, op):
        '''defined in subclasses'''
        pass

    def runop(self, op, nodes=None, local_only=False):
        '''
        Execute an operation.
        '''
        if not nodes or self.run_on_all(op):
            nodes = self.nodes
        self.last_op = op
        self.set_rscenv(op)
        real_op = (op == "probe" and "monitor" or op)
        cmd = self.exec_cmd(real_op)
        logger.debug("running %s on %s", real_op, nodes)
        for attr in self.rscenv:
            # shell doesn't allow "-" in var names
            envvar = attr.replace("-", "_")

# Path: crmsh/utils.py
# def rmdir_r(d):
#     # TODO: Make sure we're not deleting something we shouldn't!
#     if d and os.path.isdir(d):
#         shutil.rmtree(d)
# 
# def quote(s):
#     """Return a shell-escaped version of the string *s*."""
#     if not s:
#         return "''"
#     if _find_unsafe(s) is None:
#         return s
# 
#     # use single quotes, and put single quotes into double quotes
#     # the string $'b is then quoted as '$'"'"'b'
#     return "'" + s.replace("'", "'\"'\"'") + "'"
# 
# @memoize
# def this_node():
#     'returns name of this node (hostname)'
#     return os.uname()[1]
# 
# def ext_cmd(cmd, shell=True):
#     cmd = add_sudo(cmd)
#     if options.regression_tests:
#         print(".EXT", cmd)
#     logger.debug("invoke: %s", cmd)
#     return subprocess.call(cmd, shell=shell)
# 
# Path: crmsh/xmlutil.py
# def get_topmost_rsc(node):
#     '''
#     Return a topmost node which is a resource and contains this resource
#     '''
#     if is_container(node.getparent()):
#         return get_topmost_rsc(node.getparent())
#     return node
# 
# def get_op_timeout(rsc_node, op, default_timeout):
#     interval = (op == "monitor" and "non-0" or "0")
#     op_n = find_operation(rsc_node, op == "probe" and "monitor" or op, interval)
#     timeout = op_n is not None and op_n.get("timeout") or default_timeout
#     return crm_msec(timeout)
# 
# def get_child_nvset_node(node, attr_set="meta_attributes"):
#     if node is None:
#         return None
#     for c in node.iterchildren():
#         if c.tag != attr_set:
#             continue
#         return c
#     return None
# 
# def is_ms_or_promotable_clone(self, ident):
#     '''
#     Test if the resource is master-slave.
#     '''
#     rsc_node = self.rsc2node(ident)
#     if rsc_node is None:
#         return False
#     return is_ms_or_promotable_clone(rsc_node)
# 
# def is_cloned(node):
#     return (node.getparent().tag in constants.clonems_tags or
#             (node.getparent().tag == "group" and
#              node.getparent().getparent().tag in constants.clonems_tags))


# Path: crmsh/rsctest.py
import os
import sys
from .utils import rmdir_r, quote, this_node, ext_cmd
from .xmlutil import get_topmost_rsc, get_op_timeout, get_child_nvset_node, is_ms_or_promotable_clone, is_cloned
from . import log
        from tempfile import mkdtemp
            from .crm_pssh import show_output
                from .crm_pssh import do_pssh_cmd
    from . import ra

        show_output(self.errdir, (host,), "stderr")
        show_output(self.outdir, (host,), "stdout")

    def run_on_all(self, op):
        '''
        In case of cloned resources, it doesn't make sense to run
        (certain) operations on just one node. So, we run them
        everywhere instead.
        For instance, some clones require quorum.
        '''
        return is_cloned(self.rscdef_node) and op in ("start", "stop")

    def exec_cmd(self, op):
        '''defined in subclasses'''
        pass

    def runop(self, op, nodes=None, local_only=False):
        '''
        Execute an operation.
        '''
        if not nodes or self.run_on_all(op):
            nodes = self.nodes
        self.last_op = op
        self.set_rscenv(op)
        real_op = (op == "probe" and "monitor" or op)
        cmd = self.exec_cmd(real_op)
        logger.debug("running %s on %s", real_op, nodes)
        for attr in self.rscenv:
            # shell doesn't allow "-" in var names
            envvar = attr.replace("-", "_")

            cmd = "%s=%s %s" % (envvar, quote(self.rscenv[attr]), cmd)