-
Notifications
You must be signed in to change notification settings - Fork 0
/
ui
163 lines (123 loc) · 4.05 KB
/
ui
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
#! /usr/bin/python3
"""
Phillip Benoit
CSC346
Project 7 & 8 (FlyRC)
UI for server data via MySQL
"""
import os
import MySQLdb
import boto3
from lib import passwd, http_response, strings
connection = MySQLdb.connect(host=passwd.SQL_HOST,
user=passwd.SQL_USER,
passwd=passwd.SQL_PASS,
db=passwd.SQL_DBID)
ec2_r = boto3.resource("ec2", "us-east-1")
SELECT_SERVERS = "SELECT id, description, ec2id, ready, user1pw, user2pw FROM servers WHERE owner = %s;"
HTML_HEAD = '''
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01
Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head><title>XXX</title></head>
<body>
'''
HTML_LOGIN_BODY = '''
<h1>FlyRC Login</h1><br>
<form action=/cgi-bin/login method=post>
Username: <input type=text name=user>
<input type=submit>
</form>
'''
HTML_FOOT = '''
</body></html>
'''
HTML_TABLE_FOOT = '''
</table><br>
<input id="destroy" type=submit value="Destroy Server" disabled><br>
</form>
'''
HTML_SERVERS_FOOT = '''
<br><form action=/cgi-bin/create_server method=post>
Description: <input type=text name=desc>
<input type=submit value="Create Server">
</form>
<script>
function enable_destroy() {
document.getElementById("destroy").disabled = false;
}
</script>
''' + HTML_FOOT
# send the login page to the user
def return_login():
page = HTML_HEAD.replace("XXX", "FlyRC Login")
page += HTML_LOGIN_BODY
page += HTML_FOOT
http_response.respond(http_response.OK_IDX, connection, page)
# send the user interface
def return_servers(user):
# SQL query for user's existing servers
cursor = connection.cursor(cursorclass=MySQLdb.cursors.DictCursor)
cursor.execute(SELECT_SERVERS, (user,))
json_results = cursor.fetchall()
cursor.close()
# append results
for record in json_results:
# save fields
ec2id = record[strings.EC2ID_FKEY]
ready = record[strings.READY_FKEY]
# remove fields
del record[strings.EC2ID_FKEY]
del record[strings.READY_FKEY]
# get IP if ready
if ready == 1:
address = ec2_r.Instance(ec2id).public_ip_address
else:
address = ''
record[strings.ADDR_FKEY] = address
# HTML page
page = HTML_HEAD.replace("XXX", user + "'s FlyRC Servers")
page += "<h1>FlyRC Servers</h1><br>\n"
page += "logged in as " + user + "<br>\n"
# servers table
if json_results:
page += "<form action=/cgi-bin/destroy method=post>\n"
page += "<table border=1>\n"
# header row
page += "<tr>\n"
page += "<th>Select</th>"
for field in json_results[0]:
page += "<th>"+field+"</th>"
page += "</tr>\n"
# data from query
for result in json_results:
page += "<tr>\n"
# radio button to select for termination
page += '<td><input onclick="enable_destroy()" type="radio" ' \
'name="id" value="'+str(result[strings.ID_FKEY])+'"></td>'
for field in result:
# href for new server that will pop open a new tab
if field == strings.ADDR_FKEY:
page += '<td><a href="http://' + str(result[field]) + \
'" target="_blank" rel="noopener noreferrer">' + str(result[field]) + '</a></td>'
# all other fields
else:
page += '<td>'+str(result[field])+'</td>'
# end of table row
page += "\n</tr>\n"
# table foot and destroy button
page += HTML_TABLE_FOOT
# server create input, enable destroy button javascript, and generic footer
page += HTML_SERVERS_FOOT
# send page
http_response.respond(http_response.OK_IDX, connection, page)
def main():
# get user from cookie data
user = strings.parse_user(os.environ, connection)
# if user could not be found, redirect to login
if user == "":
return_login()
# if a valid user is logged in, display their servers
return_servers(user)
main()