forked from openshift-metal3/fakefish
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfakefish.py
executable file
·204 lines (179 loc) · 7.57 KB
/
fakefish.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
#!/usr/bin/env python3
# coding=utf-8
import flask
import json
import os
import requests
import subprocess
import argparse
from datetime import datetime
from werkzeug.http import parse_authorization_header
from requests.packages.urllib3.exceptions import InsecureRequestWarning
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
app = flask.Flask(__name__)
@app.route('/redfish/v1/')
def root_resource():
return flask.render_template('root.json')
@app.route('/redfish/v1/Managers')
def manager_collection_resource():
return flask.render_template('managers.json')
@app.route('/redfish/v1/Systems')
def system_collection_resource():
return flask.render_template('systems.json')
@app.route('/redfish/v1/Systems/1', methods=['GET', 'PATCH'])
def system_resource():
username, password = get_credentials(flask.request)
global bmc_ip
if flask.request.method == 'GET':
return flask.render_template(
'fake_system.json',
power_state=power_state,
)
else:
app.logger.info('patch request')
boot = flask.request.json.get('Boot')
if not boot:
return ('PATCH only works for Boot'), 400
if boot:
target = boot.get('BootSourceOverrideTarget')
mode = boot.get('BootSourceOverrideMode')
if not target and not mode:
return ('Missing the BootSourceOverrideTarget and/or '
'BootSourceOverrideMode element', 400)
else:
app.logger.info('Running script that sets boot from VirtualCD once')
try:
my_env = set_env_vars(bmc_ip, username, password)
subprocess.check_call(['custom_scripts/bootfromcdonce.sh'], env=my_env)
except subprocess.CalledProcessError as e:
return ('Failed to set boot from virtualcd once', 400)
return '', 204
@app.route('/redfish/v1/Systems/1/EthernetInterfaces', methods=['GET'])
def manage_interfaces():
return flask.render_template('fake_interfaces.json')
@app.route('/redfish/v1/Managers/1', methods=['GET'])
def manager_resource():
return flask.render_template(
'fake_manager.json',
date_time=datetime.now().strftime('%Y-%M-%dT%H:%M:%S+00:00'),
)
@app.route('/redfish/v1/Systems/1/Actions/ComputerSystem.Reset',
methods=['POST'])
def system_reset_action():
global bmc_ip
username, password = get_credentials(flask.request)
reset_type = flask.request.json.get('ResetType')
global power_state
if reset_type == 'On':
app.logger.info('Running script that powers on the server')
try:
my_env = set_env_vars(bmc_ip, username, password)
subprocess.check_call(['custom_scripts/poweron.sh'], env=my_env)
except subprocess.CalledProcessError as e:
return ('Failed to poweron the server', 400)
power_state = 'On'
else:
app.logger.info('Running script that powers off the server')
try:
my_env = set_env_vars(bmc_ip, username, password)
subprocess.check_call(['custom_scripts/poweroff.sh'], env=my_env)
except subprocess.CalledProcessError as e:
return ('Failed to poweroff the server', 400)
power_state = 'Off'
return '', 204
@app.route('/redfish/v1/Managers/1/VirtualMedia', methods=['GET'])
def virtualmedia_collection_resource():
return flask.render_template('virtualmedias.json')
@app.route('/redfish/v1/Managers/1/VirtualMedia/Cd', methods=['GET'])
def virtualmedia_cd_resource():
return flask.render_template(
'virtualmedia_cd.json',
inserted=inserted,
image_url=image_url,
)
@app.route('/redfish/v1/Managers/1/VirtualMedia/Cd/Actions/VirtualMedia.InsertMedia',
methods=['POST'])
def virtualmedia_insert():
global bmc_ip
username, password = get_credentials(flask.request)
image = flask.request.json.get('Image')
if not image:
return('POST only works for Image'), 400
else:
global inserted
global image_url
inserted = True
image_url = image
app.logger.info('Running script that mounts cd with iso %s', image)
try:
my_env = set_env_vars(bmc_ip, username, password)
subprocess.check_call(['custom_scripts/mountcd.sh', image_url], env=my_env)
except subprocess.CalledProcessError as e:
return ('Failed to mount virtualcd', 400)
return '', 204
@app.route('/redfish/v1/Managers/1/VirtualMedia/Cd/Actions/VirtualMedia.EjectMedia',
methods=['POST'])
def virtualmedia_eject():
global bmc_ip
global inserted
global image_url
username, password = get_credentials(flask.request)
inserted = False
image_url = ''
app.logger.info('Running script that unmounts cd')
try:
my_env = set_env_vars(bmc_ip, username, password)
subprocess.check_call(['custom_scripts/unmountcd.sh'], env=my_env)
except subprocess.CalledProcessError as e:
return ('Failed to unmount virtualcd', 400)
return '', 204
def get_credentials(flask_request):
auth = flask_request.headers.get('Authorization', None)
username = ''
password = ''
if auth is not None:
creds = parse_authorization_header(auth)
username = creds.username
password = creds.password
app.logger.debug('Returning credentials')
app.logger.debug('Username: ' + username + ', password: ' + password)
return username, password
def set_env_vars(bmc_endpoint, username, password):
my_env = os.environ.copy()
my_env["BMC_ENDPOINT"] = bmc_endpoint
my_env["BMC_USERNAME"] = username
my_env["BMC_PASSWORD"] = password
return my_env
def run(port, debug, tls_mode, cert_file, key_file):
"""
"""
if tls_mode == 'adhoc':
app.run(host='::', port=port, debug=debug, ssl_context='adhoc')
elif tls_mode == 'disabled':
app.run(host='::', port=port, debug=debug)
else:
if os.path.exists(cert_file) and os.path.exists(key_file):
app.run(host='::', port=port, debug=debug, ssl_context=(cert_file, key_file))
else:
app.logger.error('%s or %s not found.', cert_file, key_file)
exit()
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='FakeFish, an experimental RedFish proxy that calls shell scripts for executing hardware actions.')
parser.add_argument('--tls-mode', type=str, choices=['adhoc', 'self-signed', 'disabled'], default='adhoc', help='Configures TLS mode. \
\'self-signed\' mode expects users to configure a cert and a key files. (default: %(default)s)')
parser.add_argument('--cert-file', type=str, default='./cert.pem', help='Path to the certificate public key file. (default: %(default)s)')
parser.add_argument('--key-file', type=str, default='./cert.key', help='Path to the certificate private key file. (default: %(default)s)')
parser.add_argument('-r', '--remote-bmc', type=str, required=True, help='The BMC IP this FakeFish instance will connect to. e.g: 192.168.1.10')
parser.add_argument('-p','--listen-port', type=int, required=False, default=9000, help='The port where this FakeFish instance will listen for connections.')
parser.add_argument('--debug', action='store_true')
args = parser.parse_args()
bmc_ip = args.remote_bmc
port = args.listen_port
debug = args.debug
tls_mode = args.tls_mode
cert_file = args.cert_file
key_file = args.key_file
inserted = False
image_url = ''
power_state = 'On'
run(port, debug, tls_mode, cert_file, key_file)