def run_module():
    module_args = dict(
        address=dict(type="str", required=True),
        username=dict(type="str", required=True),
        password=dict(type="str", required=True, no_log=True),
        bios=dict(type="dict", required=True),
    )
    module = AnsibleModule(argument_spec=module_args, supports_check_mode=True)
    result = dict(changed=False)

    # Skip if check mode
    if module.check_mode:
        module.exit_json(**result)

    client = dracclient.client.DRACClient(
        host=module.params["address"],
        username=module.params["username"],
        password=module.params["password"],
    )
    client.is_idrac_ready()

    jobs = client.list_jobs(only_unfinished=True)
    if len(jobs) > 0:
        module.fail_json(msg="pending idrac jobs")

    # check boot order, drop request if its really a no op
    bios_settings = module.params["bios"]
    if "SetBootOrderFqdd1" in bios_settings:
        requested = (
            f"{bios_settings['SetBootOrderFqdd1']},"
            f"{bios_settings.get('SetBootOrderFqdd2')},"
            f"{bios_settings.get('SetBootOrderFqdd3')},"
            f"{bios_settings.get('SetBootOrderFqdd4')}"
        )
        # Remove any trailing commas, if we have three interfaces
        requested = requested.strip(",")
        current_settings = to_dict(client.list_bios_settings())
        current = current_settings["SetBootOrderEn"]["current_value"]
        result["requested_setting"] = requested
        result["current_settings"] = current
        if requested == current:
            del bios_settings["SetBootOrderFqdd1"]
            del bios_settings["SetBootOrderFqdd2"]
            del bios_settings["SetBootOrderFqdd3"]
            del bios_settings["SetBootOrderFqdd4"]

    bios_result = client.set_bios_settings(bios_settings)
    if bios_result and bios_result["is_commit_required"]:
        result["changed"] = True
        reboot_required = bios_result["is_reboot_required"]
        client.commit_pending_bios_changes(reboot=reboot_required)
        wait_for_jobs({module.params["address"]: client})

    module.exit_json(**result)
Пример #2
0
    def test_is_idrac_ready_not_ready(self, mock_requests):
        expected_text = test_utils.LifecycleControllerInvocations[
            uris.DCIM_LCService]['GetRemoteServicesAPIStatus']['is_not_ready']
        mock_requests.post('https://1.2.3.4:443/wsman', text=expected_text)

        client = dracclient.client.WSManClient(**test_utils.FAKE_ENDPOINT)
        self.assertFalse(client.is_idrac_ready())
def update_settings(client, bios_settings=None, idrac_settings=None):
    if not client.is_idrac_ready():
        print("iDRAC not ready for update settings, skipping")
        return

    jobs = client.list_jobs(only_unfinished=True)
    if len(jobs) > 0:
        pprint.pprint(jobs)
        print("skip update settings, jobs in progress")
        return

    if bios_settings is None:
        bios_settings = {
          "LogicalProc": "Disabled",
          "SysProfile": "PerfOptimized",
          #"SetBootOrderEn": "NIC.Embedded.1-1-1,HardDisk.List.1-1",
          #"SetBootOrderEn": "NIC.Slot.4-1,InfiniBand.Slot.4-1,NIC.Embedded.1-1-1,HardDisk.List.1-1",
          #"SetBootOrderEn": "NIC.Embedded.1-1-1,HardDisk.List.1-1",
          "SetBootOrderFqdd1": "NIC.Embedded.1-1-1",
          "SetBootOrderFqdd2": "HardDisk.List.1-1",
          #"SetBootOrderFqdd3": "InfiniBand.Slot.4-1",
          "SetBootOrderFqdd3": "",
          #"SetBootOrderFqdd4": "InfiniBand.Slot.4-2",
          "SetBootOrderFqdd4": "",
          #"EmbNic1": "DisabledOs",
          "EmbNic1": "Enabled", # this is the default
        }
    bios_result = None
    if bios_settings:
        bios_result = client.set_bios_settings(bios_settings)
        print(bios_result)

    if idrac_settings is None:
        idrac_settings = {
          "IPMILan.1#Enable": "Enabled",
        }
    idrac_result = None
    if idrac_settings: 
        idrac_result = client.set_idrac_settings(idrac_settings)
        print(idrac_result)

    reboot_required = bios_result['is_reboot_required']
    if bios_result and bios_result['is_commit_required']:
        reboot_required = bios_result['is_reboot_required']
        print(client.commit_pending_bios_changes(reboot=reboot_required))

    if idrac_result and idrac_result['is_commit_required']:
        reboot_required = idrac_result['is_reboot_required']
        print(client.commit_pending_idrac_changes(reboot=reboot_required))

    return {
        "rebooted": reboot_required
    }
def wait_for_jobs(clients):
    pending = {}

    for name, client in clients.items():
        if not client.is_idrac_ready():
            pending[name] = client
        else:
            jobs = client.list_jobs(only_unfinished=True)
            if len(jobs) > 0:
                pending[name] = client
            else:
                # TODO: check job was a success
                print(name + " has no unfinished jobs")

    if len(pending) > 0:
        hosts = ",".join(list(pending.keys()))
        print("Unfinished jobs found: " + hosts)
        time.sleep(5)
        wait_for_jobs(pending)
    conn = openstack.connection.from_config(cloud="arcus", debug=True)

    nodes = get_nodes_in_rack(conn, "DR06")

    clients = {}
    for node in nodes:
        ip = node["driver_info"]["drac_address"]
        name = node["name"]
        client = dracclient.client.DRACClient(
            host=ip,
            username="******",
            password="******")
        #print(json.dumps(get_all_settings(client), indent=2))
        #exit(0)
        #print("Try BIOS Update for " + ip + " " + name)
        #update_settings(client)
        #print("Submitted BIOS Update for " + ip + " " + name)

        #double check we can talk to idrac
        is_working = False
        try:
            client.is_idrac_ready()
            is_working = True
        except Exception as e:
            print("ERROR: host %s has error: %s", name, e)

        if is_working:
            clients[name] = client

    wait_for_jobs(clients)