forked from luci/luci-py
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandlers_frontend.py
287 lines (220 loc) · 8.49 KB
/
handlers_frontend.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
# Copyright 2013 The LUCI Authors. All rights reserved.
# Use of this source code is governed under the Apache License, Version 2.0
# that can be found in the LICENSE file.
"""Main entry point for Swarming service.
This file contains the URL handlers for all the Swarming service URLs,
implemented using the webapp2 framework.
"""
import collections
import os
import webapp2
import handlers_bot
import handlers_endpoints
import mapreduce_jobs
import template
from components import auth
from components import utils
from server import acl
from server import bot_code
from server import config
ROOT_DIR = os.path.dirname(os.path.abspath(__file__))
# Helper class for displaying the sort options in html templates.
SortOptions = collections.namedtuple('SortOptions', ['key', 'name'])
### is_admin pages.
class RestrictedConfigHandler(auth.AuthenticatingHandler):
@auth.autologin
@auth.require(acl.can_view_config)
def get(self):
# Template parameters schema matches settings_info() return value.
self.response.write(template.render(
'swarming/restricted_config.html', config.settings_info()))
class UploadBotConfigHandler(auth.AuthenticatingHandler):
"""Stores a new bot_config.py script."""
@auth.autologin
@auth.require(acl.can_view_config)
def get(self):
bot_config = bot_code.get_bot_config()
params = {
'content': bot_config.content.decode('utf-8'),
'path': self.request.path,
'version': bot_config.version,
'when': bot_config.when,
'who': bot_config.who or 'N/A',
'xsrf_token': self.generate_xsrf_token(),
}
self.response.write(
template.render('swarming/restricted_upload_bot_config.html', params))
@auth.require(acl.can_edit_config)
def post(self):
script = self.request.get('script', '')
if not script:
self.abort(400, 'No script uploaded')
# Make sure the script is valid utf-8. For some odd reason, the script
# instead may or may not be an unicode instance. This depends if it is on
# AppEngine production or not.
if isinstance(script, str):
script = script.decode('utf-8', 'replace')
script = script.encode('utf-8')
bot_code.store_bot_config(self.request.host_url, script)
self.get()
class UploadBootstrapHandler(auth.AuthenticatingHandler):
"""Stores a new bootstrap.py script."""
@auth.autologin
@auth.require(acl.can_view_config)
def get(self):
bootstrap = bot_code.get_bootstrap(self.request.host_url)
params = {
'content': bootstrap.content.decode('utf-8'),
'path': self.request.path,
'version': bootstrap.version,
'when': bootstrap.when,
'who': bootstrap.who or 'N/A',
'xsrf_token': self.generate_xsrf_token(),
}
self.response.write(
template.render('swarming/restricted_upload_bootstrap.html', params))
@auth.require(acl.can_edit_config)
def post(self):
script = self.request.get('script', '')
if not script:
self.abort(400, 'No script uploaded')
# Make sure the script is valid utf-8. For some odd reason, the script
# instead may or may not be an unicode instance. This depends if it is on
# AppEngine production or not.
if isinstance(script, str):
script = script.decode('utf-8', 'replace')
script = script.encode('utf-8')
bot_code.store_bootstrap(script)
self.get()
### Mapreduce related handlers
class RestrictedLaunchMapReduceJob(auth.AuthenticatingHandler):
"""Enqueues a task to start a map reduce job on the backend module.
A tree of map reduce jobs inherits module and version of a handler that
launched it. All UI handlers are executes by 'default' module. So to run a
map reduce on a backend module one needs to pass a request to a task running
on backend module.
"""
@auth.require(acl.can_edit_config)
def post(self):
job_id = self.request.get('job_id')
assert job_id in mapreduce_jobs.MAPREDUCE_JOBS
success = utils.enqueue_task(
url='/internal/taskqueue/mapreduce/launch/%s' % job_id,
queue_name=mapreduce_jobs.MAPREDUCE_TASK_QUEUE,
use_dedicated_module=False)
# New tasks should show up on the status page.
if success:
self.redirect('/restricted/mapreduce/status')
else:
self.abort(500, 'Failed to launch the job')
### Redirectors.
class BotsListHandler(auth.AuthenticatingHandler):
"""Redirects to a list of known bots."""
@auth.public
def get(self):
limit = int(self.request.get('limit', 100))
dimensions = (
l.strip() for l in self.request.get('dimensions', '').splitlines()
)
dimensions = [i for i in dimensions if i]
new_ui_link = '/botlist?l=%d' % limit
if dimensions:
new_ui_link += '&f=' + '&f='.join(dimensions)
self.redirect(new_ui_link)
class BotHandler(auth.AuthenticatingHandler):
"""Redirects to a page about the bot, including last tasks and events."""
@auth.public
def get(self, bot_id):
self.redirect('/bot?id=%s' % bot_id)
class TasksHandler(auth.AuthenticatingHandler):
"""Redirects to a list of all task requests."""
@auth.public
def get(self):
limit = int(self.request.get('limit', 100))
task_tags = [
line for line in self.request.get('task_tag', '').splitlines() if line
]
new_ui_link = '/tasklist?l=%d' % limit
if task_tags:
new_ui_link += '&f=' + '&f='.join(task_tags)
self.redirect(new_ui_link)
class TaskHandler(auth.AuthenticatingHandler):
"""Redirects to a page containing task request and result."""
@auth.public
def get(self, task_id):
self.redirect('/task?id=%s' % task_id)
### Public pages.
class UIHandler(auth.AuthenticatingHandler):
"""Serves the landing page for the new UI of the requested page.
This landing page is stamped with the OAuth 2.0 client id from the
configuration."""
@auth.public
def get(self, page):
if not page:
page = 'swarming'
params = {
'client_id': config.settings().ui_client_id,
}
# Can cache for 1 week, because the only thing that would change in this
# template is the oauth client id, which changes very infrequently.
self.response.cache_control.no_cache = None
self.response.cache_control.public = True
self.response.cache_control.max_age = 604800
try:
self.response.write(template.render(
'swarming/public_%s_index.html' % page, params))
except template.TemplateNotFound:
self.abort(404, 'Page not found.')
def get_content_security_policy(self):
# We use iframes to display pages at display_server_url_template. Need to
# allow it in CSP.
csp = super(UIHandler, self).get_content_security_policy()
tmpl = config.settings().display_server_url_template
if tmpl:
if tmpl.startswith('/'):
csp['child-src'].append("'self'")
else:
# We assume the template specifies '%s' in its last path component.
# We strip it to get a "parent" path that we can put into CSP. Note that
# whitelisting an entire display server domain is unnecessary wide.
assert tmpl.startswith('https://'), tmpl
csp['child-src'].append(tmpl[:tmpl.rfind('/')+1])
return csp
class WarmupHandler(webapp2.RequestHandler):
def get(self):
auth.warmup()
bot_code.get_swarming_bot_zip(self.request.host_url)
utils.get_module_version_list(None, None)
self.response.headers['Content-Type'] = 'text/plain; charset=utf-8'
self.response.write('ok')
class EmailHandler(webapp2.RequestHandler):
"""Blackhole any email sent."""
def post(self, to):
pass
def get_routes():
routes = [
# Frontend pages. They return HTML.
# Public pages.
('/<page:(bot|botlist|task|tasklist|)>', UIHandler),
# Redirects to Polymer UI
('/user/tasks', TasksHandler),
('/user/task/<task_id:[0-9a-fA-F]+>', TaskHandler),
('/restricted/bots', BotsListHandler),
('/restricted/bot/<bot_id:[^/]+>', BotHandler),
# Admin pages.
# TODO(maruel): Get rid of them.
('/restricted/config', RestrictedConfigHandler),
('/restricted/upload/bot_config', UploadBotConfigHandler),
('/restricted/upload/bootstrap', UploadBootstrapHandler),
# Mapreduce related urls.
(r'/restricted/launch_mapreduce', RestrictedLaunchMapReduceJob),
('/_ah/mail/<to:.+>', EmailHandler),
('/_ah/warmup', WarmupHandler),
]
return [webapp2.Route(*i) for i in routes]
def create_application(debug):
routes = []
routes.extend(get_routes())
routes.extend(handlers_bot.get_routes())
routes.extend(handlers_endpoints.get_routes())
return webapp2.WSGIApplication(routes, debug=debug)