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