OLD | NEW |
---|---|
(Empty) | |
1 # Copyright 2015 The Chromium Authors. All rights reserved. | |
2 # Use of this source code is governed by a BSD-style license that can be | |
3 # found in the LICENSE file. | |
4 | |
5 import logging | |
6 import random | |
7 import os | |
8 import sys | |
9 import time | |
10 import urllib2 | |
11 import webapp2 | |
12 | |
13 from protorpc import messages | |
14 from protorpc import message_types | |
15 from protorpc import remote | |
16 | |
17 from google.appengine.api import app_identity, taskqueue | |
18 | |
19 import common | |
20 from components import auth | |
21 | |
22 VM_MODULES = ['vm1', 'vm2', 'vm3'] | |
23 | |
24 | |
25 def require_group_membership(group_name): | |
26 """Authenticating decorator for handler methods. | |
27 | |
28 Requires the user to be part of group in production, and | |
29 skips authorization for dev appserver. | |
30 """ | |
31 if common.is_development_server(): | |
32 auth_decorator = auth.public | |
33 else: | |
34 auth_decorator = auth.require( # pragma: no branch | |
35 lambda: auth.is_group_member(group_name)) | |
36 | |
37 def decorator(fn): | |
38 return auth_decorator(fn) | |
39 return decorator | |
40 | |
41 | |
42 class NoBackendException(Exception): | |
43 pass | |
44 | |
45 | |
46 class LoadBalancer(object): | |
47 """Balance the load among VM modules. | |
48 | |
49 TODO(sergeyberezin): take into account health checks on the | |
50 corresponding NAT boxes. Specifically, fetch the health status | |
51 from the datastore in __init__(), and update it periodically as needed. | |
52 """ | |
53 def __init__(self): | |
54 pass | |
55 | |
56 def choose_module(self): | |
57 """Select a module to send the data to.""" | |
58 # TODO(sergeyberezin) Implement load percentages for modules, for | |
59 # draining / canary / live rolling updates. | |
60 # TODO(sergeyberezin): perform health checks for the corresponding | |
61 # NAT boxes and drain modules appropriately. | |
62 return random.choice(VM_MODULES) | |
63 | |
64 | |
65 def forward_data(data): | |
agable
2015/04/27 20:51:07
Not in this CL, but immediately after: I'd like th
Sergey Berezin
2015/04/27 21:45:51
Good idea, I'll add it to the list. I was thinking
| |
66 """Forwards the raw data to the backend.""" | |
67 # Task queue should work correctly both in dev and prod server. | |
68 lb = LoadBalancer() | |
69 module_name = lb.choose_module() | |
70 logging.info('Forwarding request to module: %s', module_name) | |
71 hostname = app_identity.get_default_version_hostname() | |
72 if common.is_development_server(): | |
73 protocol = 'http' | |
74 else: | |
75 protocol = 'https' | |
76 url = '%s://%s/%s' % (protocol, hostname, module_name) | |
77 request = urllib2.Request(url, data) | |
78 urllib2.urlopen(request) | |
79 | |
80 | |
81 class MonacqHandler(auth.AuthenticatingHandler): | |
82 # Disable XSRF in local dev appserver; otherwise requests will fail. | |
83 if common.is_development_server(): | |
84 xsrf_token_enforce_on = [] # pragma: no cover | |
85 | |
86 @require_group_membership('service-account-monitoring-proxy') | |
87 def post(self): | |
88 forward_data(self.request.body) | |
89 | |
90 | |
91 class MainHandler(common.BaseHandler): | |
92 def get(self): | |
93 self.render_response('main.html', title='Chrome Infra Monitoring Proxy') | |
94 | |
95 | |
96 logging.basicConfig(level=logging.DEBUG) | |
97 | |
98 main_handlers = [ | |
99 (r'/', MainHandler), | |
100 (r'/monacq', MonacqHandler), | |
101 ] | |
102 | |
103 app = webapp2.WSGIApplication(main_handlers, debug=True) | |
OLD | NEW |