def check_all_tasks_are_finished(module):
    status, taskdata, msg = irmc_redfish_get(module,
                                             "redfish/v1/TaskService/Tasks")
    if status < 100:
        module.fail_json(msg=msg, status=status, exception=taskdata)
    elif status not in (200, 202, 204):
        module.fail_json(msg=msg, status=status)
    tasks = get_irmc_json(taskdata.json(), ["Members"])
    for task in tasks:
        url = get_irmc_json(task, "@odata.id")
        status, sdata, msg = irmc_redfish_get(module, "{0}".format(url[1:]))
        if status < 100:
            module.fail_json(msg=msg, status=status, exception=sdata)
        elif status not in (200, 202, 204):
            module.fail_json(msg=msg, status=status)

        task_state = get_irmc_json(sdata.json(),
                                   ["Oem", "ts_fujitsu", "StatusOEM"])
        if task_state in ("Pending", "FlashImageDownloadedSuccessfully"):
            msg = "Firmware update has already been started, system reboot is required. Cannot continue new update."
            module.fail_json(msg=msg, status=30)
        task_progress = get_irmc_json(
            sdata.json(), ["Oem", "ts_fujitsu", "TotalProgressPercent"])
        if str(task_progress) != "100":
            msg = "Task '{0}' is still in progress. Cannot continue new update.". \
                  format(get_irmc_json(sdata.json(), "Name"))
            module.fail_json(msg=msg, status=31)
def irmc_fwbios_update(module):
    # initialize result
    result['changed'] = False
    result['status'] = 0

    if module.check_mode:
        result['msg'] = "module was not run"
        module.exit_json(**result)

    # preliminary parameter check
    preliminary_parameter_check(module)

    # check that all tasks are finished properly
    check_all_tasks_are_finished(module)

    # Get iRMC basic data
    status, sysdata, msg = irmc_redfish_get(module, "redfish/v1/Systems/0/")
    if status < 100:
        module.fail_json(msg=msg, status=status, exception=sysdata)
    elif status != 200:
        module.fail_json(msg=msg, status=status)

    # Get iRMC FW Update data
    update_url = "redfish/v1/Managers/iRMC/Oem/ts_fujitsu/iRMCConfiguration/FWUpdate/"
    status, fwdata, msg = irmc_redfish_get(module, update_url)
    if status < 100:
        module.fail_json(msg=msg, status=status, exception=fwdata)
    elif status != 200:
        module.fail_json(msg=msg, status=status)

    if module.params['command'] == "get":
        result['fw_update_configuration'] = setup_resultdata(fwdata, sysdata)
        module.exit_json(**result)
    elif module.params['update_type'] == "irmc":
        patch_update_data(module, update_url,
                          get_irmc_json(fwdata.json(), "@odata.etag"))

    if module.params['update_source'] == "file":
        status, udata, msg = irmc_redfish_post_file(module,
                                                    get_update_url(module),
                                                    module.params['file_name'])
    else:
        status, udata, msg = irmc_redfish_post(module, get_update_url(module),
                                               module.params['file_name'])
    if status < 100:
        module.fail_json(msg=msg, status=status, exception=udata)
    elif status not in (200, 202, 204):
        if status == 104 and module.params['update_type'] == "irmc":
            msg = "{0} This message might indicate that iRMC needs to reboot before FW update.".format(
                msg)
        if status == 400:
            msg = "{0} This message might be due to the binary file being invalid for the server.".format(
                msg)
        module.fail_json(msg=msg, status=status)

    wait_for_update_to_finish(module, udata.headers['Location'],
                              get_irmc_json(sysdata.json(), "PowerState"))
    module.exit_json(**result)
Example #3
0
def irmc_eventlog(module):
    # initialize result
    result['changed'] = False
    result['status'] = 0

    if module.check_mode:
        result['msg'] = "module was not run"
        module.exit_json(**result)

    # preliminary parameter check
    if module.params['command'] == "get" and module.params['id'] is None:
        result['msg'] = "Command 'get' requires 'id' parameter to be set!"
        result['status'] = 10
        module.fail_json(**result)

    if module.params['command'] == "list":
        status, data, msg = irmc_redfish_get(
            module, "redfish/v1/Managers/iRMC/LogServices/{0}/Entries".format(
                module.params['eventlog_type']))
        if status < 100:
            module.fail_json(msg=msg, status=status, exception=data)
        elif status not in (200, 202, 204):
            module.fail_json(msg=msg, status=status)

        eventlogs = get_irmc_json(data.json(), ["Members"])
        result['eventlog'] = []
        for item in eventlogs:
            result['eventlog'].append(get_irmc_eventlog_info(module, item))
    elif module.params['command'] == "clear":
        url = "redfish/v1/Managers/iRMC/LogServices/{0}/Actions/LogService.ClearLog". \
              format(module.params['eventlog_type'])
        status, data, msg = irmc_redfish_post(module, url, "")
        if status < 100:
            module.fail_json(msg=msg, status=status, exception=data)
        elif status not in (200, 202, 204):
            module.fail_json(msg=msg, status=status)
        result['changed'] = True
    else:
        status, data, msg = irmc_redfish_get(
            module,
            "redfish/v1/Managers/iRMC/LogServices/{0}/Entries/{1}".format(
                module.params['eventlog_type'], module.params['id']))
        if status < 100:
            module.fail_json(msg=msg, status=status, exception=data)
        elif status not in (200, 202, 204):
            module.fail_json(msg=msg, status=status)

        result['eventlog_entry'] = get_irmc_eventlog_info(module, data.json())

    module.exit_json(**result)
