-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathredfish_trawler.py
351 lines (257 loc) · 11.2 KB
/
redfish_trawler.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
# SPDX-FileCopyrightText: 2023-2024 DMTF
# SPDX-License-Identifier: BSD-3-Clause
# Copyright Notice:
# Copyright 2023-2024 DMTF. All rights reserved.
# License: BSD 3-Clause License. For full text see link: https://github.com/DMTF/Redfish-Trawler/blob/main/LICENSE.md
import argparse
import redfish
from urllib import parse
from flask import Flask, render_template, request
app = Flask(__name__)
SERVICE_PARAMS = ["base_url", "username", "password"]
LOGIN_TYPES = {
'None': None,
'Basic': redfish.AuthMethod.BASIC,
'Session': redfish.AuthMethod.SESSION
}
# TODO: Reduce code reuse in backend, such as in GET PATCH POST DELETE
# TODO: Reduce code reuse in action forms in Frontend
# TODO: Solve modals being shared between actions
# TODO: Do not store credentials locally, let the browser do it, if at all.
available_services = {}
live_services = {}
@app.route('/services', methods=['GET'])
def get_service_details():
"""Gives us list of services that are available and live
"""
return {
'available': {nick: host['base_url'] for nick, host in available_services.items()},
'live': list(live_services.keys())
}
@app.route('/add-service', methods=['POST'])
def receive_service_details():
"""POST to /add-service, add service details to program
"""
nick = request.json.get('nickname')
if nick is None or len(nick.strip()) == 0:
nick = "Host-{}".format(
len([x for x in available_services.keys() if 'Host-' in x]))
# TODO: validate information before categorizing it
available_services[nick] = {
"base_url": request.json.get('hostname'),
"username": request.json.get('username'),
"password": request.json.get('password'),
"logintype": LOGIN_TYPES.get(request.json.get('logintype'))
}
print(nick, available_services[nick])
return get_service_details()
@app.route('/delete-service', methods=['POST'])
def remove_service_details():
"""POST to /remove-service, removes service_name from active program
"""
service_name = request.json.get('hostname')
print(service_name)
print(available_services)
if service_name in available_services:
del available_services[service_name]
else:
# return 'SERVICE DOESNT EXIST'
return get_service_details()
if service_name in live_services:
# close active redfish service
pass
return get_service_details()
@app.route('/close-service', methods=['POST'])
def close_service():
service_name = request.json.get('service_name')
if service_name in live_services:
# close active redfish service
pass
else:
# return 'SERVICE NOT LIVE'
return get_service_details()
return get_service_details()
@app.route("/")
def start():
return render_template(
'compiled/index.html'
)
@app.route("/test_page")
def test_page():
return render_template(
'test_page.html',
available=available_services.keys(),
live=live_services.keys()
)
@app.route("/redfish/v1", defaults={'path': ''}, methods=["GET", "POST", "PATCH", "DELETE"])
@app.route("/redfish/v1/", defaults={'path': ''}, methods=["GET", "POST", "PATCH", "DELETE"])
@app.route("/redfish/v1/<path:path>", methods=["GET", "POST", "PATCH", "DELETE"])
def route_to_service(path):
service_name = request.args.get('service_name')
if service_name is None:
return 'NO SERVICE GIVEN', 400
try:
context = get_service_context(service_name)
except KeyError:
return 'MISSING SERVICE', 400
print(request.path)
if request.method == 'GET':
response = context.get(request.path)
# TODO: Check if we need to use headers for anything
if response.status in [200]:
contenttype = response.getheader('content-type')
if 'application/json' in contenttype:
return {'_payload': response.dict}
return "STATUS CODE {}".format(response.status)
if request.method == 'POST':
print(request.json)
# TODO: Check into sanitizing all inputs, even if this is a local program
response = context.post(request.path, body=request.json)
# TODO: Check if we need to use headers for anything
if response:
contenttype = response.getheader('content-type')
if contenttype and 'application/json' in contenttype:
return response.dict, response.status
else:
return response.text, response.status
return "STATUS CODE {}".format(response.status)
if request.method == 'PATCH':
print(request.json)
# TODO: Check into sanitizing all inputs, even if this is a local program
response = context.patch(request.path, body=request.json)
# TODO: Check if we need to use headers for anything
if response:
contenttype = response.getheader('content-type')
if contenttype and 'application/json' in contenttype:
return response.dict, response.status
else:
return response.text, response.status
return "STATUS CODE {}".format(response.status)
if request.method == 'DELETE':
print(request.json)
# TODO: Check into sanitizing all inputs, even if this is a local program
response = context.delete(request.path)
# TODO: Check if we need to use headers for anything
if response:
contenttype = response.getheader('content-type')
if contenttype and 'application/json' in contenttype:
return response.dict, response.status
else:
return response.text, response.status
return "STATUS CODE {}".format(response.status)
return "STATUS CODE {}".format(405)
def get_all_members(context, all_members):
data = []
url_payloads = {}
for url in [member['@odata.id'] for member in all_members]:
# TODO: Maybe use expected behavior from full path
scheme, netloc, path, params, query, fragment = parse.urlparse(url)
if path not in url_payloads:
response = context.get(path)
url_payloads[path] = response
response = url_payloads[path]
if response.status in [200]:
target = response.dict
if fragment:
target_path = fragment.split('/')[1:] # /path/to/rsc
for sub_path in target_path:
target = target[int(sub_path)] if sub_path.isdigit() else target[sub_path]
data.append(target)
print(data)
return data
# TODO: return proper response to frontend in any situation where a login fails or a payload is denied/400 code
@app.route('/page-view', methods=["GET"])
def gather_page_info():
service_name = request.args.get('service_name')
page_name = request.args.get('page_name')
print(service_name, page_name)
# TODO: make @app routings for consistent 400 errors
if service_name is None:
return 'NO SERVICE GIVEN', 400
if page_name is None:
return 'NO PAGE GIVEN', 400
try:
context = get_service_context(service_name)
except KeyError:
return 'MISSING SERVICE', 400
print(context)
# TODO: Work on polling individual resources, using Redfish's baked in polling registering function (and other message registry stuff)?
if page_name.lower() == 'chassis':
# if single chassis...
chassis_name = request.args.get('chassis_name')
if chassis_name:
return_data = {'_fans': [], '_poweredby': [], '_temperatures': [], '_payload': {}}
response = context.get('/redfish/v1/Chassis/{}'.format(chassis_name))
if response.status in [200]:
decoded = response.dict
return_data['_payload'] = decoded
response_thermal = context.get(decoded['Thermal'].get('@odata.id')) if decoded.get('Thermal') else None
response_links = decoded.get('Links', {})
# fans
all_fans = response_links.get('CooledBy', [])
return_data['_fans'].extend(get_all_members(context, all_fans))
# powered
all_powers = response_links.get('PoweredBy', [])
return_data['_poweredby'].extend(get_all_members(context, all_powers))
# local thermal
if response_thermal:
for inside_fan in response_thermal.dict.get('Fans', []):
return_data['_fans'].append(inside_fan)
for inside_temp in response_thermal.dict.get('Temperatures', []):
return_data['_temperatures'].append(inside_temp)
else:
return 'NO CHASSIS FOUND', 400
else:
# Return Format: _chassis: exposed chassis data, response: full response dict
return_data = {'_chassis': [], '_payload': {}}
response = context.get('/redfish/v1/Chassis')
if response.status in [200]:
decoded = response.dict
return_data['_payload'] = decoded
return_data['_chassis'] = get_all_members(context, decoded['Members'])
else:
return 'NO CHASSIS FOUND', 400
return return_data
if page_name.lower() == 'usermanagement':
# Return Format: _chassis: exposed chassis data, response: full response dict
return_data = {'_accounts': [], '_roles': [], '_payload': {}}
response = context.get('/redfish/v1/AccountService')
if response.status in [200]:
decoded = response.dict
return_data['_payload'] = decoded
response_accounts = context.get(decoded['Accounts'].get('@odata.id')) if decoded.get('Accounts') else None
if response_accounts:
return_data['_accounts'] = get_all_members(context, response_accounts.dict['Members'])
response_roles = context.get(decoded['Roles'].get('@odata.id')) if decoded.get('Roles') else None
if response_roles:
return_data['_roles'] = get_all_members(context, response_roles.dict['Members'])
else:
return 'NO ACCOUNTSERVICE FOUND', 400
return return_data
return 'OK PAGE VIEW'
def get_service_context(service_name):
"""Get service context. If it doesn't exist, create the context.
Raises:
KeyError: service_name doesn't exist
"""
if live_services.get(service_name) is None:
if available_services.get(service_name) is None:
raise KeyError(
'SERVICE {} DOESNT EXIST, GIVE 400 ERROR'.format(service_name))
params = available_services.get(service_name)
context = redfish.redfish_client(
base_url=params['base_url'],
username=params['username'],
password=params['password']
)
context.login(auth=params['logintype'])
live_services[service_name] = context
return live_services[service_name]
if __name__ == '__main__':
argget = argparse.ArgumentParser(description='Redfish Trawler')
# config
argget.add_argument('--hostip', type=str, default='0.0.0.0',
help='ip to host on, default 0.0.0.0')
args = argget.parse_args()
# Setup ENDPOINTS
# hello.py