-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.py
52 lines (38 loc) · 1.54 KB
/
app.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
import os
import subprocess
from flask import Flask, jsonify, render_template
app = Flask(__name__)
ip_address = os.getenv("IP_ADDRESS", "192.168.0.120")
username = os.getenv("IDRAC_USERNAME", "root")
password = os.getenv("IDRAC_PASSWORD", "calvin")
IDRAC_SCRIPTS_BASE_PATH = os.getenv("IDRAC_SCRIPTS_BASE_PATH", "./iDRAC-Redfish-Scripting/Redfish\ Python/")
@app.route("/")
def index():
return render_template("index.html")
def execute_redfish_command(action):
if action == 'GetPowerState':
command = f"python {IDRAC_SCRIPTS_BASE_PATH}GetSetPowerStateREDFISH.py -ip {ip_address} -u {username} -p {password} --get"
result = subprocess.run(command, capture_output=True, shell=True)
else:
command = f"python {IDRAC_SCRIPTS_BASE_PATH}GetSetPowerStateREDFISH.py -ip {ip_address} -u {username} -p {password} --set {action}"
result = subprocess.run(command, capture_output=True, shell=True)
return (
jsonify(
{
"output": result.stdout.decode("utf-8"),
"error": result.stderr.decode("utf-8"),
}
),
result.returncode,
)
@app.route("/api/v1/On", methods=["POST"])
def set_power_on():
return execute_redfish_command("On")
@app.route("/api/v1/GracefulShutdown", methods=["POST"])
def set_power_graceful_shutdown():
return execute_redfish_command("GracefulShutdown")
@app.route("/api/v1/GetPowerState", methods=["POST"])
def get_power_state():
return execute_redfish_command("GetPowerState")
if __name__ == "__main__":
app.run()