Example #4
0
def elcm_online_collection(module):
    if module.params['command'] == "get":
        status, elcmdata, msg = irmc_redfish_get(
            module, "rest/v1/Oem/eLCM/OnlineUpdate/updateCollection")
    else:
        status, elcmdata, msg = irmc_redfish_delete(
            module, "rest/v1/Oem/eLCM/OnlineUpdate/updateCollection")
    if status < 100:
        module.fail_json(msg=msg, status=status, exception=elcmdata)
    elif status == 404:
        result['msg'] = "updateCollection does not exist."
        if module.params['command'] == "get":
            result['status'] = status
            module.fail_json(**result)
        else:
            result['skipped'] = True
            module.exit_json(**result)
    elif status not in (200, 202, 204):
        module.fail_json(msg=msg, status=status)

    if module.params['command'] == "get":
        result['update_collection'] = []
        for item in get_irmc_json(elcmdata.json(), ["Links", "Contains"]):
            sw = {}
            #            sw['link'] = get_irmc_json(item, ["@odata.id"])
            #            sw['name'] = sw['link'].replace("rest/v1/Oem/eLCM/OnlineUpdate/updateCollection/PrimSupportPack-Win/", "")

            status, swdata, msg = irmc_redfish_get(
                module, get_irmc_json(item, ["@odata.id"]))
            if status < 100:
                module.fail_json(msg=msg, status=status, exception=swdata)
            elif status not in (200, 202, 204):
                module.fail_json(msg=msg, status=status)
            sw['component'] = get_irmc_json(swdata.json(),
                                            ["Update", "Component"])
            sw['subcomponent'] = get_irmc_json(swdata.json(),
                                               ["Update", "SubComponent"])
            sw['current'] = get_irmc_json(swdata.json(), ["Update", "Current"])
            sw['new'] = get_irmc_json(swdata.json(), ["Update", "New"])
            sw['severity'] = get_irmc_json(swdata.json(),
                                           ["Update", "Severity"])
            sw['status'] = get_irmc_json(swdata.json(), ["Update", "Status"])
            sw['reboot'] = get_irmc_json(swdata.json(), ["Update", "Reboot"])
            sw['selected'] = get_irmc_json(swdata.json(),
                                           ["Update", "Execution"])
            result['update_collection'].append(sw)
    else:
        result['changed'] = True
def preliminary_parameter_check(module):
    if module.params['command'] != "get":
        # Get server power state
        status, sysdata, msg = irmc_redfish_get(module,
                                                "redfish/v1/Systems/0/")
        if status < 100:
            module.fail_json(msg=msg, status=status, exception=sysdata)
        elif status != 200:
            module.fail_json(msg=msg, status=status)
        if get_irmc_json(sysdata.json(), "PowerState") == "On":
            result['msg'] = "Server is powered on. Cannot continue."
            result['status'] = 10
            module.fail_json(**result)
    if module.params['command'] == "create" and \
       module.params['adapter'] is None and module.params['level'] is None:
        result[
            'msg'] = "Command 'create' requires 'adapter' and 'level' to be set."
        result['status'] = 10
        module.fail_json(**result)
    if module.params['command'] == "delete" and module.params[
            'adapter'] is None and module.params['array'] is None:
        result[
            'msg'] = "Command 'delete' requires 'adapter' and 'array' to be set."
        result['status'] = 11
        module.fail_json(**result)
def preliminary_parameter_check(module):
    if module.params['command'] == "update":
        if module.params['update_source'] is None or module.params['update_type'] is None or \
           module.params['file_name'] is None:
            result[
                'msg'] = "Command 'update' requires 'update_source, update_type, file_name' parameters to be set!"
            result['status'] = 10
            module.fail_json(**result)
        if module.params['update_source'] == "tftp" and module.params[
                'server_name'] is None:
            result[
                'msg'] = "TFTP update requires 'server_name' parameter to be set!"
            result['status'] = 11
            module.fail_json(**result)

        if module.params['ignore_power_on'] is False:
            # Get server power state
            status, sysdata, msg = irmc_redfish_get(module,
                                                    "redfish/v1/Systems/0/")
            if status < 100:
                module.fail_json(msg=msg, status=status, exception=sysdata)
            elif status != 200:
                module.fail_json(msg=msg, status=status)

            if get_irmc_json(sysdata.json(), "PowerState") == "On":
                result['skipped'] = True
                result['warnings'] = "Server is powered on. Cannot continue."
                module.exit_json(**result)
def get_irmc_sessions(module):
    status, sessiondata, msg = irmc_redfish_get(module, "sessionInformation")
    if status < 100:
        module.fail_json(msg=msg, status=status, exception=sessiondata)
    elif status not in (200, 202, 204):
        module.fail_json(msg=msg, status=status)
    return get_irmc_json(sessiondata.json(), ["SessionList"])
def get_adapter(module, adapter):
    ctrl = {}
    ctrl['id'] = get_irmc_json(adapter, ["@AdapterId"])
    ctrl['name'] = ctrl['id']
    ctrl['level'] = get_irmc_json(adapter, ["Features", "RaidLevel"])
    ctrl['logical_drives'] = []
    ctrl['unused_disks'] = []
    status, hwdata, msg = irmc_redfish_get(
        module, "redfish/v1/Systems/0/Storage?$expand=Members")
    if status < 100:
        module.fail_json(msg=msg, status=status, exception=hwdata)
    elif status != 200:
        module.fail_json(msg=msg, status=status)
    for member in get_irmc_json(hwdata.json(), "Members"):
        # iRMC has each StroageController with its own Storage
        for sc in get_irmc_json(member, "StorageControllers"):
            if get_irmc_json(adapter, ["@AdapterId"]).replace(
                    "RAIDAdapter", "") == get_irmc_json(sc, ["MemberId"]):
                ctrl['name'] = get_irmc_json(sc, ["Model"])
                ctrl['firmware'] = get_irmc_json(sc, ["FirmwareVersion"])
                ctrl['drives'] = get_irmc_json(
                    sc, ["Oem", "ts_fujitsu", "DriveCount"])
                ctrl['volumes'] = get_irmc_json(
                    sc, ["Oem", "ts_fujitsu", "VolumeCount"])
                break
    return (ctrl)
