-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandlers.py
354 lines (288 loc) · 12.4 KB
/
handlers.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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
import logging
import os
import jinja2
import uuid
import datetime
import json
from oauth2client.appengine import OAuth2Decorator
from oauth2client.client import AccessTokenRefreshError
from google.appengine.api import users
from google.appengine.ext import webapp
from compute_engine_controller import ComputeEngineController
from load_info import LoadInfo
from google.appengine.ext import db
from google.appengine.api import app_identity
jinja_environment = jinja2.Environment(
loader=jinja2.FileSystemLoader('worker'))
jinja_environment_html = jinja2.Environment(
loader=jinja2.FileSystemLoader('static_files'))
decorator = OAuth2Decorator(
client_id = '996450208987-9259qcm3a6qh8ch15nsf6tpb0jrk2knu.apps.googleusercontent.com',
client_secret = 'DPcR1rc9oZwBBuf6fzgj231a',
scope=ComputeEngineController.SCOPE)
class IpAddressRequestLog(db.Model):
"""Datastore schema for game server IP address retrieval log."""
client_ip = db.StringProperty()
server_ip = db.StringProperty()
timestamp = db.DateTimeProperty(auto_now=True)
class LoginToken(db.Model):
# Use token as key_name
expiration = db.DateTimeProperty()
user_id = db.StringProperty()
class Entity(db.Model):
user_id = db.StringProperty()
gridkey = db.StringProperty()
__type = db.StringProperty()
class GamePageHandler(webapp.RequestHandler):
def get(self):
user = users.get_current_user()
if user:
template = jinja_environment_html.get_template('game.html')
resp = template.render({"nickname": user.nickname(), "logouturl":users.create_logout_url('/')})
else:
resp = ('<html><body><a href="%s">Sign in or register</a>.</body></html>' % users.create_login_url('/'))
self.response.out.write(resp)
class FrontendHandler(webapp.RequestHandler):
"""URL handler class for IP address request."""
def get(self):
user = users.get_current_user()
# TODO: Check to make sure game account is up to date/paid for/whatever.
# Determine what server to transfer player to
servertoload = ""
q = Entity.all()
q.filter("user_id =", user.user_id())
q.filter("__type =", "Player")
userplayers = []
for userplayer in q.run(limit=5):
userplayers.append(userplayer)
if len(userplayers) > 1:
logging.error("UserID has more than one player")
return
elif len(userplayers) == 0:
logging.info("No current players, attaching to root")
servertoload = "0,0"
else:
servertoload = userplayers[0].gridkey
logging.info("Loading server "+servertoload+" for "+user.nickname())
info = LoadInfo.GetServerLoadInfo(servertoload)
if info:
if info[LoadInfo.STATUS] == LoadInfo.STATUS_UP:
ip = info[LoadInfo.IP_ADDRESS]
port = info[LoadInfo.PORT]
# Generate login token
token_key = uuid.uuid4().hex;
expiration_time = datetime.datetime.now()+datetime.timedelta(seconds = 20);
LoginToken(key_name=token_key, expiration=expiration_time, user_id=user.user_id()).put();
# Send IP and token info
self.response.out.write(json.dumps({'status':'up', 'ipaddress': ip, 'port': port, 'token': token_key}))
IpAddressRequestLog(client_ip=self.request.remote_addr,
server_ip=ip).put()
else:
self.response.out.write(json.dumps({'status':'loading'}))
else:
ComputeEngineController().AddServer(servertoload)
self.response.out.write(json.dumps({'status':'loading'}))
class AdminUiHandler(webapp.RequestHandler):
"""URL handler class for admin UI page."""
@decorator.oauth_required
def get(self):
"""Returns admin UI of game server cluster."""
try:
ComputeEngineController(decorator.credentials)
# This handler returns stats.html as is. We still need handler here
# to take care of OAuth2.
html_path = os.path.join(os.path.dirname(__file__),
'static_files', 'stats.html')
self.response.out.write(open(html_path).read())
except AccessTokenRefreshError:
self.redirect(decorator.authorize_url())
'''
class StatsJsonHandler(webapp.RequestHandler):
"""URL handler class for stats list of the cluster."""
@decorator.oauth_required
def get(self):
"""Returns stats of managed Compute Engine instances for Admin UI."""
load_entries = []
instance_list = ComputeEngineController(
decorator.credentials).ListInstances()
all_load_info = LoadInfo.GetAll()
# First, list managed instances whose Compute Engine status is found.
for instance in instance_list:
instance_name = instance['name']
if instance_name in all_load_info:
info = all_load_info[instance_name]
load_entries.append({
'host': instance_name,
'ipaddress': info.get(LoadInfo.IP_ADDRESS, ''),
'status': instance['status'],
'load': info.get(LoadInfo.LOAD, 0),
'force_set': info.get(LoadInfo.FORCE, False),
})
del all_load_info[instance_name]
# Then, list managed instances without Compute Engine status.
for name, info in all_load_info.items():
load_entries.append({
'host': name,
'ipaddress': info.get(LoadInfo.IP_ADDRESS, ''),
'status': 'NOT FOUND',
'load': info.get(LoadInfo.LOAD, 0),
'force_set': info.get(LoadInfo.FORCE, False),
})
self.response.out.write(json.dumps(load_entries))
class StatsUserJsonHandler(webapp.RequestHandler):
"""URL handler class for game server list of the cluster."""
def get(self):
"""Returns stats of game instances for non logged-in users."""
load_entries = []
all_load_info = LoadInfo.GetAll()
for name, info in all_load_info.items():
load_entries.append({
'host': name,
'ipaddress': info.get(LoadInfo.IP_ADDRESS, ''),
'load': info.get(LoadInfo.LOAD, 0),
})
self.response.out.write(json.dumps(load_entries))
'''
class StartUpHandler(webapp.RequestHandler):
"""URL handler class for cluster start up."""
@decorator.oauth_required
def get(self):
"""Starts up initial Compute Engine cluster."""
instances = LoadInfo.GetAllInstances()
if instances:
raise SystemError("Instances already loaded, teardown first.");
ComputeEngineController(decorator.credentials).StartUpCluster()
class TearDownHandler(webapp.RequestHandler):
"""URL handler class for cluster shut down."""
@decorator.oauth_required
def get(self):
"""Deletes Compute Engine cluster."""
ComputeEngineController(decorator.credentials).TearDownCluster()
LoadInfo.RemoveAllInstancesAndServers()
class RegisterInstanceHandler(webapp.RequestHandler):
"""URL handler class for IP address registration of the instance."""
def post(self):
"""Adds the new instance to managed cluster by registering IP address."""
# TODO(user): Secure this URL by using Cloud Endpoints.
name = self.request.get('name')
instance = ComputeEngineController().GetInstanceInfo(name)
if not instance:
return
logging.info('Instance created: %s', str(instance))
external_ip = instance['networkInterfaces'][0][
'accessConfigs'][0]['natIP']
LoadInfo.RegisterInstanceIpAddress(name, external_ip)
"""Returns script to set up overarching server manager."""
template = jinja_environment.get_template('setup-and-start-game.sh')
self.response.out.write(template.render({
'apphostname': app_identity.get_default_version_hostname(),
'ip_address': self.request.remote_addr,
'name': name
}))
class RegisterServerHandler(webapp.RequestHandler):
def post(self):
""" Adds the new server to server list by registering IP/port """
# TODO(user): Secure this URL by using Cloud Endpoints
name = self.request.get('instancename')
grid = self.request.get('grid')
port = self.request.get('port')
instance = ComputeEngineController().GetInstanceInfo(name)
if not instance:
logging.error("Instance name doesn't match existing instance: %s", name)
return
logging.info('Instance created: %s', str(instance))
external_ip = instance['networkInterfaces'][0][
'accessConfigs'][0]['natIP']
LoadInfo.RegisterServerAddress(grid, external_ip, port)
resp = {'external_ip':external_ip, 'success':1}
self.response.out.write(json.dumps(resp))
class ShutdownHandler(webapp.RequestHandler):
"""URL handler class for deleting the instance."""
def post(self):
"""Delete instance to managed cluster by registering IP address."""
# TODO(user): Secure this URL by using Cloud Endpoints.
name = self.request.get('name')
ComputeEngineController().DeleteInstance(name)
self.response.out.write(json.dumps({'success':1}));
class ShutdownServerHandler(webapp.RequestHandler):
def post(self):
# TODO(user): Secure this URL by using Cloud Endpoints.
grid = self.request.get('grid')
ComputeEngineController().RemoveServer(grid)
self.response.out.write(json.dumps({'success':1}))
class RequireServerHandler(webapp.RequestHandler):
""" Respond with information to connect to requested server or notify of loading status.
Bring server up if required. """
def post(self):
# TODO(user): Secure this URL by using Cloud Endpoints.
grid = self.request.get('grid')
logging.info('Received server require for grid:' + grid)
loadresp = LoadInfo.GetServerLoadInfo(grid)
if not loadresp:
logging.info('No server so starting server for grid:'+str(grid))
ComputeEngineController().AddServer(grid)
loadresp = {LoadInfo.STATUS:LoadInfo.STATUS_LOADING}
self.response.out.write(json.dumps(loadresp))
class InstanceUpdateHandler(webapp.RequestHandler):
""" Accept updates from instance server manager """
def post(self):
# TODO: Secure this URL by using Cloud Endpoints.
name = self.request.get('name')
# TODO: Figure out credentials issues with below, likely due to ComputeEngineController credentials expiring
#instance = ComputeEngineController().GetInstanceInfo(name)
#if not instance:
# return
load = int(self.request.get('load'))
logging.info('Instance update received: ' + str(name) + ' with load: ' + str(load))
loadresp = LoadInfo.UpdateInstanceLoadInfo(name, load)
self.response.out.write(json.dumps({"loadresp":loadresp}))
class ServerUpdateHandler(webapp.RequestHandler):
""" Accept updates from servers """
def post(self):
# TODO: Secure this URL by using Cloud Endpoints.
grid = self.request.get('grid')
num = int(self.request.get('numPlayers'))
logging.info('Server update received: ' + str(grid) + ' with numPlayers: ' + str(num))
loadresp = LoadInfo.UpdateServerNumPlayers(grid, num)
self.response.out.write(json.dumps({"loadresp":loadresp}))
class HeartbeatHandler(webapp.RequestHandler):
"""URL handler class to perform cron task."""
def get(self):
# TODO(user): Secure this URL by using Cloud Endpoints.
# TODO: Rebalance load
# TODO: Make sure instances haven't crashed/stalled starting up: ComputeEngineController().checkResponse
# TODO: Clear old LoginTokens (1 day?)
logging.info("heartbeat")
'''
Possibly useful code:
if loadresp[LoadInfo.STATUS] == LoadInfo.STATUS_LOADING:
newresp = ComputeEngineController().checkResponse(loadresp[LoadInfo.LAST_RESP])
logging.info('Loading latest resp:'+str(newresp))
if newresp.has_key("error"):
logging.warn('Error loading server:'+str(newresp['error']))
logging.warn('Removing from instances:'+str(loadresp['name']))
LoadInfo.RemoveInstance(loadresp['name'])
else:
logging.info('No errors, still loading...')
'''
pass
app = webapp.WSGIApplication(
[
('/game', GamePageHandler),
('/getip.json', FrontendHandler),
('/stats', AdminUiHandler),
#('/stats.json', StatsJsonHandler),
#('/stats-user.json', StatsUserJsonHandler),
('/startup', StartUpHandler),
('/teardown', TearDownHandler),
('/register', RegisterInstanceHandler),
('/register-server', RegisterServerHandler),
('/shutdown', ShutdownHandler),
('/shutdown-server', ShutdownServerHandler),
('/require-server', RequireServerHandler),
('/update', InstanceUpdateHandler),
('/update-server', ServerUpdateHandler),
('/heartbeat', HeartbeatHandler),
(decorator.callback_path, decorator.callback_handler()),
],
debug=True)