| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
from __future__ import (absolute_import, division, print_function) |
| 14 |
__metaclass__ = type |
| 15 |
|
| 16 |
DOCUMENTATION = r''' |
| 17 |
--- |
| 18 |
module: idrac_firmware |
| 19 |
short_description: Firmware update from a repository on a network share (CIFS, NFS, HTTP, HTTPS, FTP) |
| 20 |
version_added: "2.1.0" |
| 21 |
description: |
| 22 |
- Update the Firmware by connecting to a network share (CIFS, NFS, HTTP, HTTPS, FTP) that contains a catalog of |
| 23 |
available updates. |
| 24 |
- Network share should contain a valid repository of Update Packages (DUPs) and a catalog file describing the DUPs. |
| 25 |
- All applicable updates contained in the repository are applied to the system. |
| 26 |
- This feature is available only with iDRAC Enterprise License. |
| 27 |
extends_documentation_fragment: |
| 28 |
- dellemc.openmanage.idrac_auth_options |
| 29 |
options: |
| 30 |
share_name: |
| 31 |
description: Network share path of update repository. CIFS, NFS, HTTP, HTTPS and FTP share types are supported. |
| 32 |
type: str |
| 33 |
required: true |
| 34 |
share_user: |
| 35 |
description: Network share user in the format 'user@domain' or 'domain\\user' if user is |
| 36 |
part of a domain else 'user'. This option is mandatory for CIFS Network Share. |
| 37 |
type: str |
| 38 |
share_password: |
| 39 |
description: Network share user password. This option is mandatory for CIFS Network Share. |
| 40 |
type: str |
| 41 |
aliases: ['share_pwd'] |
| 42 |
share_mnt: |
| 43 |
description: |
| 44 |
- Local mount path of the network share with read-write permission for ansible user. |
| 45 |
- This option is not applicable for HTTP, HTTPS, and FTP shares. |
| 46 |
type: str |
| 47 |
job_wait: |
| 48 |
description: Whether to wait for job completion or not. |
| 49 |
type: bool |
| 50 |
default: true |
| 51 |
catalog_file_name: |
| 52 |
description: Catalog file name relative to the I(share_name). |
| 53 |
type: str |
| 54 |
default: 'Catalog.xml' |
| 55 |
ignore_cert_warning: |
| 56 |
description: Specifies if certificate warnings are ignored when HTTPS share is used. |
| 57 |
If C(true) option is set, then the certificate warnings are ignored. |
| 58 |
type: bool |
| 59 |
default: true |
| 60 |
apply_update: |
| 61 |
description: |
| 62 |
- If I(apply_update) is set to C(true), then the packages are applied. |
| 63 |
- If I(apply_update) is set to C(false), no updates are applied, and a catalog report |
| 64 |
of packages is generated and returned. |
| 65 |
type: bool |
| 66 |
default: true |
| 67 |
reboot: |
| 68 |
description: |
| 69 |
- Provides the option to apply the update packages immediately or in the next reboot. |
| 70 |
- If I(reboot) is set to C(true), then the packages are applied immediately. |
| 71 |
- If I(reboot) is set to C(false), then the packages are staged and applied in the next reboot. |
| 72 |
- Packages that do not require a reboot are applied immediately irrespective of I (reboot). |
| 73 |
type: bool |
| 74 |
default: false |
| 75 |
proxy_support: |
| 76 |
description: |
| 77 |
- Specifies if a proxy should be used. |
| 78 |
- Proxy parameters are applicable on C(HTTP), C(HTTPS), and C(FTP) share type of repositories. |
| 79 |
- C(ParametersProxy), sets the proxy parameters for the current firmware operation. |
| 80 |
- C(DefaultProxy), iDRAC uses the proxy values set by default. |
| 81 |
- Default Proxy can be set in the Lifecycle Controller attributes using M(dellemc.openmanage.idrac_attributes). |
| 82 |
- C(Off), will not use the proxy. |
| 83 |
- For iDRAC8 based servers, use proxy server with basic authentication. |
| 84 |
- "For iDRAC9 based servers, ensure that you use digest authentication for the proxy server, |
| 85 |
basic authentication is not supported." |
| 86 |
choices: ["ParametersProxy", "DefaultProxy", "Off"] |
| 87 |
type: str |
| 88 |
default: "Off" |
| 89 |
proxy_server: |
| 90 |
description: |
| 91 |
- The IP address of the proxy server. |
| 92 |
- "This IP will not be validated. The download job will be created even for invalid I(proxy_server). |
| 93 |
Please check the results of the job for error details." |
| 94 |
- This is required when I(proxy_support) is C(ParametersProxy). |
| 95 |
type: str |
| 96 |
proxy_port: |
| 97 |
description: |
| 98 |
- The Port for the proxy server. |
| 99 |
- This is required when I(proxy_support) is C(ParametersProxy). |
| 100 |
type: int |
| 101 |
proxy_type: |
| 102 |
description: |
| 103 |
- The proxy type of the proxy server. |
| 104 |
- This is required when I(proxy_support) is C(ParametersProxy). |
| 105 |
- "Note: SOCKS4 proxy does not support IPv6 address." |
| 106 |
choices: [HTTP, SOCKS] |
| 107 |
type: str |
| 108 |
proxy_uname: |
| 109 |
description: The user name for the proxy server. |
| 110 |
type: str |
| 111 |
proxy_passwd: |
| 112 |
description: The password for the proxy server. |
| 113 |
type: str |
| 114 |
|
| 115 |
requirements: |
| 116 |
- "omsdk >= 1.2.503" |
| 117 |
- "python >= 3.9.6" |
| 118 |
author: |
| 119 |
- "Rajeev Arakkal (@rajeevarakkal)" |
| 120 |
- "Felix Stephen (@felixs88)" |
| 121 |
- "Jagadeesh N V (@jagadeeshnv)" |
| 122 |
- "Sakshi Makkar (@Sakshi-dell)" |
| 123 |
notes: |
| 124 |
- Run this module from a system that has direct access to Dell iDRAC. |
| 125 |
- Module will report success based on the iDRAC firmware update parent job status if there are no individual |
| 126 |
component jobs present. |
| 127 |
- For server with iDRAC firmware 5.00.00.00 and later, if the repository contains unsupported packages, then the |
| 128 |
module will return success with a proper message. |
| 129 |
- This module supports both IPv4 and IPv6 address for I(idrac_ip) and I(share_name). |
| 130 |
- This module supports C(check_mode). |
| 131 |
''' |
| 132 |
|
| 133 |
EXAMPLES = """ |
| 134 |
--- |
| 135 |
- name: Update firmware from repository on a NFS Share |
| 136 |
dellemc.openmanage.idrac_firmware: |
| 137 |
idrac_ip: "192.168.0.1" |
| 138 |
idrac_user: "user_name" |
| 139 |
idrac_password: "user_password" |
| 140 |
ca_path: "/path/to/ca_cert.pem" |
| 141 |
share_name: "192.168.0.0:/share" |
| 142 |
reboot: true |
| 143 |
job_wait: true |
| 144 |
apply_update: true |
| 145 |
catalog_file_name: "Catalog.xml" |
| 146 |
|
| 147 |
- name: Update firmware from repository on a CIFS Share |
| 148 |
dellemc.openmanage.idrac_firmware: |
| 149 |
idrac_ip: "192.168.0.1" |
| 150 |
idrac_user: "user_name" |
| 151 |
idrac_password: "user_password" |
| 152 |
ca_path: "/path/to/ca_cert.pem" |
| 153 |
share_name: "full_cifs_path" |
| 154 |
share_user: "share_user" |
| 155 |
share_password: "share_password" |
| 156 |
reboot: true |
| 157 |
job_wait: true |
| 158 |
apply_update: true |
| 159 |
catalog_file_name: "Catalog.xml" |
| 160 |
|
| 161 |
- name: Update firmware from repository on a HTTP |
| 162 |
dellemc.openmanage.idrac_firmware: |
| 163 |
idrac_ip: "192.168.0.1" |
| 164 |
idrac_user: "user_name" |
| 165 |
idrac_password: "user_password" |
| 166 |
ca_path: "/path/to/ca_cert.pem" |
| 167 |
share_name: "http://downloads.dell.com" |
| 168 |
reboot: true |
| 169 |
job_wait: true |
| 170 |
apply_update: true |
| 171 |
|
| 172 |
- name: Update firmware from repository on a HTTPS |
| 173 |
dellemc.openmanage.idrac_firmware: |
| 174 |
idrac_ip: "192.168.0.1" |
| 175 |
idrac_user: "user_name" |
| 176 |
idrac_password: "user_password" |
| 177 |
ca_path: "/path/to/ca_cert.pem" |
| 178 |
share_name: "https://downloads.dell.com" |
| 179 |
reboot: true |
| 180 |
job_wait: true |
| 181 |
apply_update: true |
| 182 |
|
| 183 |
- name: Update firmware from repository on a HTTPS via proxy |
| 184 |
dellemc.openmanage.idrac_firmware: |
| 185 |
idrac_ip: "192.168.0.1" |
| 186 |
idrac_user: "user_name" |
| 187 |
idrac_password: "user_password" |
| 188 |
ca_path: "/path/to/ca_cert.pem" |
| 189 |
share_name: "https://downloads.dell.com" |
| 190 |
reboot: true |
| 191 |
job_wait: true |
| 192 |
apply_update: true |
| 193 |
proxy_support: ParametersProxy |
| 194 |
proxy_server: 192.168.1.10 |
| 195 |
proxy_type: HTTP |
| 196 |
proxy_port: 80 |
| 197 |
proxy_uname: "proxy_user" |
| 198 |
proxy_passwd: "proxy_pwd" |
| 199 |
|
| 200 |
- name: Update firmware from repository on a FTP |
| 201 |
dellemc.openmanage.idrac_firmware: |
| 202 |
idrac_ip: "192.168.0.1" |
| 203 |
idrac_user: "user_name" |
| 204 |
idrac_password: "user_password" |
| 205 |
ca_path: "/path/to/ca_cert.pem" |
| 206 |
share_name: "ftp://ftp.mydomain.com" |
| 207 |
reboot: true |
| 208 |
job_wait: true |
| 209 |
apply_update: true |
| 210 |
""" |
| 211 |
|
| 212 |
RETURN = """ |
| 213 |
--- |
| 214 |
msg: |
| 215 |
type: str |
| 216 |
description: Overall firmware update status. |
| 217 |
returned: always |
| 218 |
sample: "Successfully updated the firmware." |
| 219 |
update_status: |
| 220 |
type: dict |
| 221 |
description: Firmware Update job and progress details from the iDRAC. |
| 222 |
returned: success |
| 223 |
sample: { |
| 224 |
'InstanceID': 'JID_XXXXXXXXXXXX', |
| 225 |
'JobState': 'Completed', |
| 226 |
'Message': 'Job completed successfully.', |
| 227 |
'MessageId': 'REDXXX', |
| 228 |
'Name': 'Repository Update', |
| 229 |
'JobStartTime': 'NA', |
| 230 |
'Status': 'Success', |
| 231 |
} |
| 232 |
""" |
| 233 |
|
| 234 |
|
| 235 |
import os |
| 236 |
import json |
| 237 |
import time |
| 238 |
from ssl import SSLError |
| 239 |
from xml.etree import ElementTree as ET |
| 240 |
from ansible_collections.dellemc.openmanage.plugins.module_utils.dellemc_idrac import iDRACConnection, idrac_auth_params |
| 241 |
from ansible_collections.dellemc.openmanage.plugins.module_utils.idrac_redfish import iDRACRedfishAPI |
| 242 |
from ansible.module_utils.basic import AnsibleModule |
| 243 |
from ansible.module_utils.six.moves.urllib.parse import urlparse |
| 244 |
from ansible.module_utils.urls import ConnectionError, SSLValidationError |
| 245 |
from ansible.module_utils.six.moves.urllib.error import URLError, HTTPError |
| 246 |
try: |
| 247 |
from omsdk.sdkcreds import UserCredentials |
| 248 |
from omsdk.sdkfile import FileOnShare |
| 249 |
HAS_OMSDK = True |
| 250 |
except ImportError: |
| 251 |
HAS_OMSDK = False |
| 252 |
|
| 253 |
SHARE_TYPE = {'nfs': 'NFS', 'cifs': 'CIFS', 'ftp': 'FTP', |
| 254 |
'http': 'HTTP', 'https': 'HTTPS', 'tftp': 'TFTP'} |
| 255 |
CERT_WARN = {True: 'On', False: 'Off'} |
| 256 |
PROXY_SUPPORT = {"DefaultProxy": "Use_Default_Settings", "Off": "Off", "ParametersProxy": "Use_Custom_Settings"} |
| 257 |
IDRAC_PATH = "/redfish/v1/Systems/System.Embedded.1/Oem/Dell/DellSoftwareInstallationService" |
| 258 |
PATH = "/redfish/v1/Systems/System.Embedded.1/Oem/Dell/DellSoftwareInstallationService/Actions/" \ |
| 259 |
"DellSoftwareInstallationService.InstallFromRepository" |
| 260 |
GET_REPO_BASED_UPDATE_LIST_PATH = "/redfish/v1/Systems/System.Embedded.1/Oem/Dell/DellSoftwareInstallationService/" \ |
| 261 |
"Actions/DellSoftwareInstallationService.GetRepoBasedUpdateList" |
| 262 |
JOB_URI = "/redfish/v1/JobService/Jobs/{job_id}" |
| 263 |
iDRAC_JOB_URI = "/redfish/v1/Managers/iDRAC.Embedded.1/Oem/Dell/Jobs/{job_id}" |
| 264 |
LOG_SERVICE_URI = "/redfish/v1/Managers/iDRAC.Embedded.1/LogServices/Lclog" |
| 265 |
iDRAC9_LC_LOG = "/redfish/v1/Managers/iDRAC.Embedded.1/LogServices/Lclog/Entries" |
| 266 |
iDRAC8_LC_LOG = "/redfish/v1/Managers/iDRAC.Embedded.1/Logs/Lclog" |
| 267 |
MESSAGE = "Firmware versions on server match catalog, applicable updates are not present in the repository." |
| 268 |
EXIT_MESSAGE = "The catalog in the repository specified in the operation has the same firmware versions " \ |
| 269 |
"as currently present on the server." |
| 270 |
IDEM_MSG_ID = "SUP029" |
| 271 |
REDFISH_VERSION = "3.30" |
| 272 |
INTERVAL = 30 |
| 273 |
WAIT_COUNT = 240 |
| 274 |
JOB_WAIT_MSG = 'Job wait timed out after {0} minutes' |
| 275 |
|
| 276 |
|
| 277 |
def wait_for_job_completion(module, job_uri, job_wait=False, reboot=False, apply_update=False): |
| 278 |
track_counter = 0 |
| 279 |
response = {} |
| 280 |
msg = None |
| 281 |
while track_counter < 5: |
| 282 |
try: |
| 283 |
|
| 284 |
with iDRACRedfishAPI(module.params) as redfish: |
| 285 |
response = redfish.invoke_request(job_uri, "GET") |
| 286 |
track_counter += 5 |
| 287 |
msg = None |
| 288 |
except Exception as error_message: |
| 289 |
msg = str(error_message) |
| 290 |
track_counter += 1 |
| 291 |
time.sleep(10) |
| 292 |
if track_counter < 5: |
| 293 |
msg = None |
| 294 |
|
| 295 |
track_counter = 0 |
| 296 |
while job_wait and track_counter <= WAIT_COUNT: |
| 297 |
try: |
| 298 |
with iDRACRedfishAPI(module.params) as redfish: |
| 299 |
response = redfish.invoke_request(job_uri, "GET") |
| 300 |
job_state = response.json_data.get("JobState") |
| 301 |
job_status = response.json_data.get("JobStatus") |
| 302 |
msg = None |
| 303 |
except Exception as error_message: |
| 304 |
msg = str(error_message) |
| 305 |
track_counter += 2 |
| 306 |
time.sleep(INTERVAL) |
| 307 |
else: |
| 308 |
if job_status == "Critical": |
| 309 |
msg = response.json_data.get("Messages")[0]["Message"] |
| 310 |
module.exit_json(msg=msg) |
| 311 |
if response.json_data.get("PercentComplete") == 100 and job_state == "Completed": |
| 312 |
break |
| 313 |
if job_state in ["Starting", "Running", "Pending", "New"] and not reboot and apply_update: |
| 314 |
break |
| 315 |
track_counter += 1 |
| 316 |
time.sleep(INTERVAL) |
| 317 |
if track_counter > WAIT_COUNT: |
| 318 |
|
| 319 |
msg = JOB_WAIT_MSG.format((WAIT_COUNT * INTERVAL) / 60) |
| 320 |
return response, msg |
| 321 |
|
| 322 |
|
| 323 |
def _validate_catalog_file(catalog_file_name): |
| 324 |
normilized_file_name = catalog_file_name.lower() |
| 325 |
if not normilized_file_name: |
| 326 |
raise ValueError('catalog_file_name should be a non-empty string.') |
| 327 |
elif not normilized_file_name.endswith("xml"): |
| 328 |
raise ValueError('catalog_file_name should be an XML file.') |
| 329 |
|
| 330 |
|
| 331 |
def get_check_mode_status(status, module): |
| 332 |
if status['job_details']["Data"]["GetRepoBasedUpdateList_OUTPUT"].get("Message") == MESSAGE.rstrip(".") and \ |
| 333 |
status.get('JobStatus') == "Completed": |
| 334 |
if module.check_mode: |
| 335 |
module.exit_json(msg="No changes found to commit!") |
| 336 |
module.exit_json(msg=EXIT_MESSAGE) |
| 337 |
|
| 338 |
|
| 339 |
def get_job_status(module, each_comp, idrac): |
| 340 |
failed, each_comp['JobStatus'], each_comp['Message'] = False, None, None |
| 341 |
job_wait = module.params['job_wait'] |
| 342 |
reboot = module.params['reboot'] |
| 343 |
apply_update = module.params['apply_update'] |
| 344 |
if each_comp.get("JobID") is not None: |
| 345 |
if idrac: |
| 346 |
resp = idrac.job_mgr.job_wait(each_comp.get("JobID")) |
| 347 |
while reboot and apply_update: |
| 348 |
resp = idrac.job_mgr.job_wait(each_comp.get("JobID")) |
| 349 |
if resp.get("JobStatus") is not None and (not resp.get('JobStatus') == "Scheduled"): |
| 350 |
break |
| 351 |
each_comp['Message'] = resp.get('Message') |
| 352 |
each_comp['JobStatus'] = "OK" |
| 353 |
fail_words_lower = ['fail', 'invalid', 'unable', 'not', 'cancel'] |
| 354 |
if any(x in resp.get('Message').lower() for x in fail_words_lower): |
| 355 |
each_comp['JobStatus'] = "Critical" |
| 356 |
failed = True |
| 357 |
else: |
| 358 |
resp, msg = wait_for_job_completion(module, JOB_URI.format(job_id=each_comp.get("JobID")), job_wait, reboot, |
| 359 |
apply_update) |
| 360 |
if not msg: |
| 361 |
resp_data = resp.json_data |
| 362 |
if resp_data.get('Messages'): |
| 363 |
each_comp['Message'] = resp_data.get('Messages')[0]['Message'] |
| 364 |
each_comp['JobStatus'] = resp_data.get('JobStatus') |
| 365 |
if each_comp['JobStatus'] == "Critical": |
| 366 |
failed = True |
| 367 |
else: |
| 368 |
failed = True |
| 369 |
return each_comp, failed |
| 370 |
|
| 371 |
|
| 372 |
def _convert_xmltojson(module, job_details, idrac): |
| 373 |
"""get all the xml data from PackageList and returns as valid json.""" |
| 374 |
data, repo_status, failed_status = [], False, False |
| 375 |
try: |
| 376 |
xmldata = ET.fromstring(job_details['PackageList']) |
| 377 |
for iname in xmldata.iter('INSTANCENAME'): |
| 378 |
comp_data = dict([(attr.attrib['NAME'], txt.text) for attr in iname.iter("PROPERTY") for txt in attr]) |
| 379 |
component, failed = get_job_status(module, comp_data, idrac) |
| 380 |
|
| 381 |
if not failed_status and failed: |
| 382 |
failed_status = True |
| 383 |
data.append(component) |
| 384 |
repo_status = True |
| 385 |
except ET.ParseError: |
| 386 |
data = job_details['PackageList'] |
| 387 |
return data, repo_status, failed_status |
| 388 |
|
| 389 |
|
| 390 |
def get_jobid(module, resp): |
| 391 |
"""Get the Job ID from the response header.""" |
| 392 |
jobid = None |
| 393 |
if resp.status_code == 202: |
| 394 |
joburi = resp.headers.get('Location') |
| 395 |
if joburi is None: |
| 396 |
module.fail_json(msg="Failed to update firmware.") |
| 397 |
jobid = joburi.split("/")[-1] |
| 398 |
else: |
| 399 |
module.fail_json(msg="Failed to update firmware.") |
| 400 |
return jobid |
| 401 |
|
| 402 |
|
| 403 |
def handle_HTTP_error(module, httperr): |
| 404 |
err_message = json.load(httperr) |
| 405 |
err_list = err_message.get('error', {}).get('@Message.ExtendedInfo', [{"Message": EXIT_MESSAGE}]) |
| 406 |
if err_list: |
| 407 |
err_reason = err_list[0].get("Message", EXIT_MESSAGE) |
| 408 |
if IDEM_MSG_ID in err_list[0].get('MessageId'): |
| 409 |
module.exit_json(msg=err_reason) |
| 410 |
if "error" in err_message: |
| 411 |
module.fail_json(msg=err_message) |
| 412 |
|
| 413 |
|
| 414 |
def get_error_syslog(idrac, curr_time, uri): |
| 415 |
error_log_found = False |
| 416 |
msg = None |
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
error_log_ids = ['SYS229', 'SYS227', 'RED132', 'JCP042', 'RED068', 'RED137'] |
| 424 |
intrvl = 5 |
| 425 |
retries = 60 // intrvl |
| 426 |
try: |
| 427 |
if not curr_time: |
| 428 |
resp = idrac.invoke_request(LOG_SERVICE_URI, "GET") |
| 429 |
uri = resp.json_data.get('Entries').get('@odata.id') |
| 430 |
curr_time = resp.json_data.get('DateTime') |
| 431 |
fltr = "?$filter=Created%20ge%20'{0}'".format(curr_time) |
| 432 |
fltr_uri = "{0}{1}".format(uri, fltr) |
| 433 |
while retries: |
| 434 |
resp = idrac.invoke_request(fltr_uri, "GET") |
| 435 |
logs_list = resp.json_data.get("Members") |
| 436 |
for log in logs_list: |
| 437 |
for err_id in error_log_ids: |
| 438 |
if err_id in log.get('MessageId'): |
| 439 |
error_log_found = True |
| 440 |
msg = log.get('Message') |
| 441 |
break |
| 442 |
if msg or error_log_found: |
| 443 |
break |
| 444 |
if msg or error_log_found: |
| 445 |
break |
| 446 |
retries = retries - 1 |
| 447 |
time.sleep(intrvl) |
| 448 |
else: |
| 449 |
msg = "No Error log found." |
| 450 |
error_log_found = False |
| 451 |
except Exception: |
| 452 |
msg = "No Error log found." |
| 453 |
error_log_found = False |
| 454 |
return error_log_found, msg |
| 455 |
|
| 456 |
|
| 457 |
def update_firmware_url_redfish(module, idrac, share_path, apply_update, reboot, job_wait, payload, repo_urls): |
| 458 |
"""Update firmware through HTTP/HTTPS/FTP and return the job details.""" |
| 459 |
repo_url = urlparse(share_path) |
| 460 |
job_details, status = {}, {} |
| 461 |
ipaddr = repo_url.netloc |
| 462 |
share_type = repo_url.scheme |
| 463 |
sharename = repo_url.path.strip('/') |
| 464 |
if repo_url.path: |
| 465 |
payload['ShareName'] = sharename |
| 466 |
payload['IPAddress'] = ipaddr |
| 467 |
payload['ShareType'] = SHARE_TYPE[share_type] |
| 468 |
install_url = PATH |
| 469 |
get_repo_url = GET_REPO_BASED_UPDATE_LIST_PATH |
| 470 |
actions = repo_urls.get('Actions') |
| 471 |
if actions: |
| 472 |
install_url = actions.get("#DellSoftwareInstallationService.InstallFromRepository", {}).get("target", PATH) |
| 473 |
get_repo_url = actions.get("#DellSoftwareInstallationService.GetRepoBasedUpdateList", {}).\ |
| 474 |
get("target", GET_REPO_BASED_UPDATE_LIST_PATH) |
| 475 |
try: |
| 476 |
log_resp = idrac.invoke_request(LOG_SERVICE_URI, "GET") |
| 477 |
log_uri = log_resp.json_data.get('Entries').get('@odata.id') |
| 478 |
curr_time = log_resp.json_data.get('DateTime') |
| 479 |
except Exception: |
| 480 |
log_uri = iDRAC9_LC_LOG |
| 481 |
curr_time = None |
| 482 |
resp = idrac.invoke_request(install_url, method="POST", data=payload) |
| 483 |
error_log_found, msg = get_error_syslog(idrac, curr_time, log_uri) |
| 484 |
job_id = get_jobid(module, resp) |
| 485 |
if error_log_found: |
| 486 |
module.exit_json(msg=msg, failed=True, job_id=job_id) |
| 487 |
resp, msg = wait_for_job_completion(module, JOB_URI.format(job_id=job_id), job_wait, reboot, apply_update) |
| 488 |
if not msg: |
| 489 |
status = resp.json_data |
| 490 |
else: |
| 491 |
status['update_msg'] = msg |
| 492 |
try: |
| 493 |
resp_repo_based_update_list = idrac.invoke_request(get_repo_url, method="POST", data="{}", |
| 494 |
dump=False) |
| 495 |
job_details = resp_repo_based_update_list.json_data |
| 496 |
except HTTPError as err: |
| 497 |
handle_HTTP_error(module, err) |
| 498 |
raise err |
| 499 |
return status, job_details |
| 500 |
|
| 501 |
|
| 502 |
def update_firmware_url_omsdk(module, idrac, share_name, catalog_file_name, apply_update, reboot, |
| 503 |
ignore_cert_warning, job_wait, payload): |
| 504 |
"""Update firmware through HTTP/HTTPS/FTP and return the job details.""" |
| 505 |
repo_url = urlparse(share_name) |
| 506 |
job_details, status = {}, {} |
| 507 |
ipaddr = repo_url.netloc |
| 508 |
share_type = repo_url.scheme |
| 509 |
sharename = repo_url.path.strip('/') |
| 510 |
proxy_support = PROXY_SUPPORT[module.params["proxy_support"]] |
| 511 |
proxy_type = module.params.get("proxy_type") if module.params.get("proxy_type") is not None else "HTTP" |
| 512 |
proxy_server = module.params.get("proxy_server") if module.params.get("proxy_server") is not None else "" |
| 513 |
proxy_port = module.params.get("proxy_port") if module.params.get("proxy_port") is not None else 80 |
| 514 |
proxy_uname = module.params.get("proxy_uname") |
| 515 |
proxy_passwd = module.params.get("proxy_passwd") |
| 516 |
if ipaddr == "downloads.dell.com": |
| 517 |
status = idrac.update_mgr.update_from_dell_repo_url( |
| 518 |
ipaddress=ipaddr, share_type=share_type, share_name=sharename, catalog_file=catalog_file_name, |
| 519 |
apply_update=apply_update, reboot_needed=reboot, ignore_cert_warning=ignore_cert_warning, job_wait=job_wait, |
| 520 |
proxy_support=proxy_support, proxy_type=proxy_type, proxy_server=proxy_server, proxy_port=proxy_port, |
| 521 |
proxy_uname=proxy_uname, proxy_passwd=proxy_passwd) |
| 522 |
get_check_mode_status(status, module) |
| 523 |
else: |
| 524 |
status = idrac.update_mgr.update_from_repo_url( |
| 525 |
ipaddress=ipaddr, share_type=share_type, share_name=sharename, catalog_file=catalog_file_name, |
| 526 |
apply_update=apply_update, reboot_needed=reboot, ignore_cert_warning=ignore_cert_warning, job_wait=job_wait, |
| 527 |
proxy_support=proxy_support, proxy_type=proxy_type, proxy_server=proxy_server, |
| 528 |
proxy_port=proxy_port, proxy_uname=proxy_uname, proxy_passwd=proxy_passwd) |
| 529 |
get_check_mode_status(status, module) |
| 530 |
return status, job_details |
| 531 |
|
| 532 |
|
| 533 |
def update_firmware_omsdk(idrac, module): |
| 534 |
"""Update firmware from a network share and return the job details.""" |
| 535 |
msg = {} |
| 536 |
msg['changed'], msg['failed'], msg['update_status'] = False, False, {} |
| 537 |
msg['update_msg'] = "Successfully triggered the job to update the firmware." |
| 538 |
try: |
| 539 |
share_name = module.params['share_name'] |
| 540 |
catalog_file_name = module.params['catalog_file_name'] |
| 541 |
share_user = module.params['share_user'] |
| 542 |
share_pwd = module.params['share_password'] |
| 543 |
reboot = module.params['reboot'] |
| 544 |
job_wait = module.params['job_wait'] |
| 545 |
ignore_cert_warning = module.params['ignore_cert_warning'] |
| 546 |
apply_update = module.params['apply_update'] |
| 547 |
payload = {"RebootNeeded": reboot, "CatalogFile": catalog_file_name, "ApplyUpdate": str(apply_update), |
| 548 |
"IgnoreCertWarning": CERT_WARN[ignore_cert_warning]} |
| 549 |
if share_user is not None: |
| 550 |
payload['UserName'] = share_user |
| 551 |
if share_pwd is not None: |
| 552 |
payload['Password'] = share_pwd |
| 553 |
|
| 554 |
if share_name.lower().startswith(('http://', 'https://', 'ftp://')): |
| 555 |
msg['update_status'], job_details = update_firmware_url_omsdk(module, idrac, share_name, catalog_file_name, |
| 556 |
apply_update, reboot, ignore_cert_warning, |
| 557 |
job_wait, payload) |
| 558 |
if job_details: |
| 559 |
msg['update_status']['job_details'] = job_details |
| 560 |
else: |
| 561 |
upd_share = FileOnShare(remote="{0}{1}{2}".format(share_name, os.sep, catalog_file_name), |
| 562 |
mount_point=module.params['share_mnt'], isFolder=False, |
| 563 |
creds=UserCredentials(share_user, share_pwd)) |
| 564 |
msg['update_status'] = idrac.update_mgr.update_from_repo( |
| 565 |
upd_share, apply_update=apply_update, reboot_needed=reboot, job_wait=job_wait,) |
| 566 |
get_check_mode_status(msg['update_status'], module) |
| 567 |
|
| 568 |
json_data, repo_status, failed = msg['update_status']['job_details'], False, False |
| 569 |
if "PackageList" not in json_data: |
| 570 |
job_data = json_data.get('Data') |
| 571 |
pkglst = job_data['body'] if 'body' in job_data else job_data.get('GetRepoBasedUpdateList_OUTPUT') |
| 572 |
if 'PackageList' in pkglst: |
| 573 |
pkglst['PackageList'], repo_status, failed = _convert_xmltojson(module, pkglst, idrac) |
| 574 |
else: |
| 575 |
json_data['PackageList'], repo_status, failed = _convert_xmltojson(module, json_data, None) |
| 576 |
|
| 577 |
if not apply_update and not failed: |
| 578 |
msg['update_msg'] = "Successfully fetched the applicable firmware update package list." |
| 579 |
elif apply_update and not reboot and not job_wait and not failed: |
| 580 |
msg['update_msg'] = "Successfully triggered the job to stage the firmware." |
| 581 |
elif apply_update and job_wait and not reboot and not failed: |
| 582 |
msg['update_msg'] = "Successfully staged the applicable firmware update packages." |
| 583 |
msg['changed'] = True |
| 584 |
elif apply_update and job_wait and not reboot and failed: |
| 585 |
msg['update_msg'] = "Successfully staged the applicable firmware update packages with error(s)." |
| 586 |
msg['failed'] = True |
| 587 |
|
| 588 |
except RuntimeError as e: |
| 589 |
module.fail_json(msg=str(e)) |
| 590 |
|
| 591 |
if module.check_mode and not (json_data.get('PackageList') or json_data.get('Data')) and \ |
| 592 |
msg['update_status']['JobStatus'] == 'Completed': |
| 593 |
module.exit_json(msg="No changes found to commit!") |
| 594 |
elif module.check_mode and (json_data.get('PackageList') or json_data.get('Data')) and \ |
| 595 |
msg['update_status']['JobStatus'] == 'Completed': |
| 596 |
module.exit_json(msg="Changes found to commit!", changed=True, |
| 597 |
update_status=msg['update_status']) |
| 598 |
elif module.check_mode and not msg['update_status']['JobStatus'] == 'Completed': |
| 599 |
msg['update_status'].pop('job_details') |
| 600 |
module.fail_json(msg="Unable to complete the firmware repository download.", |
| 601 |
update_status=msg['update_status']) |
| 602 |
elif not module.check_mode and "Status" in msg['update_status']: |
| 603 |
if msg['update_status']['Status'] in ["Success", "InProgress"]: |
| 604 |
if module.params['job_wait'] and module.params['apply_update'] and module.params['reboot'] and ( |
| 605 |
'job_details' in msg['update_status'] and repo_status) and not failed: |
| 606 |
msg['changed'] = True |
| 607 |
msg['update_msg'] = "Successfully updated the firmware." |
| 608 |
elif module.params['job_wait'] and module.params['apply_update'] and module.params['reboot'] and ( |
| 609 |
'job_details' in msg['update_status'] and repo_status) and failed: |
| 610 |
msg['failed'], msg['changed'] = True, False |
| 611 |
msg['update_msg'] = "Firmware update failed." |
| 612 |
else: |
| 613 |
failed_msg = "Firmware update failed." |
| 614 |
if not apply_update: |
| 615 |
failed_msg = "Unable to complete the repository update." |
| 616 |
module.fail_json(msg=failed_msg, update_status=msg['update_status']) |
| 617 |
return msg |
| 618 |
|
| 619 |
|
| 620 |
def update_firmware_redfish(idrac, module, repo_urls): |
| 621 |
"""Update firmware from a network share and return the job details.""" |
| 622 |
msg = {} |
| 623 |
msg['changed'], msg['failed'] = False, False |
| 624 |
msg['update_msg'] = "Successfully triggered the job to update the firmware." |
| 625 |
try: |
| 626 |
share_name = module.params['share_name'] |
| 627 |
catalog_file_name = module.params['catalog_file_name'] |
| 628 |
share_user = module.params['share_user'] |
| 629 |
share_pwd = module.params['share_password'] |
| 630 |
reboot = module.params['reboot'] |
| 631 |
job_wait = module.params['job_wait'] |
| 632 |
ignore_cert_warning = module.params['ignore_cert_warning'] |
| 633 |
apply_update = module.params['apply_update'] |
| 634 |
payload = {"RebootNeeded": reboot, "CatalogFile": catalog_file_name, "ApplyUpdate": str(apply_update), |
| 635 |
"IgnoreCertWarning": CERT_WARN[ignore_cert_warning]} |
| 636 |
if share_user is not None: |
| 637 |
payload['UserName'] = share_user |
| 638 |
if share_pwd is not None: |
| 639 |
payload['Password'] = share_pwd |
| 640 |
|
| 641 |
if share_name.lower().startswith(('http://', 'https://', 'ftp://')): |
| 642 |
proxy = module.params.get("proxy_support") |
| 643 |
if proxy == "ParametersProxy": |
| 644 |
proxy_dict = {"proxy_server": "ProxyServer", |
| 645 |
"proxy_port": "ProxyPort", |
| 646 |
"proxy_support": "ProxySupport", |
| 647 |
"proxy_type": "ProxyType", |
| 648 |
"proxy_uname": "ProxyUname", |
| 649 |
"proxy_passwd": "ProxyPasswd"} |
| 650 |
for pk, pv in proxy_dict.items(): |
| 651 |
prm = module.params.get(pk) |
| 652 |
if prm is not None: |
| 653 |
payload[pv] = prm |
| 654 |
elif proxy == "DefaultProxy": |
| 655 |
payload["ProxySupport"] = module.params.get("proxy_support") |
| 656 |
msg['update_status'], job_details = update_firmware_url_redfish( |
| 657 |
module, idrac, share_name, apply_update, reboot, job_wait, payload, repo_urls) |
| 658 |
if job_details: |
| 659 |
msg['update_status']['job_details'] = job_details |
| 660 |
else: |
| 661 |
if share_name.startswith('\\\\'): |
| 662 |
cifs = share_name.split('\\') |
| 663 |
payload['IPAddress'] = cifs[2] |
| 664 |
payload['ShareName'] = '\\'.join(cifs[3:]) |
| 665 |
payload['ShareType'] = 'CIFS' |
| 666 |
else: |
| 667 |
nfs = urlparse("nfs://" + share_name) |
| 668 |
payload['IPAddress'] = nfs.netloc.strip(':') |
| 669 |
payload['ShareName'] = nfs.path.strip('/') |
| 670 |
payload['ShareType'] = 'NFS' |
| 671 |
resp = idrac.invoke_request(PATH, method="POST", data=payload) |
| 672 |
job_id = get_jobid(module, resp) |
| 673 |
resp, mesg = wait_for_job_completion(module, JOB_URI.format(job_id=job_id), job_wait, reboot, apply_update) |
| 674 |
if not mesg: |
| 675 |
msg['update_status'] = resp.json_data |
| 676 |
else: |
| 677 |
msg['update_status'] = mesg |
| 678 |
try: |
| 679 |
repo_based_update_list = idrac.invoke_request(GET_REPO_BASED_UPDATE_LIST_PATH, method="POST", |
| 680 |
data="{}", dump=False) |
| 681 |
msg['update_status']['job_details'] = repo_based_update_list.json_data |
| 682 |
except HTTPError as err: |
| 683 |
handle_HTTP_error(module, err) |
| 684 |
raise err |
| 685 |
json_data, repo_status, failed = msg['update_status']['job_details'], False, False |
| 686 |
if "PackageList" not in json_data: |
| 687 |
job_data = json_data.get('Data') |
| 688 |
pkglst = job_data['body'] if 'body' in job_data else job_data.get('GetRepoBasedUpdateList_OUTPUT') |
| 689 |
if 'PackageList' in pkglst: |
| 690 |
pkglst['PackageList'], repo_status, failed = _convert_xmltojson(module, pkglst, idrac) |
| 691 |
else: |
| 692 |
json_data['PackageList'], repo_status, failed = _convert_xmltojson(module, json_data, None) |
| 693 |
|
| 694 |
if not apply_update and not failed: |
| 695 |
msg['update_msg'] = "Successfully fetched the applicable firmware update package list." |
| 696 |
elif apply_update and not reboot and not job_wait and not failed: |
| 697 |
msg['update_msg'] = "Successfully triggered the job to stage the firmware." |
| 698 |
elif apply_update and job_wait and not reboot and not failed: |
| 699 |
msg['update_msg'] = "Successfully staged the applicable firmware update packages." |
| 700 |
msg['changed'] = True |
| 701 |
elif apply_update and job_wait and not reboot and failed: |
| 702 |
msg['update_msg'] = "Successfully staged the applicable firmware update packages with error(s)." |
| 703 |
msg['failed'] = True |
| 704 |
|
| 705 |
except RuntimeError as e: |
| 706 |
module.fail_json(msg=str(e)) |
| 707 |
|
| 708 |
if module.check_mode and not (json_data.get('PackageList') or json_data.get('Data')) and \ |
| 709 |
msg['update_status']['JobStatus'] == 'OK': |
| 710 |
module.exit_json(msg="No changes found to commit!") |
| 711 |
elif module.check_mode and (json_data.get('PackageList') or json_data.get('Data')) and \ |
| 712 |
msg['update_status']['JobStatus'] == 'OK': |
| 713 |
module.exit_json(msg="Changes found to commit!", changed=True, |
| 714 |
update_status=msg['update_status']) |
| 715 |
elif module.check_mode and not msg['update_status']['JobStatus'] == 'OK': |
| 716 |
msg['update_status'].pop('job_details') |
| 717 |
module.fail_json(msg="Unable to complete the firmware repository download.", |
| 718 |
update_status=msg['update_status']) |
| 719 |
elif not module.check_mode and "JobStatus" in msg['update_status']: |
| 720 |
if not msg['update_status']['JobStatus'] == "Critical": |
| 721 |
if module.params['job_wait'] and module.params['apply_update'] and module.params['reboot'] and \ |
| 722 |
('job_details' in msg['update_status'] and repo_status) and not failed: |
| 723 |
msg['changed'] = True |
| 724 |
msg['update_msg'] = "Successfully updated the firmware." |
| 725 |
elif module.params['job_wait'] and module.params['apply_update'] and module.params['reboot'] and \ |
| 726 |
('job_details' in msg['update_status'] and repo_status) and failed: |
| 727 |
msg['failed'], msg['changed'] = True, False |
| 728 |
msg['update_msg'] = "Firmware update failed." |
| 729 |
else: |
| 730 |
failed_msg = "Firmware update failed." |
| 731 |
if not apply_update: |
| 732 |
failed_msg = "Unable to complete the repository update." |
| 733 |
module.fail_json(msg=failed_msg, update_status=msg['update_status']) |
| 734 |
return msg |
| 735 |
|
| 736 |
|
| 737 |
def main(): |
| 738 |
specs = { |
| 739 |
"share_name": {"required": True, "type": 'str'}, |
| 740 |
"share_user": {"type": 'str'}, |
| 741 |
"share_password": {"type": 'str', "aliases": ['share_pwd'], "no_log": True}, |
| 742 |
"share_mnt": {"type": 'str'}, |
| 743 |
|
| 744 |
"catalog_file_name": {"type": 'str', "default": "Catalog.xml"}, |
| 745 |
"reboot": {"type": 'bool', "default": False}, |
| 746 |
"job_wait": {"type": 'bool', "default": True}, |
| 747 |
"ignore_cert_warning": {"type": 'bool', "default": True}, |
| 748 |
"apply_update": {"type": 'bool', "default": True}, |
| 749 |
|
| 750 |
"proxy_support": {"default": 'Off', "type": 'str', "choices": ["Off", "ParametersProxy", "DefaultProxy"]}, |
| 751 |
"proxy_type": {"type": 'str', "choices": ["HTTP", "SOCKS"]}, |
| 752 |
"proxy_server": {"type": 'str'}, |
| 753 |
"proxy_port": {"type": 'int'}, |
| 754 |
"proxy_uname": {"type": 'str'}, |
| 755 |
"proxy_passwd": {"type": 'str', "no_log": True}, |
| 756 |
} |
| 757 |
specs.update(idrac_auth_params) |
| 758 |
module = AnsibleModule( |
| 759 |
argument_spec=specs, |
| 760 |
required_if=[ |
| 761 |
|
| 762 |
['proxy_support', 'ParametersProxy', ('proxy_server', 'proxy_type', 'proxy_port',)], |
| 763 |
], |
| 764 |
supports_check_mode=True) |
| 765 |
|
| 766 |
redfish_check = False |
| 767 |
try: |
| 768 |
with iDRACRedfishAPI(module.params) as obj: |
| 769 |
resp = obj.invoke_request(IDRAC_PATH, method="GET") |
| 770 |
software_service_data = resp.json_data |
| 771 |
redfish_check = True |
| 772 |
except Exception: |
| 773 |
software_service_data = {} |
| 774 |
redfish_check = False |
| 775 |
|
| 776 |
try: |
| 777 |
|
| 778 |
_validate_catalog_file(module.params['catalog_file_name']) |
| 779 |
if module.check_mode: |
| 780 |
module.params['apply_update'] = False |
| 781 |
module.params['reboot'] = False |
| 782 |
module.params['job_wait'] = True |
| 783 |
|
| 784 |
if redfish_check: |
| 785 |
with iDRACRedfishAPI(module.params) as redfish_obj: |
| 786 |
status = update_firmware_redfish(redfish_obj, module, software_service_data) |
| 787 |
else: |
| 788 |
with iDRACConnection(module.params) as idrac: |
| 789 |
status = update_firmware_omsdk(idrac, module) |
| 790 |
except HTTPError as err: |
| 791 |
module.fail_json(msg=str(err), update_status=json.load(err)) |
| 792 |
except URLError as err: |
| 793 |
message = err.reason if err.reason else err(str) |
| 794 |
module.exit_json(msg=message, unreachable=True) |
| 795 |
except (RuntimeError, SSLValidationError, ConnectionError, KeyError, |
| 796 |
ImportError, ValueError, TypeError, SSLError) as e: |
| 797 |
module.fail_json(msg=str(e)) |
| 798 |
except Exception as exc: |
| 799 |
module.fail_json(msg="Unhandled Exception {0}".format(exc)) |
| 800 |
|
| 801 |
module.exit_json(msg=status['update_msg'], update_status=status['update_status'], |
| 802 |
changed=status['changed'], failed=status['failed']) |
| 803 |
|
| 804 |
|
| 805 |
if __name__ == '__main__': |
| 806 |
main() |