def add_system_hw_info(power_state, module, result):
    # get system hardware
    for hw in ("Memory", "Processors", "EthernetInterfaces", "Storage"):
        status, hwdata, msg = irmc_redfish_get(
            module, "redfish/v1/Systems/0/{0}?$expand=Members".format(hw))
        if status < 100:
            module.fail_json(msg=msg, status=status, exception=hwdata)
        elif status != 200:
            module.fail_json(msg=msg, status=status)
        items = 0
        hw_dict = {}
        hw_dict['devices'] = []
        for member in get_irmc_json(hwdata.json(), "Members"):
            hw_list = {}
            if get_irmc_json(member, ["Status", "State"]) == "Enabled":
                if hw == "Memory":
                    hw_list['id'] = get_irmc_json(member, ["Id"])
                    hw_list['name'] = get_irmc_json(member, ["DeviceLocator"])
                    hw_list['manufacturer'] = get_irmc_json(
                        member, ["Manufacturer"])
                    hw_list['size'] = get_irmc_json(member, ["CapacityMiB"])
                if hw == "Processors":
                    hw_list['id'] = get_irmc_json(member, ["Id"])
                    hw_list['name'] = get_irmc_json(member, ["Model"])
                    hw_list['cores'] = get_irmc_json(member, ["TotalCores"])
                    hw_list['threads'] = get_irmc_json(member,
                                                       ["TotalThreads"])
                if hw == "EthernetInterfaces":
                    hw_list['id'] = get_irmc_json(member, ["Id"])
                    hw_list['name'] = get_irmc_json(member, ["Description"])
                    if "does not exist" in hw_list['name']:
                        hw_list['name'] = "{0} {1}".format(
                            get_irmc_json(member, ["Name"]), hw_list['id'])
                    hw_list['macaddress'] = get_irmc_json(
                        member, ["MACAddress"])
                if hw == "Storage" and power_state == "On":
                    # iRMC has each StroageController with its own Storage
                    for ctrl in get_irmc_json(member, "StorageControllers"):
                        hw_list['id'] = get_irmc_json(member, ["Id"])
                        hw_list['name'] = get_irmc_json(ctrl, ["Model"])
                        hw_list['firmware'] = get_irmc_json(
                            ctrl, ["FirmwareVersion"])
                        hw_list['drives'] = get_irmc_json(
                            ctrl, ["Oem", "ts_fujitsu", "DriveCount"])
                        hw_list['volumes'] = get_irmc_json(
                            ctrl, ["Oem", "ts_fujitsu", "VolumeCount"])
                items += 1
                if hw_list:
                    hw_dict['devices'].append(hw_list)
        hw_dict['count'] = items
        if hw == "Storage":
            hw = "StorageControllers"
        if hw in ("Memory", "Processors"):
            hw_dict['sockets'] = get_irmc_json(hwdata.json(),
                                               "*****@*****.**")
        result['facts']['hardware'][hw.lower()] = hw_dict
    return result
def wait_for_update_to_finish(module, location, power_state):
    rebootDone = None
    start_time = time.time()
    while True:
        time.sleep(5)
        elapsed_time = time.time() - start_time
        # make sure the module does not get stuck if anything goes wrong
        now = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
        if elapsed_time > module.params['timeout'] * 60:
            msg = "Timeout of {0} minutes exceeded. Abort.".format(
                module.params['timeout'])
            module.fail_json(msg=msg, status=20)

        status, sdata, msg = irmc_redfish_get(module,
                                              "{0}".format(location[1:]))
        if status == 99:
            time.sleep(55)
            continue
        elif status == 404:
            if rebootDone is True:
                result['changed'] = True
                break
            continue
        elif status == 503:
            if rebootDone is None:
                rebootDone = False  # just in case we miss the 'complete' message
            else:
                rebootDone = True
            time.sleep(25)
            continue
        elif status < 100 or (status not in (200, 202, 204)):
            time.sleep(5)
            continue

        if "Key" in get_irmc_json(sdata.json(), "error"):
            rebootDone = False
            oemstate = get_irmc_json(sdata.json(),
                                     ["Oem", "ts_fujitsu", "StatusOEM"])
            state = get_irmc_json(sdata.json(), "TaskState")
            # make sure the process ran through
            if power_state == "On" and oemstate == "Pending":
                msg = "A BIOS firmware update has been started and a system reboot is required to continue the update."
                result['warnings'] = msg
                break
            if power_state == "On" and oemstate == "FlashImageDownloadedSuccessfully":
                msg = "A BIOS firmware update has been started. A system reboot is required to continue the update."
                result['warnings'] = msg
                break
            if state == "Exception":
                msg = "{0}: Update failed.".format(now)
                module.fail_json(msg=msg, status=21)
            # for BIOS we are done here, for iRMC we need to wait for iRMC shutdown and reboot
            if module.params['update_type'] == "bios" and state == "Completed":
                result['changed'] = True
                break
        else:
            break
