Show More
@@ -1,308 +1,312 b'' | |||
|
1 | 1 | # -*- coding: utf-8 -*- |
|
2 | 2 | |
|
3 | 3 | # Copyright (C) 2010-2017 RhodeCode GmbH |
|
4 | 4 | # |
|
5 | 5 | # This program is free software: you can redistribute it and/or modify |
|
6 | 6 | # it under the terms of the GNU Affero General Public License, version 3 |
|
7 | 7 | # (only), as published by the Free Software Foundation. |
|
8 | 8 | # |
|
9 | 9 | # This program is distributed in the hope that it will be useful, |
|
10 | 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 | 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 | 12 | # GNU General Public License for more details. |
|
13 | 13 | # |
|
14 | 14 | # You should have received a copy of the GNU Affero General Public License |
|
15 | 15 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
16 | 16 | # |
|
17 | 17 | # This program is dual-licensed. If you wish to learn more about the |
|
18 | 18 | # RhodeCode Enterprise Edition, including its added features, Support services, |
|
19 | 19 | # and proprietary license terms, please see https://rhodecode.com/licenses/ |
|
20 | 20 | import io |
|
21 | 21 | import re |
|
22 | 22 | import datetime |
|
23 | 23 | import logging |
|
24 | 24 | import pylons |
|
25 | 25 | import Queue |
|
26 | 26 | import subprocess32 |
|
27 | 27 | import os |
|
28 | 28 | |
|
29 | 29 | from pyramid.i18n import get_localizer |
|
30 | 30 | from pyramid.threadlocal import get_current_request |
|
31 | 31 | from pyramid.interfaces import IRoutesMapper |
|
32 | 32 | from pyramid.settings import asbool |
|
33 | 33 | from pyramid.path import AssetResolver |
|
34 | 34 | from threading import Thread |
|
35 | 35 | |
|
36 | 36 | from rhodecode.translation import _ as tsf |
|
37 | 37 | from rhodecode.config.jsroutes import generate_jsroutes_content |
|
38 | 38 | |
|
39 | 39 | import rhodecode |
|
40 | 40 | |
|
41 | 41 | from pylons.i18n.translation import _get_translator |
|
42 | 42 | from pylons.util import ContextObj |
|
43 | 43 | from routes.util import URLGenerator |
|
44 | 44 | |
|
45 | 45 | from rhodecode.lib.base import attach_context_attributes, get_auth_user |
|
46 | 46 | |
|
47 | 47 | log = logging.getLogger(__name__) |
|
48 | 48 | |
|
49 | 49 | |
|
50 | 50 | def add_renderer_globals(event): |
|
51 | 51 | # Put pylons stuff into the context. This will be removed as soon as |
|
52 | 52 | # migration to pyramid is finished. |
|
53 | 53 | conf = pylons.config._current_obj() |
|
54 | 54 | event['h'] = conf.get('pylons.h') |
|
55 | 55 | event['c'] = pylons.tmpl_context |
|
56 | 56 | event['url'] = pylons.url |
|
57 | 57 | |
|
58 | 58 | # TODO: When executed in pyramid view context the request is not available |
|
59 | 59 | # in the event. Find a better solution to get the request. |
|
60 | 60 | request = event['request'] or get_current_request() |
|
61 | 61 | |
|
62 | 62 | # Add Pyramid translation as '_' to context |
|
63 | 63 | event['_'] = request.translate |
|
64 | 64 | event['_ungettext'] = request.plularize |
|
65 | 65 | |
|
66 | 66 | |
|
67 | 67 | def add_localizer(event): |
|
68 | 68 | request = event.request |
|
69 | 69 | localizer = get_localizer(request) |
|
70 | 70 | |
|
71 | 71 | def auto_translate(*args, **kwargs): |
|
72 | 72 | return localizer.translate(tsf(*args, **kwargs)) |
|
73 | 73 | |
|
74 | 74 | request.localizer = localizer |
|
75 | 75 | request.translate = auto_translate |
|
76 | 76 | request.plularize = localizer.pluralize |
|
77 | 77 | |
|
78 | 78 | |
|
79 | 79 | def set_user_lang(event): |
|
80 | 80 | request = event.request |
|
81 | 81 | cur_user = getattr(request, 'user', None) |
|
82 | 82 | |
|
83 | 83 | if cur_user: |
|
84 | 84 | user_lang = cur_user.get_instance().user_data.get('language') |
|
85 | 85 | if user_lang: |
|
86 | 86 | log.debug('lang: setting current user:%s language to: %s', cur_user, user_lang) |
|
87 | 87 | event.request._LOCALE_ = user_lang |
|
88 | 88 | |
|
89 | 89 | |
|
90 | 90 | def add_pylons_context(event): |
|
91 | 91 | request = event.request |
|
92 | 92 | |
|
93 | 93 | config = rhodecode.CONFIG |
|
94 | 94 | environ = request.environ |
|
95 | 95 | session = request.session |
|
96 | 96 | |
|
97 | 97 | if hasattr(request, 'vcs_call'): |
|
98 | 98 | # skip vcs calls |
|
99 | 99 | return |
|
100 | 100 | |
|
101 | 101 | # Setup pylons globals. |
|
102 | 102 | pylons.config._push_object(config) |
|
103 | 103 | pylons.request._push_object(request) |
|
104 | 104 | pylons.session._push_object(session) |
|
105 | 105 | pylons.translator._push_object(_get_translator(config.get('lang'))) |
|
106 | 106 | |
|
107 | 107 | pylons.url._push_object(URLGenerator(config['routes.map'], environ)) |
|
108 | 108 | session_key = ( |
|
109 | 109 | config['pylons.environ_config'].get('session', 'beaker.session')) |
|
110 | 110 | environ[session_key] = session |
|
111 | 111 | |
|
112 | 112 | if hasattr(request, 'rpc_method'): |
|
113 | 113 | # skip api calls |
|
114 | 114 | return |
|
115 | 115 | |
|
116 | 116 | # Get the rhodecode auth user object and make it available. |
|
117 | 117 | auth_user = get_auth_user(environ) |
|
118 | 118 | request.user = auth_user |
|
119 | 119 | environ['rc_auth_user'] = auth_user |
|
120 | 120 | |
|
121 | 121 | # Setup the pylons context object ('c') |
|
122 | 122 | context = ContextObj() |
|
123 | 123 | context.rhodecode_user = auth_user |
|
124 | 124 | attach_context_attributes(context, request) |
|
125 | 125 | pylons.tmpl_context._push_object(context) |
|
126 | 126 | |
|
127 | 127 | |
|
128 | 128 | def scan_repositories_if_enabled(event): |
|
129 | 129 | """ |
|
130 | 130 | This is subscribed to the `pyramid.events.ApplicationCreated` event. It |
|
131 | 131 | does a repository scan if enabled in the settings. |
|
132 | 132 | """ |
|
133 | 133 | settings = event.app.registry.settings |
|
134 | 134 | vcs_server_enabled = settings['vcs.server.enable'] |
|
135 | 135 | import_on_startup = settings['startup.import_repos'] |
|
136 | 136 | if vcs_server_enabled and import_on_startup: |
|
137 | 137 | from rhodecode.model.scm import ScmModel |
|
138 | 138 | from rhodecode.lib.utils import repo2db_mapper, get_rhodecode_base_path |
|
139 | 139 | repositories = ScmModel().repo_scan(get_rhodecode_base_path()) |
|
140 | 140 | repo2db_mapper(repositories, remove_obsolete=False) |
|
141 | 141 | |
|
142 | 142 | |
|
143 | 143 | def write_metadata_if_needed(event): |
|
144 | 144 | """ |
|
145 | 145 | Writes upgrade metadata |
|
146 | 146 | """ |
|
147 | 147 | import rhodecode |
|
148 | 148 | from rhodecode.lib import system_info |
|
149 | 149 | from rhodecode.lib import ext_json |
|
150 | 150 | |
|
151 | 151 | def write(): |
|
152 | 152 | fname = '.rcmetadata.json' |
|
153 | 153 | ini_loc = os.path.dirname(rhodecode.CONFIG.get('__file__')) |
|
154 | 154 | metadata_destination = os.path.join(ini_loc, fname) |
|
155 | 155 | |
|
156 | 156 | configuration = system_info.SysInfo( |
|
157 | 157 | system_info.rhodecode_config)()['value'] |
|
158 | 158 | license_token = configuration['config']['license_token'] |
|
159 | 159 | dbinfo = system_info.SysInfo(system_info.database_info)()['value'] |
|
160 | 160 | del dbinfo['url'] |
|
161 | 161 | metadata = dict( |
|
162 | 162 | desc='upgrade metadata info', |
|
163 | 163 | license_token=license_token, |
|
164 | 164 | created_on=datetime.datetime.utcnow().isoformat(), |
|
165 | 165 | usage=system_info.SysInfo(system_info.usage_info)()['value'], |
|
166 | 166 | platform=system_info.SysInfo(system_info.platform_type)()['value'], |
|
167 | 167 | database=dbinfo, |
|
168 | 168 | cpu=system_info.SysInfo(system_info.cpu)()['value'], |
|
169 | 169 | memory=system_info.SysInfo(system_info.memory)()['value'], |
|
170 | 170 | ) |
|
171 | 171 | |
|
172 | 172 | with open(metadata_destination, 'wb') as f: |
|
173 | 173 | f.write(ext_json.json.dumps(metadata)) |
|
174 | 174 | |
|
175 | settings = event.app.registry.settings | |
|
176 | if settings.get('metadata.skip'): | |
|
177 | return | |
|
178 | ||
|
175 | 179 | try: |
|
176 | 180 | write() |
|
177 | 181 | except Exception: |
|
178 | 182 | pass |
|
179 | 183 | |
|
180 | 184 | |
|
181 | 185 | def write_js_routes_if_enabled(event): |
|
182 | 186 | registry = event.app.registry |
|
183 | 187 | |
|
184 | 188 | mapper = registry.queryUtility(IRoutesMapper) |
|
185 | 189 | _argument_prog = re.compile('\{(.*?)\}|:\((.*)\)') |
|
186 | 190 | |
|
187 | 191 | def _extract_route_information(route): |
|
188 | 192 | """ |
|
189 | 193 | Convert a route into tuple(name, path, args), eg: |
|
190 | 194 | ('show_user', '/profile/%(username)s', ['username']) |
|
191 | 195 | """ |
|
192 | 196 | |
|
193 | 197 | routepath = route.pattern |
|
194 | 198 | pattern = route.pattern |
|
195 | 199 | |
|
196 | 200 | def replace(matchobj): |
|
197 | 201 | if matchobj.group(1): |
|
198 | 202 | return "%%(%s)s" % matchobj.group(1).split(':')[0] |
|
199 | 203 | else: |
|
200 | 204 | return "%%(%s)s" % matchobj.group(2) |
|
201 | 205 | |
|
202 | 206 | routepath = _argument_prog.sub(replace, routepath) |
|
203 | 207 | |
|
204 | 208 | return ( |
|
205 | 209 | route.name, |
|
206 | 210 | routepath, |
|
207 | 211 | [(arg[0].split(':')[0] if arg[0] != '' else arg[1]) |
|
208 | 212 | for arg in _argument_prog.findall(pattern)] |
|
209 | 213 | ) |
|
210 | 214 | |
|
211 | 215 | def get_routes(): |
|
212 | 216 | # pylons routes |
|
213 | 217 | for route in rhodecode.CONFIG['routes.map'].jsroutes(): |
|
214 | 218 | yield route |
|
215 | 219 | |
|
216 | 220 | # pyramid routes |
|
217 | 221 | for route in mapper.get_routes(): |
|
218 | 222 | if not route.name.startswith('__'): |
|
219 | 223 | yield _extract_route_information(route) |
|
220 | 224 | |
|
221 | 225 | if asbool(registry.settings.get('generate_js_files', 'false')): |
|
222 | 226 | static_path = AssetResolver().resolve('rhodecode:public').abspath() |
|
223 | 227 | jsroutes = get_routes() |
|
224 | 228 | jsroutes_file_content = generate_jsroutes_content(jsroutes) |
|
225 | 229 | jsroutes_file_path = os.path.join( |
|
226 | 230 | static_path, 'js', 'rhodecode', 'routes.js') |
|
227 | 231 | |
|
228 | 232 | with io.open(jsroutes_file_path, 'w', encoding='utf-8') as f: |
|
229 | 233 | f.write(jsroutes_file_content) |
|
230 | 234 | |
|
231 | 235 | |
|
232 | 236 | class Subscriber(object): |
|
233 | 237 | """ |
|
234 | 238 | Base class for subscribers to the pyramid event system. |
|
235 | 239 | """ |
|
236 | 240 | def __call__(self, event): |
|
237 | 241 | self.run(event) |
|
238 | 242 | |
|
239 | 243 | def run(self, event): |
|
240 | 244 | raise NotImplementedError('Subclass has to implement this.') |
|
241 | 245 | |
|
242 | 246 | |
|
243 | 247 | class AsyncSubscriber(Subscriber): |
|
244 | 248 | """ |
|
245 | 249 | Subscriber that handles the execution of events in a separate task to not |
|
246 | 250 | block the execution of the code which triggers the event. It puts the |
|
247 | 251 | received events into a queue from which the worker process takes them in |
|
248 | 252 | order. |
|
249 | 253 | """ |
|
250 | 254 | def __init__(self): |
|
251 | 255 | self._stop = False |
|
252 | 256 | self._eventq = Queue.Queue() |
|
253 | 257 | self._worker = self.create_worker() |
|
254 | 258 | self._worker.start() |
|
255 | 259 | |
|
256 | 260 | def __call__(self, event): |
|
257 | 261 | self._eventq.put(event) |
|
258 | 262 | |
|
259 | 263 | def create_worker(self): |
|
260 | 264 | worker = Thread(target=self.do_work) |
|
261 | 265 | worker.daemon = True |
|
262 | 266 | return worker |
|
263 | 267 | |
|
264 | 268 | def stop_worker(self): |
|
265 | 269 | self._stop = False |
|
266 | 270 | self._eventq.put(None) |
|
267 | 271 | self._worker.join() |
|
268 | 272 | |
|
269 | 273 | def do_work(self): |
|
270 | 274 | while not self._stop: |
|
271 | 275 | event = self._eventq.get() |
|
272 | 276 | if event is not None: |
|
273 | 277 | self.run(event) |
|
274 | 278 | |
|
275 | 279 | |
|
276 | 280 | class AsyncSubprocessSubscriber(AsyncSubscriber): |
|
277 | 281 | """ |
|
278 | 282 | Subscriber that uses the subprocess32 module to execute a command if an |
|
279 | 283 | event is received. Events are handled asynchronously. |
|
280 | 284 | """ |
|
281 | 285 | |
|
282 | 286 | def __init__(self, cmd, timeout=None): |
|
283 | 287 | super(AsyncSubprocessSubscriber, self).__init__() |
|
284 | 288 | self._cmd = cmd |
|
285 | 289 | self._timeout = timeout |
|
286 | 290 | |
|
287 | 291 | def run(self, event): |
|
288 | 292 | cmd = self._cmd |
|
289 | 293 | timeout = self._timeout |
|
290 | 294 | log.debug('Executing command %s.', cmd) |
|
291 | 295 | |
|
292 | 296 | try: |
|
293 | 297 | output = subprocess32.check_output( |
|
294 | 298 | cmd, timeout=timeout, stderr=subprocess32.STDOUT) |
|
295 | 299 | log.debug('Command finished %s', cmd) |
|
296 | 300 | if output: |
|
297 | 301 | log.debug('Command output: %s', output) |
|
298 | 302 | except subprocess32.TimeoutExpired as e: |
|
299 | 303 | log.exception('Timeout while executing command.') |
|
300 | 304 | if e.output: |
|
301 | 305 | log.error('Command output: %s', e.output) |
|
302 | 306 | except subprocess32.CalledProcessError as e: |
|
303 | 307 | log.exception('Error while executing command.') |
|
304 | 308 | if e.output: |
|
305 | 309 | log.error('Command output: %s', e.output) |
|
306 | 310 | except: |
|
307 | 311 | log.exception( |
|
308 | 312 | 'Exception while executing command %s.', cmd) |
General Comments 0
You need to be logged in to leave comments.
Login now