Beispiel #1
0
def resource_master_remove(argv):
    if len(argv) < 1:
        usage.resource()
        sys.exit(1)

    dom = utils.get_cib_dom()
    master_id = argv.pop(0)

    master_found = False
# Check to see if there's a resource/group with the master_id if so, we remove the parent
    for rg in (dom.getElementsByTagName("primitive") + dom.getElementsByTagName("group")):
        if rg.getAttribute("id") == master_id and rg.parentNode.tagName == "master":
            master_id = rg.parentNode.getAttribute("id")

    resources_to_cleanup = []
    for master in dom.getElementsByTagName("master"):
        if master.getAttribute("id") == master_id:
            childNodes = master.getElementsByTagName("primitive")
            for child in childNodes:
                resources_to_cleanup.append(child.getAttribute("id"))
            master_found = True
            break

    if not master_found:
            print "Error: Unable to find multi-state resource with id %s" % master_id
            sys.exit(1)

    master.parentNode.removeChild(master)
    utils.replace_cib_configuration(dom)
    if (not utils.usefile):
        for r in resources_to_cleanup:
            args = ["crm_resource","-C","-r",r]
            cmdoutput, retVal = utils.run(args)
Beispiel #2
0
def resource_manage(argv, set_managed):
    if len(argv) == 0:
        usage.resource()
        sys.exit(1)

    for resource in argv:
        if not utils.does_exist("//primitive[@id='"+resource+"']"):
            print "Error: %s doesn't exist." % resource
            sys.exit(1)
        exists =  utils.does_exist("//primitive[@id='"+resource+"']/meta_attributes/nvpair[@name='is-managed']")
        if set_managed and not exists:
            print "Error: %s is already managed" % resource
            sys.exit(1)
        elif not set_managed and exists:
            print "Error: %s is already unmanaged" % resource
            sys.exit(1)

    for resource in argv:
        if not set_managed:
            (output, retval) =  utils.set_unmanaged(resource)
            if retval != 0:
                print "Error attempting to unmanage resource: %s" % output
                sys.exit(1)
        else:
            xpath = "//primitive[@id='"+resource+"']/meta_attributes/nvpair[@name='is-managed']" 
            my_xml = utils.get_cib_xpath(xpath)
            utils.remove_from_cib(my_xml)
Beispiel #3
0
def resource_clone(argv):
    if len(argv) < 1:
        usage.resource()
        sys.exit(1)
    res = argv[0]
    resource_clone_create(argv)
    constraint.constraint_resource_update(res)
Beispiel #4
0
def resource_clone_remove(argv):
    if len(argv) != 1:
        usage.resource()
        sys.exit(1)

    name = argv.pop()
    dom = utils.get_cib_dom()
    re = dom.documentElement.getElementsByTagName("resources")[0]

    found = False
    for res in re.getElementsByTagName("primitive") + re.getElementsByTagName("group"):
        if res.getAttribute("id") == name:
            clone = res.parentNode
            if clone.tagName != "clone":
                print "Error: %s is not in a clone" % name
                sys.exit(1)
            clone.parentNode.appendChild(res)
            clone.parentNode.removeChild(clone)
            found = True
            break

    if found == False:
        print "Error: could not find resource or group: %s" % name
        sys.exit(1)

    xml_resource_string = re.toxml()
    args = ["cibadmin", "-o", "resources", "-R", "-X", xml_resource_string]
    output, retval = utils.run(args)

    if retval != 0:
        print output
        sys.exit(1)
Beispiel #5
0
def resource_cmd(argv):
    if len(argv) == 0:
        argv = ["show"]

    sub_cmd = argv.pop(0)
    if (sub_cmd == "help"):
        usage.resource()
    elif (sub_cmd == "create"):
        if len(argv) == 0:
            resource_list_available()
        elif len(argv) == 1:
            resource_list_options(argv[0])
        else:
            res_id = argv.pop(0)
            res_type = argv.pop(0)
            ra_values = []
            op_values = []
            op_args = False
            for arg in argv:
                if op_args:
                    op_values.append(arg)
                else:
                    if arg == "op":
                        op_args = True
                    else:
                        ra_values.append(arg)
        
            resource_create(res_id, res_type, ra_values, op_values)
    elif (sub_cmd == "update"):
        res_id = argv.pop(0)
        resource_update(res_id,argv)
    elif (sub_cmd == "delete"):
        res_id = argv.pop(0)
        resource_remove(res_id)
    elif (sub_cmd == "list" or sub_cmd == "show"):
        resource_show(argv)
    elif (sub_cmd == "group"):
        resource_group(argv)
    elif (sub_cmd == "clone"):
        resource_clone(argv)
    elif (sub_cmd == "start"):
        resource_start(argv)
    elif (sub_cmd == "stop"):
        resource_stop(argv)
    elif (sub_cmd == "restart"):