def irmc_powerstate(module):
    result = dict(changed=False, status=0)

    if module.check_mode:
        result['msg'] = "module was not run"
        module.exit_json(**result)

    # preliminary parameter check
    if module.params['command'] == "set" and module.params['state'] is None:
        result['msg'] = "Command 'set' requires 'state' parameter to be set!"
        result['status'] = 10
        module.fail_json(**result)

    # Get iRMC system data
    status, sysdata, msg = irmc_redfish_get(module, "redfish/v1/Systems/0/")
    if status < 100:
        module.fail_json(msg=msg, status=status, exception=sysdata)
    elif status != 200:
        module.fail_json(msg=msg, status=status)

    power_state = get_irmc_json(sysdata.json(), "PowerState")
    if module.params['command'] == "get":
        result['power_state'] = power_state
        module.exit_json(**result)

    # Evaluate function params against iRMC
    if "Power" + power_state == module.params['state'].replace("Graceful", ""):
        result['skipped'] = True
        result['msg'] = "PRIMERGY server is already in state '{0}'".format(
            power_state)
        module.exit_json(**result)

    allowedparams = \
        get_irmc_json(sysdata.json(),
                      ["Actions", "Oem",
                       "http://ts.fujitsu.com/redfish-schemas/v1/FTSSchema.v1_0_0#FTSComputerSystem.Reset",
                       "*****@*****.**"])
    if module.params['state'] not in allowedparams:
        result['msg'] = "Invalid parameter '{0}'. Allowed: {1}". \
                        format(module.params['state'], json.dumps(allowedparams))
        result['status'] = 11
        module.fail_json(**result)

    # Set iRMC system data
    body = {'FTSResetType': module.params['state']}
    status, sysdata, msg = irmc_redfish_post(
        module, "redfish/v1/Systems/0/Actions/Oem/FTSComputerSystem.Reset",
        json.dumps(body))
    if status < 100:
        module.fail_json(msg=msg, status=status, exception=sysdata)
    elif status not in (200, 202, 204):
        module.fail_json(msg=msg, status=status)

    result['changed'] = True
    module.exit_json(**result)
def irmc_setnextboot(module):
    result = dict(changed=False, status=0)

    if module.check_mode:
        result['msg'] = "module was not run"
        module.exit_json(**result)

    # Get iRMC system data
    status, sysdata, msg = irmc_redfish_get(module, "redfish/v1/Systems/0/")
    if status < 100:
        module.fail_json(msg=msg, status=status, exception=sysdata)
    elif status != 200:
        module.fail_json(msg=msg, status=status)

    # Evaluate function params against iRMC
    bootsourceallowed = get_irmc_json(
        sysdata.json(),
        ["Boot", "*****@*****.**"])
    if module.params['bootsource'] not in bootsourceallowed:
        result['msg'] = "Invalid parameter '" + module.params['bootsource'] + "' for function. Allowed: " + \
                        json.dumps(bootsourceallowed)
        result['status'] = 10
        module.fail_json(**result)

    # evaluate parameters
    bootoverrideallowed = get_irmc_json(
        sysdata.json(),
        ["Boot", "*****@*****.**"])
    if module.params['bootoverride'] not in bootoverrideallowed:
        result['msg'] = "Invalid parameter '" + module.params['bootoverride'] + "' for function. Allowed: " + \
                        json.dumps(bootoverrideallowed)
        result['status'] = 11
        module.fail_json(**result)

    # Set iRMC system data
    body = {
        "Boot": {
            "BootSourceOverrideTarget": module.params['bootsource'],
            "BootSourceOverrideEnabled": module.params['bootoverride']
        }
    }
    if module.params['bootmode'] is not None:
        body['Boot']['BootSourceOverrideMode'] = module.params['bootmode']
    etag = get_irmc_json(sysdata.json(), "@odata.etag")
    status, patch, msg = irmc_redfish_patch(module, "redfish/v1/Systems/0/",
                                            json.dumps(body), etag)
    if status < 100:
        module.fail_json(msg=msg, status=status, exception=patch)
    elif status != 200:
        module.fail_json(msg=msg, status=status)

    result['changed'] = True
    module.exit_json(**result)
def get_boot_profile_data(module):
    status, sysdata, msg = irmc_redfish_get(module, "rest/v1/Oem/eLCM/ProfileManagement/BiosBootOrder")
    if status < 100:
        module.fail_json(msg=msg, status=status, exception=sysdata)
    elif status == 404:     # Boot Profile does not yet exist, create
        result['msg'] = "Boot Profile does not yet exist. Create manually or restart with 'force_new' set to 'True'."
        result['status'] = status
        module.fail_json(**result)
    elif status != 200:
        module.fail_json(msg=msg, status=status)

    return sysdata.json()
Example #14
0
def irmc_elcm_online_update(module):
    # initialize result
    result['changed'] = False
    result['status'] = 0

    if module.check_mode:
        result['msg'] = "module was not run"
        module.exit_json(**result)

    # check eLCM status
    status, data, msg = elcm_check_status(module)
    if status > 30 and status < 100:
        module.fail_json(msg=msg, status=status, exception=data)
    elif status < 30 or status not in (200, 202, 204):
        module.fail_json(msg=msg, status=status)

    # Get server power state
    status, sysdata, msg = irmc_redfish_get(module, "redfish/v1/Systems/0/")
    if status < 100:
        module.fail_json(msg=msg, status=status, exception=sysdata)
    elif status != 200:
        module.fail_json(msg=msg, status=status)
    if get_irmc_json(sysdata.json(), "PowerState") == "Off":
        result['msg'] = "Server is powered off. Cannot continue."
        result['status'] = 12
        module.fail_json(**result)

    # preliminary parameter check
    if module.params['command'] == "set":
        if module.params['component'] is None and module.params[
                'subcomponent'] is None:
            result[
                'msg'] = "Command 'set' requires 'component' and 'subcomponent' parameters to be set!"
            result['status'] = 10
            module.fail_json(**result)
        if module.params['select'] is None:
            result[
                'msg'] = "Command 'set' requires 'select' parameter to be set!"
            result['status'] = 11
            module.fail_json(**result)

    # start doing the actual work
    if module.params['command'] == 'set':
        elcm_change_component(module)

    if module.params['command'] in ("get", "delete"):
        elcm_online_collection(module)

    if module.params['command'] in ("check", "execute"):
        elcm_online_update(module)

    module.exit_json(**result)
