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 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 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.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) 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): print "Error: Unable to create constraint, similar constraint already exists: %s" % order_id sys.exit(1) (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()