-
Notifications
You must be signed in to change notification settings - Fork 0
/
flask_app.py
146 lines (125 loc) · 4.34 KB
/
flask_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
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
from flask import Flask
from flask import render_template
from flask import request
from flask import session
from flask import redirect
from flask import jsonify
from server_control.objects import password_commands
from server_control.objects.docker_commands import *
from server_control.objects.server.idrac_commands_dell import *
from server_control.objects.mongo_commands import *
from flask import flash
from datetime import timedelta
app = Flask(__name__)
app._static_folder = 'static/'
app.secret_key = flask_secret_key
app.config['PERMANENT_SESSION_LIFETIME'] = timedelta(minutes=5)
service = DockerApi('docker')
server = IdracApi()
mongo = MongoApi('mongo')
@app.route("/")
def homepage():
if not session.get("logged_in?"):
return redirect('/login')
else:
return redirect('/ajax')
@app.route("/login", methods=["get", "post"])
def login():
if request.method == "POST":
un_check = request.form['un']
pw_check = request.form['pw']
if password_commands.check_plex_un_pw(username=un_check, password=pw_check):
session["logged_in?"] = True
flash('Successfully logged in!')
return redirect("/ajax")
else:
flash('Incorrect username, or password')
return render_template('login.html')
return render_template('login.html')
@app.route("/status")
def status():
# TODO fix docker integration
if not session.get("logged_in?"):
return redirect('/login')
else:
server_status = server.retrieve_idrac_data()
if server_status is not "on":
container_status = {
"plex": "exited",
"minecraft": "exited"
}
else:
container_status = service.fake_container_status()
return render_template('status.html', container_status=container_status, server_status=server_status)
@app.route("/service", methods=["post"])
def start_service():
if not session.get("logged_in?"):
return redirect('/login')
else:
server_action_dict = request.form
for server_name in server_action_dict:
action = server_action_dict[server_name]
if action == 'start':
service.start_server(server_name)
elif action == 'stop':
service.stop_server(server_name)
server_status = server.retrieve_idrac_data()
if server_status is not "on":
container_status = {
"plex": "exited",
"minecraft": "exited"
}
else:
container_status = service.fake_container_status()
return render_template('ajax.html', container_status=container_status, server_status=server_status)
@app.route("/server", methods=["post"])
def start_server():
if not session.get("logged_in?"):
return redirect('/login')
else:
server_action = request.form['server']
if server_action == 'start':
server.turn_on()
elif server_action == 'stop':
server.turn_off()
return render_template('ajax.html')
@app.route("/server_status")
def server_status_ajax():
if not session.get("logged_in?"):
return redirect('/login')
else:
server_status = server.retrieve_idrac_data()
if server_status is not "on":
container_status = {
"plex": "exited",
"minecraft": "exited"
}
else:
container_status = service.fake_container_status()
return jsonify(server_status)
@app.route("/test_render")
def test_render():
if not session.get("logged_in?"):
return redirect('/login')
else:
server_status = server.retrieve_idrac_data()
if server_status is not "on":
container_status = {
"plex": "exited",
"minecraft": "exited"
}
else:
container_status = service.fake_container_status()
return render_template('ajax_status.html', container_status=container_status, server_status=server_status)
@app.route("/logout", methods=["post", "get"])
def logout():
session['logged_in?'] = False
return redirect('/')
@app.route("/ajax")
def ajax():
if not session.get("logged_in?"):
return redirect('/login')
else:
return render_template('ajax.html')
if __name__ == '__main__':
app.run()