def irmc_idled(module):
    result = dict(changed=False, status=0)

    if module.check_mode:
        result['msg'] = "module was not run"
        module.exit_json(**result)

    # preliminary parameter check
    if module.params['command'] == "set" and module.params['state'] is None:
        result['msg'] = "Command 'set' requires 'state' parameter to be set!"
        result['status'] = 10
        module.fail_json(**result)

    # get iRMC system data
    status, sysdata, msg = irmc_redfish_get(module, "redfish/v1/Systems/0/")
    if status < 100:
        module.fail_json(msg=msg, status=status, exception=sysdata)
    elif status != 200:
        module.fail_json(msg=msg, status=status)

    idledstate = get_irmc_json(sysdata.json(), "IndicatorLED")
    if module.params['command'] == "get":
        result['idled_state'] = idledstate
        module.exit_json(**result)

    # evaluate function params against iRMC
    if idledstate == module.params['state']:
        result['skipped'] = True
        result['msg'] = "iRMC ID LED is already in state '{0}'".format(
            module.params['state'])
        module.exit_json(**result)

    allowedparams = get_irmc_json(sysdata.json(),
                                  "*****@*****.**")
    if module.params['state'] not in allowedparams:
        result['msg'] = "Invalid parameter '{0}'. Allowed: {1}".format(
            module.params['state'], json.dumps(allowedparams))
        result['status'] = 11
        module.fail_json(**result)

    # set iRMC system data
    body = {'IndicatorLED': module.params['state']}
    etag = get_irmc_json(sysdata.json(), "@odata.etag")
    status, patch, msg = irmc_redfish_patch(module, "redfish/v1/Systems/0/",
                                            json.dumps(body), etag)
    if status < 100:
        module.fail_json(msg=msg, status=status, exception=patch)
    elif status != 200:
        module.fail_json(msg=msg, status=status)

    result['changed'] = True
    module.exit_json(**result)
Example #16
0
def irmc_setvirtualmedia(module):
    result = dict(changed=False, status=0)

    if module.check_mode:
        result['msg'] = "module was not run"
        module.exit_json(**result)

    vmparams, status = setup_datadict(module)

    # Get iRMC Virtual Media data
    status, vmdata, msg = irmc_redfish_get(
        module, "redfish/v1/Systems/0/Oem/ts_fujitsu/VirtualMedia/")
    if status < 100:
        module.fail_json(msg=msg, status=status, exception=vmdata)
    elif status != 200:
        module.fail_json(msg=msg, status=status)

    # Evaluate configured Virtual Media Data
    maxdevno = get_irmc_json(
        vmdata.json(), [module.params['vm_type'], "MaximumNumberOfDevices"])
    if maxdevno == 0:
        if not module.params['force_mediatype_active']:
            result['warnings'] = "No Virtual Media of Type '" + module.params[
                'vm_type'] + "' is configured!"
            result['status'] = 20
            module.fail_json(**result)
        else:
            new_maxdevno = 1
    else:
        new_maxdevno = maxdevno

    remotemountenabled = get_irmc_json(vmdata.json(), "RemoteMountEnabled")
    if not remotemountenabled and not module.params[
            'force_remotemount_enabled']:
        result['msg'] = "Remote Mount of Virtual Media is not enabled!"
        result['status'] = 30
        module.fail_json(**result)

    # Set iRMC system data
    body = setup_vmdata(vmparams, maxdevno, new_maxdevno)
    etag = get_irmc_json(vmdata.json(), "@odata.etag")
    status, patch, msg = irmc_redfish_patch(
        module, "redfish/v1/Systems/0/Oem/ts_fujitsu/VirtualMedia/",
        json.dumps(body), etag)
    if status < 100:
        module.fail_json(msg=msg, status=status, exception=patch)
    elif status != 200:
        module.fail_json(msg=msg, status=status)

    result['changed'] = True
    module.exit_json(**result)
def add_irmc_hw_info(module, result):
    # get iRMC info
    status, hwdata, msg = irmc_redfish_get(
        module, "redfish/v1/Managers/iRMC/EthernetInterfaces?$expand=Members")
    if status < 100:
        module.fail_json(msg=msg, status=status, exception=hwdata)
    elif status != 200:
        module.fail_json(msg=msg, status=status)
    for member in get_irmc_json(hwdata.json(), "Members"):
        result['facts']['irmc']['macaddress'] = "{0}".format(
            get_irmc_json(member, ["MACAddress"]))
        result['facts']['irmc']['hostname'] = "{0}".format(
            get_irmc_json(member, ["HostName"]))
    return result
def get_profile(module):
    status, sysdata, msg = irmc_redfish_get(
        module, "/rest/v1/Oem/eLCM/ProfileManagement/{0}".format(
            module.params['profile']))
    if status < 100:
        module.fail_json(msg=msg, status=status, exception=sysdata)
    elif status == 404:
        module.fail_json(msg="Requested profile '{0}' does not exist.".format(
            module.params['profile']),
                         status=status)
    elif status != 200:
        module.fail_json(msg=msg, status=status)

    result['profile'] = sysdata.json()
