-
Notifications
You must be signed in to change notification settings - Fork 282
/
DeviceFirmwareMultipartUploadREDFISH.py
600 lines (579 loc) · 34 KB
/
DeviceFirmwareMultipartUploadREDFISH.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
#!/usr/bin/python3
#
# DeviceFirmwareMultipartUploadREDFISH.py. Python script using Redfish API to update a device firmware with DMTF MultipartUpload. Supported file image types are Windows DUPs, d7/d9 image or pm files.
#
# _author_ = Texas Roemer <Texas_Roemer@Dell.com>
# _version_ = 9.0
#
# Copyright (c) 2020, Dell, Inc.
#
# This software is licensed to you under the GNU General Public License,
# version 2 (GPLv2). There is NO WARRANTY for this software, express or
# implied, including the implied warranties of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. You should have received a copy of GPLv2
# along with this software; if not, see
# http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt.
#
import argparse
import getpass
import json
import logging
import os
import platform
import re
import requests
import subprocess
import sys
import time
import warnings
from datetime import datetime
from pprint import pprint
warnings.filterwarnings("ignore")
parser=argparse.ArgumentParser(description="Python script using Redfish API to update a device firmware with DMTF MultipartUpload from a local directory")
parser.add_argument('-ip',help='iDRAC IP address', required=False)
parser.add_argument('-u', help='iDRAC username', required=False)
parser.add_argument('-p', help='iDRAC password. If you do not pass in argument -p, script will prompt to enter user password which will not be echoed to the screen.', required=False)
parser.add_argument('-x', help='Pass in X-Auth session token for executing Redfish calls. All Redfish calls will use X-Auth token instead of username/password', required=False)
parser.add_argument('--ssl', help='SSL cert verification for all Redfish calls, pass in value \"true\" or \"false\". By default, this argument is not required and script ignores validating SSL cert for all Redfish calls.', required=False)
parser.add_argument('--script-examples', action="store_true", help='Prints script examples')
parser.add_argument('--get', help='Get current supported devices for firmware updates and their current firmware versions', action="store_true", required=False)
parser.add_argument('--location', help='Pass in the full directory path location of the firmware image. Make sure to also pass in the name of the Dell Update package (DUP) executable, example: C:\\Users\\admin\\Downloads\\Diagnostics_Application_CH7FG_WN64_4301A42_4301.43.EXE', required=False)
parser.add_argument('--reboot', help='Pass in this argument to reboot the server now to perform the update. If you do not pass in this argument, update job is still scheduled and will get applied on next server manual reboot. Note: For devices that do not need a reboot to apply the firmware update (Examples: iDRAC, DIAGS, Driver Pack), you don\'t need to pass in this agrument(update will happen immediately). See Lifecycle Controller User Guide firmware update section for more details on which devices get applied immediately or need a reboot to get updated', action="store_true", required=False)
args=vars(parser.parse_args())
logging.basicConfig(format='%(message)s', stream=sys.stdout, level=logging.INFO)
def script_examples():
print("""\n- DeviceFirmwareMultipartUploadREDFISH.py -ip 192.168.0.120 -u root -p calvin --get, this example will get current firmware versions for all devices in the server.
\n- DeviceFirmwareMultipartUploadREDFISH.py -ip 192.168.0.120 -u root --location C:\\Users\\administrator\\Downloads\\BIOS_8MRPC_C6420_WN64_2.11.2.EXE --reboot, this example will first prompt to enter iDRAC user password, then reboot the server now to execute BIOS firmware update.""")
sys.exit(0)
def check_supported_idrac_version():
if args["x"]:
response = requests.get('https://%s/redfish/v1/UpdateService' % idrac_ip, verify=verify_cert, headers={'X-Auth-Token': args["x"]})
else:
response = requests.get('https://%s/redfish/v1/UpdateService' % idrac_ip, verify=verify_cert, auth=(idrac_username, idrac_password))
data = response.json()
if response.status_code == 401:
logging.warning("\n- WARNING, status code %s returned, check your iDRAC username/password is correct or iDRAC user has correct privileges to execute Redfish commands" % response.status_code)
sys.exit(0)
if 'MultipartHttpPushUri' not in data.keys():
logging.warning("\n- WARNING, iDRAC version installed does not support this feature using Redfish API")
sys.exit(0)
def get_idrac_version():
global idrac_fw_version
if args["x"]:
response = requests.get('https://%s/redfish/v1/Managers/iDRAC.Embedded.1?$select=FirmwareVersion' % idrac_ip, verify=verify_cert, headers={'X-Auth-Token': args["x"]})
else:
response = requests.get('https://%s/redfish/v1/Managers/iDRAC.Embedded.1?$select=FirmwareVersion' % idrac_ip, verify=verify_cert, auth=(idrac_username, idrac_password))
data = response.json()
if response.status_code != 200:
logging.error("\n- ERROR, GET request failed to get iDRAC firmware version, error: \n%s" % data)
sys.exit(0)
idrac_fw_version = data["FirmwareVersion"].replace(".","")
def get_server_generation():
global idrac_model
if args["x"]:
response = requests.get('https://%s/redfish/v1/Managers/iDRAC.Embedded.1?$select=Model' % idrac_ip, verify=verify_cert, headers={'X-Auth-Token': args["x"]})
else:
response = requests.get('https://%s/redfish/v1/Managers/iDRAC.Embedded.1?$select=Model' % idrac_ip, verify=False,auth=(idrac_username,idrac_password))
data = response.json()
if response.status_code == 401:
logging.error("\n- ERROR, status code 401 detected, check to make sure your iDRAC script session has correct username/password credentials or if using X-auth token, confirm the session is still active.")
sys.exit(0)
elif response.status_code != 200:
logging.warning("\n- WARNING, unable to get current iDRAC version installed")
sys.exit(0)
if "14" in data["Model"] or "15" in data["Model"] or "16" in data["Model"]:
idrac_model = 9
else:
idrac_model = 10
def get_FW_inventory():
logging.info("\n- INFO, getting current firmware inventory for iDRAC %s -\n" % idrac_ip)
if args["x"]:
response = requests.get('https://%s/redfish/v1/UpdateService/FirmwareInventory?$expand=*($levels=1)' % idrac_ip, verify=verify_cert, headers={'X-Auth-Token': args["x"]})
else:
response = requests.get('https://%s/redfish/v1/UpdateService/FirmwareInventory?$expand=*($levels=1)' % idrac_ip, verify=verify_cert, auth=(idrac_username, idrac_password))
data = response.json()
if response.status_code != 200:
logging.error("\n- ERROR, GET request failed to get firmware inventory, error: \n%s" % data)
sys.exit(0)
installed_devices = []
for i in data['Members']:
pprint(i)
print("\n")
def download_image_create_update_job():
global job_id
global start_time
start_time = datetime.now()
logging.info("\n- INFO, downloading update package to create update job, this may take a few minutes depending on firmware image size")
url = "https://%s/redfish/v1/UpdateService/MultipartUpload" % idrac_ip
if args["reboot"]:
payload = {"Targets": [], "@Redfish.OperationApplyTime": "Immediate", "Oem": {}}
else:
payload = {"Targets": [], "@Redfish.OperationApplyTime": "OnReset", "Oem": {}}
files = {
'UpdateParameters': (None, json.dumps(payload), 'application/json'),
'UpdateFile': (os.path.basename(args["location"]), open(args["location"], 'rb'), 'application/octet-stream')
}
if args["x"]:
headers = {'X-Auth-Token': args["x"]}
response = requests.post(url, files=files, headers=headers, verify=verify_cert)
else:
response = requests.post(url, files=files, verify=verify_cert,auth=(idrac_username,idrac_password))
if response.status_code != 202:
data = response.json()
logging.error("- FAIL, status code %s returned, detailed error: %s" % (response.status_code,data))
sys.exit(0)
try:
job_id = response.headers['Location'].split("/")[-1]
except:
logging.error("- FAIL, unable to locate job ID in header")
sys.exit(0)
logging.info("- PASS, update job ID %s successfully created, script will now loop polling the job status\n" % job_id)
time.sleep(10)
if args["x"]:
response = requests.get('https://%s/redfish/v1/TaskService/Tasks/%s' % (idrac_ip, job_id), verify=verify_cert, headers={'X-Auth-Token': args["x"]})
else:
response = requests.get('https://%s/redfish/v1/TaskService/Tasks/%s' % (idrac_ip, job_id), verify=verify_cert, auth=(idrac_username, idrac_password))
if response.status_code == 200 or response.status_code == 202:
data = response.json()
time.sleep(1)
else:
data = response.json()
logging.error("\n- ERROR, GET request failed to get job ID details, status code %s returned, error: \n%s" % (response.status_code, data))
sys.exit(0)
if "cpld" in data["Name"].lower():
logging.info("- INFO, CPLD update detected, once the update is complete virtual a/c cycle will be performed. GET request to poll the job status will start failing which is expected")
def check_job_status():
retry_count = 1
schedule_job_status_count = 1
if "idrac" in args["location"].lower():
idrac_update = "yes"
else:
idrac_update = "no"
while True:
current_time = str(datetime.now()-start_time)[0:7]
check_idrac_connection()
if retry_count == 30:
logging.warning("- WARNING, GET command retry count of 30 has been reached, script will exit")
sys.exit(0)
try:
if args["x"]:
response = requests.get('https://%s/redfish/v1/TaskService/Tasks/%s' % (idrac_ip, job_id), verify=verify_cert, headers={'X-Auth-Token': args["x"]})
else:
response = requests.get('https://%s/redfish/v1/TaskService/Tasks/%s' % (idrac_ip, job_id), verify=verify_cert, auth=(idrac_username, idrac_password))
except requests.exceptions.ConnectTimeout as json_error:
print("ConnectTimeout:", json_error)
time.sleep(180)
retry_count += 1
continue
except json.decoder.JSONDecodeError as json_error:
print("JSONDecodeError:", json_error)
time.sleep(180)
retry_count += 1
continue
except requests.exceptions.ConnectionError as error_message:
logging.info("- INFO, GET request failed due to connection error, retry in 30 seconds")
time.sleep(30)
retry_count += 1
continue
except requests.exceptions.RequestException as req_error:
print("RequestException:", req_error)
time.sleep(180)
retry_count += 1
continue
try:
data = response.json()
except:
logging.warning("- WARNING, unable to get JSON data response from GET request, script will retry in 1 minute")
time.sleep(60)
retry_count +=1
continue
if response.status_code == 200 or response.status_code == 202:
time.sleep(1)
elif response.status_code == 404:
if idrac_update == "yes":
if args["x"]:
response = requests.get('https://%s/redfish/v1/JobService/Jobs/%s' % (idrac_ip, job_id), verify=verify_cert, headers={'X-Auth-Token': args["x"]})
else:
response = requests.get('https://%s/redfish/v1/JobService/Jobs/%s' % (idrac_ip, job_id), verify=verify_cert, auth=(idrac_username, idrac_password))
if response.status_code == 200 or response.status_code == 202:
time.sleep(1)
else:
logging.error("\n- ERROR, GET request failed to get job ID details, status code %s returned, error: \n%s" % (response.status_code, data))
data = response.json()
if "success" in data["Messages"][0]["Message"].lower() and data["JobState"] == "Completed":
logging.info("\n- PASS, job completed, detailed final job status results\n")
pprint(data)
logging.info("\n- JOB ID %s completed in %s" % (job_id, current_time))
sys.exit(0)
else:
time.sleep(10)
retry_count +=1
continue
elif response.status_code == 500:
logging.warning("- WARNING, status code 500 returned for internal server error, GET request will retry in 1 minute")
time.sleep(60)
retry_count +=1
continue
elif response.status_code == 401 and idrac_update == "yes":
logging.warning("- WARNING, status code 401 detected for iDRAC firmware update, GET will retry in 1 minute")
time.sleep(60)
retry_count +=1
continue
elif response.status_code == 503:
logging.warning("- WARNING, status code 503 returned for service unavailable, GET request will retry in 1 minute")
time.sleep(60)
retry_count +=1
continue
else:
logging.error("\n- ERROR, GET request failed to get job ID details, status code %s returned, error: \n%s" % (response.status_code, data))
sys.exit(0)
try:
message_string = data["Messages"]
except:
logging.warning("- WARNING, unable to get Messages property value from JSON response, script will retry in 1 minute")
time.sleep(60)
retry_count += 1
if idrac_update == "yes":
if args["x"]:
response = requests.get('https://%s/redfish/v1/JobService/Jobs/%s' % (idrac_ip, job_id), verify=verify_cert, headers={'X-Auth-Token': args["x"]})
else:
response = requests.get('https://%s/redfish/v1/JobService/Jobs/%s' % (idrac_ip, job_id), verify=verify_cert, auth=(idrac_username, idrac_password))
if response.status_code == 200 or response.status_code == 202:
time.sleep(1)
else:
logging.error("\n- ERROR, GET request failed to get job ID details, status code %s returned, error: \n%s" % (response.status_code, data))
data = response.json()
if "success" in data["Messages"][0]["Message"].lower() and data["JobState"] == "Completed":
logging.info("\n- PASS, job completed, detailed final job status results\n")
pprint(data)
logging.info("\n- JOB ID %s completed in %s" % (job_id, current_time))
sys.exit(0)
else:
time.sleep(10)
retry_count +=1
continue
if "fail" in data['Oem']['Dell']['Message'].lower() or "error" in data['Oem']['Dell']['Message'].lower() or "unable" in data['Oem']['Dell']['Message'].lower():
logging.error("- FAIL: Job failed, current message: %s" % data["Messages"])
sys.exit(0)
elif data["TaskState"] == "Completed" and data["Oem"]["Dell"]["JobState"]:
logging.info("\n- PASS, job ID successfuly marked completed, detailed final job status results\n")
for i in data['Oem']['Dell'].items():
pprint(i)
logging.info("\n- JOB ID %s completed in %s" % (job_id, current_time))
sys.exit(0)
if data["TaskState"] == "UserIntervention" and data["PercentComplete"] == 100:
logging.info("\n- JOB ID %s completed in %s but user intervention is needed, final job message: %s" % (job_id, current_time, message_string[0]["Message"].rstrip(".")))
if args["reboot"]:
if "reboot" in message_string[0]["Message"].lower():
logging.info("- INFO, rebooting server for the new firmware installed to become effective")
reboot_server()
if "virtual" in message_string[0]["Message"].lower():
logging.info("- INFO, server virtual a/c cycle is needed for the new firmware installed to become effective")
sys.exit(0)
if data["TaskState"] == "Completed":
logging.info("\n- PASS, job ID successfuly marked completed, detailed final job status results\n")
for i in data['Oem']['Dell'].items():
pprint(i)
logging.info("\n- JOB ID %s completed in %s" % (job_id, current_time))
sys.exit(0)
if str(current_time)[0:7] >= "0:30:00":
logging.error("\n- FAIL: Timeout of 30 minutes has been hit, update job should of already been marked completed. Check the iDRAC job queue and LC logs to debug the issue\n")
sys.exit(0)
elif "scheduled" in data['Oem']['Dell']['Message']:
if schedule_job_status_count == 1:
time.sleep(15)
schedule_job_status_count += 1
continue
else:
print("- PASS, job ID %s successfully marked as scheduled" % data["Id"])
if not args["reboot"]:
logging.warning("- WARNING, missing argument --reboot for rebooting the server. Job is still scheduled and will be applied on next manual server reboot")
sys.exit(0)
else:
break
elif "completed successfully" in data['Oem']['Dell']['Message']:
logging.info("\n- PASS, job ID %s successfully marked completed, detailed final job status results\n")
for i in data['Oem']['Dell'].items():
pprint(i)
logging.info("\n- %s completed in: %s" % (job_id, str(current_time)[0:7]))
break
else:
logging.info("- INFO: %s, execution time: %s" % (message_string[0]["Message"].rstrip("."), current_time))
time.sleep(1)
continue
def loop_check_final_job_status():
retry_count = 1
while True:
if retry_count == 20:
logging.warning("- WARNING, GET command retry count of 20 has been reached, script will exit")
sys.exit(0)
check_idrac_connection()
try:
if args["x"]:
response = requests.get('https://%s/redfish/v1/Managers/iDRAC.Embedded.1/Oem/Dell/Jobs/%s' % (idrac_ip, job_id), verify=verify_cert, headers={'X-Auth-Token': args["x"]})
else:
response = requests.get('https://%s/redfish/v1/Managers/iDRAC.Embedded.1/Oem/Dell/Jobs/%s' % (idrac_ip, job_id), verify=verify_cert,auth=(idrac_username, idrac_password))
if response.status_code == 500:
logging.info("- WARNING, iDRAC connection lost, script will sleep 3 minutes and then retry GET request")
time.sleep(180)
check_idrac_connection()
continue
except requests.exceptions.ConnectTimeout as json_error:
print("ConnectTimeout:", json_error)
time.sleep(180)
retry_count += 1
continue
except json.decoder.JSONDecodeError as json_error:
print("JSONDecodeError:", json_error)
time.sleep(180)
retry_count += 1
continue
except requests.exceptions.ConnectionError as error_message:
logging.info("- INFO, GET request failed due to connection error, retry")
time.sleep(10)
retry_count += 1
continue
except requests.exceptions.RequestException as req_error:
print("RequestException:", req_error)
time.sleep(180)
retry_count += 1
continue
current_time = str((datetime.now()-start_time))[0:7]
data = response.json()
if response.status_code == 200 or response.status_code == 202:
logging.debug("- PASS, GET request passed to check job status")
else:
logging.error("\n- FAIL, GET command failed to check job status, return code %s" % response.status_code)
logging.error("Extended Info Message: {0}".format(response.json()))
sys.exit(0)
if str(current_time)[0:7] >= "0:30:00":
logging.error("\n- FAIL: Timeout of 30 minutes has been hit, script stopped\n")
sys.exit(0)
elif "Fail" in data['Message'] or "fail" in data['Message'] or "fail" in data['JobState'] or "Fail" in data['JobState']:
logging.error("- FAIL: job ID %s failed" % job_id)
sys.exit(0)
elif "completed successfully" in data['Message'].lower() or "successfully completed" in data['Message'].lower():
logging.info("\n- PASS, job ID %s successfully marked completed" % job_id)
logging.info("\n- Final detailed job results -\n")
for i in data.items():
pprint(i)
logging.info("\n- JOB ID %s completed in %s" % (job_id, current_time))
sys.exit(0)
else:
logging.info("- INFO, JobStatus not completed, current status: \"%s\", execution time: \"%s\"" % (data['Message'].rstrip("."), current_time))
time.sleep(1)
def oem_ac_power_cycle():
if args["x"]:
response = requests.get('https://%s/redfish/v1/Systems/System.Embedded.1?$select=PowerState' % idrac_ip, verify=verify_cert, headers={'X-Auth-Token': args["x"]})
else:
response = requests.get('https://%s/redfish/v1/Systems/System.Embedded.1?$select=PowerState' % idrac_ip, verify=verify_cert, auth=(idrac_username, idrac_password))
data = response.json()
if response.status_code != 200:
logging.warning("\n- WARNING, GET request failed to get current server power state, status code %s returned." % response.status_code)
logging.warning(data)
sys.exit(0)
if data["PowerState"].lower() == "off":
logging.info("- INFO, server already in OFF state, skipping power off operation")
return
url = "https://%s/redfish/v1/Systems/System.Embedded.1/Actions/ComputerSystem.Reset" % idrac_ip
payload = {"ResetType": "ForceOff"}
if args["x"]:
headers = {"content-type": "application/json", "X-Auth-Token": args["x"]}
response = requests.post(url, data=json.dumps(payload), headers=headers, verify=verify_cert)
else:
headers = {"content-type": "application/json"}
response = requests.post(url, data=json.dumps(payload), headers=headers, verify=verify_cert, auth=(idrac_username, idrac_password))
if response.status_code == 204:
logging.info("\n- PASS, POST command passed to power off the server")
time.sleep(10)
else:
logging.error("\n- FAIL, POST command failed, status code %s returned\n" % response.status_code)
logging.error(response.json())
sys.exit(1)
url = 'https://%s/redfish/v1/Chassis/System.Embedded.1/Actions/Oem/DellOemChassis.ExtendedReset' % idrac_ip
payload = {"ResetType": "PowerCycle", "FinalPowerState":"On"}
if args["x"]:
headers = {'content-type': 'application/json', 'X-Auth-Token': args["x"]}
response = requests.post(url, data=json.dumps(payload), headers=headers, verify=verify_cert)
else:
headers = {'content-type': 'application/json'}
response = requests.post(url, data=json.dumps(payload), headers=headers, verify=verify_cert, auth=(idrac_username, idrac_password))
if response.status_code == 204:
logging.info("\n- PASS, POST command passed to perform full virtual server a/c power cycle, status code %s returned" % response.status_code)
logging.info("\n- INFO, wait a few minutes for the process to complete, server will automatically power back on")
else:
logging.error("\n- FAIL, POST command failed, status code %s returned\n" % response.status_code)
logging.error(response.json())
sys.exit(1)
def reboot_server():
if args["x"]:
response = requests.get('https://%s/redfish/v1/Systems/System.Embedded.1' % idrac_ip, verify=verify_cert, headers={'X-Auth-Token': args["x"]})
else:
response = requests.get('https://%s/redfish/v1/Systems/System.Embedded.1' % idrac_ip, verify=verify_cert,auth=(idrac_username, idrac_password))
data = response.json()
logging.info("- INFO, Current server power state is: %s" % data['PowerState'])
if data['PowerState'] == "On":
url = 'https://%s/redfish/v1/Systems/System.Embedded.1/Actions/ComputerSystem.Reset' % idrac_ip
payload = {'ResetType': 'GracefulShutdown'}
if args["x"]:
headers = {'content-type': 'application/json', 'X-Auth-Token': args["x"]}
response = requests.post(url, data=json.dumps(payload), headers=headers, verify=verify_cert)
else:
headers = {'content-type': 'application/json'}
response = requests.post(url, data=json.dumps(payload), headers=headers, verify=verify_cert,auth=(idrac_username,idrac_password))
if response.status_code == 204:
logging.info("- PASS, POST command passed to gracefully power OFF server")
logging.info("- INFO, script will now verify the server was able to perform a graceful shutdown. If the server was unable to perform a graceful shutdown, forced shutdown will be invoked in 5 minutes")
time.sleep(15)
start_time = datetime.now()
else:
logging.error("\n- FAIL, Command failed to gracefully power OFF server, status code is: %s\n" % response.status_code)
logging.error("Extended Info Message: {0}".format(response.json()))
sys.exit(0)
while True:
if args["x"]:
response = requests.get('https://%s/redfish/v1/Systems/System.Embedded.1' % idrac_ip, verify=verify_cert, headers={'X-Auth-Token': args["x"]})
else:
response = requests.get('https://%s/redfish/v1/Systems/System.Embedded.1' % idrac_ip, verify=verify_cert,auth=(idrac_username, idrac_password))
data = response.json()
current_time = str(datetime.now() - start_time)[0:7]
if data['PowerState'] == "Off":
logging.info("- PASS, GET command passed to verify graceful shutdown was successful and server is in OFF state")
break
elif current_time >= "0:05:00":
logging.info("- INFO, unable to perform graceful shutdown, server will now perform forced shutdown")
payload = {'ResetType': 'ForceOff'}
if args["x"]:
headers = {'content-type': 'application/json', 'X-Auth-Token': args["x"]}
response = requests.post(url, data=json.dumps(payload), headers=headers, verify=verify_cert)
else:
headers = {'content-type': 'application/json'}
response = requests.post(url, data=json.dumps(payload), headers=headers, verify=verify_cert,auth=(idrac_username,idrac_password))
if response.status_code == 204:
logging.info("- PASS, POST command passed to perform forced shutdown")
time.sleep(15)
if args["x"]:
response = requests.get('https://%s/redfish/v1/Systems/System.Embedded.1' % idrac_ip, verify=verify_cert, headers={'X-Auth-Token': args["x"]})
else:
response = requests.get('https://%s/redfish/v1/Systems/System.Embedded.1' % idrac_ip, verify=verify_cert,auth=(idrac_username, idrac_password))
data = response.json()
if data['PowerState'] == "Off":
logging.info("- PASS, GET command passed to verify forced shutdown was successful and server is in OFF state")
break
else:
logging.error("- FAIL, server not in OFF state, current power status is %s" % data['PowerState'])
sys.exit(0)
else:
continue
payload = {'ResetType': 'On'}
if args["x"]:
headers = {'content-type': 'application/json', 'X-Auth-Token': args["x"]}
response = requests.post(url, data=json.dumps(payload), headers=headers, verify=verify_cert)
else:
headers = {'content-type': 'application/json'}
response = requests.post(url, data=json.dumps(payload), headers=headers, verify=verify_cert,auth=(idrac_username,idrac_password))
if response.status_code == 204:
logging.info("- PASS, POST command passed to power ON server")
else:
logging.error("\n- FAIL, Command failed to power ON server, status code is: %s\n" % response.status_code)
logging.error("Extended Info Message: {0}".format(response.json()))
sys.exit(0)
elif data['PowerState'] == "Off":
url = 'https://%s/redfish/v1/Systems/System.Embedded.1/Actions/ComputerSystem.Reset' % idrac_ip
payload = {'ResetType': 'On'}
if args["x"]:
headers = {'content-type': 'application/json', 'X-Auth-Token': args["x"]}
response = requests.post(url, data=json.dumps(payload), headers=headers, verify=verify_cert)
else:
headers = {'content-type': 'application/json'}
response = requests.post(url, data=json.dumps(payload), headers=headers, verify=verify_cert,auth=(idrac_username,idrac_password))
if response.status_code == 204:
logging.info("- PASS, Command passed to power ON server, code return is %s" % response.status_code)
else:
logging.error("\n- FAIL, Command failed to power ON server, status code is: %s\n" % response.status_code)
logging.error("Extended Info Message: {0}".format(response.json()))
sys.exit(0)
else:
logging.error("- FAIL, unable to get current server power state to perform either reboot or power on")
sys.exit(0)
def check_idrac_connection():
run_network_connection_function = ""
if platform.system().lower() == "windows":
ping_command = "ping -n 3 %s" % idrac_ip
elif platform.system().lower() == "linux":
ping_command = "ping -c 3 %s" % idrac_ip
else:
logging.error("- FAIL, unable to determine OS type, check iDRAC connection function will not execute")
run_network_connection_function = "fail"
execute_command = subprocess.call(ping_command, stdout=subprocess.PIPE, shell=True)
if execute_command != 0:
ping_status = "lost"
else:
ping_status = "good"
pass
if ping_status == "lost":
logging.info("- INFO, iDRAC network connection lost due to slow ping network response, waiting 30 seconds to access iDRAC again")
time.sleep(30)
while True:
if run_network_connection_function == "fail":
break
execute_command=subprocess.call(ping_command, stdout=subprocess.PIPE)
if execute_command != 0:
ping_status = "lost"
else:
ping_status = "good"
if ping_status == "lost":
logging.info("- INFO, unable to ping iDRAC IP, script will wait 30 seconds and try again")
time.sleep(30)
continue
else:
break
while True:
try:
if args["x"]:
response = requests.get('https://%s/redfish/v1/TaskService/Tasks/%s' % (idrac_ip, job_id), verify=verify_cert, headers={'X-Auth-Token': args["x"]})
else:
response = requests.get('https://%s/redfish/v1/TaskService/Tasks/%s' % (idrac_ip, job_id), verify=verify_cert, auth=(idrac_username, idrac_password))
except requests.ConnectionError as error_message:
logging.info("- INFO, GET request failed due to connection error, retry")
time.sleep(10)
continue
break
if __name__ == "__main__":
if args["script_examples"]:
script_examples()
if args["ip"] or args["ssl"] or args["u"] or args["p"] or args["x"]:
idrac_ip = args["ip"]
idrac_username = args["u"]
if args["p"]:
idrac_password = args["p"]
if not args["p"] and not args["x"] and args["u"]:
idrac_password = getpass.getpass("\n- Argument -p not detected, pass in iDRAC user %s password: " % args["u"])
if args["ssl"]:
if args["ssl"].lower() == "true":
verify_cert = True
elif args["ssl"].lower() == "false":
verify_cert = False
else:
verify_cert = False
else:
verify_cert = False
check_supported_idrac_version()
get_server_generation()
else:
logging.error("\n- FAIL, invalid argument values or not all required parameters passed in. See help text or argument --script-examples for more details.")
sys.exit(0)
if args["get"]:
get_FW_inventory()
elif args["location"]:
get_idrac_version()
download_image_create_update_job()
check_job_status()
if args["reboot"]:
logging.info("- INFO, powering on or rebooting server to apply the firmware")
loop_check_final_job_status()
else:
logging.info("- INFO, argument --reboot not detected. Update job is marked as scheduled and will be applied on next server reboot")
sys.exit(0)
else:
logging.error("\n- FAIL, invalid argument values or not all required parameters passed in. See help text or argument --script-examples for more details.")