##// END OF EJS Templates
metadata: write license token for license migration for installer.
marcink -
r1524:4f05cb3a default
parent child Browse files
Show More
@@ -1,248 +1,252 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
21 21 import datetime
22 22 import logging
23 23 import pylons
24 24 import Queue
25 25 import subprocess32
26 26 import os
27 27
28 28 from pyramid.i18n import get_localizer
29 29 from pyramid.threadlocal import get_current_request
30 30 from threading import Thread
31 31
32 32 from rhodecode.translation import _ as tsf
33 33
34 34 import rhodecode
35 35
36 36 from pylons.i18n.translation import _get_translator
37 37 from pylons.util import ContextObj
38 38 from routes.util import URLGenerator
39 39
40 40 from rhodecode.lib.base import attach_context_attributes, get_auth_user
41 41
42 42 log = logging.getLogger(__name__)
43 43
44 44
45 45 def add_renderer_globals(event):
46 46 # Put pylons stuff into the context. This will be removed as soon as
47 47 # migration to pyramid is finished.
48 48 conf = pylons.config._current_obj()
49 49 event['h'] = conf.get('pylons.h')
50 50 event['c'] = pylons.tmpl_context
51 51 event['url'] = pylons.url
52 52
53 53 # TODO: When executed in pyramid view context the request is not available
54 54 # in the event. Find a better solution to get the request.
55 55 request = event['request'] or get_current_request()
56 56
57 57 # Add Pyramid translation as '_' to context
58 58 event['_'] = request.translate
59 59 event['_ungettext'] = request.plularize
60 60
61 61
62 62 def add_localizer(event):
63 63 request = event.request
64 64 localizer = get_localizer(request)
65 65
66 66 def auto_translate(*args, **kwargs):
67 67 return localizer.translate(tsf(*args, **kwargs))
68 68
69 69 request.localizer = localizer
70 70 request.translate = auto_translate
71 71 request.plularize = localizer.pluralize
72 72
73 73
74 74 def set_user_lang(event):
75 75 request = event.request
76 76 cur_user = getattr(request, 'user', None)
77 77
78 78 if cur_user:
79 79 user_lang = cur_user.get_instance().user_data.get('language')
80 80 if user_lang:
81 81 log.debug('lang: setting current user:%s language to: %s', cur_user, user_lang)
82 82 event.request._LOCALE_ = user_lang
83 83
84 84
85 85 def add_pylons_context(event):
86 86 request = event.request
87 87
88 88 config = rhodecode.CONFIG
89 89 environ = request.environ
90 90 session = request.session
91 91
92 92 if hasattr(request, 'vcs_call'):
93 93 # skip vcs calls
94 94 return
95 95
96 96 # Setup pylons globals.
97 97 pylons.config._push_object(config)
98 98 pylons.request._push_object(request)
99 99 pylons.session._push_object(session)
100 100 pylons.translator._push_object(_get_translator(config.get('lang')))
101 101
102 102 pylons.url._push_object(URLGenerator(config['routes.map'], environ))
103 103 session_key = (
104 104 config['pylons.environ_config'].get('session', 'beaker.session'))
105 105 environ[session_key] = session
106 106
107 107 if hasattr(request, 'rpc_method'):
108 108 # skip api calls
109 109 return
110 110
111 111 # Get the rhodecode auth user object and make it available.
112 112 auth_user = get_auth_user(environ)
113 113 request.user = auth_user
114 114 environ['rc_auth_user'] = auth_user
115 115
116 116 # Setup the pylons context object ('c')
117 117 context = ContextObj()
118 118 context.rhodecode_user = auth_user
119 119 attach_context_attributes(context, request)
120 120 pylons.tmpl_context._push_object(context)
121 121
122 122
123 123 def scan_repositories_if_enabled(event):
124 124 """
125 125 This is subscribed to the `pyramid.events.ApplicationCreated` event. It
126 126 does a repository scan if enabled in the settings.
127 127 """
128 128 from rhodecode.model.scm import ScmModel
129 129 from rhodecode.lib.utils import repo2db_mapper, get_rhodecode_base_path
130 130 settings = event.app.registry.settings
131 131 vcs_server_enabled = settings['vcs.server.enable']
132 132 import_on_startup = settings['startup.import_repos']
133 133 if vcs_server_enabled and import_on_startup:
134 134 repositories = ScmModel().repo_scan(get_rhodecode_base_path())
135 135 repo2db_mapper(repositories, remove_obsolete=False)
136 136
137 137
138 138 def write_metadata_if_needed(event):
139 139 """
140 140 Writes upgrade metadata
141 141 """
142 142 import rhodecode
143 143 from rhodecode.lib import system_info
144 144 from rhodecode.lib import ext_json
145 145
146 146 def write():
147 147 fname = '.rcmetadata.json'
148 148 ini_loc = os.path.dirname(rhodecode.CONFIG.get('__file__'))
149 149 metadata_destination = os.path.join(ini_loc, fname)
150 150
151 configuration = system_info.SysInfo(
152 system_info.rhodecode_config)()['value']
153 license_token = configuration['config']['license_token']
151 154 dbinfo = system_info.SysInfo(system_info.database_info)()['value']
152 155 del dbinfo['url']
153 156 metadata = dict(
154 157 desc='upgrade metadata info',
158 license_token=license_token,
155 159 created_on=datetime.datetime.utcnow().isoformat(),
156 160 usage=system_info.SysInfo(system_info.usage_info)()['value'],
157 161 platform=system_info.SysInfo(system_info.platform_type)()['value'],
158 162 database=dbinfo,
159 163 cpu=system_info.SysInfo(system_info.cpu)()['value'],
160 164 memory=system_info.SysInfo(system_info.memory)()['value'],
161 165 )
162 166
163 167 with open(metadata_destination, 'wb') as f:
164 168 f.write(ext_json.json.dumps(metadata))
165 169
166 170 try:
167 171 write()
168 172 except Exception:
169 173 pass
170 174
171 175
172 176 class Subscriber(object):
173 177 """
174 178 Base class for subscribers to the pyramid event system.
175 179 """
176 180 def __call__(self, event):
177 181 self.run(event)
178 182
179 183 def run(self, event):
180 184 raise NotImplementedError('Subclass has to implement this.')
181 185
182 186
183 187 class AsyncSubscriber(Subscriber):
184 188 """
185 189 Subscriber that handles the execution of events in a separate task to not
186 190 block the execution of the code which triggers the event. It puts the
187 191 received events into a queue from which the worker process takes them in
188 192 order.
189 193 """
190 194 def __init__(self):
191 195 self._stop = False
192 196 self._eventq = Queue.Queue()
193 197 self._worker = self.create_worker()
194 198 self._worker.start()
195 199
196 200 def __call__(self, event):
197 201 self._eventq.put(event)
198 202
199 203 def create_worker(self):
200 204 worker = Thread(target=self.do_work)
201 205 worker.daemon = True
202 206 return worker
203 207
204 208 def stop_worker(self):
205 209 self._stop = False
206 210 self._eventq.put(None)
207 211 self._worker.join()
208 212
209 213 def do_work(self):
210 214 while not self._stop:
211 215 event = self._eventq.get()
212 216 if event is not None:
213 217 self.run(event)
214 218
215 219
216 220 class AsyncSubprocessSubscriber(AsyncSubscriber):
217 221 """
218 222 Subscriber that uses the subprocess32 module to execute a command if an
219 223 event is received. Events are handled asynchronously.
220 224 """
221 225
222 226 def __init__(self, cmd, timeout=None):
223 227 super(AsyncSubprocessSubscriber, self).__init__()
224 228 self._cmd = cmd
225 229 self._timeout = timeout
226 230
227 231 def run(self, event):
228 232 cmd = self._cmd
229 233 timeout = self._timeout
230 234 log.debug('Executing command %s.', cmd)
231 235
232 236 try:
233 237 output = subprocess32.check_output(
234 238 cmd, timeout=timeout, stderr=subprocess32.STDOUT)
235 239 log.debug('Command finished %s', cmd)
236 240 if output:
237 241 log.debug('Command output: %s', output)
238 242 except subprocess32.TimeoutExpired as e:
239 243 log.exception('Timeout while executing command.')
240 244 if e.output:
241 245 log.error('Command output: %s', e.output)
242 246 except subprocess32.CalledProcessError as e:
243 247 log.exception('Error while executing command.')
244 248 if e.output:
245 249 log.error('Command output: %s', e.output)
246 250 except:
247 251 log.exception(
248 252 'Exception while executing command %s.', cmd)
General Comments 0
You need to be logged in to leave comments. Login now