def handle_irmc_session(module, command, item):
    status, sdata, msg = irmc_redfish_get(
        module, "sessionInformation/{0}/status".format(item['@Id']))
    if status < 100:
        module.fail_json(msg=msg, status=status, exception=sdata)
    elif status not in (200, 202, 204):
        module.fail_json(msg=msg, status=status)

    sstatus = get_irmc_json(sdata.json(), ["Session", "Status"])
    if command == "get":
        status, sdata, msg = irmc_redfish_get(
            module, "sessionInformation/{0}/log".format(item['@Id']))
        if status < 100:
            module.fail_json(msg=msg, status=status, exception=sdata)
        elif status not in (200, 202, 204):
            module.fail_json(msg=msg, status=status)
        result['session_status'] = sstatus
        result['session_log'] = sdata.json()
        module.exit_json(**result)
    elif command == "terminate" and "terminat" in sstatus:
        result['msg'] = "Session '{0}'/'{1}' is already terminated.".format(
            item['@Id'], item['#text'])
        result['skipped'] = True
    elif command == "remove" and "terminated" not in sstatus:
        if module.params['command'] != "clearall":
            result['msg'] = "Session '{0}'/'{1}' is not yet terminated and cannot be removed.". \
                            format(item['@Id'], item['#text'])
            module.exit_json(**result)
    else:
        status, sdata, msg = irmc_redfish_delete(
            module, "sessionInformation/{0}/{1}".format(item['@Id'], command))
        if status < 100:
            module.fail_json(msg=msg, status=status, exception=sdata)
        elif status not in (200, 202, 204):
            module.fail_json(msg=msg, status=status)
        result['changed'] = True
def waitForIrmcSessionsInactive(module):
    # Get iRMC Profile processing state
    status, sessiondata, msg = irmc_redfish_get(module, "sessionInformation")
    if status < 100:
        module.fail_json(msg=msg, status=status, exception=sessiondata)
    elif status not in (200, 202, 204):
        module.fail_json(msg=msg, status=status)

    sessions = get_irmc_json(sessiondata.json(), ["SessionList"])
    for status, session in sessions.items():
        for item in session:
            for ikey, value in item.items():
                if ikey == "#text" and "Profile" in value:
                    status, sessiondata, msg = waitForSessionToFinish(module, item['@Id'])
        continue
def get_irmc_session_info(module, item):
    status, sdata, msg = irmc_redfish_get(
        module, "sessionInformation/{0}/status".format(item['@Id']))
    if status < 100:
        module.fail_json(msg=msg, status=status, exception=sdata)
    elif status not in (200, 202, 204):
        module.fail_json(msg=msg, status=status)

    session = {}
    session['Id'] = item['@Id']
    session['Text'] = item['#text']
    session['Tag'] = item['@Tag']
    session['Status'] = get_irmc_json(sdata.json(), ["Session", "Status"])
    session['Start'] = get_irmc_json(sdata.json(), ["Session", "Start"])
    session['Duration'] = get_irmc_json(sdata.json(), ["Session", "Duration"])
    return session
def get_raid_data(module):
    # make sure RAIDAdapter profile is up-to-date
    status, sysdata, msg = irmc_redfish_delete(
        module, "/rest/v1/Oem/eLCM/ProfileManagement/RAIDAdapter")
    if status < 100:
        module.fail_json(msg=msg, status=status, exception=sysdata)
    elif status not in (200, 202, 204, 404):
        module.fail_json(msg=msg, status=status)

    url = "rest/v1/Oem/eLCM/ProfileManagement/get?PARAM_PATH=Server/HWConfigurationIrmc/Adapters/RAIDAdapter"
    status, sysdata, msg = irmc_redfish_post(module, url, "")
    if status < 100:
        module.fail_json(msg=msg, status=status, exception=sysdata)
    elif status == 404:
        result[
            'msg'] = "Requested profile 'HWConfigurationIrmc/Adapters/RAIDAdapter' cannot be created."
        module.fail_json(msg=msg, status=status)
    elif status == 409:
        result[
            'msg'] = "Requested profile 'HWConfigurationIrmc/Adapters/RAIDAdapter' already exists."
        module.fail_json(msg=msg, status=status)
    elif status not in (200, 202, 204):
        module.fail_json(msg=msg, status=status)

    if module.params['wait_for_finish'] is True:
        # check that current session is terminated
        status, data, msg = waitForSessionToFinish(
            module, get_irmc_json(sysdata.json(), ["Session", "Id"]))
        if status > 30 and status < 100:
            module.fail_json(msg=msg, status=status, exception=data)
        elif status not in (200, 202, 204):
            module.fail_json(msg=msg, log=data, status=status)

    status, sysdata, msg = irmc_redfish_get(
        module, "/rest/v1/Oem/eLCM/ProfileManagement/RAIDAdapter")
    if status < 100:
        module.fail_json(msg=msg, status=status, exception=sysdata)
    elif status == 404:
        module.fail_json(
            msg=
            "Requested profile 'HWConfigurationIrmc/Adapters/RAIDAdapter' does not exist.",
            status=status)
    elif status != 200:
        module.fail_json(msg=msg, status=status)

    return sysdata.json()
def list_profiles(module):
    status, sysdata, msg = irmc_redfish_get(
        module, "/rest/v1/Oem/eLCM/ProfileManagement")
    if status < 100:
        module.fail_json(msg=msg, status=status, exception=sysdata)
    elif status != 200:
        module.fail_json(msg=msg, status=status)

    result['profiles'] = {}
    for profile in get_irmc_json(sysdata.json(), ["Links", "profileStore"]):
        for status, value in profile.items():
            profile = {}
            profile['Name'] = value.replace(
                "rest/v1/Oem/eLCM/ProfileManagement/", "")
            profile['Location'] = value
            result['profiles'][value.replace(
                "rest/v1/Oem/eLCM/ProfileManagement/", "")] = profile
def preliminary_parameter_check(module):
    if module.params['command'] == "set" and module.params['boot_device'] is None:
        result['msg'] = "Command 'set' requires 'boot_device' parameter to be set!"
        result['status'] = 10
        module.fail_json(**result)

    if module.params['command'] in ("set", "default") and module.params['ignore_power_on'] is False:
        # Get server power state
        status, sysdata, msg = irmc_redfish_get(module, "redfish/v1/Systems/0/")
        if status < 100:
            module.fail_json(msg=msg, status=status, exception=sysdata)
        elif status != 200:
            module.fail_json(msg=msg, status=status)

        if get_irmc_json(sysdata.json(), "PowerState") == "On":
            result['skipped'] = True
            result['warnings'] = "Server is powered on. Cannot continue."
            module.exit_json(**result)
