Exemplo n.º 1
0
def process_txt(session, filename):
    current_effective = pyfos_zone.effective_configuration.get(session)

    file = open(filename, "r")
    for line in file:
        if len(line) > 0:
            # pylint: disable=W1401
            command_line = re.split('\s|\,', line)
            if command_line[0] == "alicreate":
                aliases = parse_alicreate(command_line)
                result = aliascreate.aliascreate(session, aliases)
                if 'success-code' in result and result['success-code'] == 201:
                    print("\tcreate succeeded")
                else:
                    print(result)
            elif command_line[0] == "zonecreate":
                zones = parse_zonecreate(command_line)
                result = zonecreate.zonecreate(session, zones)
                if 'success-code' in result and result['success-code'] == 201:
                    print("\tcreate succeeded")
                else:
                    print(result)
            elif command_line[0] == "cfgadd":
                cfgs = parse_cfgadd_or_create(command_line)
                result = cfgcreate.cfgcreate(session, cfgs)
                if 'success-code' in result and result['success-code'] == 201:
                    print("\tadd succeeded")
                else:
                    print(result)
            elif command_line[0] == "cfgcreate":
                cfgs = parse_cfgadd_or_create(command_line)
                result = cfgcreate.cfgcreate(session, cfgs)
                if 'success-code' in result and result['success-code'] == 201:
                    print("\tcreate succeeded")
                else:
                    print(result)
            elif command_line[0] == "cfgenable":
                command_line[1] = command_line[1].replace("\"", "")
                print("cfgenable with", command_line[1])
                result = cfgenable.cfgenable(session, command_line[1],
                                             current_effective.peek_checksum())
                if 'success-code' in result and result['success-code'] == 204:
                    print("\tenable succeeded")
                else:
                    print(result)
