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