-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathweb.py
107 lines (90 loc) · 3.26 KB
/
web.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
#!/usr/bin/python
# Frodo - A web app for monitoring SGE cluster status: https://bitbucket.org/yoavram/frodo
# Copyright (c) 2012 by Yoav Ram.
# This work is licensed under the Creative Commons Attribution-ShareAlike 3.0 Unported License.
# To view a copy of this license, visit http://creativecommons.org/licenses/by-sa/3.0/.from flask import Flask, render_template, request, session, redirect, url_for, jsonify
from flask import Flask, render_template, request, session, redirect, url_for, jsonify
import time
import common
import qstat
JOB_ID_KEY = "jobID"
cfg = common.configuration()
app = Flask(__name__)
app.debug = cfg.getboolean("web", "development")
app.secret_key = cfg.get("web", "secret")
@app.route("/")
def index():
return redirect(url_for("qstat_html"))
@app.route("/qstat")
@app.route("/qstat/jobID/<int:jobID>")
@app.route("/qstat/username/<qusername>")
def qstat_html(jobID=None, qusername=None):
if "username" not in session:
return redirect(url_for("login"))
username = session["username"]
password = session["password"]
now = time.asctime()
result = qstat.exec_qstat(username, password, qstat_username=qusername)
if result.startswith("Error"):
return render_template("error.html", msg=result)
result = qstat.parse_qstat1(result)
fields = result["fields"]
records = result["records"]
summary = qstat.summarize1(fields, records)
if jobID:
job_details = qstat.parse_qstat_jobID(
qstat.exec_qstat(username, password, jobID=jobID)
)
else:
job_details = None
return render_template(
"qstat.html",
username=username,
time=now,
summary=summary,
fields=fields,
records=records,
job=job_details,
)
@app.route("/qstat/json")
def qstat_json():
if "username" not in session:
return {"error": "please login", "url": url_for("login")}
username = session["username"]
password = session["password"]
now = time.asctime()
qstat_result = qstat.parse_qstat1(qstat.exec_qstat(username, password))
qstat_result.update(
qstat.summarize1(qstat_result["fields"], qstat_result["records"])
)
qstat_result["time"] = now
return jsonify(**qstat_result)
@app.route("/qstat/jobID/<int:jobID>/json")
def qstat_job_json(jobID):
if "username" not in session:
return {"error": "please login", "url": url_for("login")}
username = session["username"]
password = session["password"]
now = time.asctime()
qstat_result = qstat.parse_qstat_jobID(
qstat.exec_qstat(username, password, jobID=jobID)
)
qstat_result["time"] = now
return jsonify(**qstat_result)
@app.route("/login", methods=["POST", "GET"])
def login():
if request.method == "POST":
session["username"] = request.form["username"]
session["password"] = request.form["password"]
return redirect(url_for("index"))
return render_template("login.html")
@app.route("/logout")
def logout():
# remove the username from the session if it's there
session.pop("username", None)
session.pop("password", None)
return redirect(url_for("login"))
if __name__ == "__main__":
host = cfg.get("web", "host")
port = cfg.getint("web", "port")
app.run(host=host, port=port)