11import os
22import subprocess
33from flask import Flask , jsonify , render_template
4+ import requests
5+ from requests .auth import HTTPBasicAuth
46
57app = Flask (__name__ )
68
79ip_address = os .getenv ("IP_ADDRESS" , "192.168.0.120" )
810username = os .getenv ("IDRAC_USERNAME" , "root" )
911password = os .getenv ("IDRAC_PASSWORD" , "calvin" )
1012
11- IDRAC_SCRIPTS_BASE_PATH = os .getenv ("IDRAC_SCRIPTS_BASE_PATH" , "./iDRAC-Redfish-Scripting/Redfish\ Python/" )
13+ IDRAC_SCRIPTS_BASE_PATH = os .getenv (
14+ "IDRAC_SCRIPTS_BASE_PATH" , "./iDRAC-Redfish-Scripting/Redfish Python/"
15+ )
1216
1317
1418@app .route ("/" )
@@ -17,16 +21,203 @@ def index():
1721
1822
1923def execute_redfish_command (action ):
20- if action == 'GetPowerState' :
21- breakpoint ()
22- command = f"python { IDRAC_SCRIPTS_BASE_PATH } GetSetPowerStateREDFISH.py -ip { ip_address } -u { username } -p { password } --get"
24+ if action == "ForceRestart" :
25+ url = f"https://{ ip_address } /redfish/v1/Systems/System.Embedded.1/Actions/ComputerSystem.Reset"
26+ headers = {"Content-Type" : "application/json" }
27+ payload = {"ResetType" : "ForceRestart" }
28+ auth = (username , password )
29+
30+ req = requests .post (
31+ url , headers = headers , json = payload , auth = auth , verify = False
32+ ) # noqa: E501
33+ return (
34+ jsonify (
35+ {
36+ "output" : req .text ,
37+ "error" : req .text ,
38+ }
39+ ),
40+ req .status_code ,
41+ )
42+
43+ if action == "MountiPXE" :
44+ command = (
45+ f'curl -v -H "Content-Type: application/json" -X POST -u { username } :{ password } '
46+ f"-k https://{ ip_address } /redfish/v1/Managers/iDRAC.Embedded.1/VirtualMedia/CD/Actions/VirtualMedia.InsertMedia "
47+ f'-d \' {{"Image": "http://138.201.59.208/sites/default/files/ipxe.iso"}}\' '
48+ )
49+
50+ # URL
51+ url = f"https://{ ip_address } /redfish/v1/Managers/iDRAC.Embedded.1/VirtualMedia/CD/Actions/VirtualMedia.InsertMedia"
52+
53+ # Headers
54+ headers = {"Content-Type" : "application/json" }
55+
56+ # Data
57+ data = {"Image" : "http://138.201.59.208/sites/default/files/ipxe.iso" }
58+
59+ # Making the request
60+ req = requests .post (
61+ url ,
62+ headers = headers ,
63+ json = data ,
64+ auth = HTTPBasicAuth (username , password ),
65+ verify = False ,
66+ )
67+ return (
68+ jsonify (
69+ {
70+ "output" : req .text ,
71+ "error" : req .text ,
72+ }
73+ ),
74+ req .status_code ,
75+ )
76+
77+ if action == "UnMountiPXE" :
78+
79+ # URL
80+ url = f"https://{ ip_address } /redfish/v1/Managers/iDRAC.Embedded.1/VirtualMedia/CD/Actions/VirtualMedia.EjectMedia"
81+
82+ # Headers
83+ headers = {"Content-Type" : "application/json" }
84+
85+ # Data
86+ data = {}
87+
88+ # Making the request
89+ req = requests .post (
90+ url ,
91+ headers = headers ,
92+ json = data ,
93+ auth = HTTPBasicAuth (username , password ),
94+ verify = False ,
95+ )
96+ return (
97+ jsonify (
98+ {
99+ "output" : req .text ,
100+ "error" : req .text ,
101+ }
102+ ),
103+ req .status_code ,
104+ )
105+
106+ if action == "SetOnetimeBootValueVirtualCD" :
107+
108+ # URL
109+ url = f"https://{ ip_address } /redfish/v1/Systems/System.Embedded.1"
110+
111+ # Headers
112+ headers = {"Content-Type" : "application/json" }
113+
114+ # Data
115+ data = {"Boot" : {"BootSourceOverrideTarget" : "None" }}
116+
117+ # Making the request
118+ req = requests .patch (
119+ url ,
120+ headers = headers ,
121+ json = data ,
122+ auth = HTTPBasicAuth (username , password ),
123+ verify = False ,
124+ )
125+ return (
126+ jsonify (
127+ {
128+ "output" : req .text ,
129+ "error" : req .text ,
130+ }
131+ ),
132+ req .status_code ,
133+ )
134+
135+ if action == "GetOnetimeBootValue" :
136+
137+ # URL
138+ url = f"https://{ ip_address } /redfish/v1/Systems/System.Embedded.1"
139+
140+ # Making the request
141+ req = requests .get (
142+ url ,
143+ auth = HTTPBasicAuth (username , password ),
144+ verify = False ,
145+ )
146+ return (
147+ jsonify (
148+ {
149+ "output" : req .text ,
150+ "error" : req .text ,
151+ }
152+ ),
153+ req .status_code ,
154+ )
155+
156+ if action == "GetPowerState" :
157+ command = f"python { IDRAC_SCRIPTS_BASE_PATH } GetSetPowerStateREDFISH.py -ip { ip_address } -u { username } -p { password } --get" # noqa: E501
23158 result = subprocess .run (command , capture_output = True , shell = True )
24- if action == ' ChangeBiosBootOrderREDFISH' :
25- command = f"python { IDRAC_SCRIPTS_BASE_PATH } ChangeBiosBootOrderREDFISH.py -ip { ip_address } -u { username } -p { password } --get"
159+ if action == " ChangeBiosBootOrderREDFISH" :
160+ command = f"python { IDRAC_SCRIPTS_BASE_PATH } ChangeBiosBootOrderREDFISH.py -ip { ip_address } -u { username } -p { password } --get" # noqa: E501
26161 result = subprocess .run (command , capture_output = True , shell = True )
27162
163+ if action == "On" :
164+ # URL
165+ url = f"https://{ ip_address } /redfish/v1/Systems/System.Embedded.1/Actions/ComputerSystem.Reset"
166+
167+ # Headers
168+ headers = {"Content-Type" : "application/json" }
169+
170+ # Data
171+ data = {"ResetType" : "On" }
172+
173+ # Making the request
174+ req = requests .post (
175+ url ,
176+ headers = headers ,
177+ json = data ,
178+ auth = HTTPBasicAuth (username , password ),
179+ verify = False ,
180+ )
181+ return (
182+ jsonify (
183+ {
184+ "output" : req .text ,
185+ "error" : req .text ,
186+ }
187+ ),
188+ req .status_code ,
189+ )
190+
191+ if action == "ForceOff" :
192+ # URL
193+ url = f"https://{ ip_address } /redfish/v1/Systems/System.Embedded.1/Actions/ComputerSystem.Reset"
194+
195+ # Headers
196+ headers = {"Content-Type" : "application/json" }
197+
198+ # Data
199+ data = {"ResetType" : "ForceOff" }
200+
201+ # Making the request
202+ req = requests .post (
203+ url ,
204+ headers = headers ,
205+ json = data ,
206+ auth = HTTPBasicAuth (username , password ),
207+ verify = False ,
208+ )
209+ return (
210+ jsonify (
211+ {
212+ "output" : req .text ,
213+ "error" : req .text ,
214+ }
215+ ),
216+ req .status_code ,
217+ )
218+
28219 else :
29- command = f"python { IDRAC_SCRIPTS_BASE_PATH } GetSetPowerStateREDFISH.py -ip { ip_address } -u { username } -p { password } --set { action } "
220+ command = f"python { IDRAC_SCRIPTS_BASE_PATH } GetSetPowerStateREDFISH.py -ip { ip_address } -u { username } -p { password } --set { action } " # noqa: E501
30221 result = subprocess .run (command , capture_output = True , shell = True )
31222 return (
32223 jsonify (
@@ -44,18 +235,50 @@ def set_power_on():
44235 return execute_redfish_command ("On" )
45236
46237
238+ @app .route ("/api/v1/ForceOff" , methods = ["POST" ])
239+ def set_power_off ():
240+ return execute_redfish_command ("ForceOff" )
241+
242+
243+ @app .route ("/api/v1/ForceRestart" , methods = ["POST" ])
244+ def force_restart ():
245+ return execute_redfish_command ("ForceRestart" )
246+
247+
47248@app .route ("/api/v1/GracefulShutdown" , methods = ["POST" ])
48249def set_power_graceful_shutdown ():
49250 return execute_redfish_command ("GracefulShutdown" )
50251
252+
51253@app .route ("/api/v1/GetPowerState" , methods = ["POST" ])
52254def get_power_state ():
53255 return execute_redfish_command ("GetPowerState" )
54256
257+
55258@app .route ("/api/v1/ChangeBiosBootOrderREDFISH" , methods = ["POST" ])
56259def get_bios_boot_order ():
57260 return execute_redfish_command ("ChangeBiosBootOrderREDFISH" )
58261
59262
263+ @app .route ("/api/v1/MountiPXE" , methods = ["POST" ])
264+ def mount_iPXE_iso ():
265+ return execute_redfish_command ("MountiPXE" )
266+
267+
268+ @app .route ("/api/v1/UnmountISO" , methods = ["POST" ])
269+ def unmount_iPXE_iso ():
270+ return execute_redfish_command ("UnMountiPXE" )
271+
272+
273+ @app .route ("/api/v1/GetOnetimeBootValue" , methods = ["POST" ])
274+ def get_current_onetime_boot_order ():
275+ return execute_redfish_command ("GetOnetimeBootValue" )
276+
277+
278+ @app .route ("/api/v1/SetOnetimeBootValueVirtualCD" , methods = ["POST" ])
279+ def get_boot_from_virtual_cd ():
280+ return execute_redfish_command ("SetOnetimeBootValueVirtualCD" )
281+
282+
60283if __name__ == "__main__" :
61284 app .run ()
0 commit comments