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