# 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

    def nvset2env(self, set_n):
        if set_n is None:
            return
        try:
            pfx = self.pfx[set_n.tag]
        except:
            self.err("unknown attributes set: %s" % set_n.tag)
            return
        for nvpair in set_n.iterchildren():
            if nvpair.tag != "nvpair":
                continue
            n = nvpair.get("name")
            v = nvpair.get("value")
            self.rscenv["%s%s" % (pfx, n)] = v

    def set_rscenv(self, op):
        '''
        Setup the environment. Class specific.
        '''
        self.rscenv = {}
        n = self.rscdef_node
        self.timeout = get_op_timeout(n, op, "20s")
        self.rscenv["%stimeout" % self.pfx["meta_attributes"]] = str(self.timeout)
        if op == "monitor":
            self.rscenv["%sinterval" % self.pfx["meta_attributes"]] = "10000"
        if is_cloned(n):
            # some of the meta attributes for clones/ms are used
            # by resource agents
            cn = get_topmost_rsc(n)

# 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


    def nvset2env(self, set_n):
        if set_n is None:
            return
        try:
            pfx = self.pfx[set_n.tag]
        except:
            self.err("unknown attributes set: %s" % set_n.tag)
            return
        for nvpair in set_n.iterchildren():
            if nvpair.tag != "nvpair":
                continue
            n = nvpair.get("name")
            v = nvpair.get("value")
            self.rscenv["%s%s" % (pfx, n)] = v

    def set_rscenv(self, op):
        '''
        Setup the environment. Class specific.
        '''
        self.rscenv = {}
        n = self.rscdef_node
        self.timeout = get_op_timeout(n, op, "20s")
        self.rscenv["%stimeout" % self.pfx["meta_attributes"]] = str(self.timeout)
        if op == "monitor":
            self.rscenv["%sinterval" % self.pfx["meta_attributes"]] = "10000"
        if is_cloned(n):
            # some of the meta attributes for clones/ms are used
            # by resource agents
            cn = get_topmost_rsc(n)

            self.nvset2env(get_child_nvset_node(cn))