Exemplo n.º 2
0
def process_xlsx_direct_generic(session, filename, sheet_name, apply_to_fos,
                                usepeer, usepeer_wwn):

    HOST_STR = "Initiator Alias"
    HOST_WWN_STR = "Initiator Alias WWN"
    STORAGE_STR = "Target Alias"
    STORAGE_WWN_STR = "Target Alias WWN"
    CFG_STR = "Active Zone CFG"
    ZONE_PREFIX_STR = "Zone Prefix"
    ZONE_NAME_STR = "Zone Name"

    current_effective = pyfos_zone.effective_configuration.get(session)
    current_defined = pyfos_zone.defined_configuration.get(session)

    host_index = -1
    host_wwn_index = -1
    target_index = -1
    target_wwn_index = -1
    cfg_index = -1
    zone_prefix_index = -1
    zone_name_index = -1

    print("processing file", filename)

    book = xlrd.open_workbook(filename)

    sheet_index = -1
    if sheet_name:
        for index in range(0, book.nsheets):
            if book.sheet_names()[index] == sheet_name:
                sheet_index = index

        if sheet_index == -1:
            print("unknown sheet", sheet_name)
            return
        print("processing sheet", sheet_name)
    else:
        sheet_index = 0
        print("processing the first sheet")

    sh = book.sheet_by_index(sheet_index)

    for column in range(sh.ncols):
        # print(sh.row(0)[column].value)
        if HOST_STR == sh.row(0)[column].value:
            host_index = column
        elif HOST_WWN_STR == sh.row(0)[column].value:
            host_wwn_index = column
        elif STORAGE_STR == sh.row(0)[column].value:
            target_index = column
        elif STORAGE_WWN_STR == sh.row(0)[column].value:
            target_wwn_index = column
        elif CFG_STR == sh.row(0)[column].value:
            cfg_index = column
        elif ZONE_PREFIX_STR == sh.row(0)[column].value:
            zone_prefix_index = column
        elif ZONE_NAME_STR == sh.row(0)[column].value:
            zone_name_index = column

    if host_index is -1:
        print("missing required header", HOST_STR)
        return
    elif host_wwn_index is -1:
        print("missing required header", HOST_WWN_STR)
        return
    elif target_index is -1:
        print("missing required header", STORAGE_STR)
        return
    elif target_wwn_index is -1:
        print("missing required header", STORAGE_WWN_STR)
        return
    elif cfg_index is -1:
        print("missing required header", CFG_STR)
        return
    elif zone_prefix_index is -1:
        print("missing required header", ZONE_PREFIX_STR)
        return
    elif zone_name_index is -1:
        print("missing required header", ZONE_NAME_STR)
        return

    host_alias_create = {}
    target_alias_create = {}
    zone_create = {}
    cfg_add = {}
    cfg_name = None

    for row in range(1, sh.nrows):
        host_port_name = None
        target_port_name = None
        new_zone_name = None

        if (not sh.row(row)[host_index].value
                or not sh.row(row)[host_wwn_index].value
                or not sh.row(row)[target_index].value
                or not sh.row(row)[target_wwn_index].value
                or not sh.row(row)[cfg_index].value
                or not sh.row(row)[zone_name_index].value):
            print(
                WARN, "Row number", row + 1, "is not processed"
                " due to missing fields. All fields are required"
                " except Zone Prefix", END)
            continue

        if sh.row(row)[host_index].value:
            host_port_name = sh.row(row)[host_index].value
            if host_port_name not in host_alias_create:
                host_alias_create[host_port_name] = (
                    sh.row(row)[host_wwn_index].value)
            else:
                if (host_alias_create[host_port_name] !=
                        sh.row(row)[host_wwn_index].value):
                    print(WARN, "initiator alias", host_port_name,
                          "is associated with both",
                          host_alias_create[host_port_name],
                          sh.row(row)[host_wwn_index].value, END)
                    return

        if sh.row(row)[target_index].value:
            target_port_name = sh.row(row)[target_index].value
            if target_port_name not in target_alias_create:
                target_alias_create[target_port_name] = (
                    sh.row(row)[target_wwn_index].value)
            else:
                if (target_alias_create[target_port_name] !=
                        sh.row(row)[target_wwn_index].value):
                    print(WARN, "target alias", target_port_name,
                          "is associated with both",
                          target_alias_create[target_port_name],
                          sh.row(row)[target_wwn_index].value, END)
                    return

        if usepeer:
            if host_port_name and target_port_name:
                if (not sh.row(row)[zone_name_index].value
                        or sh.row(row)[zone_name_index].value == "auto"):
                    if sh.row(row)[zone_prefix_index].value:
                        new_zone_name = (sh.row(row)[zone_prefix_index].value +
                                         "_" + target_port_name)
                    else:
                        new_zone_name = target_port_name
                else:
                    new_zone_name = sh.row(row)[zone_name_index].value

                if new_zone_name not in zone_create:
                    members = []
                    if usepeer_wwn:
                        members.append(target_alias_create[target_port_name])
                        members.append(host_alias_create[host_port_name])
                    else:
                        members.append(target_port_name)
                        members.append(host_port_name)
                    zone_create[new_zone_name] = members
                    cfg_add[new_zone_name] = sh.row(row)[cfg_index].value
                    if (cfg_name is not None
                            and cfg_name != sh.row(row)[cfg_index].value):
                        print(WARN, "two different cfgs are specified",
                              cfg_name,
                              sh.row(row)[cfg_index].value, END)
                        return
                    cfg_name = sh.row(row)[cfg_index].value
                else:
                    if usepeer_wwn:
                        if (target_alias_create[target_port_name] !=
                                zone_create[new_zone_name][0]):
                            print(WARN, "two different targets specified",
                                  target_alias_create[target_port_name],
                                  zone_create[new_zone_name][0], "for",
                                  new_zone_name, END)
                            return
                        zone_create[new_zone_name].append(
                            host_alias_create[host_port_name])
                    else:
                        if target_port_name != zone_create[new_zone_name][0]:
                            print(WARN, "two different targets specified",
                                  target_port_name,
                                  zone_create[new_zone_name][0], "for",
                                  new_zone_name, END)
                            return
                        zone_create[new_zone_name].append(host_port_name)
            else:
                print(WARN, "invalid initiator", host_port_name,
                      "or invalid target name", target_port_name, END)
                return
        else:
            if host_port_name and target_port_name:
                if (not sh.row(row)[zone_name_index].value
                        or sh.row(row)[zone_name_index].value == "auto"):
                    if sh.row(row)[zone_prefix_index].value:
                        new_zone_name = (sh.row(row)[zone_prefix_index].value +
                                         "_" + host_port_name + "_" +
                                         target_port_name)
                    else:
                        new_zone_name = host_port_name + "_" + target_port_name
                else:
                    new_zone_name = sh.row(row)[zone_name_index].value

                if new_zone_name not in zone_create:
                    members = []
                    members.append(host_port_name)
                    members.append(target_port_name)
                    zone_create[new_zone_name] = members
                    cfg_add[new_zone_name] = sh.row(row)[cfg_index].value
                    if (cfg_name is not None
                            and cfg_name != sh.row(row)[cfg_index].value):
                        print(WARN, "two different cfgs are specified",
                              cfg_name,
                              sh.row(row)[cfg_index].value, END)
                        return
                    cfg_name = sh.row(row)[cfg_index].value
                else:
                    if host_port_name not in zone_create[new_zone_name]:
                        zone_create[new_zone_name].append(host_port_name)
                    if target_port_name not in zone_create[new_zone_name]:
                        zone_create[new_zone_name].append(target_port_name)
                    if (cfg_name is not None
                            and cfg_name != sh.row(row)[cfg_index].value):
                        print(WARN, "two different cfgs are specified",
                              cfg_name,
                              sh.row(row)[cfg_index].value, END)
                        return
                    cfg_name = sh.row(row)[cfg_index].value
            else:
                print(WARN, "invalid initiator", host_port_name,
                      "or invalid target", target_port_name, END)
                return

    changed = 0
    for key, value in host_alias_create.items():
        changed += handle_alias_create(session, current_defined, key, [value],
                                       apply_to_fos)
    for key, value in target_alias_create.items():
        changed += handle_alias_create(session, current_defined, key, [value],
                                       apply_to_fos)
    for key, value in zone_create.items():
        changed += handle_zone_create(session, current_defined, key, value,
                                      usepeer, apply_to_fos)

    cfg_members = []
    for key, value in cfg_add.items():
        cfg_members.append(key)
    changed += handle_cfg_create(session, current_defined, cfg_name,
                                 cfg_members, apply_to_fos)

    if changed > 0:
        print("enable cfg", cfg_name)
        if apply_to_fos:
            result = cfgenable.cfgenable(session, cfg_name,
                                         current_effective.peek_checksum())
            if 'success-code' in result and result['success-code'] == 204:
                print(CHANGED, "\tenable succeeded", END)
            else:
                print(result)
        else:
            print(CHANGED, "\tneed change", END)
    else:
        if current_effective.peek_cfg_name() == cfg_name:
            print("Zone DB not changed. No reason to enable", cfg_name)
        else:
            print(cfg_name, "is not enabled. enabling")
            if apply_to_fos:
                result = cfgenable.cfgenable(session, cfg_name,
                                             current_effective.peek_checksum())
                if 'success-code' in result and result['success-code'] == 204:
                    print(CHANGED, "\tenable succeeded", END)
                else:
                    print(result)
            else:
                print(CHANGED, "\tneed change", END)