def constraint_rule(argv): if len(argv) < 2: usage.constraint("rule") sys.exit(1) found = False command = argv.pop(0) constraint_id = None rule_id = None if command == "add": constraint_id = argv.pop(0) cib = utils.get_cib_dom() constraint = utils.dom_get_element_with_id( cib.getElementsByTagName("constraints")[0], "rsc_location", constraint_id ) if not constraint: utils.err("Unable to find constraint: " + constraint_id) options, rule_argv = rule_utils.parse_argv(argv) rule_utils.dom_rule_add(constraint, options, rule_argv) location_rule_check_duplicates(cib, constraint) utils.replace_cib_configuration(cib) elif command in ["remove","delete"]: cib = utils.get_cib_etree() temp_id = argv.pop(0) constraints = cib.find('.//constraints') loc_cons = cib.findall(str('.//rsc_location')) rules = cib.findall(str('.//rule')) for loc_con in loc_cons: for rule in loc_con: if rule.get("id") == temp_id: if len(loc_con) > 1: print("Removing Rule: {0}".format(rule.get("id"))) loc_con.remove(rule) found = True break else: print( "Removing Constraint: {0}".format(loc_con.get("id")) ) constraints.remove(loc_con) found = True break if found == True: break if found: utils.replace_cib_configuration(cib) else: utils.err("unable to find rule with id: %s" % temp_id) else: usage.constraint("rule") sys.exit(1)
def order_set(argv): setoptions = [] for i in range(len(argv)): if argv[i] == "setoptions": setoptions = argv[i + 1:] argv[i:] = [] break argv.insert(0, "set") resource_sets = set_args_into_array(argv) if not check_empty_resource_sets(resource_sets): usage.constraint(["order set"]) sys.exit(1) cib, constraints = getCurrentConstraints(utils.get_cib_dom()) attributes = [] id_specified = False for opt in setoptions: if "=" not in opt: utils.err("missing value of '%s' option" % opt) name, value = opt.split("=", 1) if name == "id": id_valid, id_error = utils.validate_xml_id(value, 'constraint id') if not id_valid: utils.err(id_error) if utils.does_id_exist(cib, value): utils.err( "id '%s' is already in use, please specify another one" % value) id_specified = True attributes.append((name, value)) elif name == "kind": normalized_value = value.lower().capitalize() if normalized_value not in OPTIONS_KIND: utils.err("invalid kind value '%s', allowed values are: %s" % (value, ", ".join(OPTIONS_KIND))) attributes.append((name, normalized_value)) elif name == "symmetrical": if value.lower() not in OPTIONS_SYMMETRICAL: utils.err( "invalid symmetrical value '%s', allowed values are: %s" % (value, ", ".join(OPTIONS_SYMMETRICAL))) attributes.append((name, value.lower())) else: utils.err("invalid option '%s', allowed options are: %s" % (name, "kind, symmetrical, id")) if not id_specified: order_id = "pcs_rsc_order" for a in argv: if "=" not in a: order_id += "_" + a attributes.append(("id", utils.find_unique_id(cib, order_id))) rsc_order = cib.createElement("rsc_order") for name, value in attributes: rsc_order.setAttribute(name, value) set_add_resource_sets(rsc_order, resource_sets, cib) constraints.appendChild(rsc_order) utils.replace_cib_configuration(cib)
def colocation_add(argv): if len(argv) < 2: usage.constraint() sys.exit(1) resource1 = argv.pop(0) resource2 = argv.pop(0) if not utils.does_resource_exist(resource1): print "Error: Resource '" + resource1 + "' does not exist" sys.exit(1) if not utils.does_resource_exist(resource2): print "Error: Resource '" + resource2 + "' does not exist" sys.exit(1) score,nv_pairs = parse_score_options(argv) (dom,constraintsElement) = getCurrentConstraints() cl_id = utils.find_unique_id(dom, "colocation-" + resource1 + "-" + resource2 + "-" + score) element = dom.createElement("rsc_colocation") element.setAttribute("id",cl_id) element.setAttribute("rsc",resource1) element.setAttribute("with-rsc",resource2) element.setAttribute("score",score) for nv_pair in nv_pairs: element.setAttribute(nv_pair[0], nv_pair[1]) constraintsElement.appendChild(element) xml_constraint_string = constraintsElement.toxml() args = ["cibadmin", "-c", "-R", "--xml-text", xml_constraint_string] output,retval = utils.run(args) if output != "": print output
def colocation_rm(argv): elementFound = False if len(argv) < 2: usage.constraint() sys.exit(1) (dom,constraintsElement) = getCurrentConstraints() resource1 = argv[0] resource2 = argv[1] for co_loc in constraintsElement.getElementsByTagName('rsc_colocation')[:]: if co_loc.getAttribute("rsc") == resource1 and co_loc.getAttribute("with-rsc") == resource2: constraintsElement.removeChild(co_loc) elementFound = True if co_loc.getAttribute("rsc") == resource2 and co_loc.getAttribute("with-rsc") == resource1: constraintsElement.removeChild(co_loc) elementFound = True if elementFound == True: xml_constraint_string = constraintsElement.toxml() args = ["cibadmin", "-c", "-R", "--xml-text", xml_constraint_string] output,retval = utils.run(args) if output != "": print output else: print "No matching resources found in ordering list"
def colocation_rm(argv): elementFound = False if len(argv) < 2: usage.constraint() sys.exit(1) (dom, constraintsElement) = getCurrentConstraints() resource1 = argv[0] resource2 = argv[1] for co_loc in constraintsElement.getElementsByTagName('rsc_colocation')[:]: if co_loc.getAttribute("rsc") == resource1 and co_loc.getAttribute( "with-rsc") == resource2: constraintsElement.removeChild(co_loc) elementFound = True if co_loc.getAttribute("rsc") == resource2 and co_loc.getAttribute( "with-rsc") == resource1: constraintsElement.removeChild(co_loc) elementFound = True if elementFound == True: xml_constraint_string = constraintsElement.toxml() args = ["cibadmin", "-c", "-R", "--xml-text", xml_constraint_string] output, retval = utils.run(args) if output != "": print output else: print "No matching resources found in ordering list"
def constraint_rm(argv): if len(argv) < 1: usage.constraint() sys.exit(1) if len(argv) != 1: for arg in argv: constraint_rm([arg]) return else: c_id = argv.pop(0) elementFound = False (dom, constraintsElement) = getCurrentConstraints() for co in constraintsElement.childNodes[:]: if co.nodeType != xml.dom.Node.ELEMENT_NODE: continue if co.getAttribute("id") == c_id: constraintsElement.removeChild(co) elementFound = True if elementFound == True: xml_constraint_string = constraintsElement.toxml() args = ["cibadmin", "-c", "-R", "--xml-text", xml_constraint_string] output, retval = utils.run(args) if output != "": print output else: print "No matching resources found in ordering list"
def order_add(argv,returnElementOnly=False): if len(argv) != 3 and len(argv) != 4: usage.constraint() sys.exit(1) resource1 = argv.pop(0) resource2 = argv.pop(0) score = argv.pop(0) sym = "true" if (len(argv) == 0 or argv.pop(0) != "nonsymmetrical") else "false" order_id = "order-" + resource1 + "-" + resource2 + "-" + score print "Adding " + resource1 + " " + resource2 + " score: " + score + " Symmetrical: "+sym (dom,constraintsElement) = getCurrentConstraints() element = dom.createElement("rsc_order") element.setAttribute("id",order_id) element.setAttribute("first",resource1) element.setAttribute("then",resource2) element.setAttribute("score",score) if (sym == "false"): element.setAttribute("symmetrical", "false") constraintsElement.appendChild(element) if returnElementOnly == False: xml_constraint_string = constraintsElement.toxml() args = ["cibadmin", "-c", "-R", "--xml-text", xml_constraint_string] output,retval = utils.run(args) if output != "": print output else: return element.toxml()
def order_rm(argv): if len(argv) == 0: usage.constraint() sys.exit(1) elementFound = False (dom,constraintsElement) = getCurrentConstraints() for resource in argv: for ord_loc in constraintsElement.getElementsByTagName('rsc_order')[:]: if ord_loc.getAttribute("first") == resource or ord_loc.getAttribute("then") == resource: constraintsElement.removeChild(ord_loc) elementFound = True resource_refs_to_remove = [] for ord_set in constraintsElement.getElementsByTagName('resource_ref'): if ord_set.getAttribute("id") == resource: resource_refs_to_remove.append(ord_set) elementFound = True for res_ref in resource_refs_to_remove: res_set = res_ref.parentNode res_order = res_set.parentNode res_ref.parentNode.removeChild(res_ref) if len(res_set.getElementsByTagName('resource_ref')) <= 0: res_set.parentNode.removeChild(res_set) if len(res_order.getElementsByTagName('resource_set')) <= 0: res_order.parentNode.removeChild(res_order) if elementFound == True: utils.replace_cib_configuration(dom) else: utils.err("No matching resources found in ordering list")
def colocation_add(argv): if len(argv) < 2: usage.constraint() sys.exit(1) resource1 = argv.pop(0) resource2 = argv.pop(0) score, nv_pairs = parse_score_options(argv) (dom, constraintsElement) = getCurrentConstraints() cl_id = utils.find_unique_id( dom, "colocation-" + resource1 + "-" + resource2 + "-" + score) element = dom.createElement("rsc_colocation") element.setAttribute("id", cl_id) element.setAttribute("rsc", resource1) element.setAttribute("with-rsc", resource2) element.setAttribute("score", score) for nv_pair in nv_pairs: element.setAttribute(nv_pair[0], nv_pair[1]) constraintsElement.appendChild(element) xml_constraint_string = constraintsElement.toxml() args = ["cibadmin", "-c", "-R", "--xml-text", xml_constraint_string] output, retval = utils.run(args) if output != "": print output
def location_rule(argv): if len(argv) < 3: usage.constraint("location rule") sys.exit(1) res_name = argv.pop(0) resource_valid, resource_error = utils.validate_constraint_resource( utils.get_cib_dom(), res_name ) if not resource_valid: utils.err(resource_error) argv.pop(0) cib = utils.get_cib_dom() constraints = cib.getElementsByTagName("constraints")[0] lc = cib.createElement("rsc_location") constraints.appendChild(lc) lc_id = utils.find_unique_id(cib, "location-" + res_name) lc.setAttribute("id", lc_id) lc.setAttribute("rsc", res_name) rule_utils.dom_rule_add(lc, argv) utils.replace_cib_configuration(cib)
def location_prefer(argv): rsc = argv.pop(0) prefer_option = argv.pop(0) if prefer_option == "prefers": prefer = True elif prefer_option == "avoids": prefer = False else: usage.constraint() sys.exit(1) for nodeconf in argv: nodeconf_a = nodeconf.split("=",1) if len(nodeconf_a) == 1: node = nodeconf_a[0] if prefer: score = "INFINITY" else: score = "-INFINITY" else: score = nodeconf_a[1] if not utils.is_score(score): utils.err("invalid score '%s', use integer or INFINITY or -INFINITY" % score) if not prefer: if score[0] == "-": score = score[1:] else: score = "-" + score node = nodeconf_a[0] location_add(["location-" +rsc+"-"+node+"-"+score,rsc,node,score])
def colocation_rm(argv): elementFound = False if len(argv) < 2: usage.constraint() sys.exit(1) (dom, constraintsElement) = getCurrentConstraints() resource1 = argv[0] resource2 = argv[1] for co_loc in constraintsElement.getElementsByTagName('rsc_colocation')[:]: if co_loc.getAttribute("rsc") == resource1 and co_loc.getAttribute( "with-rsc") == resource2: constraintsElement.removeChild(co_loc) elementFound = True if co_loc.getAttribute("rsc") == resource2 and co_loc.getAttribute( "with-rsc") == resource1: constraintsElement.removeChild(co_loc) elementFound = True if elementFound == True: utils.replace_cib_configuration(dom) else: print("No matching resources found in ordering list")
def constraint_rm(argv): if len(argv) < 1: usage.constraint() sys.exit(1) if len(argv) != 1: for arg in argv: constraint_rm([arg]) return else: c_id = argv.pop(0) elementFound = False (dom,constraintsElement) = getCurrentConstraints() for co in constraintsElement.childNodes[:]: if co.nodeType != xml.dom.Node.ELEMENT_NODE: continue if co.getAttribute("id") == c_id: constraintsElement.removeChild(co) elementFound = True if elementFound == True: xml_constraint_string = constraintsElement.toxml() args = ["cibadmin", "-c", "-R", "--xml-text", xml_constraint_string] output,retval = utils.run(args) if output != "": print output else: print "No matching resources found in ordering list"
def constraint_rule(argv): if len(argv) < 2: usage.constraint("rule") sys.exit(1) found = False command = argv.pop(0) constraint_id = None rule_id = None cib = utils.get_cib_etree() if command == "add": constraint_id = argv.pop(0) constraint = None for a in cib.findall(".//configuration//*"): if a.get("id") == constraint_id and a.tag == "rsc_location": found = True constraint = a if not found: utils.err("Unable to find constraint: " + constraint_id) utils.rule_add(constraint, argv) utils.replace_cib_configuration(cib) elif command in ["remove","delete"]: temp_id = argv.pop(0) constraints = cib.find('.//constraints') loc_cons = cib.findall('.//rsc_location') rules = cib.findall('.//rule') for loc_con in loc_cons: for rule in loc_con: if rule.get("id") == temp_id: if len(loc_con) > 1: print "Removing Rule:",rule.get("id") loc_con.remove(rule) found = True break else: print "Removing Constraint:",loc_con.get("id") constraints.remove(loc_con) found = True break if found == True: break if found: utils.replace_cib_configuration(cib) else: utils.err("unable to find rule with id: %s" % temp_id) else: usage.constraint("rule") sys.exit(1)
def constraint_rm(argv, returnStatus=False, constraintsElement=None, passed_dom=None): if len(argv) < 1: usage.constraint() sys.exit(1) bad_constraint = False if len(argv) != 1: for arg in argv: if not constraint_rm([arg], True, passed_dom=passed_dom): bad_constraint = True if bad_constraint: sys.exit(1) return else: c_id = argv.pop(0) elementFound = False if not constraintsElement: (dom, constraintsElement) = getCurrentConstraints(passed_dom) use_cibadmin = True else: use_cibadmin = False for co in constraintsElement.childNodes[:]: if co.nodeType != xml.dom.Node.ELEMENT_NODE: continue if co.getAttribute("id") == c_id: constraintsElement.removeChild(co) elementFound = True if not elementFound: for rule in constraintsElement.getElementsByTagName("rule")[:]: if rule.getAttribute("id") == c_id: elementFound = True parent = rule.parentNode parent.removeChild(rule) if len(parent.getElementsByTagName("rule")) == 0: parent.parentNode.removeChild(parent) if elementFound == True: if passed_dom: return dom if use_cibadmin: utils.replace_cib_configuration(dom) if returnStatus: return True else: print("Error: Unable to find constraint - '%s'" % c_id, file=sys.stderr) if returnStatus: return False sys.exit(1)
def location_add(argv, rm=False): if len(argv) != 4 and (rm == False or len(argv) < 1): usage.constraint() sys.exit(1) constraint_id = argv.pop(0) # If we're removing, we only care about the id if (rm == True): resource_name = "" node = "" score = "" else: id_valid, id_error = utils.validate_xml_id(constraint_id, 'constraint id') if not id_valid: utils.err(id_error) resource_name = argv.pop(0) node = argv.pop(0) score = argv.pop(0) resource_valid, resource_error = utils.validate_constraint_resource( utils.get_cib_dom(), resource_name) if not resource_valid: utils.err(resource_error) if not utils.is_score(score): utils.err( "invalid score '%s', use integer or INFINITY or -INFINITY" % score) # Verify current constraint doesn't already exist # If it does we replace it with the new constraint (dom, constraintsElement) = getCurrentConstraints() elementsToRemove = [] # If the id matches, or the rsc & node match, then we replace/remove for rsc_loc in constraintsElement.getElementsByTagName('rsc_location'): if (constraint_id == rsc_loc.getAttribute("id")) or \ (rsc_loc.getAttribute("rsc") == resource_name and \ rsc_loc.getAttribute("node") == node and not rm): elementsToRemove.append(rsc_loc) for etr in elementsToRemove: constraintsElement.removeChild(etr) if (rm == True and len(elementsToRemove) == 0): utils.err("resource location id: " + constraint_id + " not found.") if (not rm): element = dom.createElement("rsc_location") element.setAttribute("id", constraint_id) element.setAttribute("rsc", resource_name) element.setAttribute("node", node) element.setAttribute("score", score) constraintsElement.appendChild(element) utils.replace_cib_configuration(dom)
def constraint_rule(argv): if len(argv) < 2: usage.constraint("rule") sys.exit(1) found = False command = argv.pop(0) constraint_id = None rule_id = None if command == "add": constraint_id = argv.pop(0) cib = utils.get_cib_dom() constraint = utils.dom_get_element_with_id( cib.getElementsByTagName("constraints")[0], "rsc_location", constraint_id) if not constraint: utils.err("Unable to find constraint: " + constraint_id) options, rule_argv = rule_utils.parse_argv(argv) rule_utils.dom_rule_add(constraint, options, rule_argv) location_rule_check_duplicates(cib, constraint) utils.replace_cib_configuration(cib) elif command in ["remove", "delete"]: cib = utils.get_cib_etree() temp_id = argv.pop(0) constraints = cib.find('.//constraints') loc_cons = cib.findall('.//rsc_location') rules = cib.findall('.//rule') for loc_con in loc_cons: for rule in loc_con: if rule.get("id") == temp_id: if len(loc_con) > 1: print("Removing Rule:", rule.get("id")) loc_con.remove(rule) found = True break else: print("Removing Constraint:", loc_con.get("id")) constraints.remove(loc_con) found = True break if found == True: break if found: utils.replace_cib_configuration(cib) else: utils.err("unable to find rule with id: %s" % temp_id) else: usage.constraint("rule") sys.exit(1)
def location_add(argv,rm=False): if len(argv) != 4 and (rm == False or len(argv) < 1): usage.constraint() sys.exit(1) constraint_id = argv.pop(0) # If we're removing, we only care about the id if (rm == True): resource_name = "" node = "" score = "" else: id_valid, id_error = utils.validate_xml_id(constraint_id, 'constraint id') if not id_valid: utils.err(id_error) resource_name = argv.pop(0) node = argv.pop(0) score = argv.pop(0) resource_valid, resource_error = utils.validate_constraint_resource( utils.get_cib_dom(), resource_name ) if not resource_valid: utils.err(resource_error) if not utils.is_score(score): utils.err("invalid score '%s', use integer or INFINITY or -INFINITY" % score) # Verify current constraint doesn't already exist # If it does we replace it with the new constraint (dom,constraintsElement) = getCurrentConstraints() elementsToRemove = [] # If the id matches, or the rsc & node match, then we replace/remove for rsc_loc in constraintsElement.getElementsByTagName('rsc_location'): if (constraint_id == rsc_loc.getAttribute("id")) or \ (rsc_loc.getAttribute("rsc") == resource_name and \ rsc_loc.getAttribute("node") == node and not rm): elementsToRemove.append(rsc_loc) for etr in elementsToRemove: constraintsElement.removeChild(etr) if (rm == True and len(elementsToRemove) == 0): utils.err("resource location id: " + constraint_id + " not found.") if (not rm): element = dom.createElement("rsc_location") element.setAttribute("id",constraint_id) element.setAttribute("rsc",resource_name) element.setAttribute("node",node) element.setAttribute("score",score) constraintsElement.appendChild(element) utils.replace_cib_configuration(dom)
def location_add(argv,rm=False): if len(argv) != 4 and (rm == False or len(argv) < 1): usage.constraint() sys.exit(1) constraint_id = argv.pop(0) # If we're removing, we only care about the id if (rm == True): resource_name = "" node = "" score = "" else: resource_name = argv.pop(0) node = argv.pop(0) score = argv.pop(0) # If resource doesn't exist, we error out if not utils.does_resource_exist(resource_name): print "Error: Resource " + resource_name + "' does not exist" sys.exit(1) # Verify current constraint doesn't already exist # If it does we replace it with the new constraint (dom,constraintsElement) = getCurrentConstraints() elementsToRemove = [] # If the id matches, or the rsc & node match, then we replace/remove for rsc_loc in constraintsElement.getElementsByTagName('rsc_location'): if (constraint_id == rsc_loc.getAttribute("id")) or \ (rsc_loc.getAttribute("rsc") == resource_name and \ rsc_loc.getAttribute("node") == node and not rm): elementsToRemove.append(rsc_loc) for etr in elementsToRemove: constraintsElement.removeChild(etr) if (rm == True and len(elementsToRemove) == 0): print "Resource location id: " + constraint_id + " not found." sys.exit(1) if (not rm): element = dom.createElement("rsc_location") element.setAttribute("id",constraint_id) element.setAttribute("rsc",resource_name) element.setAttribute("node",node) element.setAttribute("score",score) constraintsElement.appendChild(element) xml_constraint_string = constraintsElement.toxml() args = ["cibadmin", "-c", "-R", "--xml-text", xml_constraint_string] output,retval = utils.run(args) if output != "": print output
def constraint_rm(argv,returnStatus=False, constraintsElement=None, passed_dom=None): if len(argv) < 1: usage.constraint() sys.exit(1) bad_constraint = False if len(argv) != 1: for arg in argv: if not constraint_rm([arg],True, passed_dom=passed_dom): bad_constraint = True if bad_constraint: sys.exit(1) return else: c_id = argv.pop(0) elementFound = False if not constraintsElement: (dom, constraintsElement) = getCurrentConstraints(passed_dom) use_cibadmin = True else: use_cibadmin = False for co in constraintsElement.childNodes[:]: if co.nodeType != xml.dom.Node.ELEMENT_NODE: continue if co.getAttribute("id") == c_id: constraintsElement.removeChild(co) elementFound = True if not elementFound: for rule in constraintsElement.getElementsByTagName("rule")[:]: if rule.getAttribute("id") == c_id: elementFound = True parent = rule.parentNode parent.removeChild(rule) if len(parent.getElementsByTagName("rule")) == 0: parent.parentNode.removeChild(parent) if elementFound == True: if passed_dom: return dom if use_cibadmin: utils.replace_cib_configuration(dom) if returnStatus: return True else: print >> sys.stderr, "Error: Unable to find constraint - '%s'" % c_id if returnStatus: return False sys.exit(1)
def constraint_ref(argv): if len(argv) == 0: usage.constraint() sys.exit(1) for arg in argv: print "Resource: %s" % arg constraints = find_constraints_containing(arg) if len(constraints) == 0: print " No Matches." else: for constraint in constraints: print " " + constraint
def location_rule(argv): if len(argv) < 3: usage.constraint(["location", "rule"]) sys.exit(1) res_name = argv.pop(0) resource_valid, resource_error, correct_id \ = utils.validate_constraint_resource(utils.get_cib_dom(), res_name) if "--autocorrect" in utils.pcs_options and correct_id: res_name = correct_id elif not resource_valid: utils.err(resource_error) argv.pop(0) # pop "rule" options, rule_argv = rule_utils.parse_argv(argv, { "constraint-id": None, "resource-discovery": None, }) # If resource-discovery is specified, we use it with the rsc_location # element not the rule if "resource-discovery" in options and options["resource-discovery"]: utils.checkAndUpgradeCIB(2, 2, 0) cib, constraints = getCurrentConstraints(utils.get_cib_dom()) lc = cib.createElement("rsc_location") lc.setAttribute("resource-discovery", options.pop("resource-discovery")) else: cib, constraints = getCurrentConstraints(utils.get_cib_dom()) lc = cib.createElement("rsc_location") constraints.appendChild(lc) if options.get("constraint-id"): id_valid, id_error = utils.validate_xml_id(options["constraint-id"], 'constraint id') if not id_valid: utils.err(id_error) if utils.does_id_exist(cib, options["constraint-id"]): utils.err("id '%s' is already in use, please specify another one" % options["constraint-id"]) lc.setAttribute("id", options["constraint-id"]) del options["constraint-id"] else: lc.setAttribute("id", utils.find_unique_id(cib, "location-" + res_name)) lc.setAttribute("rsc", res_name) rule_utils.dom_rule_add(lc, options, rule_argv) location_rule_check_duplicates(constraints, lc) utils.replace_cib_configuration(cib)
def location_add(argv, rm=False): if len(argv) != 4 and (rm == False or len(argv) < 1): usage.constraint() sys.exit(1) constraint_id = argv.pop(0) # If we're removing, we only care about the id if (rm == True): resource_name = "" node = "" score = "" else: resource_name = argv.pop(0) node = argv.pop(0) score = argv.pop(0) # If resource doesn't exist, we error out if not utils.is_valid_constraint_resource(resource_name): utils.err("Resource " + resource_name + "' does not exist") # Verify current constraint doesn't already exist # If it does we replace it with the new constraint (dom, constraintsElement) = getCurrentConstraints() elementsToRemove = [] # If the id matches, or the rsc & node match, then we replace/remove for rsc_loc in constraintsElement.getElementsByTagName('rsc_location'): if (constraint_id == rsc_loc.getAttribute("id")) or \ (rsc_loc.getAttribute("rsc") == resource_name and \ rsc_loc.getAttribute("node") == node and not rm): elementsToRemove.append(rsc_loc) for etr in elementsToRemove: constraintsElement.removeChild(etr) if (rm == True and len(elementsToRemove) == 0): utils.err("resource location id: " + constraint_id + " not found.") if (not rm): element = dom.createElement("rsc_location") element.setAttribute("id", constraint_id) element.setAttribute("rsc", resource_name) element.setAttribute("node", node) element.setAttribute("score", score) constraintsElement.appendChild(element) xml_constraint_string = constraintsElement.toxml() args = ["cibadmin", "-c", "-R", "--xml-text", xml_constraint_string] output, retval = utils.run(args) if output != "": print output
def constraint_rm(argv,returnStatus=False, constraintsElement=None): if len(argv) < 1: usage.constraint() sys.exit(1) bad_constraint = False if len(argv) != 1: for arg in argv: if not constraint_rm([arg],True): bad_constraint = True if bad_constraint: sys.exit(1) return else: c_id = argv.pop(0) elementFound = False if not constraintsElement: (dom, constraintsElement) = getCurrentConstraints() use_cibadmin = True else: use_cibadmin = False for co in constraintsElement.childNodes[:]: if co.nodeType != xml.dom.Node.ELEMENT_NODE: continue if co.getAttribute("id") == c_id: constraintsElement.removeChild(co) elementFound = True if not elementFound: for rule in constraintsElement.getElementsByTagName("rule")[:]: if rule.getAttribute("id") == c_id: elementFound = True parent = rule.parentNode parent.removeChild(rule) if len(parent.getElementsByTagName("rule")) == 0: parent.parentNode.removeChild(parent) if elementFound == True: if use_cibadmin: xml_constraint_string = constraintsElement.toxml() args = ["cibadmin", "-c", "-R", "--xml-text", xml_constraint_string] output,retval = utils.run(args) if output != "": print output else: print >> sys.stderr, "Error: Unable to find constraint - '%s'" % c_id if not returnStatus: sys.exit(1)
def location_rule(argv): if len(argv) < 3: usage.constraint(["location", "rule"]) sys.exit(1) res_name = argv.pop(0) resource_valid, resource_error, correct_id \ = utils.validate_constraint_resource(utils.get_cib_dom(), res_name) if "--autocorrect" in utils.pcs_options and correct_id: res_name = correct_id elif not resource_valid: utils.err(resource_error) argv.pop(0) # pop "rule" options, rule_argv = rule_utils.parse_argv(argv, {"constraint-id": None, "resource-discovery": None,}) # If resource-discovery is specified, we use it with the rsc_location # element not the rule if "resource-discovery" in options and options["resource-discovery"]: utils.checkAndUpgradeCIB(2,2,0) cib, constraints = getCurrentConstraints(utils.get_cib_dom()) lc = cib.createElement("rsc_location") lc.setAttribute("resource-discovery", options.pop("resource-discovery")) else: cib, constraints = getCurrentConstraints(utils.get_cib_dom()) lc = cib.createElement("rsc_location") constraints.appendChild(lc) if options.get("constraint-id"): id_valid, id_error = utils.validate_xml_id( options["constraint-id"], 'constraint id' ) if not id_valid: utils.err(id_error) if utils.does_id_exist(cib, options["constraint-id"]): utils.err( "id '%s' is already in use, please specify another one" % options["constraint-id"] ) lc.setAttribute("id", options["constraint-id"]) del options["constraint-id"] else: lc.setAttribute("id", utils.find_unique_id(cib, "location-" + res_name)) lc.setAttribute("rsc", res_name) rule_utils.dom_rule_add(lc, options, rule_argv) location_rule_check_duplicates(constraints, lc) utils.replace_cib_configuration(cib)
def constraint_ref(argv): if len(argv) == 0: usage.constraint() sys.exit(1) for arg in argv: print("Resource: %s" % arg) constraints,set_constraints = find_constraints_containing(arg) if len(constraints) == 0 and len(set_constraints) == 0: print(" No Matches.") else: for constraint in constraints: print(" " + constraint) for constraint in set_constraints: print(" " + constraint)
def constraint_ref(argv): if len(argv) == 0: usage.constraint() sys.exit(1) for arg in argv: print("Resource: %s" % arg) constraints, set_constraints = find_constraints_containing(arg) if len(constraints) == 0 and len(set_constraints) == 0: print(" No Matches.") else: for constraint in constraints: print(" " + constraint) for constraint in set_constraints: print(" " + constraint)
def location_rule(argv): if len(argv) < 3: usage.constraint("location rule") sys.exit(1) res_name = argv.pop(0) argv.pop(0) cib = utils.get_cib_etree() constraints = cib.find(".//constraints") lc = ET.SubElement(constraints,"rsc_location") lc_id = utils.find_unique_id(cib, "location-" + res_name) lc.set("id", lc_id) lc.set("rsc", res_name) utils.rule_add(lc, argv) utils.replace_cib_configuration(cib)
def location_rule(argv): if len(argv) < 3: usage.constraint("location rule") sys.exit(1) res_name = argv.pop(0) argv.pop(0) cib = utils.get_cib_etree() constraints = cib.find(".//constraints") lc = ET.SubElement(constraints, "rsc_location") lc_id = utils.find_unique_id(cib, "location-" + res_name) lc.set("id", lc_id) lc.set("rsc", res_name) utils.rule_add(lc, argv) utils.replace_cib_configuration(cib)
def constraint_rm(argv, returnStatus=False, constraintsElement=None): if len(argv) < 1: usage.constraint() sys.exit(1) bad_constraint = False if len(argv) != 1: for arg in argv: if not constraint_rm([arg], True): bad_constraint = True if bad_constraint: sys.exit(1) return else: c_id = argv.pop(0) elementFound = False if not constraintsElement: (dom, constraintsElement) = getCurrentConstraints() use_cibadmin = True else: use_cibadmin = False for co in constraintsElement.childNodes[:]: if co.nodeType != xml.dom.Node.ELEMENT_NODE: continue if co.getAttribute("id") == c_id: constraintsElement.removeChild(co) elementFound = True if elementFound == True: if use_cibadmin: xml_constraint_string = constraintsElement.toxml() args = [ "cibadmin", "-c", "-R", "--xml-text", xml_constraint_string ] output, retval = utils.run(args) if output != "": print output else: print >> sys.stderr, "Error: Unable to find constraint - '%s'" % c_id if not returnStatus: sys.exit(1)
def location_rule(argv): if len(argv) < 3: usage.constraint("location rule") sys.exit(1) res_name = argv.pop(0) (dom, constraintsElement) = getCurrentConstraints() lc = dom.createElement("rsc_location") lc_id = utils.find_unique_id(dom, "location-" + res_name) lc.setAttribute("id", lc_id) lc.setAttribute("rsc", res_name) rule = utils.getRule(dom, lc, argv) lc.appendChild(rule) constraintsElement.appendChild(lc) utils.replace_cib_configuration(dom)
def order_rm(argv): if len(argv) == 0: usage.constraint() sys.exit(1) elementFound = False (dom,constraintsElement) = getCurrentConstraints() for resource in argv: for ord_loc in constraintsElement.getElementsByTagName('rsc_order')[:]: if ord_loc.getAttribute("first") == resource or ord_loc.getAttribute("then") == resource: constraintsElement.removeChild(ord_loc) elementFound = True if elementFound == True: xml_constraint_string = constraintsElement.toxml() args = ["cibadmin", "-c", "-R", "--xml-text", xml_constraint_string] output,retval = utils.run(args) if output != "": print output else: print "No matching resources found in ordering list"
def colocation_add(argv): if len(argv) < 2: usage.constraint() sys.exit(1) resource1 = argv.pop(0) resource2 = argv.pop(0) score = "INFINITY" if (len(argv) == 0) else argv.pop(0) cl_id = "colocation-" + resource1 + "-" + resource2 + "-" + score (dom,constraintsElement) = getCurrentConstraints() element = dom.createElement("rsc_colocation") element.setAttribute("id",cl_id) element.setAttribute("rsc",resource1) element.setAttribute("with-rsc",resource2) element.setAttribute("score",score) constraintsElement.appendChild(element) xml_constraint_string = constraintsElement.toxml() args = ["cibadmin", "-c", "-R", "--xml-text", xml_constraint_string] output,retval = utils.run(args) if output != "": print output
def order_rm(argv): if len(argv) == 0: usage.constraint() sys.exit(1) elementFound = False (dom,constraintsElement) = getCurrentConstraints() for resource in argv: for ord_loc in constraintsElement.getElementsByTagName('rsc_order')[:]: if ord_loc.getAttribute("first") == resource or ord_loc.getAttribute("then") == resource: constraintsElement.removeChild(ord_loc) elementFound = True resource_refs_to_remove = [] for ord_set in constraintsElement.getElementsByTagName('resource_ref'): if ord_set.getAttribute("id") == resource: resource_refs_to_remove.append(ord_set) elementFound = True for res_ref in resource_refs_to_remove: res_set = res_ref.parentNode res_order = res_set.parentNode res_ref.parentNode.removeChild(res_ref) if len(res_set.getElementsByTagName('resource_ref')) <= 0: res_set.parentNode.removeChild(res_set) if len(res_order.getElementsByTagName('resource_set')) <= 0: res_order.parentNode.removeChild(res_order) if elementFound == True: xml_constraint_string = constraintsElement.toxml() args = ["cibadmin", "-c", "-R", "--xml-text", xml_constraint_string] output,retval = utils.run(args) if output != "": print output else: utils.err("No matching resources found in ordering list")
def location_force(argv, remove=False): if len(argv) != 2 and len(argv) != 3: usage.constraint() sys.exit(1) on = True rsc = argv[0] if len(argv) == 2: node = argv[1] else: if (argv[1] != "on") and (argv[1] != "off"): usage.constraint() sys.exit(1) if (argv[1] == "off"): on = False node = argv[2] loc_id = "loc_" + node + "_" + rsc if (on == True): location_add([loc_id,rsc,node,"INFINITY"],remove) else: location_add([loc_id,rsc,node,"-INFINITY"],remove)
def colocation_rm(argv): elementFound = False if len(argv) < 2: usage.constraint() sys.exit(1) (dom,constraintsElement) = getCurrentConstraints() resource1 = argv[0] resource2 = argv[1] for co_loc in constraintsElement.getElementsByTagName('rsc_colocation')[:]: if co_loc.getAttribute("rsc") == resource1 and co_loc.getAttribute("with-rsc") == resource2: constraintsElement.removeChild(co_loc) elementFound = True if co_loc.getAttribute("rsc") == resource2 and co_loc.getAttribute("with-rsc") == resource1: constraintsElement.removeChild(co_loc) elementFound = True if elementFound == True: utils.replace_cib_configuration(dom) else: print "No matching resources found in ordering list"
def order_rm(argv): if len(argv) == 0: usage.constraint() sys.exit(1) elementFound = False (dom, constraintsElement) = getCurrentConstraints() for resource in argv: for ord_loc in constraintsElement.getElementsByTagName('rsc_order')[:]: if ord_loc.getAttribute( "first") == resource or ord_loc.getAttribute( "then") == resource: constraintsElement.removeChild(ord_loc) elementFound = True if elementFound == True: xml_constraint_string = constraintsElement.toxml() args = ["cibadmin", "-c", "-R", "--xml-text", xml_constraint_string] output, retval = utils.run(args) if output != "": print output else: print "No matching resources found in ordering list"
def location_rule(argv): if len(argv) < 3: usage.constraint("location rule") sys.exit(1) res_name = argv.pop(0) resource_valid, resource_error = utils.validate_constraint_resource( utils.get_cib_dom(), res_name) if not resource_valid: utils.err(resource_error) argv.pop(0) cib = utils.get_cib_dom() constraints = cib.getElementsByTagName("constraints")[0] lc = cib.createElement("rsc_location") constraints.appendChild(lc) lc_id = utils.find_unique_id(cib, "location-" + res_name) lc.setAttribute("id", lc_id) lc.setAttribute("rsc", res_name) rule_utils.dom_rule_add(lc, argv) utils.replace_cib_configuration(cib)
def order_start(argv): if len(argv) < 3: usage.constraint() sys.exit(1) first_action = "start" then_action = "start" action = argv[0] if action == "start" or action == "promote" or action == "stop" or action == "demote": first_action = action argv.pop(0) resource1 = argv.pop(0) if argv.pop(0) != "then": usage.constraint() sys.exit(1) if len(argv) == 0: usage.constraint() sys.exit(1) action = argv[0] if action == "start" or action == "promote" or action == "stop" or action == "demote": then_action = action argv.pop(0) if len(argv) == 0: usage.constraint() sys.exit(1) resource2 = argv.pop(0) if utils.is_resource_masterslave(resource1): utils.err(resource1 + " is a master/slave resource, you must use the master id: "+utils.get_resource_master_id(resource1)+ " when adding constraints") if utils.is_resource_masterslave(resource2): utils.err(resource2 + " is a master/slave resource, you must use the master id: "+utils.get_resource_master_id(resource2)+ " when adding constraints") order_options = [] if len(argv) != 0: order_options = order_options + argv[:] order_options.append("first-action="+first_action) order_options.append("then-action="+then_action) order_add([resource1, resource2] + order_options)
def order_start(argv): if len(argv) < 3: usage.constraint() sys.exit(1) first_action = DEFAULT_ACTION then_action = DEFAULT_ACTION action = argv[0] if action in OPTIONS_ACTION: first_action = action argv.pop(0) resource1 = argv.pop(0) if argv.pop(0) != "then": usage.constraint() sys.exit(1) if len(argv) == 0: usage.constraint() sys.exit(1) action = argv[0] if action in OPTIONS_ACTION: then_action = action argv.pop(0) if len(argv) == 0: usage.constraint() sys.exit(1) resource2 = argv.pop(0) order_options = [] if len(argv) != 0: order_options = order_options + argv[:] order_options.append("first-action=" + first_action) order_options.append("then-action=" + then_action) order_add([resource1, resource2] + order_options)
def order_start(argv): if len(argv) < 3: usage.constraint() sys.exit(1) first_action = "start" then_action = "start" action = argv[0] if action == "start" or action == "promote" or action == "stop" or action == "demote": first_action = action argv.pop(0) resource1 = argv.pop(0) if argv.pop(0) != "then": usage.constraint() sys.exit(1) if len(argv) == 0: usage.constraint() sys.exit(1) action = argv[0] if action == "start" or action == "promote" or action == "stop" or action == "demote": then_action = action argv.pop(0) if len(argv) == 0: usage.constraint() sys.exit(1) resource2 = argv.pop(0) order_options = [] if len(argv) != 0: order_options = order_options + argv[:] order_options.append("first-action="+first_action) order_options.append("then-action="+then_action) order_add([resource1, resource2] + order_options)
def order_start(argv): if len(argv) < 3: usage.constraint() sys.exit(1) first_action = DEFAULT_ACTION then_action = DEFAULT_ACTION action = argv[0] if action in OPTIONS_ACTION: first_action = action argv.pop(0) resource1 = argv.pop(0) if argv.pop(0) != "then": usage.constraint() sys.exit(1) if len(argv) == 0: usage.constraint() sys.exit(1) action = argv[0] if action in OPTIONS_ACTION: then_action = action argv.pop(0) if len(argv) == 0: usage.constraint() sys.exit(1) resource2 = argv.pop(0) order_options = [] if len(argv) != 0: order_options = order_options + argv[:] order_options.append("first-action="+first_action) order_options.append("then-action="+then_action) order_add([resource1, resource2] + order_options)
def order_start(argv): if len(argv) < 3: usage.constraint() sys.exit(1) first_action = "start" then_action = "start" action = argv[0] if action == "start" or action == "promote" or action == "stop" or action == "demote": first_action = action argv.pop(0) resource1 = argv.pop(0) if argv.pop(0) != "then": usage.constraint() sys.exit(1) if len(argv) == 0: usage.constraint() sys.exit(1) action = argv[0] if action == "start" or action == "promote" or action == "stop" or action == "demote": then_action = action argv.pop(0) if len(argv) == 0: usage.constraint() sys.exit(1) resource2 = argv.pop(0) order_options = [] if len(argv) != 0: order_options = order_options + argv[:] order_options.append("first-action=" + first_action) order_options.append("then-action=" + then_action) order_add([resource1, resource2] + order_options)
def order_add(argv,returnElementOnly=False): if len(argv) < 2: usage.constraint() sys.exit(1) resource1 = argv.pop(0) resource2 = argv.pop(0) if not utils.is_valid_constraint_resource(resource1): utils.err("Resource '" + resource1 + "' does not exist") if not utils.is_valid_constraint_resource(resource2): utils.err("Resource '" + resource2 + "' does not exist") sym = "true" if (len(argv) == 0 or argv[0] != "nonsymmetrical") else "false" order_options = [] if len(argv) != 0: if argv[0] == "nonsymmetrical" or argv[0] == "symmetrical": argv.pop(0) for arg in argv: if arg.count("=") == 1: mysplit = arg.split("=") order_options.append((mysplit[0],mysplit[1])) if len(argv) != 0: options = " (Options: " + " ".join(argv)+")" else: options = "" scorekind = "kind: Mandatory" id_suffix = "mandatory" for opt in order_options: if opt[0] == "score": scorekind = "score: " + opt[1] id_suffix = opt[1] break if opt[0] == "kind": scorekind = "kind: " + opt[1] id_suffix = opt[1] break print "Adding " + resource1 + " " + resource2 + " ("+scorekind+")" + options order_id = "order-" + resource1 + "-" + resource2 + "-" + id_suffix order_id = utils.find_unique_id(utils.get_cib_dom(), order_id) (dom,constraintsElement) = getCurrentConstraints() element = dom.createElement("rsc_order") element.setAttribute("id",order_id) element.setAttribute("first",resource1) element.setAttribute("then",resource2) for order_opt in order_options: element.setAttribute(order_opt[0], order_opt[1]) if (sym == "false"): element.setAttribute("symmetrical", "false") constraintsElement.appendChild(element) if returnElementOnly == False: utils.replace_cib_configuration(dom) else: return element.toxml()
def order_add(argv, returnElementOnly=False): if len(argv) < 2: usage.constraint() sys.exit(1) resource1 = argv.pop(0) resource2 = argv.pop(0) cib_dom = utils.get_cib_dom() resource_valid, resource_error, correct_id \ = utils.validate_constraint_resource(cib_dom, resource1) if "--autocorrect" in utils.pcs_options and correct_id: resource1 = correct_id elif not resource_valid: utils.err(resource_error) resource_valid, resource_error, correct_id \ = utils.validate_constraint_resource(cib_dom, resource2) if "--autocorrect" in utils.pcs_options and correct_id: resource2 = correct_id elif not resource_valid: utils.err(resource_error) order_options = [] id_specified = False sym = None for arg in argv: if arg == "symmetrical": sym = "true" elif arg == "nonsymmetrical": sym = "false" elif "=" in arg: name, value = arg.split("=", 1) if name == "id": id_valid, id_error = utils.validate_xml_id( value, 'constraint id') if not id_valid: utils.err(id_error) if utils.does_id_exist(cib_dom, value): utils.err( "id '%s' is already in use, please specify another one" % value) id_specified = True order_options.append((name, value)) elif name == "symmetrical": if value.lower() in OPTIONS_SYMMETRICAL: sym = value.lower() else: utils.err( "invalid symmetrical value '%s', allowed values are: %s" % (value, ", ".join(OPTIONS_SYMMETRICAL))) else: order_options.append((name, value)) if sym: order_options.append(("symmetrical", sym)) options = "" if order_options: options = " (Options: %s)" % " ".join([ "%s=%s" % (name, value) for name, value in order_options if name not in ("kind", "score") ]) scorekind = "kind: Mandatory" id_suffix = "mandatory" for opt in order_options: if opt[0] == "score": scorekind = "score: " + opt[1] id_suffix = opt[1] break if opt[0] == "kind": scorekind = "kind: " + opt[1] id_suffix = opt[1] break if not id_specified: order_id = "order-" + resource1 + "-" + resource2 + "-" + id_suffix order_id = utils.find_unique_id(cib_dom, order_id) order_options.append(("id", order_id)) (dom, constraintsElement) = getCurrentConstraints() element = dom.createElement("rsc_order") element.setAttribute("first", resource1) element.setAttribute("then", resource2) for order_opt in order_options: element.setAttribute(order_opt[0], order_opt[1]) constraintsElement.appendChild(element) if "--force" not in utils.pcs_options: duplicates = order_find_duplicates(constraintsElement, element) if duplicates: utils.err( "duplicate constraint already exists, use --force to override\n" + "\n".join([ " " + order_el_to_string(dup, True) for dup in duplicates ])) print("Adding " + resource1 + " " + resource2 + " (" + scorekind + ")" + options) if returnElementOnly == False: utils.replace_cib_configuration(dom) else: return element.toxml()
def location_add(argv,rm=False): if len(argv) < 4 and (rm == False or len(argv) < 1): usage.constraint() sys.exit(1) constraint_id = argv.pop(0) # If we're removing, we only care about the id if (rm == True): resource_name = "" node = "" score = "" else: id_valid, id_error = utils.validate_xml_id(constraint_id, 'constraint id') if not id_valid: utils.err(id_error) resource_name = argv.pop(0) node = argv.pop(0) score = argv.pop(0) options = [] # For now we only allow setting resource-discovery if len(argv) > 0: for arg in argv: if '=' in arg: options.append(arg.split('=',1)) else: print("Error: bad option '%s'" % arg) usage.constraint(["location add"]) sys.exit(1) if options[-1][0] != "resource-discovery" and "--force" not in utils.pcs_options: utils.err("bad option '%s', use --force to override" % options[-1][0]) resource_valid, resource_error, correct_id \ = utils.validate_constraint_resource( utils.get_cib_dom(), resource_name ) if "--autocorrect" in utils.pcs_options and correct_id: resource_name = correct_id elif not resource_valid: utils.err(resource_error) if not utils.is_score(score): utils.err("invalid score '%s', use integer or INFINITY or -INFINITY" % score) # Verify current constraint doesn't already exist # If it does we replace it with the new constraint (dom,constraintsElement) = getCurrentConstraints() elementsToRemove = [] # If the id matches, or the rsc & node match, then we replace/remove for rsc_loc in constraintsElement.getElementsByTagName('rsc_location'): if (constraint_id == rsc_loc.getAttribute("id")) or \ (rsc_loc.getAttribute("rsc") == resource_name and \ rsc_loc.getAttribute("node") == node and not rm): elementsToRemove.append(rsc_loc) for etr in elementsToRemove: constraintsElement.removeChild(etr) if (rm == True and len(elementsToRemove) == 0): utils.err("resource location id: " + constraint_id + " not found.") if (not rm): element = dom.createElement("rsc_location") element.setAttribute("id",constraint_id) element.setAttribute("rsc",resource_name) element.setAttribute("node",node) element.setAttribute("score",score) for option in options: element.setAttribute(option[0], option[1]) constraintsElement.appendChild(element) utils.replace_cib_configuration(dom)
def colocation_add(argv): if len(argv) < 2: usage.constraint() sys.exit(1) role1 = "" role2 = "" if len(argv) > 2: if not utils.is_score_or_opt(argv[2]): if argv[2] == "with": role1 = argv.pop(0).lower().capitalize() resource1 = argv.pop(0) else: resource1 = argv.pop(0) argv.pop(0) # Pop 'with' if len(argv) == 1: resource2 = argv.pop(0) else: if utils.is_score_or_opt(argv[1]): resource2 = argv.pop(0) else: role2 = argv.pop(0).lower().capitalize() resource2 = argv.pop(0) else: resource1 = argv.pop(0) resource2 = argv.pop(0) else: resource1 = argv.pop(0) resource2 = argv.pop(0) if not utils.is_valid_constraint_resource(resource1): utils.err("Resource '" + resource1 + "' does not exist") if not utils.is_valid_constraint_resource(resource2): utils.err("Resource '" + resource2 + "' does not exist") if utils.is_resource_masterslave(resource1): utils.err(resource1 + " is a master/slave resource, you must use the master id: "+utils.get_resource_master_id(resource1)+ " when adding constraints") if utils.is_resource_masterslave(resource2): utils.err(resource2 + " is a master/slave resource, you must use the master id: "+utils.get_resource_master_id(resource2)+ " when adding constraints") score,nv_pairs = parse_score_options(argv) (dom,constraintsElement) = getCurrentConstraints() cl_id = utils.find_unique_id(dom, "colocation-" + resource1 + "-" + resource2 + "-" + score) # If one role is specified, the other should default to "started" if role1 != "" and role2 == "": role2 = "Started" if role2 != "" and role1 == "": role1 = "Started" element = dom.createElement("rsc_colocation") element.setAttribute("id",cl_id) element.setAttribute("rsc",resource1) element.setAttribute("with-rsc",resource2) element.setAttribute("score",score) if role1 != "": element.setAttribute("rsc-role", role1) if role2 != "": element.setAttribute("with-rsc-role", role2) for nv_pair in nv_pairs: element.setAttribute(nv_pair[0], nv_pair[1]) constraintsElement.appendChild(element) utils.replace_cib_configuration(dom)
def order_add(argv, returnElementOnly=False): if len(argv) < 2: usage.constraint() sys.exit(1) resource1 = argv.pop(0) resource2 = argv.pop(0) if not utils.is_valid_constraint_resource(resource1): utils.err("Resource '" + resource1 + "' does not exist") if not utils.is_valid_constraint_resource(resource2): utils.err("Resource '" + resource2 + "' does not exist") sym = "true" if (len(argv) == 0 or argv[0] != "nonsymmetrical") else "false" order_options = [] if len(argv) != 0: if argv[0] == "nonsymmetrical" or argv[0] == "symmetrical": argv.pop(0) for arg in argv: if arg.count("=") == 1: mysplit = arg.split("=") order_options.append((mysplit[0], mysplit[1])) if len(argv) != 0: options = " (Options: " + " ".join(argv) + ")" else: options = "" scorekind = "kind: Mandatory" id_suffix = "mandatory" for opt in order_options: if opt[0] == "score": scorekind = "score: " + opt[1] id_suffix = opt[1] break if opt[0] == "kind": scorekind = "kind: " + opt[1] id_suffix = opt[1] break print "Adding " + resource1 + " " + resource2 + " (" + scorekind + ")" + options order_id = "order-" + resource1 + "-" + resource2 + "-" + id_suffix if utils.does_id_exist(utils.get_cib_dom(), order_id): utils.err( "Unable to create constraint, similar constraint already exists: %s" % order_id) (dom, constraintsElement) = getCurrentConstraints() element = dom.createElement("rsc_order") element.setAttribute("id", order_id) element.setAttribute("first", resource1) element.setAttribute("then", resource2) for order_opt in order_options: element.setAttribute(order_opt[0], order_opt[1]) if (sym == "false"): element.setAttribute("symmetrical", "false") constraintsElement.appendChild(element) if returnElementOnly == False: xml_constraint_string = constraintsElement.toxml() args = [ "cibadmin", "-o", "constraints", "-R", "--xml-text", xml_constraint_string ] output, retval = utils.run(args) if output != "": print output if retval != 0: sys.exit(1) else: return element.toxml()
def constraint_cmd(argv): if len(argv) == 0: argv = ["list"] sub_cmd = argv.pop(0) if (sub_cmd == "help"): usage.constraint(argv) elif (sub_cmd == "location"): if len (argv) == 0: sub_cmd2 = "show" else: sub_cmd2 = argv.pop(0) if (sub_cmd2 == "add"): location_add(argv) elif (sub_cmd2 in ["remove","delete"]): location_add(argv,True) elif (sub_cmd2 == "show"): location_show(argv) elif len(argv) >= 2: if argv[0] == "rule": location_rule([sub_cmd2] + argv) else: location_prefer([sub_cmd2] + argv) else: usage.constraint() sys.exit(1) elif (sub_cmd == "order"): if (len(argv) == 0): sub_cmd2 = "show" else: sub_cmd2 = argv.pop(0) if (sub_cmd2 == "set"): order_set(argv) elif (sub_cmd2 in ["remove","delete"]): order_rm(argv) elif (sub_cmd2 == "show"): order_show(argv) else: order_start([sub_cmd2] + argv) elif (sub_cmd == "colocation"): if (len(argv) == 0): sub_cmd2 = "show" else: sub_cmd2 = argv.pop(0) if (sub_cmd2 == "add"): colocation_add(argv) elif (sub_cmd2 in ["remove","delete"]): colocation_rm(argv) elif (sub_cmd2 == "set"): colocation_set(argv) elif (sub_cmd2 == "show"): colocation_show(argv) else: usage.constraint() sys.exit(1) elif (sub_cmd in ["remove","delete"]): constraint_rm(argv) elif (sub_cmd == "show" or sub_cmd == "list"): location_show(argv) order_show(argv) colocation_show(argv) elif (sub_cmd == "ref"): constraint_ref(argv) elif (sub_cmd == "rule"): constraint_rule(argv) else: usage.constraint() sys.exit(1)
def colocation_add(argv): if len(argv) < 2: usage.constraint() sys.exit(1) role1 = "" role2 = "" if len(argv) > 2: if not utils.is_score_or_opt(argv[2]): if argv[2] == "with": role1 = argv.pop(0).lower().capitalize() resource1 = argv.pop(0) else: resource1 = argv.pop(0) argv.pop(0) # Pop 'with' if len(argv) == 1: resource2 = argv.pop(0) else: if utils.is_score_or_opt(argv[1]): resource2 = argv.pop(0) else: role2 = argv.pop(0).lower().capitalize() resource2 = argv.pop(0) else: resource1 = argv.pop(0) resource2 = argv.pop(0) else: resource1 = argv.pop(0) resource2 = argv.pop(0) if not utils.is_valid_constraint_resource(resource1): utils.err("Resource '" + resource1 + "' does not exist") if not utils.is_valid_constraint_resource(resource2): utils.err("Resource '" + resource2 + "' does not exist") score, nv_pairs = parse_score_options(argv) (dom, constraintsElement) = getCurrentConstraints() cl_id = utils.find_unique_id( dom, "colocation-" + resource1 + "-" + resource2 + "-" + score) # If one role is specified, the other should default to "started" if role1 != "" and role2 == "": role2 = "Started" if role2 != "" and role1 == "": role1 = "Started" element = dom.createElement("rsc_colocation") element.setAttribute("id", cl_id) element.setAttribute("rsc", resource1) element.setAttribute("with-rsc", resource2) element.setAttribute("score", score) if role1 != "": element.setAttribute("rsc-role", role1) if role2 != "": element.setAttribute("with-rsc-role", role2) for nv_pair in nv_pairs: element.setAttribute(nv_pair[0], nv_pair[1]) constraintsElement.appendChild(element) xml_constraint_string = constraintsElement.toxml() args = ["cibadmin", "-c", "-R", "--xml-text", xml_constraint_string] output, retval = utils.run(args) if output != "": print output
def location_add(argv, rm=False): if len(argv) < 4 and (rm == False or len(argv) < 1): usage.constraint() sys.exit(1) constraint_id = argv.pop(0) # If we're removing, we only care about the id if (rm == True): resource_name = "" node = "" score = "" else: id_valid, id_error = utils.validate_xml_id(constraint_id, 'constraint id') if not id_valid: utils.err(id_error) resource_name = argv.pop(0) node = argv.pop(0) score = argv.pop(0) options = [] # For now we only allow setting resource-discovery if len(argv) > 0: for arg in argv: if '=' in arg: options.append(arg.split('=', 1)) else: print("Error: bad option '%s'" % arg) usage.constraint(["location add"]) sys.exit(1) if options[-1][ 0] != "resource-discovery" and "--force" not in utils.pcs_options: utils.err("bad option '%s', use --force to override" % options[-1][0]) resource_valid, resource_error, correct_id \ = utils.validate_constraint_resource( utils.get_cib_dom(), resource_name ) if "--autocorrect" in utils.pcs_options and correct_id: resource_name = correct_id elif not resource_valid: utils.err(resource_error) if not utils.is_score(score): utils.err( "invalid score '%s', use integer or INFINITY or -INFINITY" % score) # Verify current constraint doesn't already exist # If it does we replace it with the new constraint (dom, constraintsElement) = getCurrentConstraints() elementsToRemove = [] # If the id matches, or the rsc & node match, then we replace/remove for rsc_loc in constraintsElement.getElementsByTagName('rsc_location'): if (constraint_id == rsc_loc.getAttribute("id")) or \ (rsc_loc.getAttribute("rsc") == resource_name and \ rsc_loc.getAttribute("node") == node and not rm): elementsToRemove.append(rsc_loc) for etr in elementsToRemove: constraintsElement.removeChild(etr) if (rm == True and len(elementsToRemove) == 0): utils.err("resource location id: " + constraint_id + " not found.") if (not rm): element = dom.createElement("rsc_location") element.setAttribute("id", constraint_id) element.setAttribute("rsc", resource_name) element.setAttribute("node", node) element.setAttribute("score", score) for option in options: element.setAttribute(option[0], option[1]) constraintsElement.appendChild(element) utils.replace_cib_configuration(dom)