Show More
@@ -1,619 +1,621 b'' | |||||
1 | # RhodeCode VCSServer provides access to different vcs backends via network. |
|
1 | # RhodeCode VCSServer provides access to different vcs backends via network. | |
2 | # Copyright (C) 2014-2019 RhodeCode GmbH |
|
2 | # Copyright (C) 2014-2019 RhodeCode GmbH | |
3 | # |
|
3 | # | |
4 | # This program is free software; you can redistribute it and/or modify |
|
4 | # This program is free software; you can redistribute it and/or modify | |
5 | # it under the terms of the GNU General Public License as published by |
|
5 | # it under the terms of the GNU General Public License as published by | |
6 | # the Free Software Foundation; either version 3 of the License, or |
|
6 | # the Free Software Foundation; either version 3 of the License, or | |
7 | # (at your option) any later version. |
|
7 | # (at your option) any later version. | |
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 General Public License |
|
14 | # You should have received a copy of the GNU General Public License | |
15 | # along with this program; if not, write to the Free Software Foundation, |
|
15 | # along with this program; if not, write to the Free Software Foundation, | |
16 | # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
|
16 | # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
17 |
|
17 | |||
18 | import os |
|
18 | import os | |
19 | import sys |
|
19 | import sys | |
20 | import base64 |
|
20 | import base64 | |
21 | import locale |
|
21 | import locale | |
22 | import logging |
|
22 | import logging | |
23 | import uuid |
|
23 | import uuid | |
24 | import wsgiref.util |
|
24 | import wsgiref.util | |
25 | import traceback |
|
25 | import traceback | |
26 | import tempfile |
|
26 | import tempfile | |
27 | from itertools import chain |
|
27 | from itertools import chain | |
28 |
|
28 | |||
29 | import simplejson as json |
|
29 | import simplejson as json | |
30 | import msgpack |
|
30 | import msgpack | |
31 | from pyramid.config import Configurator |
|
31 | from pyramid.config import Configurator | |
32 | from pyramid.settings import asbool, aslist |
|
32 | from pyramid.settings import asbool, aslist | |
33 | from pyramid.wsgi import wsgiapp |
|
33 | from pyramid.wsgi import wsgiapp | |
34 | from pyramid.compat import configparser |
|
34 | from pyramid.compat import configparser | |
35 |
|
35 | |||
36 |
|
36 | |||
37 | log = logging.getLogger(__name__) |
|
37 | log = logging.getLogger(__name__) | |
38 |
|
38 | |||
39 | # due to Mercurial/glibc2.27 problems we need to detect if locale settings are |
|
39 | # due to Mercurial/glibc2.27 problems we need to detect if locale settings are | |
40 | # causing problems and "fix" it in case they do and fallback to LC_ALL = C |
|
40 | # causing problems and "fix" it in case they do and fallback to LC_ALL = C | |
41 |
|
41 | |||
42 | try: |
|
42 | try: | |
43 | locale.setlocale(locale.LC_ALL, '') |
|
43 | locale.setlocale(locale.LC_ALL, '') | |
44 | except locale.Error as e: |
|
44 | except locale.Error as e: | |
45 | log.error( |
|
45 | log.error( | |
46 | 'LOCALE ERROR: failed to set LC_ALL, fallback to LC_ALL=C, org error: %s', e) |
|
46 | 'LOCALE ERROR: failed to set LC_ALL, fallback to LC_ALL=C, org error: %s', e) | |
47 | os.environ['LC_ALL'] = 'C' |
|
47 | os.environ['LC_ALL'] = 'C' | |
48 |
|
48 | |||
49 | import vcsserver |
|
49 | import vcsserver | |
50 | from vcsserver import remote_wsgi, scm_app, settings, hgpatches |
|
50 | from vcsserver import remote_wsgi, scm_app, settings, hgpatches | |
51 | from vcsserver.git_lfs.app import GIT_LFS_CONTENT_TYPE, GIT_LFS_PROTO_PAT |
|
51 | from vcsserver.git_lfs.app import GIT_LFS_CONTENT_TYPE, GIT_LFS_PROTO_PAT | |
52 | from vcsserver.echo_stub import remote_wsgi as remote_wsgi_stub |
|
52 | from vcsserver.echo_stub import remote_wsgi as remote_wsgi_stub | |
53 | from vcsserver.echo_stub.echo_app import EchoApp |
|
53 | from vcsserver.echo_stub.echo_app import EchoApp | |
54 | from vcsserver.exceptions import HTTPRepoLocked, HTTPRepoBranchProtected |
|
54 | from vcsserver.exceptions import HTTPRepoLocked, HTTPRepoBranchProtected | |
55 | from vcsserver.lib.exc_tracking import store_exception |
|
55 | from vcsserver.lib.exc_tracking import store_exception | |
56 | from vcsserver.server import VcsServer |
|
56 | from vcsserver.server import VcsServer | |
57 |
|
57 | |||
58 | try: |
|
58 | try: | |
59 | from vcsserver.git import GitFactory, GitRemote |
|
59 | from vcsserver.git import GitFactory, GitRemote | |
60 | except ImportError: |
|
60 | except ImportError: | |
61 | GitFactory = None |
|
61 | GitFactory = None | |
62 | GitRemote = None |
|
62 | GitRemote = None | |
63 |
|
63 | |||
64 | try: |
|
64 | try: | |
65 | from vcsserver.hg import MercurialFactory, HgRemote |
|
65 | from vcsserver.hg import MercurialFactory, HgRemote | |
66 | except ImportError: |
|
66 | except ImportError: | |
67 | MercurialFactory = None |
|
67 | MercurialFactory = None | |
68 | HgRemote = None |
|
68 | HgRemote = None | |
69 |
|
69 | |||
70 | try: |
|
70 | try: | |
71 | from vcsserver.svn import SubversionFactory, SvnRemote |
|
71 | from vcsserver.svn import SubversionFactory, SvnRemote | |
72 | except ImportError: |
|
72 | except ImportError: | |
73 | SubversionFactory = None |
|
73 | SubversionFactory = None | |
74 | SvnRemote = None |
|
74 | SvnRemote = None | |
75 |
|
75 | |||
76 |
|
76 | |||
77 | def _is_request_chunked(environ): |
|
77 | def _is_request_chunked(environ): | |
78 | stream = environ.get('HTTP_TRANSFER_ENCODING', '') == 'chunked' |
|
78 | stream = environ.get('HTTP_TRANSFER_ENCODING', '') == 'chunked' | |
79 | return stream |
|
79 | return stream | |
80 |
|
80 | |||
81 |
|
81 | |||
82 | def _int_setting(settings, name, default): |
|
82 | def _int_setting(settings, name, default): | |
83 | settings[name] = int(settings.get(name, default)) |
|
83 | settings[name] = int(settings.get(name, default)) | |
84 | return settings[name] |
|
84 | return settings[name] | |
85 |
|
85 | |||
86 |
|
86 | |||
87 | def _bool_setting(settings, name, default): |
|
87 | def _bool_setting(settings, name, default): | |
88 | input_val = settings.get(name, default) |
|
88 | input_val = settings.get(name, default) | |
89 | if isinstance(input_val, unicode): |
|
89 | if isinstance(input_val, unicode): | |
90 | input_val = input_val.encode('utf8') |
|
90 | input_val = input_val.encode('utf8') | |
91 | settings[name] = asbool(input_val) |
|
91 | settings[name] = asbool(input_val) | |
92 | return settings[name] |
|
92 | return settings[name] | |
93 |
|
93 | |||
94 |
|
94 | |||
95 | def _list_setting(settings, name, default): |
|
95 | def _list_setting(settings, name, default): | |
96 | raw_value = settings.get(name, default) |
|
96 | raw_value = settings.get(name, default) | |
97 |
|
97 | |||
98 | # Otherwise we assume it uses pyramids space/newline separation. |
|
98 | # Otherwise we assume it uses pyramids space/newline separation. | |
99 | settings[name] = aslist(raw_value) |
|
99 | settings[name] = aslist(raw_value) | |
100 | return settings[name] |
|
100 | return settings[name] | |
101 |
|
101 | |||
102 |
|
102 | |||
103 | def _string_setting(settings, name, default, lower=True, default_when_empty=False): |
|
103 | def _string_setting(settings, name, default, lower=True, default_when_empty=False): | |
104 | value = settings.get(name, default) |
|
104 | value = settings.get(name, default) | |
105 |
|
105 | |||
106 | if default_when_empty and not value: |
|
106 | if default_when_empty and not value: | |
107 | # use default value when value is empty |
|
107 | # use default value when value is empty | |
108 | value = default |
|
108 | value = default | |
109 |
|
109 | |||
110 | if lower: |
|
110 | if lower: | |
111 | value = value.lower() |
|
111 | value = value.lower() | |
112 | settings[name] = value |
|
112 | settings[name] = value | |
113 | return settings[name] |
|
113 | return settings[name] | |
114 |
|
114 | |||
115 |
|
115 | |||
116 | class VCS(object): |
|
116 | class VCS(object): | |
117 | def __init__(self, locale=None, cache_config=None): |
|
117 | def __init__(self, locale=None, cache_config=None): | |
118 | self.locale = locale |
|
118 | self.locale = locale | |
119 | self.cache_config = cache_config |
|
119 | self.cache_config = cache_config | |
120 | self._configure_locale() |
|
120 | self._configure_locale() | |
121 |
|
121 | |||
122 | if GitFactory and GitRemote: |
|
122 | if GitFactory and GitRemote: | |
123 | git_factory = GitFactory() |
|
123 | git_factory = GitFactory() | |
124 | self._git_remote = GitRemote(git_factory) |
|
124 | self._git_remote = GitRemote(git_factory) | |
125 | else: |
|
125 | else: | |
126 | log.info("Git client import failed") |
|
126 | log.info("Git client import failed") | |
127 |
|
127 | |||
128 | if MercurialFactory and HgRemote: |
|
128 | if MercurialFactory and HgRemote: | |
129 | hg_factory = MercurialFactory() |
|
129 | hg_factory = MercurialFactory() | |
130 | self._hg_remote = HgRemote(hg_factory) |
|
130 | self._hg_remote = HgRemote(hg_factory) | |
131 | else: |
|
131 | else: | |
132 | log.info("Mercurial client import failed") |
|
132 | log.info("Mercurial client import failed") | |
133 |
|
133 | |||
134 | if SubversionFactory and SvnRemote: |
|
134 | if SubversionFactory and SvnRemote: | |
135 | svn_factory = SubversionFactory() |
|
135 | svn_factory = SubversionFactory() | |
136 |
|
136 | |||
137 | # hg factory is used for svn url validation |
|
137 | # hg factory is used for svn url validation | |
138 | hg_factory = MercurialFactory() |
|
138 | hg_factory = MercurialFactory() | |
139 | self._svn_remote = SvnRemote(svn_factory, hg_factory=hg_factory) |
|
139 | self._svn_remote = SvnRemote(svn_factory, hg_factory=hg_factory) | |
140 | else: |
|
140 | else: | |
141 | log.info("Subversion client import failed") |
|
141 | log.info("Subversion client import failed") | |
142 |
|
142 | |||
143 | self._vcsserver = VcsServer() |
|
143 | self._vcsserver = VcsServer() | |
144 |
|
144 | |||
145 | def _configure_locale(self): |
|
145 | def _configure_locale(self): | |
146 | if self.locale: |
|
146 | if self.locale: | |
147 | log.info('Settings locale: `LC_ALL` to %s', self.locale) |
|
147 | log.info('Settings locale: `LC_ALL` to %s', self.locale) | |
148 | else: |
|
148 | else: | |
149 | log.info( |
|
149 | log.info( | |
150 | 'Configuring locale subsystem based on environment variables') |
|
150 | 'Configuring locale subsystem based on environment variables') | |
151 | try: |
|
151 | try: | |
152 | # If self.locale is the empty string, then the locale |
|
152 | # If self.locale is the empty string, then the locale | |
153 | # module will use the environment variables. See the |
|
153 | # module will use the environment variables. See the | |
154 | # documentation of the package `locale`. |
|
154 | # documentation of the package `locale`. | |
155 | locale.setlocale(locale.LC_ALL, self.locale) |
|
155 | locale.setlocale(locale.LC_ALL, self.locale) | |
156 |
|
156 | |||
157 | language_code, encoding = locale.getlocale() |
|
157 | language_code, encoding = locale.getlocale() | |
158 | log.info( |
|
158 | log.info( | |
159 | 'Locale set to language code "%s" with encoding "%s".', |
|
159 | 'Locale set to language code "%s" with encoding "%s".', | |
160 | language_code, encoding) |
|
160 | language_code, encoding) | |
161 | except locale.Error: |
|
161 | except locale.Error: | |
162 | log.exception( |
|
162 | log.exception( | |
163 | 'Cannot set locale, not configuring the locale system') |
|
163 | 'Cannot set locale, not configuring the locale system') | |
164 |
|
164 | |||
165 |
|
165 | |||
166 | class WsgiProxy(object): |
|
166 | class WsgiProxy(object): | |
167 | def __init__(self, wsgi): |
|
167 | def __init__(self, wsgi): | |
168 | self.wsgi = wsgi |
|
168 | self.wsgi = wsgi | |
169 |
|
169 | |||
170 | def __call__(self, environ, start_response): |
|
170 | def __call__(self, environ, start_response): | |
171 | input_data = environ['wsgi.input'].read() |
|
171 | input_data = environ['wsgi.input'].read() | |
172 | input_data = msgpack.unpackb(input_data) |
|
172 | input_data = msgpack.unpackb(input_data) | |
173 |
|
173 | |||
174 | error = None |
|
174 | error = None | |
175 | try: |
|
175 | try: | |
176 | data, status, headers = self.wsgi.handle( |
|
176 | data, status, headers = self.wsgi.handle( | |
177 | input_data['environment'], input_data['input_data'], |
|
177 | input_data['environment'], input_data['input_data'], | |
178 | *input_data['args'], **input_data['kwargs']) |
|
178 | *input_data['args'], **input_data['kwargs']) | |
179 | except Exception as e: |
|
179 | except Exception as e: | |
180 | data, status, headers = [], None, None |
|
180 | data, status, headers = [], None, None | |
181 | error = { |
|
181 | error = { | |
182 | 'message': str(e), |
|
182 | 'message': str(e), | |
183 | '_vcs_kind': getattr(e, '_vcs_kind', None) |
|
183 | '_vcs_kind': getattr(e, '_vcs_kind', None) | |
184 | } |
|
184 | } | |
185 |
|
185 | |||
186 | start_response(200, {}) |
|
186 | start_response(200, {}) | |
187 | return self._iterator(error, status, headers, data) |
|
187 | return self._iterator(error, status, headers, data) | |
188 |
|
188 | |||
189 | def _iterator(self, error, status, headers, data): |
|
189 | def _iterator(self, error, status, headers, data): | |
190 | initial_data = [ |
|
190 | initial_data = [ | |
191 | error, |
|
191 | error, | |
192 | status, |
|
192 | status, | |
193 | headers, |
|
193 | headers, | |
194 | ] |
|
194 | ] | |
195 |
|
195 | |||
196 | for d in chain(initial_data, data): |
|
196 | for d in chain(initial_data, data): | |
197 | yield msgpack.packb(d) |
|
197 | yield msgpack.packb(d) | |
198 |
|
198 | |||
199 |
|
199 | |||
200 | def not_found(request): |
|
200 | def not_found(request): | |
201 | return {'status': '404 NOT FOUND'} |
|
201 | return {'status': '404 NOT FOUND'} | |
202 |
|
202 | |||
203 |
|
203 | |||
204 | class VCSViewPredicate(object): |
|
204 | class VCSViewPredicate(object): | |
205 | def __init__(self, val, config): |
|
205 | def __init__(self, val, config): | |
206 | self.remotes = val |
|
206 | self.remotes = val | |
207 |
|
207 | |||
208 | def text(self): |
|
208 | def text(self): | |
209 | return 'vcs view method = %s' % (self.remotes.keys(),) |
|
209 | return 'vcs view method = %s' % (self.remotes.keys(),) | |
210 |
|
210 | |||
211 | phash = text |
|
211 | phash = text | |
212 |
|
212 | |||
213 | def __call__(self, context, request): |
|
213 | def __call__(self, context, request): | |
214 | """ |
|
214 | """ | |
215 | View predicate that returns true if given backend is supported by |
|
215 | View predicate that returns true if given backend is supported by | |
216 | defined remotes. |
|
216 | defined remotes. | |
217 | """ |
|
217 | """ | |
218 | backend = request.matchdict.get('backend') |
|
218 | backend = request.matchdict.get('backend') | |
219 | return backend in self.remotes |
|
219 | return backend in self.remotes | |
220 |
|
220 | |||
221 |
|
221 | |||
222 | class HTTPApplication(object): |
|
222 | class HTTPApplication(object): | |
223 | ALLOWED_EXCEPTIONS = ('KeyError', 'URLError') |
|
223 | ALLOWED_EXCEPTIONS = ('KeyError', 'URLError') | |
224 |
|
224 | |||
225 | remote_wsgi = remote_wsgi |
|
225 | remote_wsgi = remote_wsgi | |
226 | _use_echo_app = False |
|
226 | _use_echo_app = False | |
227 |
|
227 | |||
228 | def __init__(self, settings=None, global_config=None): |
|
228 | def __init__(self, settings=None, global_config=None): | |
229 | self._sanitize_settings_and_apply_defaults(settings) |
|
229 | self._sanitize_settings_and_apply_defaults(settings) | |
230 |
|
230 | |||
231 | self.config = Configurator(settings=settings) |
|
231 | self.config = Configurator(settings=settings) | |
232 | self.global_config = global_config |
|
232 | self.global_config = global_config | |
233 | self.config.include('vcsserver.lib.rc_cache') |
|
233 | self.config.include('vcsserver.lib.rc_cache') | |
234 |
|
234 | |||
235 | settings_locale = settings.get('locale', '') or 'en_US.UTF-8' |
|
235 | settings_locale = settings.get('locale', '') or 'en_US.UTF-8' | |
236 | vcs = VCS(locale=settings_locale, cache_config=settings) |
|
236 | vcs = VCS(locale=settings_locale, cache_config=settings) | |
237 | self._remotes = { |
|
237 | self._remotes = { | |
238 | 'hg': vcs._hg_remote, |
|
238 | 'hg': vcs._hg_remote, | |
239 | 'git': vcs._git_remote, |
|
239 | 'git': vcs._git_remote, | |
240 | 'svn': vcs._svn_remote, |
|
240 | 'svn': vcs._svn_remote, | |
241 | 'server': vcs._vcsserver, |
|
241 | 'server': vcs._vcsserver, | |
242 | } |
|
242 | } | |
243 | if settings.get('dev.use_echo_app', 'false').lower() == 'true': |
|
243 | if settings.get('dev.use_echo_app', 'false').lower() == 'true': | |
244 | self._use_echo_app = True |
|
244 | self._use_echo_app = True | |
245 | log.warning("Using EchoApp for VCS operations.") |
|
245 | log.warning("Using EchoApp for VCS operations.") | |
246 | self.remote_wsgi = remote_wsgi_stub |
|
246 | self.remote_wsgi = remote_wsgi_stub | |
247 |
|
247 | |||
248 | self._configure_settings(global_config, settings) |
|
248 | self._configure_settings(global_config, settings) | |
249 | self._configure() |
|
249 | self._configure() | |
250 |
|
250 | |||
251 | def _configure_settings(self, global_config, app_settings): |
|
251 | def _configure_settings(self, global_config, app_settings): | |
252 | """ |
|
252 | """ | |
253 | Configure the settings module. |
|
253 | Configure the settings module. | |
254 | """ |
|
254 | """ | |
255 | settings_merged = global_config.copy() |
|
255 | settings_merged = global_config.copy() | |
256 | settings_merged.update(app_settings) |
|
256 | settings_merged.update(app_settings) | |
257 |
|
257 | |||
258 | git_path = app_settings.get('git_path', None) |
|
258 | git_path = app_settings.get('git_path', None) | |
259 | if git_path: |
|
259 | if git_path: | |
260 | settings.GIT_EXECUTABLE = git_path |
|
260 | settings.GIT_EXECUTABLE = git_path | |
261 | binary_dir = app_settings.get('core.binary_dir', None) |
|
261 | binary_dir = app_settings.get('core.binary_dir', None) | |
262 | if binary_dir: |
|
262 | if binary_dir: | |
263 | settings.BINARY_DIR = binary_dir |
|
263 | settings.BINARY_DIR = binary_dir | |
264 |
|
264 | |||
265 | # Store the settings to make them available to other modules. |
|
265 | # Store the settings to make them available to other modules. | |
266 | vcsserver.PYRAMID_SETTINGS = settings_merged |
|
266 | vcsserver.PYRAMID_SETTINGS = settings_merged | |
267 | vcsserver.CONFIG = settings_merged |
|
267 | vcsserver.CONFIG = settings_merged | |
268 |
|
268 | |||
269 | def _sanitize_settings_and_apply_defaults(self, settings): |
|
269 | def _sanitize_settings_and_apply_defaults(self, settings): | |
270 | temp_store = tempfile.gettempdir() |
|
270 | temp_store = tempfile.gettempdir() | |
271 | default_cache_dir = os.path.join(temp_store, 'rc_cache') |
|
271 | default_cache_dir = os.path.join(temp_store, 'rc_cache') | |
272 |
|
272 | |||
273 | # save default, cache dir, and use it for all backends later. |
|
273 | # save default, cache dir, and use it for all backends later. | |
274 | default_cache_dir = _string_setting( |
|
274 | default_cache_dir = _string_setting( | |
275 | settings, |
|
275 | settings, | |
276 | 'cache_dir', |
|
276 | 'cache_dir', | |
277 | default_cache_dir, lower=False, default_when_empty=True) |
|
277 | default_cache_dir, lower=False, default_when_empty=True) | |
278 |
|
278 | |||
279 | # ensure we have our dir created |
|
279 | # ensure we have our dir created | |
280 | if not os.path.isdir(default_cache_dir): |
|
280 | if not os.path.isdir(default_cache_dir): | |
281 | os.makedirs(default_cache_dir, mode=0o755) |
|
281 | os.makedirs(default_cache_dir, mode=0o755) | |
282 |
|
282 | |||
283 | # exception store cache |
|
283 | # exception store cache | |
284 | _string_setting( |
|
284 | _string_setting( | |
285 | settings, |
|
285 | settings, | |
286 | 'exception_tracker.store_path', |
|
286 | 'exception_tracker.store_path', | |
287 | temp_store, lower=False, default_when_empty=True) |
|
287 | temp_store, lower=False, default_when_empty=True) | |
288 |
|
288 | |||
289 | # repo_object cache |
|
289 | # repo_object cache | |
290 | _string_setting( |
|
290 | _string_setting( | |
291 | settings, |
|
291 | settings, | |
292 | 'rc_cache.repo_object.backend', |
|
292 | 'rc_cache.repo_object.backend', | |
293 | 'dogpile.cache.rc.memory_lru') |
|
293 | 'dogpile.cache.rc.memory_lru') | |
294 | _int_setting( |
|
294 | _int_setting( | |
295 | settings, |
|
295 | settings, | |
296 | 'rc_cache.repo_object.expiration_time', |
|
296 | 'rc_cache.repo_object.expiration_time', | |
297 | 300) |
|
297 | 300) | |
298 | _int_setting( |
|
298 | _int_setting( | |
299 | settings, |
|
299 | settings, | |
300 | 'rc_cache.repo_object.max_size', |
|
300 | 'rc_cache.repo_object.max_size', | |
301 | 1024) |
|
301 | 1024) | |
302 |
|
302 | |||
303 | def _configure(self): |
|
303 | def _configure(self): | |
304 | self.config.add_renderer(name='msgpack', factory=self._msgpack_renderer_factory) |
|
304 | self.config.add_renderer(name='msgpack', factory=self._msgpack_renderer_factory) | |
305 |
|
305 | |||
306 | self.config.add_route('service', '/_service') |
|
306 | self.config.add_route('service', '/_service') | |
307 | self.config.add_route('status', '/status') |
|
307 | self.config.add_route('status', '/status') | |
308 | self.config.add_route('hg_proxy', '/proxy/hg') |
|
308 | self.config.add_route('hg_proxy', '/proxy/hg') | |
309 | self.config.add_route('git_proxy', '/proxy/git') |
|
309 | self.config.add_route('git_proxy', '/proxy/git') | |
310 | self.config.add_route('vcs', '/{backend}') |
|
310 | self.config.add_route('vcs', '/{backend}') | |
311 | self.config.add_route('stream_git', '/stream/git/*repo_name') |
|
311 | self.config.add_route('stream_git', '/stream/git/*repo_name') | |
312 | self.config.add_route('stream_hg', '/stream/hg/*repo_name') |
|
312 | self.config.add_route('stream_hg', '/stream/hg/*repo_name') | |
313 |
|
313 | |||
314 | self.config.add_view(self.status_view, route_name='status', renderer='json') |
|
314 | self.config.add_view(self.status_view, route_name='status', renderer='json') | |
315 | self.config.add_view(self.service_view, route_name='service', renderer='msgpack') |
|
315 | self.config.add_view(self.service_view, route_name='service', renderer='msgpack') | |
316 |
|
316 | |||
317 | self.config.add_view(self.hg_proxy(), route_name='hg_proxy') |
|
317 | self.config.add_view(self.hg_proxy(), route_name='hg_proxy') | |
318 | self.config.add_view(self.git_proxy(), route_name='git_proxy') |
|
318 | self.config.add_view(self.git_proxy(), route_name='git_proxy') | |
319 | self.config.add_view(self.vcs_view, route_name='vcs', renderer='msgpack', |
|
319 | self.config.add_view(self.vcs_view, route_name='vcs', renderer='msgpack', | |
320 | vcs_view=self._remotes) |
|
320 | vcs_view=self._remotes) | |
321 |
|
321 | |||
322 | self.config.add_view(self.hg_stream(), route_name='stream_hg') |
|
322 | self.config.add_view(self.hg_stream(), route_name='stream_hg') | |
323 | self.config.add_view(self.git_stream(), route_name='stream_git') |
|
323 | self.config.add_view(self.git_stream(), route_name='stream_git') | |
324 |
|
324 | |||
325 | self.config.add_view_predicate('vcs_view', VCSViewPredicate) |
|
325 | self.config.add_view_predicate('vcs_view', VCSViewPredicate) | |
326 |
|
326 | |||
327 | self.config.add_notfound_view(not_found, renderer='json') |
|
327 | self.config.add_notfound_view(not_found, renderer='json') | |
328 |
|
328 | |||
329 | self.config.add_view(self.handle_vcs_exception, context=Exception) |
|
329 | self.config.add_view(self.handle_vcs_exception, context=Exception) | |
330 |
|
330 | |||
331 | self.config.add_tween( |
|
331 | self.config.add_tween( | |
332 | 'vcsserver.tweens.request_wrapper.RequestWrapperTween', |
|
332 | 'vcsserver.tweens.request_wrapper.RequestWrapperTween', | |
333 | ) |
|
333 | ) | |
334 |
|
334 | |||
335 | def wsgi_app(self): |
|
335 | def wsgi_app(self): | |
336 | return self.config.make_wsgi_app() |
|
336 | return self.config.make_wsgi_app() | |
337 |
|
337 | |||
338 | def vcs_view(self, request): |
|
338 | def vcs_view(self, request): | |
339 | remote = self._remotes[request.matchdict['backend']] |
|
339 | remote = self._remotes[request.matchdict['backend']] | |
340 | payload = msgpack.unpackb(request.body, use_list=True) |
|
340 | payload = msgpack.unpackb(request.body, use_list=True) | |
341 | method = payload.get('method') |
|
341 | method = payload.get('method') | |
342 | params = payload.get('params') |
|
342 | params = payload.get('params') | |
343 | wire = params.get('wire') |
|
343 | wire = params.get('wire') | |
344 | args = params.get('args') |
|
344 | args = params.get('args') | |
345 | kwargs = params.get('kwargs') |
|
345 | kwargs = params.get('kwargs') | |
346 | context_uid = None |
|
346 | context_uid = None | |
347 |
|
347 | |||
348 | if wire: |
|
348 | if wire: | |
349 | try: |
|
349 | try: | |
350 | wire['context'] = context_uid = uuid.UUID(wire['context']) |
|
350 | wire['context'] = context_uid = uuid.UUID(wire['context']) | |
351 | except KeyError: |
|
351 | except KeyError: | |
352 | pass |
|
352 | pass | |
353 | args.insert(0, wire) |
|
353 | args.insert(0, wire) | |
354 |
|
354 | |||
355 | # NOTE(marcink): trading complexity for slight performance |
|
355 | # NOTE(marcink): trading complexity for slight performance | |
356 | if log.isEnabledFor(logging.DEBUG): |
|
356 | if log.isEnabledFor(logging.DEBUG): | |
357 | no_args_methods = [ |
|
357 | no_args_methods = [ | |
358 | 'archive_repo' |
|
358 | 'archive_repo' | |
359 | ] |
|
359 | ] | |
360 | if method in no_args_methods: |
|
360 | if method in no_args_methods: | |
361 | call_args = '' |
|
361 | call_args = '' | |
362 | else: |
|
362 | else: | |
363 | call_args = args[1:] |
|
363 | call_args = args[1:] | |
364 | log.debug('method called:%s with args:%s kwargs:%s context_uid: %s', |
|
364 | ||
365 | method, call_args, kwargs, context_uid) |
|
365 | repo_state_uid = wire.get('repo_state_uid') if wire else None | |
|
366 | log.debug('method called:%s with args:%s kwargs:%s context_uid: %s, repo_state_uid:%s', | |||
|
367 | method, call_args, kwargs, context_uid, repo_state_uid) | |||
366 |
|
368 | |||
367 | try: |
|
369 | try: | |
368 | resp = getattr(remote, method)(*args, **kwargs) |
|
370 | resp = getattr(remote, method)(*args, **kwargs) | |
369 | except Exception as e: |
|
371 | except Exception as e: | |
370 | exc_info = list(sys.exc_info()) |
|
372 | exc_info = list(sys.exc_info()) | |
371 | exc_type, exc_value, exc_traceback = exc_info |
|
373 | exc_type, exc_value, exc_traceback = exc_info | |
372 |
|
374 | |||
373 | org_exc = getattr(e, '_org_exc', None) |
|
375 | org_exc = getattr(e, '_org_exc', None) | |
374 | org_exc_name = None |
|
376 | org_exc_name = None | |
375 | org_exc_tb = '' |
|
377 | org_exc_tb = '' | |
376 | if org_exc: |
|
378 | if org_exc: | |
377 | org_exc_name = org_exc.__class__.__name__ |
|
379 | org_exc_name = org_exc.__class__.__name__ | |
378 | org_exc_tb = getattr(e, '_org_exc_tb', '') |
|
380 | org_exc_tb = getattr(e, '_org_exc_tb', '') | |
379 | # replace our "faked" exception with our org |
|
381 | # replace our "faked" exception with our org | |
380 | exc_info[0] = org_exc.__class__ |
|
382 | exc_info[0] = org_exc.__class__ | |
381 | exc_info[1] = org_exc |
|
383 | exc_info[1] = org_exc | |
382 |
|
384 | |||
383 | store_exception(id(exc_info), exc_info) |
|
385 | store_exception(id(exc_info), exc_info) | |
384 |
|
386 | |||
385 | tb_info = ''.join( |
|
387 | tb_info = ''.join( | |
386 | traceback.format_exception(exc_type, exc_value, exc_traceback)) |
|
388 | traceback.format_exception(exc_type, exc_value, exc_traceback)) | |
387 |
|
389 | |||
388 | type_ = e.__class__.__name__ |
|
390 | type_ = e.__class__.__name__ | |
389 | if type_ not in self.ALLOWED_EXCEPTIONS: |
|
391 | if type_ not in self.ALLOWED_EXCEPTIONS: | |
390 | type_ = None |
|
392 | type_ = None | |
391 |
|
393 | |||
392 | resp = { |
|
394 | resp = { | |
393 | 'id': payload.get('id'), |
|
395 | 'id': payload.get('id'), | |
394 | 'error': { |
|
396 | 'error': { | |
395 | 'message': e.message, |
|
397 | 'message': e.message, | |
396 | 'traceback': tb_info, |
|
398 | 'traceback': tb_info, | |
397 | 'org_exc': org_exc_name, |
|
399 | 'org_exc': org_exc_name, | |
398 | 'org_exc_tb': org_exc_tb, |
|
400 | 'org_exc_tb': org_exc_tb, | |
399 | 'type': type_ |
|
401 | 'type': type_ | |
400 | } |
|
402 | } | |
401 | } |
|
403 | } | |
402 | try: |
|
404 | try: | |
403 | resp['error']['_vcs_kind'] = getattr(e, '_vcs_kind', None) |
|
405 | resp['error']['_vcs_kind'] = getattr(e, '_vcs_kind', None) | |
404 | except AttributeError: |
|
406 | except AttributeError: | |
405 | pass |
|
407 | pass | |
406 | else: |
|
408 | else: | |
407 | resp = { |
|
409 | resp = { | |
408 | 'id': payload.get('id'), |
|
410 | 'id': payload.get('id'), | |
409 | 'result': resp |
|
411 | 'result': resp | |
410 | } |
|
412 | } | |
411 |
|
413 | |||
412 | return resp |
|
414 | return resp | |
413 |
|
415 | |||
414 | def status_view(self, request): |
|
416 | def status_view(self, request): | |
415 | import vcsserver |
|
417 | import vcsserver | |
416 | return {'status': 'OK', 'vcsserver_version': vcsserver.__version__, |
|
418 | return {'status': 'OK', 'vcsserver_version': vcsserver.__version__, | |
417 | 'pid': os.getpid()} |
|
419 | 'pid': os.getpid()} | |
418 |
|
420 | |||
419 | def service_view(self, request): |
|
421 | def service_view(self, request): | |
420 | import vcsserver |
|
422 | import vcsserver | |
421 |
|
423 | |||
422 | payload = msgpack.unpackb(request.body, use_list=True) |
|
424 | payload = msgpack.unpackb(request.body, use_list=True) | |
423 |
|
425 | |||
424 | try: |
|
426 | try: | |
425 | path = self.global_config['__file__'] |
|
427 | path = self.global_config['__file__'] | |
426 | config = configparser.ConfigParser() |
|
428 | config = configparser.ConfigParser() | |
427 | config.read(path) |
|
429 | config.read(path) | |
428 | parsed_ini = config |
|
430 | parsed_ini = config | |
429 | if parsed_ini.has_section('server:main'): |
|
431 | if parsed_ini.has_section('server:main'): | |
430 | parsed_ini = dict(parsed_ini.items('server:main')) |
|
432 | parsed_ini = dict(parsed_ini.items('server:main')) | |
431 | except Exception: |
|
433 | except Exception: | |
432 | log.exception('Failed to read .ini file for display') |
|
434 | log.exception('Failed to read .ini file for display') | |
433 | parsed_ini = {} |
|
435 | parsed_ini = {} | |
434 |
|
436 | |||
435 | resp = { |
|
437 | resp = { | |
436 | 'id': payload.get('id'), |
|
438 | 'id': payload.get('id'), | |
437 | 'result': dict( |
|
439 | 'result': dict( | |
438 | version=vcsserver.__version__, |
|
440 | version=vcsserver.__version__, | |
439 | config=parsed_ini, |
|
441 | config=parsed_ini, | |
440 | payload=payload, |
|
442 | payload=payload, | |
441 | ) |
|
443 | ) | |
442 | } |
|
444 | } | |
443 | return resp |
|
445 | return resp | |
444 |
|
446 | |||
445 | def _msgpack_renderer_factory(self, info): |
|
447 | def _msgpack_renderer_factory(self, info): | |
446 | def _render(value, system): |
|
448 | def _render(value, system): | |
447 | request = system.get('request') |
|
449 | request = system.get('request') | |
448 | if request is not None: |
|
450 | if request is not None: | |
449 | response = request.response |
|
451 | response = request.response | |
450 | ct = response.content_type |
|
452 | ct = response.content_type | |
451 | if ct == response.default_content_type: |
|
453 | if ct == response.default_content_type: | |
452 | response.content_type = 'application/x-msgpack' |
|
454 | response.content_type = 'application/x-msgpack' | |
453 | return msgpack.packb(value) |
|
455 | return msgpack.packb(value) | |
454 | return _render |
|
456 | return _render | |
455 |
|
457 | |||
456 | def set_env_from_config(self, environ, config): |
|
458 | def set_env_from_config(self, environ, config): | |
457 | dict_conf = {} |
|
459 | dict_conf = {} | |
458 | try: |
|
460 | try: | |
459 | for elem in config: |
|
461 | for elem in config: | |
460 | if elem[0] == 'rhodecode': |
|
462 | if elem[0] == 'rhodecode': | |
461 | dict_conf = json.loads(elem[2]) |
|
463 | dict_conf = json.loads(elem[2]) | |
462 | break |
|
464 | break | |
463 | except Exception: |
|
465 | except Exception: | |
464 | log.exception('Failed to fetch SCM CONFIG') |
|
466 | log.exception('Failed to fetch SCM CONFIG') | |
465 | return |
|
467 | return | |
466 |
|
468 | |||
467 | username = dict_conf.get('username') |
|
469 | username = dict_conf.get('username') | |
468 | if username: |
|
470 | if username: | |
469 | environ['REMOTE_USER'] = username |
|
471 | environ['REMOTE_USER'] = username | |
470 | # mercurial specific, some extension api rely on this |
|
472 | # mercurial specific, some extension api rely on this | |
471 | environ['HGUSER'] = username |
|
473 | environ['HGUSER'] = username | |
472 |
|
474 | |||
473 | ip = dict_conf.get('ip') |
|
475 | ip = dict_conf.get('ip') | |
474 | if ip: |
|
476 | if ip: | |
475 | environ['REMOTE_HOST'] = ip |
|
477 | environ['REMOTE_HOST'] = ip | |
476 |
|
478 | |||
477 | if _is_request_chunked(environ): |
|
479 | if _is_request_chunked(environ): | |
478 | # set the compatibility flag for webob |
|
480 | # set the compatibility flag for webob | |
479 | environ['wsgi.input_terminated'] = True |
|
481 | environ['wsgi.input_terminated'] = True | |
480 |
|
482 | |||
481 | def hg_proxy(self): |
|
483 | def hg_proxy(self): | |
482 | @wsgiapp |
|
484 | @wsgiapp | |
483 | def _hg_proxy(environ, start_response): |
|
485 | def _hg_proxy(environ, start_response): | |
484 | app = WsgiProxy(self.remote_wsgi.HgRemoteWsgi()) |
|
486 | app = WsgiProxy(self.remote_wsgi.HgRemoteWsgi()) | |
485 | return app(environ, start_response) |
|
487 | return app(environ, start_response) | |
486 | return _hg_proxy |
|
488 | return _hg_proxy | |
487 |
|
489 | |||
488 | def git_proxy(self): |
|
490 | def git_proxy(self): | |
489 | @wsgiapp |
|
491 | @wsgiapp | |
490 | def _git_proxy(environ, start_response): |
|
492 | def _git_proxy(environ, start_response): | |
491 | app = WsgiProxy(self.remote_wsgi.GitRemoteWsgi()) |
|
493 | app = WsgiProxy(self.remote_wsgi.GitRemoteWsgi()) | |
492 | return app(environ, start_response) |
|
494 | return app(environ, start_response) | |
493 | return _git_proxy |
|
495 | return _git_proxy | |
494 |
|
496 | |||
495 | def hg_stream(self): |
|
497 | def hg_stream(self): | |
496 | if self._use_echo_app: |
|
498 | if self._use_echo_app: | |
497 | @wsgiapp |
|
499 | @wsgiapp | |
498 | def _hg_stream(environ, start_response): |
|
500 | def _hg_stream(environ, start_response): | |
499 | app = EchoApp('fake_path', 'fake_name', None) |
|
501 | app = EchoApp('fake_path', 'fake_name', None) | |
500 | return app(environ, start_response) |
|
502 | return app(environ, start_response) | |
501 | return _hg_stream |
|
503 | return _hg_stream | |
502 | else: |
|
504 | else: | |
503 | @wsgiapp |
|
505 | @wsgiapp | |
504 | def _hg_stream(environ, start_response): |
|
506 | def _hg_stream(environ, start_response): | |
505 | log.debug('http-app: handling hg stream') |
|
507 | log.debug('http-app: handling hg stream') | |
506 | repo_path = environ['HTTP_X_RC_REPO_PATH'] |
|
508 | repo_path = environ['HTTP_X_RC_REPO_PATH'] | |
507 | repo_name = environ['HTTP_X_RC_REPO_NAME'] |
|
509 | repo_name = environ['HTTP_X_RC_REPO_NAME'] | |
508 | packed_config = base64.b64decode( |
|
510 | packed_config = base64.b64decode( | |
509 | environ['HTTP_X_RC_REPO_CONFIG']) |
|
511 | environ['HTTP_X_RC_REPO_CONFIG']) | |
510 | config = msgpack.unpackb(packed_config) |
|
512 | config = msgpack.unpackb(packed_config) | |
511 | app = scm_app.create_hg_wsgi_app( |
|
513 | app = scm_app.create_hg_wsgi_app( | |
512 | repo_path, repo_name, config) |
|
514 | repo_path, repo_name, config) | |
513 |
|
515 | |||
514 | # Consistent path information for hgweb |
|
516 | # Consistent path information for hgweb | |
515 | environ['PATH_INFO'] = environ['HTTP_X_RC_PATH_INFO'] |
|
517 | environ['PATH_INFO'] = environ['HTTP_X_RC_PATH_INFO'] | |
516 | environ['REPO_NAME'] = repo_name |
|
518 | environ['REPO_NAME'] = repo_name | |
517 | self.set_env_from_config(environ, config) |
|
519 | self.set_env_from_config(environ, config) | |
518 |
|
520 | |||
519 | log.debug('http-app: starting app handler ' |
|
521 | log.debug('http-app: starting app handler ' | |
520 | 'with %s and process request', app) |
|
522 | 'with %s and process request', app) | |
521 | return app(environ, ResponseFilter(start_response)) |
|
523 | return app(environ, ResponseFilter(start_response)) | |
522 | return _hg_stream |
|
524 | return _hg_stream | |
523 |
|
525 | |||
524 | def git_stream(self): |
|
526 | def git_stream(self): | |
525 | if self._use_echo_app: |
|
527 | if self._use_echo_app: | |
526 | @wsgiapp |
|
528 | @wsgiapp | |
527 | def _git_stream(environ, start_response): |
|
529 | def _git_stream(environ, start_response): | |
528 | app = EchoApp('fake_path', 'fake_name', None) |
|
530 | app = EchoApp('fake_path', 'fake_name', None) | |
529 | return app(environ, start_response) |
|
531 | return app(environ, start_response) | |
530 | return _git_stream |
|
532 | return _git_stream | |
531 | else: |
|
533 | else: | |
532 | @wsgiapp |
|
534 | @wsgiapp | |
533 | def _git_stream(environ, start_response): |
|
535 | def _git_stream(environ, start_response): | |
534 | log.debug('http-app: handling git stream') |
|
536 | log.debug('http-app: handling git stream') | |
535 | repo_path = environ['HTTP_X_RC_REPO_PATH'] |
|
537 | repo_path = environ['HTTP_X_RC_REPO_PATH'] | |
536 | repo_name = environ['HTTP_X_RC_REPO_NAME'] |
|
538 | repo_name = environ['HTTP_X_RC_REPO_NAME'] | |
537 | packed_config = base64.b64decode( |
|
539 | packed_config = base64.b64decode( | |
538 | environ['HTTP_X_RC_REPO_CONFIG']) |
|
540 | environ['HTTP_X_RC_REPO_CONFIG']) | |
539 | config = msgpack.unpackb(packed_config) |
|
541 | config = msgpack.unpackb(packed_config) | |
540 |
|
542 | |||
541 | environ['PATH_INFO'] = environ['HTTP_X_RC_PATH_INFO'] |
|
543 | environ['PATH_INFO'] = environ['HTTP_X_RC_PATH_INFO'] | |
542 | self.set_env_from_config(environ, config) |
|
544 | self.set_env_from_config(environ, config) | |
543 |
|
545 | |||
544 | content_type = environ.get('CONTENT_TYPE', '') |
|
546 | content_type = environ.get('CONTENT_TYPE', '') | |
545 |
|
547 | |||
546 | path = environ['PATH_INFO'] |
|
548 | path = environ['PATH_INFO'] | |
547 | is_lfs_request = GIT_LFS_CONTENT_TYPE in content_type |
|
549 | is_lfs_request = GIT_LFS_CONTENT_TYPE in content_type | |
548 | log.debug( |
|
550 | log.debug( | |
549 | 'LFS: Detecting if request `%s` is LFS server path based ' |
|
551 | 'LFS: Detecting if request `%s` is LFS server path based ' | |
550 | 'on content type:`%s`, is_lfs:%s', |
|
552 | 'on content type:`%s`, is_lfs:%s', | |
551 | path, content_type, is_lfs_request) |
|
553 | path, content_type, is_lfs_request) | |
552 |
|
554 | |||
553 | if not is_lfs_request: |
|
555 | if not is_lfs_request: | |
554 | # fallback detection by path |
|
556 | # fallback detection by path | |
555 | if GIT_LFS_PROTO_PAT.match(path): |
|
557 | if GIT_LFS_PROTO_PAT.match(path): | |
556 | is_lfs_request = True |
|
558 | is_lfs_request = True | |
557 | log.debug( |
|
559 | log.debug( | |
558 | 'LFS: fallback detection by path of: `%s`, is_lfs:%s', |
|
560 | 'LFS: fallback detection by path of: `%s`, is_lfs:%s', | |
559 | path, is_lfs_request) |
|
561 | path, is_lfs_request) | |
560 |
|
562 | |||
561 | if is_lfs_request: |
|
563 | if is_lfs_request: | |
562 | app = scm_app.create_git_lfs_wsgi_app( |
|
564 | app = scm_app.create_git_lfs_wsgi_app( | |
563 | repo_path, repo_name, config) |
|
565 | repo_path, repo_name, config) | |
564 | else: |
|
566 | else: | |
565 | app = scm_app.create_git_wsgi_app( |
|
567 | app = scm_app.create_git_wsgi_app( | |
566 | repo_path, repo_name, config) |
|
568 | repo_path, repo_name, config) | |
567 |
|
569 | |||
568 | log.debug('http-app: starting app handler ' |
|
570 | log.debug('http-app: starting app handler ' | |
569 | 'with %s and process request', app) |
|
571 | 'with %s and process request', app) | |
570 |
|
572 | |||
571 | return app(environ, start_response) |
|
573 | return app(environ, start_response) | |
572 |
|
574 | |||
573 | return _git_stream |
|
575 | return _git_stream | |
574 |
|
576 | |||
575 | def handle_vcs_exception(self, exception, request): |
|
577 | def handle_vcs_exception(self, exception, request): | |
576 | _vcs_kind = getattr(exception, '_vcs_kind', '') |
|
578 | _vcs_kind = getattr(exception, '_vcs_kind', '') | |
577 | if _vcs_kind == 'repo_locked': |
|
579 | if _vcs_kind == 'repo_locked': | |
578 | # Get custom repo-locked status code if present. |
|
580 | # Get custom repo-locked status code if present. | |
579 | status_code = request.headers.get('X-RC-Locked-Status-Code') |
|
581 | status_code = request.headers.get('X-RC-Locked-Status-Code') | |
580 | return HTTPRepoLocked( |
|
582 | return HTTPRepoLocked( | |
581 | title=exception.message, status_code=status_code) |
|
583 | title=exception.message, status_code=status_code) | |
582 |
|
584 | |||
583 | elif _vcs_kind == 'repo_branch_protected': |
|
585 | elif _vcs_kind == 'repo_branch_protected': | |
584 | # Get custom repo-branch-protected status code if present. |
|
586 | # Get custom repo-branch-protected status code if present. | |
585 | return HTTPRepoBranchProtected(title=exception.message) |
|
587 | return HTTPRepoBranchProtected(title=exception.message) | |
586 |
|
588 | |||
587 | exc_info = request.exc_info |
|
589 | exc_info = request.exc_info | |
588 | store_exception(id(exc_info), exc_info) |
|
590 | store_exception(id(exc_info), exc_info) | |
589 |
|
591 | |||
590 | traceback_info = 'unavailable' |
|
592 | traceback_info = 'unavailable' | |
591 | if request.exc_info: |
|
593 | if request.exc_info: | |
592 | exc_type, exc_value, exc_tb = request.exc_info |
|
594 | exc_type, exc_value, exc_tb = request.exc_info | |
593 | traceback_info = ''.join(traceback.format_exception(exc_type, exc_value, exc_tb)) |
|
595 | traceback_info = ''.join(traceback.format_exception(exc_type, exc_value, exc_tb)) | |
594 |
|
596 | |||
595 | log.error( |
|
597 | log.error( | |
596 | 'error occurred handling this request for path: %s, \n tb: %s', |
|
598 | 'error occurred handling this request for path: %s, \n tb: %s', | |
597 | request.path, traceback_info) |
|
599 | request.path, traceback_info) | |
598 | raise exception |
|
600 | raise exception | |
599 |
|
601 | |||
600 |
|
602 | |||
601 | class ResponseFilter(object): |
|
603 | class ResponseFilter(object): | |
602 |
|
604 | |||
603 | def __init__(self, start_response): |
|
605 | def __init__(self, start_response): | |
604 | self._start_response = start_response |
|
606 | self._start_response = start_response | |
605 |
|
607 | |||
606 | def __call__(self, status, response_headers, exc_info=None): |
|
608 | def __call__(self, status, response_headers, exc_info=None): | |
607 | headers = tuple( |
|
609 | headers = tuple( | |
608 | (h, v) for h, v in response_headers |
|
610 | (h, v) for h, v in response_headers | |
609 | if not wsgiref.util.is_hop_by_hop(h)) |
|
611 | if not wsgiref.util.is_hop_by_hop(h)) | |
610 | return self._start_response(status, headers, exc_info) |
|
612 | return self._start_response(status, headers, exc_info) | |
611 |
|
613 | |||
612 |
|
614 | |||
613 | def main(global_config, **settings): |
|
615 | def main(global_config, **settings): | |
614 | if MercurialFactory: |
|
616 | if MercurialFactory: | |
615 | hgpatches.patch_largefiles_capabilities() |
|
617 | hgpatches.patch_largefiles_capabilities() | |
616 | hgpatches.patch_subrepo_type_mapping() |
|
618 | hgpatches.patch_subrepo_type_mapping() | |
617 |
|
619 | |||
618 | app = HTTPApplication(settings=settings, global_config=global_config) |
|
620 | app = HTTPApplication(settings=settings, global_config=global_config) | |
619 | return app.wsgi_app() |
|
621 | return app.wsgi_app() |
@@ -1,149 +1,150 b'' | |||||
1 | # RhodeCode VCSServer provides access to different vcs backends via network. |
|
1 | # RhodeCode VCSServer provides access to different vcs backends via network. | |
2 | # Copyright (C) 2014-2019 RhodeCode GmbH |
|
2 | # Copyright (C) 2014-2019 RhodeCode GmbH | |
3 | # |
|
3 | # | |
4 | # This program is free software; you can redistribute it and/or modify |
|
4 | # This program is free software; you can redistribute it and/or modify | |
5 | # it under the terms of the GNU General Public License as published by |
|
5 | # it under the terms of the GNU General Public License as published by | |
6 | # the Free Software Foundation; either version 3 of the License, or |
|
6 | # the Free Software Foundation; either version 3 of the License, or | |
7 | # (at your option) any later version. |
|
7 | # (at your option) any later version. | |
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 General Public License |
|
14 | # You should have received a copy of the GNU General Public License | |
15 | # along with this program; if not, write to the Free Software Foundation, |
|
15 | # along with this program; if not, write to the Free Software Foundation, | |
16 | # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
|
16 | # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
17 |
|
17 | |||
18 | import os |
|
18 | import os | |
19 | import logging |
|
19 | import logging | |
20 | import functools |
|
20 | import functools | |
21 |
|
21 | |||
22 | from dogpile.cache import CacheRegion |
|
22 | from dogpile.cache import CacheRegion | |
23 | from dogpile.cache.util import compat |
|
23 | from dogpile.cache.util import compat | |
24 |
|
24 | |||
25 | from vcsserver.utils import safe_str, sha1 |
|
25 | from vcsserver.utils import safe_str, sha1 | |
26 |
|
26 | |||
27 |
|
27 | |||
28 | log = logging.getLogger(__name__) |
|
28 | log = logging.getLogger(__name__) | |
29 |
|
29 | |||
30 |
|
30 | |||
31 | class RhodeCodeCacheRegion(CacheRegion): |
|
31 | class RhodeCodeCacheRegion(CacheRegion): | |
32 |
|
32 | |||
33 | def conditional_cache_on_arguments( |
|
33 | def conditional_cache_on_arguments( | |
34 | self, namespace=None, |
|
34 | self, namespace=None, | |
35 | expiration_time=None, |
|
35 | expiration_time=None, | |
36 | should_cache_fn=None, |
|
36 | should_cache_fn=None, | |
37 | to_str=compat.string_type, |
|
37 | to_str=compat.string_type, | |
38 | function_key_generator=None, |
|
38 | function_key_generator=None, | |
39 | condition=True): |
|
39 | condition=True): | |
40 | """ |
|
40 | """ | |
41 | Custom conditional decorator, that will not touch any dogpile internals if |
|
41 | Custom conditional decorator, that will not touch any dogpile internals if | |
42 | condition isn't meet. This works a bit different than should_cache_fn |
|
42 | condition isn't meet. This works a bit different than should_cache_fn | |
43 | And it's faster in cases we don't ever want to compute cached values |
|
43 | And it's faster in cases we don't ever want to compute cached values | |
44 | """ |
|
44 | """ | |
45 | expiration_time_is_callable = compat.callable(expiration_time) |
|
45 | expiration_time_is_callable = compat.callable(expiration_time) | |
46 |
|
46 | |||
47 | if function_key_generator is None: |
|
47 | if function_key_generator is None: | |
48 | function_key_generator = self.function_key_generator |
|
48 | function_key_generator = self.function_key_generator | |
49 |
|
49 | |||
50 | def decorator(fn): |
|
50 | def decorator(fn): | |
51 | if to_str is compat.string_type: |
|
51 | if to_str is compat.string_type: | |
52 | # backwards compatible |
|
52 | # backwards compatible | |
53 | key_generator = function_key_generator(namespace, fn) |
|
53 | key_generator = function_key_generator(namespace, fn) | |
54 | else: |
|
54 | else: | |
55 | key_generator = function_key_generator(namespace, fn, to_str=to_str) |
|
55 | key_generator = function_key_generator(namespace, fn, to_str=to_str) | |
56 |
|
56 | |||
57 | @functools.wraps(fn) |
|
57 | @functools.wraps(fn) | |
58 | def decorate(*arg, **kw): |
|
58 | def decorate(*arg, **kw): | |
59 | key = key_generator(*arg, **kw) |
|
59 | key = key_generator(*arg, **kw) | |
60 |
|
60 | |||
61 | @functools.wraps(fn) |
|
61 | @functools.wraps(fn) | |
62 | def creator(): |
|
62 | def creator(): | |
|
63 | log.debug('Calling cached fn:%s', fn) | |||
63 | return fn(*arg, **kw) |
|
64 | return fn(*arg, **kw) | |
64 |
|
65 | |||
65 | if not condition: |
|
66 | if not condition: | |
66 | return creator() |
|
67 | return creator() | |
67 |
|
68 | |||
68 | timeout = expiration_time() if expiration_time_is_callable \ |
|
69 | timeout = expiration_time() if expiration_time_is_callable \ | |
69 | else expiration_time |
|
70 | else expiration_time | |
70 |
|
71 | |||
71 | return self.get_or_create(key, creator, timeout, should_cache_fn) |
|
72 | return self.get_or_create(key, creator, timeout, should_cache_fn) | |
72 |
|
73 | |||
73 | def invalidate(*arg, **kw): |
|
74 | def invalidate(*arg, **kw): | |
74 | key = key_generator(*arg, **kw) |
|
75 | key = key_generator(*arg, **kw) | |
75 | self.delete(key) |
|
76 | self.delete(key) | |
76 |
|
77 | |||
77 | def set_(value, *arg, **kw): |
|
78 | def set_(value, *arg, **kw): | |
78 | key = key_generator(*arg, **kw) |
|
79 | key = key_generator(*arg, **kw) | |
79 | self.set(key, value) |
|
80 | self.set(key, value) | |
80 |
|
81 | |||
81 | def get(*arg, **kw): |
|
82 | def get(*arg, **kw): | |
82 | key = key_generator(*arg, **kw) |
|
83 | key = key_generator(*arg, **kw) | |
83 | return self.get(key) |
|
84 | return self.get(key) | |
84 |
|
85 | |||
85 | def refresh(*arg, **kw): |
|
86 | def refresh(*arg, **kw): | |
86 | key = key_generator(*arg, **kw) |
|
87 | key = key_generator(*arg, **kw) | |
87 | value = fn(*arg, **kw) |
|
88 | value = fn(*arg, **kw) | |
88 | self.set(key, value) |
|
89 | self.set(key, value) | |
89 | return value |
|
90 | return value | |
90 |
|
91 | |||
91 | decorate.set = set_ |
|
92 | decorate.set = set_ | |
92 | decorate.invalidate = invalidate |
|
93 | decorate.invalidate = invalidate | |
93 | decorate.refresh = refresh |
|
94 | decorate.refresh = refresh | |
94 | decorate.get = get |
|
95 | decorate.get = get | |
95 | decorate.original = fn |
|
96 | decorate.original = fn | |
96 | decorate.key_generator = key_generator |
|
97 | decorate.key_generator = key_generator | |
97 | decorate.__wrapped__ = fn |
|
98 | decorate.__wrapped__ = fn | |
98 |
|
99 | |||
99 | return decorate |
|
100 | return decorate | |
100 |
|
101 | |||
101 | return decorator |
|
102 | return decorator | |
102 |
|
103 | |||
103 |
|
104 | |||
104 | def make_region(*arg, **kw): |
|
105 | def make_region(*arg, **kw): | |
105 | return RhodeCodeCacheRegion(*arg, **kw) |
|
106 | return RhodeCodeCacheRegion(*arg, **kw) | |
106 |
|
107 | |||
107 |
|
108 | |||
108 | def get_default_cache_settings(settings, prefixes=None): |
|
109 | def get_default_cache_settings(settings, prefixes=None): | |
109 | prefixes = prefixes or [] |
|
110 | prefixes = prefixes or [] | |
110 | cache_settings = {} |
|
111 | cache_settings = {} | |
111 | for key in settings.keys(): |
|
112 | for key in settings.keys(): | |
112 | for prefix in prefixes: |
|
113 | for prefix in prefixes: | |
113 | if key.startswith(prefix): |
|
114 | if key.startswith(prefix): | |
114 | name = key.split(prefix)[1].strip() |
|
115 | name = key.split(prefix)[1].strip() | |
115 | val = settings[key] |
|
116 | val = settings[key] | |
116 | if isinstance(val, compat.string_types): |
|
117 | if isinstance(val, compat.string_types): | |
117 | val = val.strip() |
|
118 | val = val.strip() | |
118 | cache_settings[name] = val |
|
119 | cache_settings[name] = val | |
119 | return cache_settings |
|
120 | return cache_settings | |
120 |
|
121 | |||
121 |
|
122 | |||
122 | def compute_key_from_params(*args): |
|
123 | def compute_key_from_params(*args): | |
123 | """ |
|
124 | """ | |
124 | Helper to compute key from given params to be used in cache manager |
|
125 | Helper to compute key from given params to be used in cache manager | |
125 | """ |
|
126 | """ | |
126 | return sha1("_".join(map(safe_str, args))) |
|
127 | return sha1("_".join(map(safe_str, args))) | |
127 |
|
128 | |||
128 |
|
129 | |||
129 | def backend_key_generator(backend): |
|
130 | def backend_key_generator(backend): | |
130 | """ |
|
131 | """ | |
131 | Special wrapper that also sends over the backend to the key generator |
|
132 | Special wrapper that also sends over the backend to the key generator | |
132 | """ |
|
133 | """ | |
133 | def wrapper(namespace, fn): |
|
134 | def wrapper(namespace, fn): | |
134 | return key_generator(backend, namespace, fn) |
|
135 | return key_generator(backend, namespace, fn) | |
135 | return wrapper |
|
136 | return wrapper | |
136 |
|
137 | |||
137 |
|
138 | |||
138 | def key_generator(backend, namespace, fn): |
|
139 | def key_generator(backend, namespace, fn): | |
139 | fname = fn.__name__ |
|
140 | fname = fn.__name__ | |
140 |
|
141 | |||
141 | def generate_key(*args): |
|
142 | def generate_key(*args): | |
142 | backend_prefix = getattr(backend, 'key_prefix', None) or 'backend_prefix' |
|
143 | backend_prefix = getattr(backend, 'key_prefix', None) or 'backend_prefix' | |
143 | namespace_pref = namespace or 'default_namespace' |
|
144 | namespace_pref = namespace or 'default_namespace' | |
144 | arg_key = compute_key_from_params(*args) |
|
145 | arg_key = compute_key_from_params(*args) | |
145 | final_key = "{}:{}:{}_{}".format(backend_prefix, namespace_pref, fname, arg_key) |
|
146 | final_key = "{}:{}:{}_{}".format(backend_prefix, namespace_pref, fname, arg_key) | |
146 |
|
147 | |||
147 | return final_key |
|
148 | return final_key | |
148 |
|
149 | |||
149 | return generate_key |
|
150 | return generate_key |
General Comments 0
You need to be logged in to leave comments.
Login now