##// END OF EJS Templates
config: remove unused DbManage import and adjust other missing imports
Mads Kiilerich -
r6565:80c30110 default
parent child Browse files
Show More
@@ -1,196 +1,195 b''
1 # -*- coding: utf-8 -*-
1 # -*- coding: utf-8 -*-
2 # This program is free software: you can redistribute it and/or modify
2 # This program is free software: you can redistribute it and/or modify
3 # it under the terms of the GNU General Public License as published by
3 # it under the terms of the GNU General Public License as published by
4 # the Free Software Foundation, either version 3 of the License, or
4 # the Free Software Foundation, either version 3 of the License, or
5 # (at your option) any later version.
5 # (at your option) any later version.
6 #
6 #
7 # This program is distributed in the hope that it will be useful,
7 # This program is distributed in the hope that it will be useful,
8 # but WITHOUT ANY WARRANTY; without even the implied warranty of
8 # but WITHOUT ANY WARRANTY; without even the implied warranty of
9 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 # GNU General Public License for more details.
10 # GNU General Public License for more details.
11 #
11 #
12 # You should have received a copy of the GNU General Public License
12 # You should have received a copy of the GNU General Public License
13 # along with this program. If not, see <http://www.gnu.org/licenses/>.
13 # along with this program. If not, see <http://www.gnu.org/licenses/>.
14 """
14 """
15 Global configuration file for TurboGears2 specific settings in Kallithea.
15 Global configuration file for TurboGears2 specific settings in Kallithea.
16
16
17 This file complements the .ini file.
17 This file complements the .ini file.
18 """
18 """
19
19
20 import platform
20 import platform
21 import os, sys, logging
21 import os, sys, logging
22
22
23 import tg
23 import tg
24 from tg import hooks
24 from tg import hooks
25 from tg.configuration import AppConfig
25 from tg.configuration import AppConfig
26 from tg.support.converters import asbool
26 from tg.support.converters import asbool
27 import alembic
27 import alembic.config
28 from alembic.script.base import ScriptDirectory
28 from alembic.script.base import ScriptDirectory
29 from alembic.migration import MigrationContext
29 from alembic.migration import MigrationContext
30 from sqlalchemy import create_engine
30 from sqlalchemy import create_engine
31
31
32 from kallithea.lib.middleware.https_fixup import HttpsFixup
32 from kallithea.lib.middleware.https_fixup import HttpsFixup
33 from kallithea.lib.middleware.simplegit import SimpleGit
33 from kallithea.lib.middleware.simplegit import SimpleGit
34 from kallithea.lib.middleware.simplehg import SimpleHg
34 from kallithea.lib.middleware.simplehg import SimpleHg
35 from kallithea.lib.auth import set_available_permissions
35 from kallithea.lib.auth import set_available_permissions
36 from kallithea.lib.db_manage import DbManage
37 from kallithea.lib.utils import load_rcextensions, make_ui, set_app_settings, set_vcs_config, \
36 from kallithea.lib.utils import load_rcextensions, make_ui, set_app_settings, set_vcs_config, \
38 set_indexer_config, check_git_version, repo2db_mapper
37 set_indexer_config, check_git_version, repo2db_mapper
39 from kallithea.lib.utils2 import str2bool
38 from kallithea.lib.utils2 import str2bool
39 import kallithea.model.base
40 from kallithea.model.scm import ScmModel
40 from kallithea.model.scm import ScmModel
41
41
42 import formencode
42 import formencode
43 import kallithea
44
43
45 log = logging.getLogger(__name__)
44 log = logging.getLogger(__name__)
46
45
47
46
48 class KallitheaAppConfig(AppConfig):
47 class KallitheaAppConfig(AppConfig):
49 # Note: AppConfig has a misleading name, as it's not the application
48 # Note: AppConfig has a misleading name, as it's not the application
50 # configuration, but the application configurator. The AppConfig values are
49 # configuration, but the application configurator. The AppConfig values are
51 # used as a template to create the actual configuration, which might
50 # used as a template to create the actual configuration, which might
52 # overwrite or extend the one provided by the configurator template.
51 # overwrite or extend the one provided by the configurator template.
53
52
54 # To make it clear, AppConfig creates the config and sets into it the same
53 # To make it clear, AppConfig creates the config and sets into it the same
55 # values that AppConfig itself has. Then the values from the config file and
54 # values that AppConfig itself has. Then the values from the config file and
56 # gearbox options are loaded and merged into the configuration. Then an
55 # gearbox options are loaded and merged into the configuration. Then an
57 # after_init_config(conf) method of AppConfig is called for any change that
56 # after_init_config(conf) method of AppConfig is called for any change that
58 # might depend on options provided by configuration files.
57 # might depend on options provided by configuration files.
59
58
60 def __init__(self):
59 def __init__(self):
61 super(KallitheaAppConfig, self).__init__()
60 super(KallitheaAppConfig, self).__init__()
62
61
63 self['package'] = kallithea
62 self['package'] = kallithea
64
63
65 self['prefer_toscawidgets2'] = False
64 self['prefer_toscawidgets2'] = False
66 self['use_toscawidgets'] = False
65 self['use_toscawidgets'] = False
67
66
68 self['renderers'] = []
67 self['renderers'] = []
69
68
70 # Enable json in expose
69 # Enable json in expose
71 self['renderers'].append('json')
70 self['renderers'].append('json')
72
71
73 # Configure template rendering
72 # Configure template rendering
74 self['renderers'].append('mako')
73 self['renderers'].append('mako')
75 self['default_renderer'] = 'mako'
74 self['default_renderer'] = 'mako'
76 self['use_dotted_templatenames'] = False
75 self['use_dotted_templatenames'] = False
77
76
78 # Configure Sessions, store data as JSON to avoid pickle security issues
77 # Configure Sessions, store data as JSON to avoid pickle security issues
79 self['session.enabled'] = True
78 self['session.enabled'] = True
80 self['session.data_serializer'] = 'json'
79 self['session.data_serializer'] = 'json'
81
80
82 # Configure the base SQLALchemy Setup
81 # Configure the base SQLALchemy Setup
83 self['use_sqlalchemy'] = True
82 self['use_sqlalchemy'] = True
84 self['model'] = kallithea.model.base
83 self['model'] = kallithea.model.base
85 self['DBSession'] = kallithea.model.meta.Session
84 self['DBSession'] = kallithea.model.meta.Session
86
85
87 # Configure App without an authentication backend.
86 # Configure App without an authentication backend.
88 self['auth_backend'] = None
87 self['auth_backend'] = None
89
88
90 # Use custom error page for these errors. By default, Turbogears2 does not add
89 # Use custom error page for these errors. By default, Turbogears2 does not add
91 # 400 in this list.
90 # 400 in this list.
92 # Explicitly listing all is considered more robust than appending to defaults,
91 # Explicitly listing all is considered more robust than appending to defaults,
93 # in light of possible future framework changes.
92 # in light of possible future framework changes.
94 self['errorpage.status_codes'] = [400, 401, 403, 404]
93 self['errorpage.status_codes'] = [400, 401, 403, 404]
95
94
96 # Disable transaction manager -- currently Kallithea takes care of transactions itself
95 # Disable transaction manager -- currently Kallithea takes care of transactions itself
97 self['tm.enabled'] = False
96 self['tm.enabled'] = False
98
97
99 base_config = KallitheaAppConfig()
98 base_config = KallitheaAppConfig()
100
99
101 # TODO still needed as long as we use pylonslib
100 # TODO still needed as long as we use pylonslib
102 sys.modules['pylons'] = tg
101 sys.modules['pylons'] = tg
103
102
104 # DebugBar, a debug toolbar for TurboGears2.
103 # DebugBar, a debug toolbar for TurboGears2.
105 # (https://github.com/TurboGears/tgext.debugbar)
104 # (https://github.com/TurboGears/tgext.debugbar)
106 # To enable it, install 'tgext.debugbar' and 'kajiki', and run Kallithea with
105 # To enable it, install 'tgext.debugbar' and 'kajiki', and run Kallithea with
107 # 'debug = true' (not in production!)
106 # 'debug = true' (not in production!)
108 # See the Kallithea documentation for more information.
107 # See the Kallithea documentation for more information.
109 try:
108 try:
110 from tgext.debugbar import enable_debugbar
109 from tgext.debugbar import enable_debugbar
111 import kajiki # only to check its existence
110 import kajiki # only to check its existence
112 except ImportError:
111 except ImportError:
113 pass
112 pass
114 else:
113 else:
115 base_config['renderers'].append('kajiki')
114 base_config['renderers'].append('kajiki')
116 enable_debugbar(base_config)
115 enable_debugbar(base_config)
117
116
118
117
119 def setup_configuration(app):
118 def setup_configuration(app):
120 config = app.config
119 config = app.config
121
120
122 if config.get('ignore_alembic_revision', False):
121 if config.get('ignore_alembic_revision', False):
123 log.warn('database alembic revision checking is disabled')
122 log.warn('database alembic revision checking is disabled')
124 else:
123 else:
125 dbconf = config['sqlalchemy.url']
124 dbconf = config['sqlalchemy.url']
126 alembic_cfg = alembic.config.Config()
125 alembic_cfg = alembic.config.Config()
127 alembic_cfg.set_main_option('script_location', 'kallithea:alembic')
126 alembic_cfg.set_main_option('script_location', 'kallithea:alembic')
128 alembic_cfg.set_main_option('sqlalchemy.url', dbconf)
127 alembic_cfg.set_main_option('sqlalchemy.url', dbconf)
129 script_dir = ScriptDirectory.from_config(alembic_cfg)
128 script_dir = ScriptDirectory.from_config(alembic_cfg)
130 available_heads = sorted(script_dir.get_heads())
129 available_heads = sorted(script_dir.get_heads())
131
130
132 engine = create_engine(dbconf)
131 engine = create_engine(dbconf)
133 with engine.connect() as conn:
132 with engine.connect() as conn:
134 context = MigrationContext.configure(conn)
133 context = MigrationContext.configure(conn)
135 current_heads = sorted(str(s) for s in context.get_current_heads())
134 current_heads = sorted(str(s) for s in context.get_current_heads())
136 if current_heads != available_heads:
135 if current_heads != available_heads:
137 log.error('Failed to run Kallithea:\n\n'
136 log.error('Failed to run Kallithea:\n\n'
138 'The database version does not match the Kallithea version.\n'
137 'The database version does not match the Kallithea version.\n'
139 'Please read the documentation on how to upgrade or downgrade the database.\n'
138 'Please read the documentation on how to upgrade or downgrade the database.\n'
140 'Current database version id(s): %s\n'
139 'Current database version id(s): %s\n'
141 'Expected database version id(s): %s\n'
140 'Expected database version id(s): %s\n'
142 'If you are a developer and you know what you are doing, you can add `ignore_alembic_revision = True` '
141 'If you are a developer and you know what you are doing, you can add `ignore_alembic_revision = True` '
143 'to your .ini file to skip the check.\n' % (' '.join(current_heads), ' '.join(available_heads)))
142 'to your .ini file to skip the check.\n' % (' '.join(current_heads), ' '.join(available_heads)))
144 sys.exit(1)
143 sys.exit(1)
145
144
146 # store some globals into kallithea
145 # store some globals into kallithea
147 kallithea.CELERY_ON = str2bool(config['app_conf'].get('use_celery'))
146 kallithea.CELERY_ON = str2bool(config['app_conf'].get('use_celery'))
148 kallithea.CELERY_EAGER = str2bool(config['app_conf'].get('celery.always.eager'))
147 kallithea.CELERY_EAGER = str2bool(config['app_conf'].get('celery.always.eager'))
149 kallithea.CONFIG = config
148 kallithea.CONFIG = config
150
149
151 load_rcextensions(root_path=config['here'])
150 load_rcextensions(root_path=config['here'])
152
151
153 set_available_permissions(config)
152 set_available_permissions(config)
154 repos_path = make_ui('db').configitems('paths')[0][1]
153 repos_path = make_ui('db').configitems('paths')[0][1]
155 config['base_path'] = repos_path
154 config['base_path'] = repos_path
156 set_app_settings(config)
155 set_app_settings(config)
157
156
158 instance_id = kallithea.CONFIG.get('instance_id', '*')
157 instance_id = kallithea.CONFIG.get('instance_id', '*')
159 if instance_id == '*':
158 if instance_id == '*':
160 instance_id = '%s-%s' % (platform.uname()[1], os.getpid())
159 instance_id = '%s-%s' % (platform.uname()[1], os.getpid())
161 kallithea.CONFIG['instance_id'] = instance_id
160 kallithea.CONFIG['instance_id'] = instance_id
162
161
163 # update kallithea.CONFIG with the meanwhile changed 'config'
162 # update kallithea.CONFIG with the meanwhile changed 'config'
164 kallithea.CONFIG.update(config)
163 kallithea.CONFIG.update(config)
165
164
166 # configure vcs and indexer libraries (they are supposed to be independent
165 # configure vcs and indexer libraries (they are supposed to be independent
167 # as much as possible and thus avoid importing tg.config or
166 # as much as possible and thus avoid importing tg.config or
168 # kallithea.CONFIG).
167 # kallithea.CONFIG).
169 set_vcs_config(kallithea.CONFIG)
168 set_vcs_config(kallithea.CONFIG)
170 set_indexer_config(kallithea.CONFIG)
169 set_indexer_config(kallithea.CONFIG)
171
170
172 check_git_version()
171 check_git_version()
173
172
174 if str2bool(config.get('initial_repo_scan', True)):
173 if str2bool(config.get('initial_repo_scan', True)):
175 repo2db_mapper(ScmModel().repo_scan(repos_path),
174 repo2db_mapper(ScmModel().repo_scan(repos_path),
176 remove_obsolete=False, install_git_hooks=False)
175 remove_obsolete=False, install_git_hooks=False)
177
176
178 formencode.api.set_stdtranslation(languages=[config.get('lang')])
177 formencode.api.set_stdtranslation(languages=[config.get('lang')])
179
178
180 hooks.register('configure_new_app', setup_configuration)
179 hooks.register('configure_new_app', setup_configuration)
181
180
182
181
183 def setup_application(app):
182 def setup_application(app):
184 config = app.config
183 config = app.config
185
184
186 # we want our low level middleware to get to the request ASAP. We don't
185 # we want our low level middleware to get to the request ASAP. We don't
187 # need any stack middleware in them - especially no StatusCodeRedirect buffering
186 # need any stack middleware in them - especially no StatusCodeRedirect buffering
188 app = SimpleHg(app, config)
187 app = SimpleHg(app, config)
189 app = SimpleGit(app, config)
188 app = SimpleGit(app, config)
190
189
191 # Enable https redirects based on HTTP_X_URL_SCHEME set by proxy
190 # Enable https redirects based on HTTP_X_URL_SCHEME set by proxy
192 if any(asbool(config.get(x)) for x in ['https_fixup', 'force_https', 'use_htsts']):
191 if any(asbool(config.get(x)) for x in ['https_fixup', 'force_https', 'use_htsts']):
193 app = HttpsFixup(app, config)
192 app = HttpsFixup(app, config)
194 return app
193 return app
195
194
196 hooks.register('before_config', setup_application)
195 hooks.register('before_config', setup_application)
General Comments 0
You need to be logged in to leave comments. Login now