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