##// END OF EJS Templates
db: Change ordering of test env setup and db connection init....
johbo -
r117:25511505 default
parent child Browse files
Show More
@@ -1,323 +1,324 b''
1 # -*- coding: utf-8 -*-
1 # -*- coding: utf-8 -*-
2
2
3 # Copyright (C) 2010-2016 RhodeCode GmbH
3 # Copyright (C) 2010-2016 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
20
21 """
21 """
22 Pylons middleware initialization
22 Pylons middleware initialization
23 """
23 """
24 import logging
24 import logging
25
25
26 from paste.registry import RegistryManager
26 from paste.registry import RegistryManager
27 from paste.gzipper import make_gzip_middleware
27 from paste.gzipper import make_gzip_middleware
28 from pylons.middleware import ErrorHandler, StatusCodeRedirect
28 from pylons.middleware import ErrorHandler, StatusCodeRedirect
29 from pylons.wsgiapp import PylonsApp
29 from pylons.wsgiapp import PylonsApp
30 from pyramid.authorization import ACLAuthorizationPolicy
30 from pyramid.authorization import ACLAuthorizationPolicy
31 from pyramid.config import Configurator
31 from pyramid.config import Configurator
32 from pyramid.static import static_view
32 from pyramid.static import static_view
33 from pyramid.settings import asbool, aslist
33 from pyramid.settings import asbool, aslist
34 from pyramid.wsgi import wsgiapp
34 from pyramid.wsgi import wsgiapp
35 from routes.middleware import RoutesMiddleware
35 from routes.middleware import RoutesMiddleware
36 import routes.util
36 import routes.util
37
37
38 import rhodecode
38 import rhodecode
39 from rhodecode.config import patches, utils
39 from rhodecode.config import patches, utils
40 from rhodecode.config.environment import load_environment
40 from rhodecode.config.environment import load_environment
41 from rhodecode.lib.middleware import csrf
41 from rhodecode.lib.middleware import csrf
42 from rhodecode.lib.middleware.appenlight import wrap_in_appenlight_if_enabled
42 from rhodecode.lib.middleware.appenlight import wrap_in_appenlight_if_enabled
43 from rhodecode.lib.middleware.disable_vcs import DisableVCSPagesWrapper
43 from rhodecode.lib.middleware.disable_vcs import DisableVCSPagesWrapper
44 from rhodecode.lib.middleware.https_fixup import HttpsFixup
44 from rhodecode.lib.middleware.https_fixup import HttpsFixup
45 from rhodecode.lib.middleware.vcs import VCSMiddleware
45 from rhodecode.lib.middleware.vcs import VCSMiddleware
46 from rhodecode.lib.plugins.utils import register_rhodecode_plugin
46 from rhodecode.lib.plugins.utils import register_rhodecode_plugin
47
47
48
48
49 log = logging.getLogger(__name__)
49 log = logging.getLogger(__name__)
50
50
51
51
52 def make_app(global_conf, full_stack=True, static_files=True, **app_conf):
52 def make_app(global_conf, full_stack=True, static_files=True, **app_conf):
53 """Create a Pylons WSGI application and return it
53 """Create a Pylons WSGI application and return it
54
54
55 ``global_conf``
55 ``global_conf``
56 The inherited configuration for this application. Normally from
56 The inherited configuration for this application. Normally from
57 the [DEFAULT] section of the Paste ini file.
57 the [DEFAULT] section of the Paste ini file.
58
58
59 ``full_stack``
59 ``full_stack``
60 Whether or not this application provides a full WSGI stack (by
60 Whether or not this application provides a full WSGI stack (by
61 default, meaning it handles its own exceptions and errors).
61 default, meaning it handles its own exceptions and errors).
62 Disable full_stack when this application is "managed" by
62 Disable full_stack when this application is "managed" by
63 another WSGI middleware.
63 another WSGI middleware.
64
64
65 ``app_conf``
65 ``app_conf``
66 The application's local configuration. Normally specified in
66 The application's local configuration. Normally specified in
67 the [app:<name>] section of the Paste ini file (where <name>
67 the [app:<name>] section of the Paste ini file (where <name>
68 defaults to main).
68 defaults to main).
69
69
70 """
70 """
71 # Apply compatibility patches
71 # Apply compatibility patches
72 patches.kombu_1_5_1_python_2_7_11()
72 patches.kombu_1_5_1_python_2_7_11()
73 patches.inspect_getargspec()
73 patches.inspect_getargspec()
74
74
75 # Configure the Pylons environment
75 # Configure the Pylons environment
76 config = load_environment(global_conf, app_conf)
76 config = load_environment(global_conf, app_conf)
77
77
78 # The Pylons WSGI app
78 # The Pylons WSGI app
79 app = PylonsApp(config=config)
79 app = PylonsApp(config=config)
80 if rhodecode.is_test:
80 if rhodecode.is_test:
81 app = csrf.CSRFDetector(app)
81 app = csrf.CSRFDetector(app)
82
82
83 expected_origin = config.get('expected_origin')
83 expected_origin = config.get('expected_origin')
84 if expected_origin:
84 if expected_origin:
85 # The API can be accessed from other Origins.
85 # The API can be accessed from other Origins.
86 app = csrf.OriginChecker(app, expected_origin,
86 app = csrf.OriginChecker(app, expected_origin,
87 skip_urls=[routes.util.url_for('api')])
87 skip_urls=[routes.util.url_for('api')])
88
88
89 # Add RoutesMiddleware. Currently we have two instances in the stack. This
89 # Add RoutesMiddleware. Currently we have two instances in the stack. This
90 # is the lower one to make the StatusCodeRedirect middleware happy.
90 # is the lower one to make the StatusCodeRedirect middleware happy.
91 # TODO: johbo: This is not optimal, search for a better solution.
91 # TODO: johbo: This is not optimal, search for a better solution.
92 app = RoutesMiddleware(app, config['routes.map'])
92 app = RoutesMiddleware(app, config['routes.map'])
93
93
94 # CUSTOM MIDDLEWARE HERE (filtered by error handling middlewares)
94 # CUSTOM MIDDLEWARE HERE (filtered by error handling middlewares)
95 if asbool(config['pdebug']):
95 if asbool(config['pdebug']):
96 from rhodecode.lib.profiler import ProfilingMiddleware
96 from rhodecode.lib.profiler import ProfilingMiddleware
97 app = ProfilingMiddleware(app)
97 app = ProfilingMiddleware(app)
98
98
99 # Protect from VCS Server error related pages when server is not available
99 # Protect from VCS Server error related pages when server is not available
100 vcs_server_enabled = asbool(config.get('vcs.server.enable', 'true'))
100 vcs_server_enabled = asbool(config.get('vcs.server.enable', 'true'))
101 if not vcs_server_enabled:
101 if not vcs_server_enabled:
102 app = DisableVCSPagesWrapper(app)
102 app = DisableVCSPagesWrapper(app)
103
103
104 if asbool(full_stack):
104 if asbool(full_stack):
105
105
106 # Appenlight monitoring and error handler
106 # Appenlight monitoring and error handler
107 app, appenlight_client = wrap_in_appenlight_if_enabled(app, config)
107 app, appenlight_client = wrap_in_appenlight_if_enabled(app, config)
108
108
109 # Handle Python exceptions
109 # Handle Python exceptions
110 app = ErrorHandler(app, global_conf, **config['pylons.errorware'])
110 app = ErrorHandler(app, global_conf, **config['pylons.errorware'])
111
111
112 # we want our low level middleware to get to the request ASAP. We don't
112 # we want our low level middleware to get to the request ASAP. We don't
113 # need any pylons stack middleware in them
113 # need any pylons stack middleware in them
114 app = VCSMiddleware(app, config, appenlight_client)
114 app = VCSMiddleware(app, config, appenlight_client)
115 # Display error documents for 401, 403, 404 status codes (and
115 # Display error documents for 401, 403, 404 status codes (and
116 # 500 when debug is disabled)
116 # 500 when debug is disabled)
117 if asbool(config['debug']):
117 if asbool(config['debug']):
118 app = StatusCodeRedirect(app)
118 app = StatusCodeRedirect(app)
119 else:
119 else:
120 app = StatusCodeRedirect(app, [400, 401, 403, 404, 500])
120 app = StatusCodeRedirect(app, [400, 401, 403, 404, 500])
121
121
122 # enable https redirects based on HTTP_X_URL_SCHEME set by proxy
122 # enable https redirects based on HTTP_X_URL_SCHEME set by proxy
123 app = HttpsFixup(app, config)
123 app = HttpsFixup(app, config)
124
124
125 # Establish the Registry for this application
125 # Establish the Registry for this application
126 app = RegistryManager(app)
126 app = RegistryManager(app)
127
127
128 app.config = config
128 app.config = config
129
129
130 return app
130 return app
131
131
132
132
133 def make_pyramid_app(global_config, **settings):
133 def make_pyramid_app(global_config, **settings):
134 """
134 """
135 Constructs the WSGI application based on Pyramid and wraps the Pylons based
135 Constructs the WSGI application based on Pyramid and wraps the Pylons based
136 application.
136 application.
137
137
138 Specials:
138 Specials:
139
139
140 * We migrate from Pylons to Pyramid. While doing this, we keep both
140 * We migrate from Pylons to Pyramid. While doing this, we keep both
141 frameworks functional. This involves moving some WSGI middlewares around
141 frameworks functional. This involves moving some WSGI middlewares around
142 and providing access to some data internals, so that the old code is
142 and providing access to some data internals, so that the old code is
143 still functional.
143 still functional.
144
144
145 * The application can also be integrated like a plugin via the call to
145 * The application can also be integrated like a plugin via the call to
146 `includeme`. This is accompanied with the other utility functions which
146 `includeme`. This is accompanied with the other utility functions which
147 are called. Changing this should be done with great care to not break
147 are called. Changing this should be done with great care to not break
148 cases when these fragments are assembled from another place.
148 cases when these fragments are assembled from another place.
149
149
150 """
150 """
151 # The edition string should be available in pylons too, so we add it here
151 # The edition string should be available in pylons too, so we add it here
152 # before copying the settings.
152 # before copying the settings.
153 settings.setdefault('rhodecode.edition', 'Community Edition')
153 settings.setdefault('rhodecode.edition', 'Community Edition')
154
154
155 # As long as our Pylons application does expect "unprepared" settings, make
155 # As long as our Pylons application does expect "unprepared" settings, make
156 # sure that we keep an unmodified copy. This avoids unintentional change of
156 # sure that we keep an unmodified copy. This avoids unintentional change of
157 # behavior in the old application.
157 # behavior in the old application.
158 settings_pylons = settings.copy()
158 settings_pylons = settings.copy()
159
159
160 # Some parts of the code expect a merge of global and app settings.
160 # Some parts of the code expect a merge of global and app settings.
161 settings_merged = global_config.copy()
161 settings_merged = global_config.copy()
162 settings_merged.update(settings)
162 settings_merged.update(settings)
163
163
164 sanitize_settings_and_apply_defaults(settings)
164 sanitize_settings_and_apply_defaults(settings)
165 config = Configurator(settings=settings)
165 config = Configurator(settings=settings)
166 add_pylons_compat_data(config.registry, global_config, settings_pylons)
166 add_pylons_compat_data(config.registry, global_config, settings_pylons)
167
167
168 # If this is a test run we prepare the test environment like
169 # creating a test database, test search index and test repositories.
170 # This has to be done before the database connection is initialized.
171 if settings['is_test']:
172 utils.initialize_test_environment(settings_merged)
173
168 # Initialize the database connection.
174 # Initialize the database connection.
169 utils.initialize_database(settings_merged)
175 utils.initialize_database(settings_merged)
170
176
171 # If this is a test run we prepare the test environment like
172 # creating a test database, test search index and test repositories.
173 if settings['is_test']:
174 utils.initialize_test_environment(settings_merged)
175
176 includeme(config)
177 includeme(config)
177 includeme_last(config)
178 includeme_last(config)
178 pyramid_app = config.make_wsgi_app()
179 pyramid_app = config.make_wsgi_app()
179 pyramid_app = wrap_app_in_wsgi_middlewares(pyramid_app, config)
180 pyramid_app = wrap_app_in_wsgi_middlewares(pyramid_app, config)
180 return pyramid_app
181 return pyramid_app
181
182
182
183
183 def add_pylons_compat_data(registry, global_config, settings):
184 def add_pylons_compat_data(registry, global_config, settings):
184 """
185 """
185 Attach data to the registry to support the Pylons integration.
186 Attach data to the registry to support the Pylons integration.
186 """
187 """
187 registry._pylons_compat_global_config = global_config
188 registry._pylons_compat_global_config = global_config
188 registry._pylons_compat_settings = settings
189 registry._pylons_compat_settings = settings
189
190
190
191
191 def includeme(config):
192 def includeme(config):
192 settings = config.registry.settings
193 settings = config.registry.settings
193
194
194 # Includes which are required. The application would fail without them.
195 # Includes which are required. The application would fail without them.
195 config.include('pyramid_mako')
196 config.include('pyramid_mako')
196 config.include('pyramid_beaker')
197 config.include('pyramid_beaker')
197 config.include('rhodecode.authentication')
198 config.include('rhodecode.authentication')
198 config.include('rhodecode.login')
199 config.include('rhodecode.login')
199 config.include('rhodecode.tweens')
200 config.include('rhodecode.tweens')
200 config.include('rhodecode.api')
201 config.include('rhodecode.api')
201
202
202 # Set the authorization policy.
203 # Set the authorization policy.
203 authz_policy = ACLAuthorizationPolicy()
204 authz_policy = ACLAuthorizationPolicy()
204 config.set_authorization_policy(authz_policy)
205 config.set_authorization_policy(authz_policy)
205
206
206 # Set the default renderer for HTML templates to mako.
207 # Set the default renderer for HTML templates to mako.
207 config.add_mako_renderer('.html')
208 config.add_mako_renderer('.html')
208
209
209 # plugin information
210 # plugin information
210 config.registry.rhodecode_plugins = {}
211 config.registry.rhodecode_plugins = {}
211
212
212 config.add_directive(
213 config.add_directive(
213 'register_rhodecode_plugin', register_rhodecode_plugin)
214 'register_rhodecode_plugin', register_rhodecode_plugin)
214 # include RhodeCode plugins
215 # include RhodeCode plugins
215 includes = aslist(settings.get('rhodecode.includes', []))
216 includes = aslist(settings.get('rhodecode.includes', []))
216 for inc in includes:
217 for inc in includes:
217 config.include(inc)
218 config.include(inc)
218
219
219 # This is the glue which allows us to migrate in chunks. By registering the
220 # This is the glue which allows us to migrate in chunks. By registering the
220 # pylons based application as the "Not Found" view in Pyramid, we will
221 # pylons based application as the "Not Found" view in Pyramid, we will
221 # fallback to the old application each time the new one does not yet know
222 # fallback to the old application each time the new one does not yet know
222 # how to handle a request.
223 # how to handle a request.
223 pylons_app = make_app(
224 pylons_app = make_app(
224 config.registry._pylons_compat_global_config,
225 config.registry._pylons_compat_global_config,
225 **config.registry._pylons_compat_settings)
226 **config.registry._pylons_compat_settings)
226 config.registry._pylons_compat_config = pylons_app.config
227 config.registry._pylons_compat_config = pylons_app.config
227 pylons_app_as_view = wsgiapp(pylons_app)
228 pylons_app_as_view = wsgiapp(pylons_app)
228 config.add_notfound_view(pylons_app_as_view)
229 config.add_notfound_view(pylons_app_as_view)
229
230
230
231
231 def includeme_last(config):
232 def includeme_last(config):
232 """
233 """
233 The static file catchall needs to be last in the view configuration.
234 The static file catchall needs to be last in the view configuration.
234 """
235 """
235 settings = config.registry.settings
236 settings = config.registry.settings
236
237
237 # Note: johbo: I would prefer to register a prefix for static files at some
238 # Note: johbo: I would prefer to register a prefix for static files at some
238 # point, e.g. move them under '_static/'. This would fully avoid that we
239 # point, e.g. move them under '_static/'. This would fully avoid that we
239 # can have name clashes with a repository name. Imaging someone calling his
240 # can have name clashes with a repository name. Imaging someone calling his
240 # repo "css" ;-) Also having an external web server to serve out the static
241 # repo "css" ;-) Also having an external web server to serve out the static
241 # files seems to be easier to set up if they have a common prefix.
242 # files seems to be easier to set up if they have a common prefix.
242 #
243 #
243 # Example: config.add_static_view('_static', path='rhodecode:public')
244 # Example: config.add_static_view('_static', path='rhodecode:public')
244 #
245 #
245 # It might be an option to register both paths for a while and then migrate
246 # It might be an option to register both paths for a while and then migrate
246 # over to the new location.
247 # over to the new location.
247
248
248 # Serving static files with a catchall.
249 # Serving static files with a catchall.
249 if settings['static_files']:
250 if settings['static_files']:
250 config.add_route('catchall_static', '/*subpath')
251 config.add_route('catchall_static', '/*subpath')
251 config.add_view(
252 config.add_view(
252 static_view('rhodecode:public'), route_name='catchall_static')
253 static_view('rhodecode:public'), route_name='catchall_static')
253
254
254
255
255 def wrap_app_in_wsgi_middlewares(pyramid_app, config):
256 def wrap_app_in_wsgi_middlewares(pyramid_app, config):
256 """
257 """
257 Apply outer WSGI middlewares around the application.
258 Apply outer WSGI middlewares around the application.
258
259
259 Part of this has been moved up from the Pylons layer, so that the
260 Part of this has been moved up from the Pylons layer, so that the
260 data is also available if old Pylons code is hit through an already ported
261 data is also available if old Pylons code is hit through an already ported
261 view.
262 view.
262 """
263 """
263 settings = config.registry.settings
264 settings = config.registry.settings
264
265
265 # Add RoutesMiddleware. Currently we have two instances in the stack. This
266 # Add RoutesMiddleware. Currently we have two instances in the stack. This
266 # is the upper one to support the pylons compatibility tween during
267 # is the upper one to support the pylons compatibility tween during
267 # migration to pyramid.
268 # migration to pyramid.
268 pyramid_app = RoutesMiddleware(
269 pyramid_app = RoutesMiddleware(
269 pyramid_app, config.registry._pylons_compat_config['routes.map'])
270 pyramid_app, config.registry._pylons_compat_config['routes.map'])
270
271
271 # TODO: johbo: Don't really see why we enable the gzip middleware when
272 # TODO: johbo: Don't really see why we enable the gzip middleware when
272 # serving static files, might be something that should have its own setting
273 # serving static files, might be something that should have its own setting
273 # as well?
274 # as well?
274 if settings['static_files']:
275 if settings['static_files']:
275 pyramid_app = make_gzip_middleware(
276 pyramid_app = make_gzip_middleware(
276 pyramid_app, settings, compress_level=1)
277 pyramid_app, settings, compress_level=1)
277
278
278 return pyramid_app
279 return pyramid_app
279
280
280
281
281 def sanitize_settings_and_apply_defaults(settings):
282 def sanitize_settings_and_apply_defaults(settings):
282 """
283 """
283 Applies settings defaults and does all type conversion.
284 Applies settings defaults and does all type conversion.
284
285
285 We would move all settings parsing and preparation into this place, so that
286 We would move all settings parsing and preparation into this place, so that
286 we have only one place left which deals with this part. The remaining parts
287 we have only one place left which deals with this part. The remaining parts
287 of the application would start to rely fully on well prepared settings.
288 of the application would start to rely fully on well prepared settings.
288
289
289 This piece would later be split up per topic to avoid a big fat monster
290 This piece would later be split up per topic to avoid a big fat monster
290 function.
291 function.
291 """
292 """
292
293
293 # Pyramid's mako renderer has to search in the templates folder so that the
294 # Pyramid's mako renderer has to search in the templates folder so that the
294 # old templates still work. Ported and new templates are expected to use
295 # old templates still work. Ported and new templates are expected to use
295 # real asset specifications for the includes.
296 # real asset specifications for the includes.
296 mako_directories = settings.setdefault('mako.directories', [
297 mako_directories = settings.setdefault('mako.directories', [
297 # Base templates of the original Pylons application
298 # Base templates of the original Pylons application
298 'rhodecode:templates',
299 'rhodecode:templates',
299 ])
300 ])
300 log.debug(
301 log.debug(
301 "Using the following Mako template directories: %s",
302 "Using the following Mako template directories: %s",
302 mako_directories)
303 mako_directories)
303
304
304 # Default includes, possible to change as a user
305 # Default includes, possible to change as a user
305 pyramid_includes = settings.setdefault('pyramid.includes', [
306 pyramid_includes = settings.setdefault('pyramid.includes', [
306 'rhodecode.lib.middleware.request_wrapper',
307 'rhodecode.lib.middleware.request_wrapper',
307 ])
308 ])
308 log.debug(
309 log.debug(
309 "Using the following pyramid.includes: %s",
310 "Using the following pyramid.includes: %s",
310 pyramid_includes)
311 pyramid_includes)
311
312
312 # TODO: johbo: Re-think this, usually the call to config.include
313 # TODO: johbo: Re-think this, usually the call to config.include
313 # should allow to pass in a prefix.
314 # should allow to pass in a prefix.
314 settings.setdefault('rhodecode.api.url', '/_admin/api')
315 settings.setdefault('rhodecode.api.url', '/_admin/api')
315
316
316 _bool_setting(settings, 'vcs.server.enable', 'true')
317 _bool_setting(settings, 'vcs.server.enable', 'true')
317 _bool_setting(settings, 'static_files', 'true')
318 _bool_setting(settings, 'static_files', 'true')
318
319
319 return settings
320 return settings
320
321
321
322
322 def _bool_setting(settings, name, default):
323 def _bool_setting(settings, name, default):
323 settings[name] = asbool(settings.get(name, default))
324 settings[name] = asbool(settings.get(name, default))
General Comments 0
You need to be logged in to leave comments. Login now