-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.py
281 lines (179 loc) · 7.86 KB
/
main.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
# NotebookCloud: Main App Engine Server
# Uses the Python2.7 runtime and depends on /funcs.py
# Author: Carl Smith, Piousoft
# MailTo: [email protected]
import os, random, hashlib
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.ext.webapp import template
from google.appengine.ext import db
from google.appengine.api import users
from funcs import *
class BaseHandler(webapp.RequestHandler):
user = users.get_current_user()
def check_user(self):
if not self.user: return None
try: return Account.gql('WHERE user = :1', self.user)[0]
except IndexError: return None
class MainScreen(BaseHandler):
def get(self):
acc = self.check_user()
if acc:
if not acc.valid:
html = template_dir + 'error.html'
args = {'error': 'Your account details are invalid.'}
self.response.out.write(template.render(html, args))
else:
html = template_dir + 'mainscreen.html'
args = {'username': self.user.email()}
self.response.out.write(template.render(html, args))
else: self.redirect('/login')
class InstanceInfo(BaseHandler):
'''Returns the instance info, as HTML, that the client displays as
the instance list.'''
def get(self):
acc = self.check_user()
if acc:
time.sleep(3) # Takes some time to sync
refresh, html = get_instance_list(
acc.access_key, acc.secret_key
)
html += '1' if refresh else '0'
self.response.out.write(html)
else: self.redirect('/login')
class ServeDocs(BaseHandler):
def get(self):
html = template_dir + 'docs.html'
self.response.out.write(template.render(html, {}))
class ServeForm(BaseHandler):
def get(self):
user = users.get_current_user()
if user:
html = template_dir + 'mainform.html'
args = {'username': self.user.email()}
self.response.out.write(template.render(html, args))
else: self.redirect(users.create_login_url('/login'))
class LaunchVM(BaseHandler):
def get(self):
acc = self.check_user()
if acc:
iclass = [
't1.micro', 'm1.small', 'm1.medium', 'm1.large', 'm1.xlarge',
'm2.xlarge', 'm2.2xlarge', 'm2.4xlarge', 'c1.medium',
'c1.xlarge', 'cg1.4xlarge', 'cc1.4xlarge', 'cc2.8xlarge'
][int(self.request.get('iclass'))]
reservation = str(create_vm(
acc.access_key, acc.secret_key, acc.user_data, iclass
)[1]).split(':')[1]
acc.reservations.append(reservation)
acc.put()
self.redirect('/login')
class ControlVM(BaseHandler):
def get(self):
acc = self.check_user()
if acc:
instance_list = [self.request.get('instance')]
action = self.request.get('action')
control_vm(action, instance_list, acc.access_key, acc.secret_key)
self.redirect('/')
else: self.redirect('/login')
class UpdateUserDetails(BaseHandler):
def post(self):
if not self.user: self.redirect('/login')
else:
password_0 = self.request.get('pwd0')
password_1 = self.request.get('pwd1')
access_key = self.request.get('key0')
secret_key = self.request.get('key1')
rejection = (
'<br><br> Your account has '
'<span class=bolder>not</span> been updated.'
)
if password_0 != password_1:
path = os.path.join(
os.path.dirname(__file__), 'templates/error.html'
)
args = {'error': 'Passwords must match.'+rejection}
self.response.out.write(template.render(path, args))
elif not valid_keys(access_key, secret_key):
path = os.path.join(
os.path.dirname(__file__), 'templates/error.html'
)
args = {'error': 'Invalid AWS keys.'+rejection}
self.response.out.write(template.render(path, args))
else:
user_data = random.choice(('UK', 'US'))
for x in range(4):
user_data += '|'
for y in range(8):
user_data += random.choice(
'abcdefghijklmnopqrstuvwxyz'
)
user_data += '|' + hash_password(password_0)
try:
acc = Account.gql('WHERE user = :1', user)[0]
except:
acc = Account()
acc.user = self.user
acc.reservations = []
acc.user_data = user_data
acc.access_key = access_key
acc.secret_key = secret_key
acc.valid = True
acc.put()
self.redirect('/')
class DeleteUserDetails(BaseHandler):
def get(self):
acc = self.check_user()
if acc: acc.delete()
self.redirect('/login')
class Login(BaseHandler):
def get(self):
acc = self.check_user()
if acc:
self.redirect('/')
else:
if self.user:
lastline = (
'You\'re logged in with your Google Account.'
'<br><br><span class=pwd_good>{}</span><br><br>'
'You will need to create a NotebookCloud account'
' to continue.<br><br><br>'
).format(self.user.email())
button = 'Create NotebookCloud Account'
goto = '/mainform'
else: # if the user isn't logged into to google
lastline = (
'You need to sign in with a Google Account. '
'Read the docs to learn more.<br><br><br>'
)
button = 'Log In With Google Account'
goto = '/google_login'
html = template_dir + 'homepage.html'
args = {'goto': goto, 'lastline': lastline, 'button': button}
self.response.out.write(template.render(html, args))
class GoogleLogin(BaseHandler):
'''Just redirects users to Google's login thing.'''
def get(self):
self.redirect(users.create_login_url('/login'))
class Account(db.Model):
user = db.UserProperty()
user_data = db.StringProperty(multiline=False)
access_key = db.StringProperty(multiline=False)
secret_key = db.StringProperty(multiline=False)
reservations = db.ListProperty(str)
valid = db.BooleanProperty()
# Map and Serve
routes = [
('/login', Login),
('/instance_info', InstanceInfo),
('/google_login', GoogleLogin),
('/control/.*', ControlVM),
('/mainform', ServeForm),
('/docs', ServeDocs),
('/formsubmit', UpdateUserDetails),
('/delete', DeleteUserDetails),
('/launch/.*', LaunchVM),
('/.*', MainScreen)
]
run_wsgi_app(webapp.WSGIApplication(routes, debug=True))