def resource_remove(resource_id, output = True): group = utils.get_cib_xpath('//resources/group/primitive[@id="'+resource_id+'"]/..') num_resources_in_group = 0 master = utils.get_cib_xpath('//resources/master/primitive[@id="'+resource_id+'"]/..') clone = utils.get_cib_xpath('//resources/clone/primitive[@id="'+resource_id+'"]/..') if not utils.does_exist('//resources/descendant::primitive[@id="'+resource_id+'"]'): if utils.does_exist('//resources/master[@id="'+resource_id+'"]'): return resource_master_remove([resource_id]) print "Error: Resource does not exist." sys.exit(1) if (group != ""): num_resources_in_group = len(parseString(group).documentElement.getElementsByTagName("primitive")) if (group == "" or num_resources_in_group > 1): if clone != "": args = ["cibadmin", "-o", "resources", "-D", "--xml-text", clone] elif master != "": args = ["cibadmin", "-o", "resources", "-D", "--xml-text", master] else: args = ["cibadmin", "-o", "resources", "-D", "--xpath", "//primitive[@id='"+resource_id+"']"] constraints = constraint.find_constraints_containing(resource_id) for c in constraints: if output == True: print "Removing Constraint - " + c constraint.constraint_rm([c]) if output == True: print "Deleting Resource - " + resource_id output,retVal = utils.run(args) if retVal != 0: print "Unable to remove resource: %s, it may still be referenced in constraints." % resource_id sys.exit(1) return False else: args = ["cibadmin", "-o", "resources", "-D", "--xml-text", group] if output == True: print "Deleting Resource (and group) - " + resource_id cmdoutput,retVal = utils.run(args) if retVal != 0: if output == True: print "ERROR: Unable to remove resource '%s' (do constraints exist?)" % (resource_id) return False # Only clean up resource if we're *not* using a file (otherwise we get a 60s timeout) if (not utils.usefile): args = ["crm_resource","-C","-r",resource_id] cmdoutput, retVal = utils.run(args) # We don't currently check output because the resource may have already been # properly cleaned up return True
def resource_group_add(group_name, resource_ids): group_xpath = "//group[@id='"+group_name+"']" group_xml = utils.get_cib_xpath(group_xpath) if (group_xml == ""): impl = getDOMImplementation() newdoc = impl.createDocument(None, "group", None) element = newdoc.documentElement element.setAttribute("id", group_name) xml_resource_string = element.toxml() else: element = parseString(group_xml).documentElement resources_to_move = "" for resource_id in resource_ids: # If resource already exists in group then we skip if (utils.get_cib_xpath("//group[@id='"+group_name+"']/primitive[@id='"+resource_id+"']") != ""): print resource_id + " already exists in " + group_name + "\n" continue args = ["cibadmin", "-o", "resources", "-Q", "--xpath", "//primitive[@id='"+resource_id+"']"] output,retVal = utils.run(args) if (retVal != 0): print "Bad resource: " + resource_id continue print "Query for " + resource_id, print output resources_to_move = resources_to_move + output print "Delete " + resource_id, resource_remove(resource_id) if (resources_to_move != ""): print "Resources to Move:", print resources_to_move resources_to_move = "<resources>" + resources_to_move + "</resources>" resource_children = parseString(resources_to_move).documentElement print "Child Nodes:\n" print resource_children.toprettyxml() for child in resource_children.childNodes: element.appendChild(child) xml_resource_string = element.toprettyxml() print "New Group String", print xml_resource_string args = ["cibadmin", "-o", "resources", "-c", "-M", "-X", xml_resource_string] output,retval = utils.run(args) print output, else: print "No resources to add.\n" sys.exit(1)
def resource_manage(argv, set_managed): if len(argv) == 0: usage.resource() sys.exit(1) for resource in argv: if not utils.does_exist("//primitive[@id='"+resource+"']"): print "Error: %s doesn't exist." % resource sys.exit(1) exists = utils.does_exist("//primitive[@id='"+resource+"']/meta_attributes/nvpair[@name='is-managed']") if set_managed and not exists: print "Error: %s is already managed" % resource sys.exit(1) elif not set_managed and exists: print "Error: %s is already unmanaged" % resource sys.exit(1) for resource in argv: if not set_managed: (output, retval) = utils.set_unmanaged(resource) if retval != 0: print "Error attempting to unmanage resource: %s" % output sys.exit(1) else: xpath = "//primitive[@id='"+resource+"']/meta_attributes/nvpair[@name='is-managed']" my_xml = utils.get_cib_xpath(xpath) utils.remove_from_cib(my_xml)
def resource_remove(resource_id, output = True): group = utils.get_cib_xpath('//resources/group/primitive[@id="'+resource_id+'"]/..') num_resources_in_group = 0 if (group != ""): num_resources_in_group = len(parseString(group).documentElement.getElementsByTagName("primitive")) if (group == "" or num_resources_in_group > 1): args = ["cibadmin", "-o", "resources", "-D", "--xpath", "//primitive[@id='"+resource_id+"']"] if output == True: print "Deleting Resource - " + resource_id, output,retVal = utils.run(args) if retVal != 0: return False else: args = ["cibadmin", "-o", "resources", "-D", "--xml-text", group] if output == True: print "Deleting Resource (and group) - " + resource_id cmdoutput,retVal = utils.run(args) if retVal != 0: if output == True: print "ERROR: Unable to remove resource '%s' (do constraints exist?)" % (resource_id) return False args = ["crm_resource","-C","-r",resource_id] cmdoutput, retVal = utils.run(args) # We don't currently check output because the resource may have already been # properly cleaned up return True
def getCurrentConstraints(): current_constraints_xml = utils.get_cib_xpath('//constraints') # Verify current constraint doesn't already exist # If it does we replace it with the new constraint dom = parseString(current_constraints_xml) constraintsElement = dom.getElementsByTagName('constraints')[0] return (dom, constraintsElement)
def getCurrentConstraints(): current_constraints_xml = utils.get_cib_xpath('//constraints') if current_constraints_xml == "": utils.err("unable to process cib") # Verify current constraint doesn't already exist # If it does we replace it with the new constraint dom = parseString(current_constraints_xml) constraintsElement = dom.getElementsByTagName('constraints')[0] return (dom, constraintsElement)
def getCurrentConstraints(): current_constraints_xml = utils.get_cib_xpath('//constraints') if current_constraints_xml == "": print "Error: unable to process cib" sys.exit(1) # Verify current constraint doesn't already exist # If it does we replace it with the new constraint dom = parseString(current_constraints_xml) constraintsElement = dom.getElementsByTagName('constraints')[0] return (dom, constraintsElement)
def getCurrentConstraints(passed_dom=None): if passed_dom: dom = passed_dom else: current_constraints_xml = utils.get_cib_xpath('//constraints') if current_constraints_xml == "": utils.err("unable to process cib") # Verify current constraint doesn't already exist # If it does we replace it with the new constraint dom = parseString(current_constraints_xml) constraintsElement = dom.getElementsByTagName('constraints')[0] return (dom, constraintsElement)
def resource_remove(resource_id): group = utils.get_cib_xpath('//resources/group/primitive[@id="'+resource_id+'"]/..') num_resources_in_group = 0 if (group != ""): num_resources_in_group = len(parseString(group).documentElement.getElementsByTagName("primitive")) if (group == "" or num_resources_in_group > 1): args = ["cibadmin", "-o", "resources", "-D", "--xpath", "//primitive[@id='"+resource_id+"']"] print "Deleting Resource - " + resource_id, output,retVal = utils.run(args) else: args = ["cibadmin", "-o", "resources", "-D", "--xml-text", group] print "Deleting Resource (and group) - " + resource_id, output,retVal = utils.run(args)
def resource_group_rm(group_name, resource_ids): resource_id = resource_ids[0] group_xpath = "//group[@id='"+group_name+"']" group_xml = utils.get_cib_xpath(group_xpath) if group_xml == "": print "ERROR: Group '%s' does not exist" % group_name sys.exit(1) for resource_id in resource_ids: element = parseString(group_xml).documentElement resource_xml = utils.get_cib_xpath("//group[@id='"+group_name+"']/primitive[@id='"+resource_id+"']") if resource_xml == "": print "ERROR Resource '%s' does not exist in group '%s'" % (resource_id, group_name) sys.exit(1) if not resource_remove(resource_id, False): print "Unable to move resource" sys.exit(1) output, retval = utils.add_to_cib("resources", resource_xml) if retval != 0: print "ERROR: Unable to re-add resource" print output sys.exit(1) return True
def resource_group_list(argv): group_xpath = "//group" group_xml = utils.get_cib_xpath(group_xpath) # If no groups exist, we silently return if (group_xml == ""): return element = parseString(group_xml).documentElement # If there is more than one group returned it's wrapped in an xpath-query # element if element.tagName == "xpath-query": elements = element.getElementsByTagName("group") else: elements = [element] for e in elements: print e.getAttribute("id") + ":", for resource in e.getElementsByTagName("primitive"): print resource.getAttribute("id"), print ""