Example #25
0
def irmc_task(module):
    # initialize result
    result['changed'] = False
    result['status'] = 0

    if module.check_mode:
        result['msg'] = "module was not run"
        module.exit_json(**result)

    # preliminary parameter check
    if (module.params['command']
            in ("get", "remove", "terminate")) and module.params['id'] is None:
        result[
            'msg'] = "Command '{0}' requires 'id' parameter to be set!".format(
                module.params['command'])
        result['status'] = 10
        module.fail_json(**result)

    id_found = 0
    if module.params['command'] == "get":
        id_found = 1
        result['task'] = get_irmc_task_info(
            module,
            "/redfish/v1/TaskService/Tasks/{0}".format(module.params['id']),
            module.params['id'])

    if module.params['command'] == "list":
        status, taskdata, msg = irmc_redfish_get(
            module, "redfish/v1/TaskService/Tasks")
        if status < 100:
            module.fail_json(msg=msg, status=status, exception=taskdata)
        elif status not in (200, 202, 204):
            module.fail_json(msg=msg, status=status)
        tasks = get_irmc_json(taskdata.json(), ["Members"])

        result['tasks'] = []
        for task in tasks:
            id_found += 1
            task_url = get_irmc_json(task, "@odata.id")
            myID = task_url.replace("/redfish/v1/TaskService/Tasks/", "")
            task_info = get_irmc_task_info(module, task_url, myID)
            result['tasks'].append(task_info)

    module.exit_json(**result)
Example #26
0
def get_irmc_task_info(module, url, task_id):
    status, sdata, msg = irmc_redfish_get(module, "{0}".format(url[1:]))
    if status < 100:
        module.fail_json(msg=msg, status=status, exception=sdata)
    elif status not in (200, 202, 204):
        module.fail_json(msg=msg, status=status)

    task = {}
    task['Id'] = task_id
    task['Name'] = get_irmc_json(sdata.json(), "Name")
    task['State'] = get_irmc_json(sdata.json(), "TaskState")
    task['StateOem'] = get_irmc_json(sdata.json(),
                                     ["Oem", "ts_fujitsu", "StatusOEM"])
    task['StateProgressPercent'] = get_irmc_json(
        sdata.json(), ["Oem", "ts_fujitsu", "StateProgressPercent"])
    task['TotalProgressPercent'] = get_irmc_json(
        sdata.json(), ["Oem", "ts_fujitsu", "TotalProgressPercent"])
    task['StartTime'] = get_irmc_json(sdata.json(), "StartTime")
    task['EndTime'] = get_irmc_json(sdata.json(), "EndTime")
    return task
def add_chassis_hw_info(module, result):
    # get chassis hardware
    hw_source = {
        "redfish/v1/Chassis/0/Thermal#/Fans": ["Fans"],
        "redfish/v1/Chassis/0/Power#/PowerSupplies": ["PowerSupplies"],
    }
    for hw_link, hw_list in hw_source.items():
        status, hwdata, msg = irmc_redfish_get(module, hw_link)
        if status < 100:
            module.fail_json(msg=msg, status=status, exception=hwdata)
        elif status != 200:
            module.fail_json(msg=msg, status=status)
        for hw in hw_list:
            items = 0
            hw_dict = {}
            hw_dict['devices'] = []
            for member in get_irmc_json(hwdata.json(), hw):
                hw_list = {}
                if get_irmc_json(member, ["Status", "State"]) == "Enabled":
                    if hw == "PowerSupplies":
                        hw_list['id'] = get_irmc_json(member, ["MemberId"])
                        hw_list['name'] = get_irmc_json(member, ["Name"])
                        hw_list['manufacturer'] = get_irmc_json(
                            member, ["Manufacturer"])
                        hw_list['model'] = get_irmc_json(member, ["Model"])
                    elif hw == "Voltages":
                        hw_list['id'] = get_irmc_json(member, ["MemberId"])
                        hw_list['name'] = get_irmc_json(member, ["Name"])
                    else:
                        hw_list['id'] = get_irmc_json(member, ["MemberId"])
                        hw_list['name'] = get_irmc_json(member, ["Name"])
                        hw_list['location'] = get_irmc_json(
                            member, ["PhysicalContext"])
                    items += 1
                    if hw_list:
                        hw_dict['devices'].append(hw_list)
            hw_dict['count'] = items
            hw_dict['sockets'] = get_irmc_json(hwdata.json(),
                                               "{0}@odata.count".format(hw))
            result['facts']['hardware'][hw.lower()] = hw_dict
    return result
Example #28
0
def get_elcm_data(module):
    status, elcmdata, msg = irmc_redfish_get(
        module, "rest/v1/Oem/eLCM/Repository/Update")
    if status < 100:
        module.fail_json(msg=msg, status=status, exception=elcmdata)
    elif status not in (200, 202, 204):
        module.fail_json(msg=msg, status=status)

    result['repository'] = {}
    result['repository']['server'] = get_irmc_json(
        elcmdata.json(), ["Repository", "Server", "URL"])
    result['repository']['catalog'] = get_irmc_json(
        elcmdata.json(), ["Repository", "Server", "Catalog"])
    result['repository']['use_proxy'] = get_irmc_json(
        elcmdata.json(), ["Repository", "Server", "UseProxy"])
    result['repository']['proxy_url'] = get_irmc_json(
        elcmdata.json(), ["Repository", "Proxy", "URL"])
    result['repository']['proxy_port'] = get_irmc_json(
        elcmdata.json(), ["Repository", "Proxy", "Port"])
    result['repository']['proxy_user'] = get_irmc_json(
        elcmdata.json(), ["Repository", "Proxy", "User"])
    result['repository']['proxy_password'] = get_irmc_json(
        elcmdata.json(), ["Repository", "Proxy", "Password"])
