def set_add_resource_sets(elem, sets, cib): allowed_options = { "sequential": ("true", "false"), "require-all": ("true", "false"), "action": ("start", "promote", "demote", "stop"), "role": ("Stopped", "Started", "Master", "Slave"), } for o_set in sets: set_id = "pcs_rsc_set" res_set = cib.createElement("resource_set") elem.appendChild(res_set) for opts in o_set: if opts.find("=") != -1: key, val = opts.split("=") if key not in allowed_options: utils.err("invalid option '%s', allowed options are: %s" % (key, ", ".join(allowed_options.keys()))) if val not in allowed_options[key]: utils.err( "invalid value '%s' of option '%s', allowed values are: %s" % (val, key, ", ".join(allowed_options[key]))) res_set.setAttribute(key, val) else: res_valid, res_error = utils.validate_constraint_resource( cib, opts) if not res_valid: utils.err(res_error) se = cib.createElement("resource_ref") res_set.appendChild(se) se.setAttribute("id", opts) set_id = set_id + "_" + opts res_set.setAttribute("id", utils.find_unique_id(cib, set_id))
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_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: 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_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_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 set_add_resource_sets(elem, sets, cib): allowed_options = { "sequential": ("true", "false"), "require-all": ("true", "false"), "action" : OPTIONS_ACTION, "role" : OPTIONS_ROLE, } for o_set in sets: set_id = "pcs_rsc_set" res_set = cib.createElement("resource_set") elem.appendChild(res_set) for opts in o_set: if opts.find("=") != -1: key,val = opts.split("=") if key not in allowed_options: utils.err( "invalid option '%s', allowed options are: %s" % (key, ", ".join(allowed_options.keys())) ) if val not in allowed_options[key]: utils.err( "invalid value '%s' of option '%s', allowed values are: %s" % (val, key, ", ".join(allowed_options[key])) ) res_set.setAttribute(key, val) else: res_valid, res_error, correct_id \ = utils.validate_constraint_resource(cib, opts) if "--autocorrect" in utils.pcs_options and correct_id: opts = correct_id elif not res_valid: utils.err(res_error) se = cib.createElement("resource_ref") res_set.appendChild(se) se.setAttribute("id", opts) set_id = set_id + "_" + opts res_set.setAttribute("id", utils.find_unique_id(cib, set_id)) if "--force" not in utils.pcs_options: duplicates = set_constraint_find_duplicates(cib, elem) if duplicates: utils.err( "duplicate constraint already exists, use --force to override\n" + "\n".join([ " " + set_constraint_el_to_string(dup, True) for dup in duplicates ]) )
def set_add_resource_sets(elem, sets, cib): allowed_options = { "sequential": ("true", "false"), "require-all": ("true", "false"), "action": OPTIONS_ACTION, "role": OPTIONS_ROLE, } for o_set in sets: set_id = "pcs_rsc_set" res_set = cib.createElement("resource_set") elem.appendChild(res_set) for opts in o_set: if opts.find("=") != -1: key, val = opts.split("=") if key not in allowed_options: utils.err("invalid option '%s', allowed options are: %s" % (key, ", ".join(allowed_options.keys()))) if val not in allowed_options[key]: utils.err( "invalid value '%s' of option '%s', allowed values are: %s" % (val, key, ", ".join(allowed_options[key]))) res_set.setAttribute(key, val) else: res_valid, res_error, correct_id \ = utils.validate_constraint_resource(cib, opts) if "--autocorrect" in utils.pcs_options and correct_id: opts = correct_id elif not res_valid: utils.err(res_error) se = cib.createElement("resource_ref") res_set.appendChild(se) se.setAttribute("id", opts) set_id = set_id + "_" + opts res_set.setAttribute("id", utils.find_unique_id(cib, set_id)) if "--force" not in utils.pcs_options: duplicates = set_constraint_find_duplicates(cib, elem) if duplicates: utils.err( "duplicate constraint already exists, use --force to override\n" + "\n".join([ " " + set_constraint_el_to_string(dup, True) for dup in duplicates ]))
def set_add_resource_sets(elem, sets, cib): allowed_options = { "sequential": ("true", "false"), "require-all": ("true", "false"), "action" : ("start", "promote", "demote", "stop"), "role" : ("Stopped", "Started", "Master", "Slave"), } for o_set in sets: set_id = "pcs_rsc_set" res_set = cib.createElement("resource_set") elem.appendChild(res_set) for opts in o_set: if opts.find("=") != -1: key,val = opts.split("=") if key not in allowed_options: utils.err( "invalid option '%s', allowed options are: %s" % (key, ", ".join(allowed_options.keys())) ) if val not in allowed_options[key]: utils.err( "invalid value '%s' of option '%s', allowed values are: %s" % (val, key, ", ".join(allowed_options[key])) ) res_set.setAttribute(key, val) else: res_valid, res_error = utils.validate_constraint_resource( cib, opts ) if not res_valid: utils.err(res_error) se = cib.createElement("resource_ref") res_set.appendChild(se) se.setAttribute("id", opts) set_id = set_id + "_" + opts res_set.setAttribute("id", utils.find_unique_id(cib, set_id))
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 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) 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) score,nv_pairs = parse_score_options(argv) id_in_nvpairs = None for name, value in nv_pairs: 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_in_nvpairs = True if not id_in_nvpairs: nv_pairs.append(( "id", utils.find_unique_id( cib_dom, "colocation-%s-%s-%s" % (resource1, resource2, score) ) )) (dom,constraintsElement) = getCurrentConstraints(cib_dom) # If one role is specified, the other should default to "started" if role1 != "" and role2 == "": role2 = DEFAULT_ROLE if role2 != "" and role1 == "": role1 = DEFAULT_ROLE element = dom.createElement("rsc_colocation") 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]) if "--force" not in utils.pcs_options: duplicates = colocation_find_duplicates(constraintsElement, element) if duplicates: utils.err( "duplicate constraint already exists, use --force to override\n" + "\n".join([ " " + colocation_el_to_string(dup, True) for dup in duplicates ]) ) 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: 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 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 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) 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) score, nv_pairs = parse_score_options(argv) id_in_nvpairs = None for name, value in nv_pairs: 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_in_nvpairs = True if not id_in_nvpairs: nv_pairs.append( ("id", utils.find_unique_id( cib_dom, "colocation-%s-%s-%s" % (resource1, resource2, score)))) (dom, constraintsElement) = getCurrentConstraints(cib_dom) # If one role is specified, the other should default to "started" if role1 != "" and role2 == "": role2 = DEFAULT_ROLE if role2 != "" and role1 == "": role1 = DEFAULT_ROLE element = dom.createElement("rsc_colocation") 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]) if "--force" not in utils.pcs_options: duplicates = colocation_find_duplicates(constraintsElement, element) if duplicates: utils.err( "duplicate constraint already exists, use --force to override\n" + "\n".join([ " " + colocation_el_to_string(dup, True) for dup in duplicates ])) 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) cib_dom = utils.get_cib_dom() resource_valid, resource_error = utils.validate_constraint_resource( cib_dom, resource1) if not resource_valid: utils.err(resource_error) resource_valid, resource_error = utils.validate_constraint_resource( cib_dom, resource2) if not resource_valid: utils.err(resource_error) 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(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 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 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 = utils.validate_constraint_resource( cib_dom, resource1 ) if not resource_valid: utils.err(resource_error) resource_valid, resource_error = utils.validate_constraint_resource( cib_dom, resource2 ) if not resource_valid: utils.err(resource_error) 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(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 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) cib_dom = utils.get_cib_dom() resource_valid, resource_error = utils.validate_constraint_resource( cib_dom, resource1 ) if not resource_valid: utils.err(resource_error) resource_valid, resource_error = utils.validate_constraint_resource( cib_dom, resource2 ) if not resource_valid: utils.err(resource_error) 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) 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 testValidateConstraintResource(self): dom = self.get_cib_resources() self.assertEquals((True, ""), utils.validate_constraint_resource(dom, "myClone")) self.assertEquals( (True, ""), utils.validate_constraint_resource(dom, "myGroupClone")) self.assertEquals((True, ""), utils.validate_constraint_resource(dom, "myMaster")) self.assertEquals( (True, ""), utils.validate_constraint_resource(dom, "myGroupMaster")) self.assertEquals( (True, ""), utils.validate_constraint_resource(dom, "myResource")) self.assertEquals((True, ""), utils.validate_constraint_resource(dom, "myGroup")) self.assertEquals( (True, ""), utils.validate_constraint_resource(dom, "myGroupedResource")) self.assertEquals( (False, "Resource 'myNonexistent' does not exist"), utils.validate_constraint_resource(dom, "myNonexistent")) message = ("%s is a clone resource, you should use the clone id: " "%s when adding constraints. Use --force to override.") self.assertEquals( (False, message % ("myClonedResource", "myClone")), utils.validate_constraint_resource(dom, "myClonedResource")) self.assertEquals( (False, message % ("myClonedGroup", "myGroupClone")), utils.validate_constraint_resource(dom, "myClonedGroup")) self.assertEquals( (False, message % ("myClonedGroupedResource", "myGroupClone")), utils.validate_constraint_resource(dom, "myClonedGroupedResource")) message = ( "%s is a master/slave resource, you should use the master id: " "%s when adding constraints. Use --force to override.") self.assertEquals( (False, message % ("myMasteredResource", "myMaster")), utils.validate_constraint_resource(dom, "myMasteredResource")) self.assertEquals( (False, message % ("myMasteredGroup", "myGroupMaster")), utils.validate_constraint_resource(dom, "myMasteredGroup")) self.assertEquals( (False, message % ("myMasteredGroupedResource", "myGroupMaster")), utils.validate_constraint_resource(dom, "myMasteredGroupedResource")) utils.pcs_options["--force"] = True self.assertEquals( (True, ""), utils.validate_constraint_resource(dom, "myClonedResource")) self.assertEquals( (True, ""), utils.validate_constraint_resource(dom, "myClonedGroup")) self.assertEquals( (True, ""), utils.validate_constraint_resource(dom, "myClonedGroupedResource")) self.assertEquals( (True, ""), utils.validate_constraint_resource(dom, "myMasteredResource")) self.assertEquals( (True, ""), utils.validate_constraint_resource(dom, "myMasteredGroup")) self.assertEquals( (True, ""), utils.validate_constraint_resource(dom, "myMasteredGroupedResource"))
def testValidateConstraintResource(self): dom = self.get_cib_resources() self.assertEquals( (True, "", "myClone"), utils.validate_constraint_resource(dom, "myClone") ) self.assertEquals( (True, "", "myGroupClone"), utils.validate_constraint_resource(dom, "myGroupClone") ) self.assertEquals( (True, "", "myMaster"), utils.validate_constraint_resource(dom, "myMaster") ) self.assertEquals( (True, "", "myGroupMaster"), utils.validate_constraint_resource(dom, "myGroupMaster") ) self.assertEquals( (True, "", "myResource"), utils.validate_constraint_resource(dom, "myResource") ) self.assertEquals( (True, "", "myGroup"), utils.validate_constraint_resource(dom, "myGroup") ) self.assertEquals( (True, "", "myGroupedResource"), utils.validate_constraint_resource(dom, "myGroupedResource") ) self.assertEquals( (False, "Resource 'myNonexistent' does not exist", None), utils.validate_constraint_resource(dom, "myNonexistent") ) message = ( "%s is a clone resource, you should use the clone id: " "%s when adding constraints. Use --force to override." ) self.assertEquals( ( False, message % ("myClonedResource", "myClone"), "myClone" ), utils.validate_constraint_resource(dom, "myClonedResource") ) self.assertEquals( ( False, message % ("myClonedGroup", "myGroupClone"), "myGroupClone" ), utils.validate_constraint_resource(dom, "myClonedGroup") ) self.assertEquals( ( False, message % ("myClonedGroupedResource", "myGroupClone"), "myGroupClone" ), utils.validate_constraint_resource(dom, "myClonedGroupedResource") ) message = ( "%s is a master/slave resource, you should use the master id: " "%s when adding constraints. Use --force to override." ) self.assertEquals( ( False, message % ("myMasteredResource", "myMaster"), "myMaster" ), utils.validate_constraint_resource(dom, "myMasteredResource") ) self.assertEquals( ( False, message % ("myMasteredGroup", "myGroupMaster"), "myGroupMaster" ), utils.validate_constraint_resource(dom, "myMasteredGroup") ) self.assertEquals( ( False, message % ("myMasteredGroupedResource", "myGroupMaster"), "myGroupMaster" ), utils.validate_constraint_resource(dom, "myMasteredGroupedResource") ) utils.pcs_options["--force"] = True self.assertEquals( (True, "", "myClone"), utils.validate_constraint_resource(dom, "myClonedResource") ) self.assertEquals( (True, "", "myGroupClone"), utils.validate_constraint_resource(dom, "myClonedGroup") ) self.assertEquals( (True, "", "myGroupClone"), utils.validate_constraint_resource(dom, "myClonedGroupedResource") ) self.assertEquals( (True, "", "myMaster"), utils.validate_constraint_resource(dom, "myMasteredResource") ) self.assertEquals( (True, "", "myGroupMaster"), utils.validate_constraint_resource(dom, "myMasteredGroup") ) self.assertEquals( (True, "", "myGroupMaster"), utils.validate_constraint_resource(dom, "myMasteredGroupedResource") )
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) cib_dom = utils.get_cib_dom() resource_valid, resource_error = utils.validate_constraint_resource( cib_dom, resource1) if not resource_valid: utils.err(resource_error) resource_valid, resource_error = utils.validate_constraint_resource( cib_dom, resource2) if not resource_valid: utils.err(resource_error) 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)