Commit | Line | Data |
---|---|---|
1b579e18 | 1 | # GNU MediaGoblin -- federated, autonomous media hosting |
12a100e4 | 2 | # Copyright (C) 2011 MediaGoblin contributors. See AUTHORS. |
1b579e18 E |
3 | # |
4 | # This program is free software: you can redistribute it and/or modify | |
5 | # it under the terms of the GNU Affero General Public License as published by | |
6 | # the Free Software Foundation, either version 3 of the License, or | |
7 | # (at your option) any later version. | |
8 | # | |
9 | # This program is distributed in the hope that it will be useful, | |
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | # GNU Affero General Public License for more details. | |
13 | # | |
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/>. | |
42ef819c | 16 | |
0533f117 CAW |
17 | from beaker.cache import CacheManager |
18 | from beaker.util import parse_cache_config_options | |
42ef819c | 19 | import jinja2 |
0533f117 | 20 | |
c85c9dc7 | 21 | from mediagoblin import staticdirect |
fe289be4 E |
22 | from mediagoblin.init.config import ( |
23 | read_mediagoblin_config, generate_validation_report) | |
7664b4db | 24 | from mediagoblin import mg_globals |
cca5d55d | 25 | from mediagoblin.mg_globals import setup_globals |
3f4b5e4a E |
26 | from mediagoblin.db.open import setup_connection_and_db_from_config |
27 | from mediagoblin.db.util import MigrationManager | |
7664b4db | 28 | from mediagoblin.workbench import WorkbenchManager |
dccef262 | 29 | from mediagoblin.storage import storage_system_from_config |
c85c9dc7 E |
30 | |
31 | ||
32 | class Error(Exception): pass | |
33 | class ImproperlyConfigured(Error): pass | |
42ef819c E |
34 | |
35 | ||
fe289be4 E |
36 | def setup_global_and_app_config(config_path): |
37 | global_config, validation_result = read_mediagoblin_config(config_path) | |
38 | app_config = global_config['mediagoblin'] | |
39 | # report errors if necessary | |
40 | validation_report = generate_validation_report( | |
41 | global_config, validation_result) | |
42 | if validation_report: | |
43 | raise ImproperlyConfigured(validation_report) | |
44 | ||
cca5d55d E |
45 | setup_globals( |
46 | app_config=app_config, | |
47 | global_config=global_config) | |
48 | ||
fe289be4 E |
49 | return global_config, app_config |
50 | ||
3f4b5e4a E |
51 | |
52 | def setup_database(): | |
53 | app_config = mg_globals.app_config | |
54 | ||
55 | # This MUST be imported so as to set up the appropriate migrations! | |
56 | from mediagoblin.db import migrations | |
57 | ||
58 | # Set up the database | |
59 | connection, db = setup_connection_and_db_from_config(app_config) | |
60 | ||
61 | # Init the migration number if necessary | |
62 | migration_manager = MigrationManager(db) | |
63 | migration_manager.install_migration_version_if_missing() | |
64 | ||
65 | # Tiny hack to warn user if our migration is out of date | |
66 | if not migration_manager.database_at_latest_migration(): | |
482d53cd CAW |
67 | db_migration_num = migration_manager.database_current_migration() |
68 | latest_migration_num = migration_manager.latest_migration() | |
69 | if db_migration_num < latest_migration_num: | |
70 | print ( | |
71 | "*WARNING:* Your migrations are out of date, " | |
72 | "maybe run ./bin/gmg migrate?") | |
73 | elif db_migration_num > latest_migration_num: | |
74 | print ( | |
75 | "*WARNING:* Your migrations are out of date... " | |
76 | "in fact they appear to be from the future?!") | |
3f4b5e4a E |
77 | |
78 | setup_globals( | |
79 | db_connection = connection, | |
80 | database = db) | |
81 | ||
82 | return connection, db | |
83 | ||
84 | ||
42ef819c E |
85 | def get_jinja_loader(user_template_path=None): |
86 | """ | |
87 | Set up the Jinja template loaders, possibly allowing for user | |
88 | overridden templates. | |
89 | ||
90 | (In the future we may have another system for providing theming; | |
91 | for now this is good enough.) | |
92 | """ | |
93 | if user_template_path: | |
94 | return jinja2.ChoiceLoader( | |
95 | [jinja2.FileSystemLoader(user_template_path), | |
96 | jinja2.PackageLoader('mediagoblin', 'templates')]) | |
97 | else: | |
98 | return jinja2.PackageLoader('mediagoblin', 'templates') | |
c85c9dc7 E |
99 | |
100 | ||
101 | def get_staticdirector(app_config): | |
102 | if app_config.has_key('direct_remote_path'): | |
103 | return staticdirect.RemoteStaticDirect( | |
104 | app_config['direct_remote_path'].strip()) | |
105 | elif app_config.has_key('direct_remote_paths'): | |
106 | direct_remote_path_lines = app_config[ | |
107 | 'direct_remote_paths'].strip().splitlines() | |
108 | return staticdirect.MultiRemoteStaticDirect( | |
109 | dict([line.strip().split(' ', 1) | |
110 | for line in direct_remote_path_lines])) | |
111 | else: | |
112 | raise ImproperlyConfigured( | |
113 | "One of direct_remote_path or " | |
114 | "direct_remote_paths must be provided") | |
7664b4db E |
115 | |
116 | ||
dccef262 | 117 | def setup_storage(): |
ed797069 | 118 | global_config = mg_globals.global_config |
dccef262 | 119 | |
ed797069 E |
120 | key_short = 'publicstore' |
121 | key_long = "storage:" + key_short | |
56fc7186 | 122 | public_store = storage_system_from_config(global_config[key_long]) |
ed797069 E |
123 | |
124 | key_short = 'queuestore' | |
125 | key_long = "storage:" + key_short | |
56fc7186 | 126 | queue_store = storage_system_from_config(global_config[key_long]) |
dccef262 E |
127 | |
128 | setup_globals( | |
129 | public_store = public_store, | |
130 | queue_store = queue_store) | |
131 | ||
132 | return public_store, queue_store | |
133 | ||
134 | ||
7664b4db E |
135 | def setup_workbench(): |
136 | app_config = mg_globals.app_config | |
137 | ||
138 | workbench_manager = WorkbenchManager(app_config['workbench_path']) | |
139 | ||
140 | setup_globals(workbench_manager = workbench_manager) | |
0533f117 CAW |
141 | |
142 | ||
143 | def setup_beaker_cache(): | |
144 | """ | |
145 | Setup the Beaker Cache manager. | |
146 | """ | |
147 | cache_config = mg_globals.global_config['beaker.cache'] | |
148 | cache_config = dict( | |
149 | [(u'cache.%s' % key, value) | |
150 | for key, value in cache_config.iteritems()]) | |
151 | cache = CacheManager(**parse_cache_config_options(cache_config)) | |
152 | setup_globals(cache=cache) | |
153 | return cache |