##// END OF EJS Templates
config: Move vcs hooks protocol and direct call setting to vcs configure mehod.
Martin Bornhold -
r588:741f0635 default
parent child Browse files
Show More
@@ -1,195 +1,180 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 environment configuration
22 Pylons environment configuration
23 """
23 """
24
24
25 import os
25 import os
26 import logging
26 import logging
27 import rhodecode
27 import rhodecode
28 import platform
28 import platform
29 import re
29 import re
30 import io
30 import io
31
31
32 from mako.lookup import TemplateLookup
32 from mako.lookup import TemplateLookup
33 from pylons.configuration import PylonsConfig
33 from pylons.configuration import PylonsConfig
34 from pylons.error import handle_mako_error
34 from pylons.error import handle_mako_error
35 from pyramid.settings import asbool
35 from pyramid.settings import asbool
36
36
37 # don't remove this import it does magic for celery
37 # don't remove this import it does magic for celery
38 from rhodecode.lib import celerypylons # noqa
38 from rhodecode.lib import celerypylons # noqa
39
39
40 import rhodecode.lib.app_globals as app_globals
40 import rhodecode.lib.app_globals as app_globals
41
41
42 from rhodecode.config import utils
42 from rhodecode.config import utils
43 from rhodecode.config.routing import make_map
43 from rhodecode.config.routing import make_map
44 from rhodecode.config.jsroutes import generate_jsroutes_content
44 from rhodecode.config.jsroutes import generate_jsroutes_content
45
45
46 from rhodecode.lib import helpers
46 from rhodecode.lib import helpers
47 from rhodecode.lib.auth import set_available_permissions
47 from rhodecode.lib.auth import set_available_permissions
48 from rhodecode.lib.utils import (
48 from rhodecode.lib.utils import (
49 repo2db_mapper, make_db_config, set_rhodecode_config,
49 repo2db_mapper, make_db_config, set_rhodecode_config,
50 load_rcextensions)
50 load_rcextensions)
51 from rhodecode.lib.utils2 import str2bool, aslist
51 from rhodecode.lib.utils2 import str2bool, aslist
52 from rhodecode.lib.vcs import connect_vcs, start_vcs_server
52 from rhodecode.lib.vcs import connect_vcs, start_vcs_server
53 from rhodecode.model.scm import ScmModel
53 from rhodecode.model.scm import ScmModel
54
54
55 log = logging.getLogger(__name__)
55 log = logging.getLogger(__name__)
56
56
57 def load_environment(global_conf, app_conf, initial=False,
57 def load_environment(global_conf, app_conf, initial=False,
58 test_env=None, test_index=None):
58 test_env=None, test_index=None):
59 """
59 """
60 Configure the Pylons environment via the ``pylons.config``
60 Configure the Pylons environment via the ``pylons.config``
61 object
61 object
62 """
62 """
63 config = PylonsConfig()
63 config = PylonsConfig()
64
64
65
65
66 # Pylons paths
66 # Pylons paths
67 root = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
67 root = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
68 paths = {
68 paths = {
69 'root': root,
69 'root': root,
70 'controllers': os.path.join(root, 'controllers'),
70 'controllers': os.path.join(root, 'controllers'),
71 'static_files': os.path.join(root, 'public'),
71 'static_files': os.path.join(root, 'public'),
72 'templates': [os.path.join(root, 'templates')],
72 'templates': [os.path.join(root, 'templates')],
73 }
73 }
74
74
75 # Initialize config with the basic options
75 # Initialize config with the basic options
76 config.init_app(global_conf, app_conf, package='rhodecode', paths=paths)
76 config.init_app(global_conf, app_conf, package='rhodecode', paths=paths)
77
77
78 # store some globals into rhodecode
78 # store some globals into rhodecode
79 rhodecode.CELERY_ENABLED = str2bool(config['app_conf'].get('use_celery'))
79 rhodecode.CELERY_ENABLED = str2bool(config['app_conf'].get('use_celery'))
80 rhodecode.CELERY_EAGER = str2bool(
80 rhodecode.CELERY_EAGER = str2bool(
81 config['app_conf'].get('celery.always.eager'))
81 config['app_conf'].get('celery.always.eager'))
82
82
83 config['routes.map'] = make_map(config)
83 config['routes.map'] = make_map(config)
84
84
85 if asbool(config.get('generate_js_files', 'false')):
85 if asbool(config.get('generate_js_files', 'false')):
86 jsroutes = config['routes.map'].jsroutes()
86 jsroutes = config['routes.map'].jsroutes()
87 jsroutes_file_content = generate_jsroutes_content(jsroutes)
87 jsroutes_file_content = generate_jsroutes_content(jsroutes)
88 jsroutes_file_path = os.path.join(
88 jsroutes_file_path = os.path.join(
89 paths['static_files'], 'js', 'rhodecode', 'routes.js')
89 paths['static_files'], 'js', 'rhodecode', 'routes.js')
90
90
91 with io.open(jsroutes_file_path, 'w', encoding='utf-8') as f:
91 with io.open(jsroutes_file_path, 'w', encoding='utf-8') as f:
92 f.write(jsroutes_file_content)
92 f.write(jsroutes_file_content)
93
93
94 config['pylons.app_globals'] = app_globals.Globals(config)
94 config['pylons.app_globals'] = app_globals.Globals(config)
95 config['pylons.h'] = helpers
95 config['pylons.h'] = helpers
96 rhodecode.CONFIG = config
96 rhodecode.CONFIG = config
97
97
98 load_rcextensions(root_path=config['here'])
98 load_rcextensions(root_path=config['here'])
99
99
100 # Setup cache object as early as possible
100 # Setup cache object as early as possible
101 import pylons
101 import pylons
102 pylons.cache._push_object(config['pylons.app_globals'].cache)
102 pylons.cache._push_object(config['pylons.app_globals'].cache)
103
103
104 # Create the Mako TemplateLookup, with the default auto-escaping
104 # Create the Mako TemplateLookup, with the default auto-escaping
105 config['pylons.app_globals'].mako_lookup = TemplateLookup(
105 config['pylons.app_globals'].mako_lookup = TemplateLookup(
106 directories=paths['templates'],
106 directories=paths['templates'],
107 error_handler=handle_mako_error,
107 error_handler=handle_mako_error,
108 module_directory=os.path.join(app_conf['cache_dir'], 'templates'),
108 module_directory=os.path.join(app_conf['cache_dir'], 'templates'),
109 input_encoding='utf-8', default_filters=['escape'],
109 input_encoding='utf-8', default_filters=['escape'],
110 imports=['from webhelpers.html import escape'])
110 imports=['from webhelpers.html import escape'])
111
111
112 # sets the c attribute access when don't existing attribute are accessed
112 # sets the c attribute access when don't existing attribute are accessed
113 config['pylons.strict_tmpl_context'] = True
113 config['pylons.strict_tmpl_context'] = True
114
114
115 # configure channelstream
115 # configure channelstream
116 config['channelstream_config'] = {
116 config['channelstream_config'] = {
117 'enabled': asbool(config.get('channelstream.enabled', False)),
117 'enabled': asbool(config.get('channelstream.enabled', False)),
118 'server': config.get('channelstream.server'),
118 'server': config.get('channelstream.server'),
119 'secret': config.get('channelstream.secret')
119 'secret': config.get('channelstream.secret')
120 }
120 }
121
121
122 set_available_permissions(config)
122 set_available_permissions(config)
123 db_cfg = make_db_config(clear_session=True)
123 db_cfg = make_db_config(clear_session=True)
124
124
125 repos_path = list(db_cfg.items('paths'))[0][1]
125 repos_path = list(db_cfg.items('paths'))[0][1]
126 config['base_path'] = repos_path
126 config['base_path'] = repos_path
127
127
128 config['vcs.hooks.direct_calls'] = _use_direct_hook_calls(config)
129 config['vcs.hooks.protocol'] = _get_vcs_hooks_protocol(config)
130
131 # store db config also in main global CONFIG
128 # store db config also in main global CONFIG
132 set_rhodecode_config(config)
129 set_rhodecode_config(config)
133
130
134 # configure instance id
131 # configure instance id
135 utils.set_instance_id(config)
132 utils.set_instance_id(config)
136
133
137 # CONFIGURATION OPTIONS HERE (note: all config options will override
134 # CONFIGURATION OPTIONS HERE (note: all config options will override
138 # any Pylons config options)
135 # any Pylons config options)
139
136
140 # store config reference into our module to skip import magic of pylons
137 # store config reference into our module to skip import magic of pylons
141 rhodecode.CONFIG.update(config)
138 rhodecode.CONFIG.update(config)
142
139
143 utils.configure_pyro4(config)
140 utils.configure_pyro4(config)
144 return config
141 return config
145
142
146
143
147 def _use_direct_hook_calls(config):
148 default_direct_hook_calls = 'false'
149 direct_hook_calls = str2bool(
150 config.get('vcs.hooks.direct_calls', default_direct_hook_calls))
151 return direct_hook_calls
152
153
154 def _get_vcs_hooks_protocol(config):
155 protocol = config.get('vcs.hooks.protocol', 'pyro4').lower()
156 return protocol
157
158
159 def load_pyramid_environment(global_config, settings):
144 def load_pyramid_environment(global_config, settings):
160 # Some parts of the code expect a merge of global and app settings.
145 # Some parts of the code expect a merge of global and app settings.
161 settings_merged = global_config.copy()
146 settings_merged = global_config.copy()
162 settings_merged.update(settings)
147 settings_merged.update(settings)
163
148
164 # If this is a test run we prepare the test environment like
149 # If this is a test run we prepare the test environment like
165 # creating a test database, test search index and test repositories.
150 # creating a test database, test search index and test repositories.
166 # This has to be done before the database connection is initialized.
151 # This has to be done before the database connection is initialized.
167 if settings['is_test']:
152 if settings['is_test']:
168 rhodecode.is_test = True
153 rhodecode.is_test = True
169 utils.initialize_test_environment(settings_merged)
154 utils.initialize_test_environment(settings_merged)
170
155
171 # Initialize the database connection.
156 # Initialize the database connection.
172 utils.initialize_database(settings_merged)
157 utils.initialize_database(settings_merged)
173
158
174 # Limit backends to `vcs.backends` from configuration
159 # Limit backends to `vcs.backends` from configuration
175 for alias in rhodecode.BACKENDS.keys():
160 for alias in rhodecode.BACKENDS.keys():
176 if alias not in settings['vcs.backends']:
161 if alias not in settings['vcs.backends']:
177 del rhodecode.BACKENDS[alias]
162 del rhodecode.BACKENDS[alias]
178 log.info('Enabled VCS backends: %s', rhodecode.BACKENDS.keys())
163 log.info('Enabled VCS backends: %s', rhodecode.BACKENDS.keys())
179
164
180 # initialize vcs client and optionally run the server if enabled
165 # initialize vcs client and optionally run the server if enabled
181 vcs_server_uri = settings['vcs.server']
166 vcs_server_uri = settings['vcs.server']
182 vcs_server_enabled = settings['vcs.server.enable']
167 vcs_server_enabled = settings['vcs.server.enable']
183 start_server = (
168 start_server = (
184 settings['vcs.start_server'] and
169 settings['vcs.start_server'] and
185 not int(os.environ.get('RC_VCSSERVER_TEST_DISABLE', '0')))
170 not int(os.environ.get('RC_VCSSERVER_TEST_DISABLE', '0')))
186
171
187 if vcs_server_enabled and start_server:
172 if vcs_server_enabled and start_server:
188 log.info("Starting vcsserver")
173 log.info("Starting vcsserver")
189 start_vcs_server(server_and_port=vcs_server_uri,
174 start_vcs_server(server_and_port=vcs_server_uri,
190 protocol=utils.get_vcs_server_protocol(settings),
175 protocol=utils.get_vcs_server_protocol(settings),
191 log_level=settings['vcs.server.log_level'])
176 log_level=settings['vcs.server.log_level'])
192
177
193 utils.configure_vcs(settings)
178 utils.configure_vcs(settings)
194 if vcs_server_enabled:
179 if vcs_server_enabled:
195 connect_vcs(vcs_server_uri, utils.get_vcs_server_protocol(settings))
180 connect_vcs(vcs_server_uri, utils.get_vcs_server_protocol(settings))
@@ -1,101 +1,98 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 import os
21 import os
22 import shlex
22 import shlex
23 import Pyro4
23 import Pyro4
24 import platform
24 import platform
25
25
26 from rhodecode.model import init_model
26 from rhodecode.model import init_model
27
27
28
28
29 def configure_pyro4(config):
29 def configure_pyro4(config):
30 """
30 """
31 Configure Pyro4 based on `config`.
31 Configure Pyro4 based on `config`.
32
32
33 This will mainly set the different configuration parameters of the Pyro4
33 This will mainly set the different configuration parameters of the Pyro4
34 library based on the settings in our INI files. The Pyro4 documentation
34 library based on the settings in our INI files. The Pyro4 documentation
35 lists more details about the specific settings and their meaning.
35 lists more details about the specific settings and their meaning.
36 """
36 """
37 Pyro4.config.COMMTIMEOUT = float(config['vcs.connection_timeout'])
37 Pyro4.config.COMMTIMEOUT = float(config['vcs.connection_timeout'])
38 Pyro4.config.SERIALIZER = 'pickle'
38 Pyro4.config.SERIALIZER = 'pickle'
39 Pyro4.config.SERIALIZERS_ACCEPTED.add('pickle')
39 Pyro4.config.SERIALIZERS_ACCEPTED.add('pickle')
40
40
41 # Note: We need server configuration in the WSGI processes
41 # Note: We need server configuration in the WSGI processes
42 # because we provide a callback server in certain vcs operations.
42 # because we provide a callback server in certain vcs operations.
43 Pyro4.config.SERVERTYPE = "multiplex"
43 Pyro4.config.SERVERTYPE = "multiplex"
44 Pyro4.config.POLLTIMEOUT = 0.01
44 Pyro4.config.POLLTIMEOUT = 0.01
45
45
46
46
47 def configure_vcs(config):
47 def configure_vcs(config):
48 """
48 """
49 Patch VCS config with some RhodeCode specific stuff
49 Patch VCS config with some RhodeCode specific stuff
50 """
50 """
51 from rhodecode.lib.vcs import conf
51 from rhodecode.lib.vcs import conf
52 from rhodecode.lib.utils2 import aslist
53 conf.settings.BACKENDS = {
52 conf.settings.BACKENDS = {
54 'hg': 'rhodecode.lib.vcs.backends.hg.MercurialRepository',
53 'hg': 'rhodecode.lib.vcs.backends.hg.MercurialRepository',
55 'git': 'rhodecode.lib.vcs.backends.git.GitRepository',
54 'git': 'rhodecode.lib.vcs.backends.git.GitRepository',
56 'svn': 'rhodecode.lib.vcs.backends.svn.SubversionRepository',
55 'svn': 'rhodecode.lib.vcs.backends.svn.SubversionRepository',
57 }
56 }
58
57
59 conf.settings.GIT_REV_FILTER = shlex.split(
58 conf.settings.HOOKS_PROTOCOL = config['vcs.hooks.protocol']
60 config.get('git_rev_filter', '--all').strip())
59 conf.settings.HOOKS_DIRECT_CALLS = config['vcs.hooks.direct_calls']
61 conf.settings.DEFAULT_ENCODINGS = aslist(config.get('default_encoding',
60 conf.settings.GIT_REV_FILTER = shlex.split(config['git_rev_filter'])
62 'UTF-8'), sep=',')
61 conf.settings.DEFAULT_ENCODINGS = config['default_encoding']
63 conf.settings.ALIASES[:] = config.get('vcs.backends')
62 conf.settings.ALIASES[:] = config['vcs.backends']
64 conf.settings.SVN_COMPATIBLE_VERSION = config.get(
63 conf.settings.SVN_COMPATIBLE_VERSION = config['vcs.svn.compatible_version']
65 'vcs.svn.compatible_version')
66
64
67
65
68 def initialize_database(config):
66 def initialize_database(config):
69 from rhodecode.lib.utils2 import engine_from_config, get_encryption_key
67 from rhodecode.lib.utils2 import engine_from_config, get_encryption_key
70 engine = engine_from_config(config, 'sqlalchemy.db1.')
68 engine = engine_from_config(config, 'sqlalchemy.db1.')
71 init_model(engine, encryption_key=get_encryption_key(config))
69 init_model(engine, encryption_key=get_encryption_key(config))
72
70
73
71
74 def initialize_test_environment(settings, test_env=None):
72 def initialize_test_environment(settings, test_env=None):
75 if test_env is None:
73 if test_env is None:
76 test_env = not int(os.environ.get('RC_NO_TMP_PATH', 0))
74 test_env = not int(os.environ.get('RC_NO_TMP_PATH', 0))
77
75
78 from rhodecode.lib.utils import (
76 from rhodecode.lib.utils import (
79 create_test_directory, create_test_database, create_test_repositories,
77 create_test_directory, create_test_database, create_test_repositories,
80 create_test_index)
78 create_test_index)
81 from rhodecode.tests import TESTS_TMP_PATH
79 from rhodecode.tests import TESTS_TMP_PATH
82 # test repos
80 # test repos
83 if test_env:
81 if test_env:
84 create_test_directory(TESTS_TMP_PATH)
82 create_test_directory(TESTS_TMP_PATH)
85 create_test_database(TESTS_TMP_PATH, settings)
83 create_test_database(TESTS_TMP_PATH, settings)
86 create_test_repositories(TESTS_TMP_PATH, settings)
84 create_test_repositories(TESTS_TMP_PATH, settings)
87 create_test_index(TESTS_TMP_PATH, settings)
85 create_test_index(TESTS_TMP_PATH, settings)
88
86
89
87
90 def get_vcs_server_protocol(config):
88 def get_vcs_server_protocol(config):
91 protocol = config.get('vcs.server.protocol', 'pyro4')
89 return config['vcs.server.protocol']
92 return protocol
93
90
94
91
95 def set_instance_id(config):
92 def set_instance_id(config):
96 """ Sets a dynamic generated config['instance_id'] if missing or '*' """
93 """ Sets a dynamic generated config['instance_id'] if missing or '*' """
97
94
98 config['instance_id'] = config.get('instance_id') or ''
95 config['instance_id'] = config.get('instance_id') or ''
99 if config['instance_id'] == '*' or not config['instance_id']:
96 if config['instance_id'] == '*' or not config['instance_id']:
100 _platform_id = platform.uname()[1] or 'instance'
97 _platform_id = platform.uname()[1] or 'instance'
101 config['instance_id'] = '%s-%s' % (_platform_id, os.getpid())
98 config['instance_id'] = '%s-%s' % (_platform_id, os.getpid())
General Comments 0
You need to be logged in to leave comments. Login now