# Need to have a wait in here to make sure the stop registers
        print "Not Yet Implemented"
#        if resource_stop(argv):
#            resource_start(argv)
    elif (sub_cmd == "manage"):
        resource_manage(argv, True)
    elif (sub_cmd == "unmanage"):
        resource_manage(argv, False)
    else:
        usage.resource()
Beispiel #6
0
def resource_clone(argv):
    if len(argv) < 2:
        usage.resource()
        sys.exit(1)

    sub_cmd = argv.pop(0)
    if sub_cmd == "create":
        resource_clone_create(argv)
    elif sub_cmd == "update":
        resource_clone_create(argv,True)
    elif sub_cmd == "remove":
        resource_clone_remove(argv)
    else:
        usage.resource()
        sys.exit(1)
Beispiel #7
0
def resource_agents(argv):
    if len(argv) > 1:
        usage.resource()
        sys.exit(1)
    elif len(argv) == 1:
        standards = [argv[0]]
    else:
        output = resource_standards(True)
        standards = output.split('\n')

    for s in standards:
        output, retval = utils.run(["crm_resource", "--list-agents", s])
        preg = re.compile(r'\d+ agents found for standard.*$', re.MULTILINE)
        output = preg.sub("", output)
        output = output.strip()
        print output
Beispiel #8
0
def resource_operation_add(res_id, argv):
    if len(argv) < 1:
        usage.resource()
        sys.exit(1)

    op_name = argv.pop(0)
    dom = utils.get_cib_dom()
    resource_found = False

    for resource in dom.getElementsByTagName("primitive"):
        if resource.getAttribute("id") == res_id:
            resource_found = True
            break

    if not resource_found:
        print "Unable to find resource: %s" % res_id
        sys.exit(1)

    op_properties = convert_args_to_tuples(argv)
    op_properties.sort(key=lambda a:a[0])
    op_properties.insert(0,('name', op_name))
    found_match = False

    op = dom.createElement("op")
    op_id = res_id + "-"
    for prop in op_properties:
        op.setAttribute(prop[0], prop[1])
        op_id += prop[0] + "-" + prop[1] + "-"
    op_id = op_id[:-1]
    op_id = utils.find_unique_id(dom, op_id)
    op.setAttribute("id", op_id)

    operations = resource.getElementsByTagName("operations")
    if len(operations) == 0:
        operations = dom.createElement("operations")
        resource.appendChild(operations)
    else:
        operations = operations[0]

    if utils.operation_exists(operations,op):
        print "Error: identical operation already exists for %s" % res_id
        sys.exit(1)

    operations.appendChild(op)
    utils.replace_cib_configuration(dom)
Beispiel #9
0
def resource_operation_remove(res_id, argv):
    if len(argv) < 1:
        usage.resource()
        sys.exit(1)

    original_argv = " ".join(argv)

    op_name = argv.pop(0)
    dom = utils.get_cib_dom()
    resource_found = False

    for resource in dom.getElementsByTagName("primitive"):
        if resource.getAttribute("id") == res_id:
            resource_found = True
            break

    if not resource_found:
        print "Unable to find resource: %s" % res_id
        sys.exit(1)

    op_properties = convert_args_to_tuples(argv)
    op_properties.append(('name', op_name))
    found_match = False
    for op in resource.getElementsByTagName("op"):
        temp_properties = []
        for attrName in op.attributes.keys():
            if attrName == "id":
                continue
            temp_properties.append((attrName,op.attributes.get(attrName).nodeValue))

        if len(set(op_properties) ^ set(temp_properties)) == 0:
            found_match = True
            parent = op.parentNode
            parent.removeChild(op)
            if len(parent.getElementsByTagName("op")) == 0:
                parent.parentNode.removeChild(parent)
            break

    if not found_match:
        print "Unable to find operation matching: %s" % original_argv
        sys.exit(1)

    utils.replace_cib_configuration(dom)