def irmc_getvirtualmedia(module):
    result = dict(
        changed=False,
        status=0
    )

    if module.check_mode:
        result['msg'] = "module was not run"
        module.exit_json(**result)

    # get iRMC system data
    status, sysdata, msg = irmc_redfish_get(module, "redfish/v1/Systems/0/")
    if status < 100:
        module.fail_json(msg=msg, status=status, exception=sysdata)
    elif status != 200:
        module.fail_json(msg=msg, status=status)

    # Evaluate VM connection state
    vm_type = module.params['vm_type'].replace("Image", "")
    allowedparams = \
        get_irmc_json(sysdata.json(),
                      ["Actions", "Oem",
                       "http://ts.fujitsu.com/redfish-schemas/v1/FTSSchema.v1_0_0#FTSComputerSystem.VirtualMedia",
                       "*****@*****.**"])

    # determine current connection state
    vmdict = dict()
    if "Connect" + vm_type not in allowedparams:
        if "Disconnect" + vm_type not in allowedparams:
            vmdict[module.params['vm_type']] = "NotConfigured"
        else:
            vmdict[module.params['vm_type']] = "Connected"
    else:
        vmdict[module.params['vm_type']] = "Disconnected"

    # eet iRMC Virtual Media data
    status, vmdata, msg = irmc_redfish_get(module, "redfish/v1/Systems/0/Oem/ts_fujitsu/VirtualMedia/")
    if status < 100:
        module.fail_json(msg=msg, status=status, exception=vmdata)
    elif status != 200:
        module.fail_json(msg=msg, status=status)

    # extract specified Virtual Media data
    remotemountenabled = get_irmc_json(vmdata.json(), "RemoteMountEnabled")
    if not remotemountenabled:
        vmdict['remote_mount_disabled'] = "Remote Mount of Virtual Media is not enabled!"
    vmdict['usb_attach_mode'] = get_irmc_json(vmdata.json(), "UsbAttachMode")
    vmdict['bootsource'] = get_irmc_json(sysdata.json(), ["Boot", "BootSourceOverrideTarget"])
    vmdict['bootoverride'] = get_irmc_json(sysdata.json(), ["Boot", "BootSourceOverrideEnabled"])
    vmdict['bootmode'] = get_irmc_json(sysdata.json(), ["Boot", "BootSourceOverrideMode"])
    maxdevno = get_irmc_json(vmdata.json(), [module.params['vm_type'], "MaximumNumberOfDevices"])
    if maxdevno == 0:
        vmdict['no_vm_configured'] = "No Virtual Media of Type '" + module.params['vm_type'] + \
                                     "' is configured!"
    else:
        vmdict['image_name'] = get_irmc_json(vmdata.json(), [module.params['vm_type'], "ImageName"])
        vmdict['server'] = get_irmc_json(vmdata.json(), [module.params['vm_type'], "Server"])
        vmdict['share_name'] = get_irmc_json(vmdata.json(), [module.params['vm_type'], "ShareName"])
        vmdict['share_type'] = get_irmc_json(vmdata.json(), [module.params['vm_type'], "ShareType"])
        vmdict['user_domain'] = get_irmc_json(vmdata.json(), [module.params['vm_type'], "UserDomain"])
        vmdict['user_name'] = get_irmc_json(vmdata.json(), [module.params['vm_type'], "UserName"])
    result['virtual_media_data'] = vmdict
    module.exit_json(**result)
Example #30
0
def irmc_elcm_offline_update(module):
    # initialize result
    result['changed'] = False
    result['status'] = 0

    if module.check_mode:
        result['msg'] = "module was not run"
        module.exit_json(**result)

    # check eLCM status
    status, data, msg = elcm_check_status(module)
    if status > 30 and status < 100:
        module.fail_json(msg=msg, status=status, exception=data)
    elif status < 30 or status not in (200, 202, 204):
        module.fail_json(msg=msg, status=status)

    if module.params['command'] == "execute" and module.params[
            'ignore_power_on'] is False:
        # Get server power state
        status, sysdata, msg = irmc_redfish_get(module,
                                                "redfish/v1/Systems/0/")
        if status < 100:
            module.fail_json(msg=msg, status=status, exception=sysdata)
        elif status != 200:
            module.fail_json(msg=msg, status=status)
        if get_irmc_json(sysdata.json(), "PowerState") == "On":
            result['msg'] = "Server is powered on. Cannot continue."
            result['status'] = 10
            module.fail_json(**result)

    if module.params['command'] == "prepare":
        uri = "rest/v1/Oem/eLCM/OfflineUpdate"
        if module.params['skip_hcl_verify'] is True:
            uri = uri + "?skipHCLVerification=yes"
        status, elcmdata, msg = irmc_redfish_post(module, uri, "")
    else:
        status, elcmdata, msg = irmc_redfish_put(
            module, "rest/v1/Oem/eLCM/OfflineUpdate", "")
    if status < 100:
        module.fail_json(msg=msg, status=status, exception=elcmdata)
    elif status == 409:
        result[
            'msg'] = "Cannot {0} eLCM update, another session is in progress.".format(
                module.params['command'])
        result['status'] = status
        module.fail_json(**result)
    elif status not in (200, 202, 204):
        module.fail_json(msg=msg, status=status)

    if module.params['wait_for_finish'] is True:
        # check that current session is terminated
        status, data, msg = waitForSessionToFinish(
            module, get_irmc_json(elcmdata.json(), ["Session", "Id"]))
        if status > 30 and status < 100:
            module.fail_json(msg=msg, status=status, exception=data)
        elif status not in (200, 202, 204):
            module.fail_json(msg=msg, log=data, status=status)

    result['changed'] = True

    module.exit_json(**result)