-
Notifications
You must be signed in to change notification settings - Fork 49
/
get_firmware_baselines.py
286 lines (238 loc) · 11.8 KB
/
get_firmware_baselines.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
#
# _author_ = Grant Curell <grant_curell@dell.com>
#
# Copyright (c) 2022 Dell EMC Corporation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
"""
#### Synopsis
Gets a list of all firmware baselines available from an OME server or baselines associated
with a specific device.
#### Description
This script uses the OME REST API to find baselines associated
with a given server. For authentication X-Auth is used over Basic
Authentication. Note: The credentials entered are not stored to disk.
#### Python Example
`python get_firmware_baseline.py -i 192.168.1.93 -u admin -p somepass -r 192.168.1.45`
"""
import argparse
import json
import sys
from argparse import RawTextHelpFormatter
from getpass import getpass
from pprint import pprint
from urllib.parse import urlparse
import requests
import urllib3
def authenticate(ome_ip_address: str, ome_username: str, ome_password: str) -> dict:
"""
Authenticates with OME and creates a session
Args:
ome_ip_address: IP address of the OME server
ome_username: Username for OME
ome_password: OME password
Returns: A dictionary of HTTP headers
Raises:
Exception: A generic exception in the event of a failure to connect.
"""
authenticated_headers = {'content-type': 'application/json'}
session_url = 'https://%s/api/SessionService/Sessions' % ome_ip_address
user_details = {'UserName': ome_username,
'Password': ome_password,
'SessionType': 'API'}
try:
session_info = requests.post(session_url, verify=False,
data=json.dumps(user_details),
headers=authenticated_headers)
except requests.exceptions.ConnectionError:
print("Failed to connect to OME. This typically indicates a network connectivity problem. Can you ping OME?")
sys.exit(0)
if session_info.status_code == 201:
authenticated_headers['X-Auth-Token'] = session_info.headers['X-Auth-Token']
return authenticated_headers
else:
print("There was a problem authenticating with OME. Are you sure you have the right username, password, "
"and IP?")
raise Exception("There was a problem authenticating with OME. Are you sure you have the right username, "
"password, and IP?")
def get_data(authenticated_headers: dict, url: str, odata_filter: str = None, max_pages: int = None) -> dict:
"""
This function retrieves data from a specified URL. Get requests from OME return paginated data. The code below
handles pagination. This is the equivalent in the UI of a list of results that require you to go to different
pages to get a complete listing.
Args:
authenticated_headers: A dictionary of HTTP headers generated from an authenticated session with OME
url: The API url against which you would like to make a request
odata_filter: An optional parameter for providing an odata filter to run against the API endpoint.
max_pages: The maximum number of pages you would like to return
Returns: Returns a dictionary of data received from OME
"""
next_link_url = None
if odata_filter:
count_data = requests.get(url + '?$filter=' + odata_filter, headers=authenticated_headers, verify=False)
if count_data.status_code == 400:
print("Received an error while retrieving data from %s:" % url + '?$filter=' + odata_filter)
pprint(count_data.json()['error'])
return {}
count_data = count_data.json()
if count_data['@odata.count'] <= 0:
print("No results found!")
return {}
else:
count_data = requests.get(url, headers=authenticated_headers, verify=False).json()
if 'value' in count_data:
data = count_data['value']
else:
data = count_data
if '@odata.nextLink' in count_data:
# Grab the base URI
next_link_url = '{uri.scheme}://{uri.netloc}'.format(uri=urlparse(url)) + count_data['@odata.nextLink']
i = 1
while next_link_url is not None:
# Break if we have reached the maximum number of pages to be returned
if max_pages:
if i >= max_pages:
break
else:
i = i + 1
response = requests.get(next_link_url, headers=authenticated_headers, verify=False)
next_link_url = None
if response.status_code == 200:
requested_data = response.json()
if requested_data['@odata.count'] <= 0:
print("No results found!")
return {}
# The @odata.nextLink key is only present in data if there are additional pages. We check for it and if it
# is present we get a link to the page with the next set of results.
if '@odata.nextLink' in requested_data:
next_link_url = '{uri.scheme}://{uri.netloc}'.format(uri=urlparse(url)) + \
requested_data['@odata.nextLink']
if 'value' in requested_data:
data += requested_data['value']
else:
data += requested_data
else:
print("Unknown error occurred. Received HTTP response code: " + str(response.status_code) +
" with error: " + response.text)
raise Exception("Unknown error occurred. Received HTTP response code: " + str(response.status_code)
+ " with error: " + response.text)
return data
def get_firmware_baselines(authenticated_headers: dict,
ome_ip_address: str,
device_id: int = None,
service_tag: str = None,
device_idrac_ip: str = None,
device_name: str = None):
"""
Gets a list of firmware baselines from OME
Args:
authenticated_headers: A dictionary of HTTP headers generated from an authenticated session with OME
ome_ip_address: IP address of the OME server
device_id: (optional) The device ID of a host whose firmware baselines a user wants retrieved
service_tag: (optional) The service tag of a host whose firmware baselines a user wants retrieved
device_idrac_ip: (optional) The idrac IP of a host whose firmware baselines a user wants retrieved
device_name: (optional): The name of a host whose firmware baselines a user wants retrieved
"""
print("Retrieving a list of firmware")
firmware_baselines = \
get_data(authenticated_headers, "https://%s/api/UpdateService/Baselines" % ome_ip_address) # type: dict
if not firmware_baselines:
print("Unable to retrieve firmware list from %s. This could happen for many reasons but the most likely is a"
" failure in the connection." % ome_ip_address)
exit(0)
if len(firmware_baselines) <= 0:
print("No firmware baselines found on this OME server: " + ome_ip_address + ". Exiting.")
exit(0)
# At this point all firmware data is contained in the variable firmware_baselines. This next part simply collects
# the names in a list and prints them.
# If the user passed a device name, resolve that name to a device ID
if device_name:
device_id = get_data(authenticated_headers, "https://%s/api/DeviceService/Devices" % ome_ip_address,
"DeviceName eq \'%s\'" % device_name)
if not device_id:
print("Error: We were unable to find device name " + device_name + " on this OME server. Exiting.")
exit(0)
else:
device_id = device_id[0]['Id']
elif service_tag:
device_id = get_data(authenticated_headers, "https://%s/api/DeviceService/Devices" % ome_ip_address,
"DeviceServiceTag eq \'%s\'" % service_tag)
if not device_id:
print("Error: We were unable to find service tag " + service_tag + " on this OME server. Exiting.")
exit(0)
else:
device_id = device_id[0]['Id']
elif device_idrac_ip:
device_list = get_data(authenticated_headers, "https://%s/api/DeviceService/Devices" % ome_ip_address)
if not device_list:
print("Unable to get device list from %s. This could happen for many reasons but the most likely is a"
" failure in the connection." % ome_ip_address)
exit(0)
if len(device_list) <= 0:
print("No devices found on this OME server: " + ome_ip_address + ". Exiting.")
exit(0)
for device_dictionary in device_list:
if device_dictionary['DeviceManagement'][0]['NetworkAddress'] == device_idrac_ip.strip():
device_id = device_dictionary['Id']
break
if not device_idrac_ip:
print("Error: We were unable to find idrac IP " + device_idrac_ip + " on this OME server. Exiting.")
exit(0)
firmware_baseline_names = [] # type: list
for firmware_baseline in firmware_baselines:
if device_id:
if len(firmware_baseline['Targets']) > 0:
for target in firmware_baseline['Targets']:
if target["Id"] == device_id:
firmware_baseline_names.append(firmware_baseline["Name"])
else:
firmware_baseline_names.append(firmware_baseline["Name"])
if len(firmware_baseline_names) > 0:
print("Baselines are:")
print(firmware_baseline_names)
else:
print("No firmware baselines found!")
if __name__ == '__main__':
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
parser = argparse.ArgumentParser(description=__doc__,
formatter_class=RawTextHelpFormatter)
parser.add_argument("--ip", "-i", required=True, help="OME Appliance IP")
parser.add_argument("--user", "-u", required=False,
help="Username for the OME Appliance", default="admin")
parser.add_argument("--password", "-p", required=False,
help="Password for the OME Appliance")
exclusive_group = parser.add_mutually_exclusive_group(required=False)
exclusive_group.add_argument("--device-id", "-d", help="The device ID ")
exclusive_group.add_argument("--service-tag", "-s", help="A device service tag")
exclusive_group.add_argument("--idrac-ip", "-r", help="A device idrac IP")
exclusive_group.add_argument("--device-name", "-n", help="The name of the device in OME")
args = parser.parse_args()
if not args.password:
if not sys.stdin.isatty():
# notify user that they have a bad terminal
# perhaps if os.name == 'nt': , prompt them to use winpty?
print("Your terminal is not compatible with Python's getpass module. You will need to provide the"
" --password argument instead. See https://stackoverflow.com/a/58277159/4427375")
sys.exit(0)
else:
password = getpass()
else:
password = args.password
try:
headers = authenticate(args.ip, args.user, password)
if not headers:
exit(0)
get_firmware_baselines(headers, args.ip, args.device_id, args.service_tag, args.idrac_ip, args.device_name)
except Exception as error:
print("Unexpected error:", str(error))