Beispiel #10
0
def resource_operation_add(res_id, argv):
    if len(argv) < 1:
        usage.resource()
        sys.exit(1)

    op_name = argv.pop(0)
    dom = utils.get_cib_dom()
    resource_found = False

    for resource in dom.getElementsByTagName("primitive"):
        if resource.getAttribute("id") == res_id:
            resource_found = True
            break

    if not resource_found:
        print "Unable to find resource: %s" % res_id
        sys.exit(1)

    op_properties = convert_args_to_tuples(argv)
    op_properties.sort(key=lambda a:a[0])
    op_properties.insert(0,('name', op_name))
    found_match = False

    op = dom.createElement("op")
    op_id = res_id + "-"
    for prop in op_properties:
        op.setAttribute(prop[0], prop[1])
        op_id += prop[0] + "-" + prop[1] + "-"
    op_id = op_id[:-1]
    op.setAttribute("id", op_id)

    operations = resource.getElementsByTagName("operations")
    if len(operations) == 0:
        operations = dom.createElement("operations")
        resource.appendChild(operations)
    else:
        operations = operations[0]

    operations.appendChild(op)

    utils.replace_cib_configuration(dom)
Beispiel #11
0
def resource_cmd(argv):
    if len(argv) == 0:
        usage.resource()
        exit(1)

    sub_cmd = argv.pop(0)
    if (sub_cmd == "help"):
        usage.resource()
    elif (sub_cmd == "create"):
        res_id = argv.pop(0)
        res_type = argv.pop(0)
        ra_values = []
        op_values = []
        op_args = False
        for arg in argv:
            if op_args:
                op_values.append(arg)
            else:
                if arg == "op":
                    op_args = True
                else:
                    ra_values.append(arg)
        
        resource_create(res_id, res_type, ra_values, op_values)
    elif (sub_cmd == "delete"):
        res_id = argv.pop(0)
        resource_remove(res_id)
    elif (sub_cmd == "list"):
        args = ["crm_resource","-L"]
        output,retval = utils.run(args)
        print output,
    elif (sub_cmd == "group"):
        resource_group(argv)
    else:
        usage.resource()
Beispiel #12
0
def resource_group(argv):
    if (len(argv) == 0):
        usage.resource()
        sys.exit(1)

    group_cmd = argv.pop(0)
    if (group_cmd == "add"):
        if (len(argv) < 2):
            usage.resource()
            sys.exit(1)
        group_name = argv.pop(0)
        resource_group_add(group_name, argv)
    elif (group_cmd == "remove_resource"):
        if (len(argv) < 2):
            usage.resource()
            sys.exit(1)
        group_name = argv.pop(0)
        resource_group_rm(group_name, argv)
    elif (group_cmd == "list"):
        resource_group_list(argv)

    else:
        usage.resource()
        sys.exit(1)
Beispiel #13
0
def resource_master_create(argv, update=False):
    if (len(argv) < 2 and not update) or (len(argv) < 1 and update):
        usage.resource()
        sys.exit(1)

    dom = utils.get_cib_dom()
    master_id = argv.pop(0)

    if (update):
        master_found = False
        for master in dom.getElementsByTagName("master"):
            if master.getAttribute("id") == master_id:
                master_element = master
                master_found = True
                break
        if not master_found:
            print "Error: Unable to find multi-state resource with id %s" % master_id
            sys.exit(1)
    else:
        rg_id = argv.pop(0)
        if utils.does_id_exist(dom, master_id):
            print "Error: %s already exists in the cib" % master_id
            sys.exit(1)
        resources = dom.getElementsByTagName("resources")[0]
        rg_found = False
        for resource in (resources.getElementsByTagName("primitive") +
            resources.getElementsByTagName("group")):
            if resource.getAttribute("id") == rg_id:
                rg_found = True
                break
        if not rg_found:
            print "Error: Unable to find resource or group with id %s" % rg_id
            sys.exit(1)
        master_element = dom.createElement("master")
        master_element.setAttribute("id", master_id)
        resource.parentNode.removeChild(resource)
        master_element.appendChild(resource)
        resources.appendChild(master_element)

    if len(argv) > 0:
        meta = None
        for child in master_element.childNodes:
            if child.nodeType != xml.dom.Node.ELEMENT_NODE:
                continue
            if child.tagName == "meta_attributes":
                meta = child
        if meta == None:
            meta = dom.createElement("meta_attributes")
            meta.setAttribute("id", master_id + "-meta_attributes")
            master_element.appendChild(meta)

        for arg in convert_args_to_tuples(argv):
            for nvpair in meta.getElementsByTagName("nvpair"):
                if nvpair.getAttribute("name") == arg[0]:
                    meta.removeChild(nvpair)
                    break
            if arg[1] == "":
                continue
            nvpair = dom.createElement("nvpair")
            nvpair.setAttribute("id", meta.getAttribute("id") + "-" + arg[0])
            nvpair.setAttribute("name", arg[0])
            nvpair.setAttribute("value", arg[1])
            meta.appendChild(nvpair)
        if len(meta.getElementsByTagName("nvpair")) == 0:
            master_element.removeChild(meta)
    utils.replace_cib_configuration(dom)
    if not update:
        constraint.constraint_resource_update(rg_id)
Beispiel #14
0
def resource_master(argv):
    if len(argv) < 2:
        usage.resource()
        sys.exit(1)

    resource_master_create(argv)
Beispiel #15
0
def resource_cmd(argv):
    if len(argv) == 0:
        argv = ["show"]

    sub_cmd = argv.pop(0)
    if (sub_cmd == "help"):
        usage.resource()
    elif (sub_cmd == "list"):
        resource_list_available(argv)
    elif (sub_cmd == "describe"):
        if len(argv) == 1:
            resource_list_options(argv[0])
        else:
            usage.resource()
            sys.exit(1)
    elif (sub_cmd == "create"):
        if len(argv) < 2:
            usage.resource()
            sys.exit(1)
        res_id = argv.pop(0)
        res_type = argv.pop(0)
        ra_values = []
        op_values = []
        op_args = False
        for arg in argv:
            if op_args:
                op_values.append(arg)
            else:
                if arg == "op":
                    op_args = True
                else:
                    ra_values.append(arg)
    
        resource_create(res_id, res_type, ra_values, op_values)
    elif (sub_cmd == "standards"):
        resource_standards()
    elif (sub_cmd == "providers"):
        resource_providers()
    elif (sub_cmd == "agents"):
        resource_agents(argv)
    elif (sub_cmd == "update"):
        res_id = argv.pop(0)
        resource_update(res_id,argv)
    elif (sub_cmd == "add_operation"):
        res_id = argv.pop(0)
        resource_operation_add(res_id,argv)
    elif (sub_cmd == "remove_operation"):
        res_id = argv.pop(0)
        resource_operation_remove(res_id,argv)
    elif (sub_cmd == "delete"):
        res_id = argv.pop(0)
        resource_remove(res_id)
    elif (sub_cmd == "show"):
        resource_show(argv)
    elif (sub_cmd == "group"):
        resource_group(argv)
    elif (sub_cmd == "clone"):
        resource_clone(argv)
    elif (sub_cmd == "unclone"):
        resource_clone_remove(argv)
    elif (sub_cmd == "master"):
        resource_master(argv)
    elif (sub_cmd == "start"):
        resource_start(argv)
    elif (sub_cmd == "stop"):
        resource_stop(argv)
    elif (sub_cmd == "restart"):
# Need to have a wait in here to make sure the stop registers
        print "Not Yet Implemented"
#        if resource_stop(argv):
#            resource_start(argv)
    elif (sub_cmd == "manage"):
        resource_manage(argv, True)
    elif (sub_cmd == "unmanage"):
        resource_manage(argv, False)
    elif (sub_cmd == "rsc" or sub_cmd == "op"):
        if len(argv) < 1:
            usage.resource()
            sys.exit(1)
        rsc_subcmd = argv.pop(0)
        if (sub_cmd == "rsc" and rsc_subcmd == "defaults"):
            if len(argv) == 0:
                show_defaults("rsc_defaults")
            else:
                set_default("rsc_defaults", argv)
        elif (sub_cmd == "op" and rsc_subcmd == "defaults"):
            if len(argv) == 0:
                show_defaults("op_defaults")
            else:
                set_default("op_defaults", argv)
        else:
            usage.resource()
            sys.exit(1)
    else:
        usage.resource()
        sys.exit(1)