Show More
@@ -1,642 +1,666 b'' | |||
|
1 | 1 | # -*- coding: utf-8 -*- |
|
2 | 2 | |
|
3 | 3 | # Copyright (C) 2010-2019 RhodeCode GmbH |
|
4 | 4 | # |
|
5 | 5 | # This program is free software: you can redistribute it and/or modify |
|
6 | 6 | # it under the terms of the GNU Affero General Public License, version 3 |
|
7 | 7 | # (only), as published by the Free Software Foundation. |
|
8 | 8 | # |
|
9 | 9 | # This program is distributed in the hope that it will be useful, |
|
10 | 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 | 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 | 12 | # GNU General Public License for more details. |
|
13 | 13 | # |
|
14 | 14 | # You should have received a copy of the GNU Affero General Public License |
|
15 | 15 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
16 | 16 | # |
|
17 | 17 | # This program is dual-licensed. If you wish to learn more about the |
|
18 | 18 | # RhodeCode Enterprise Edition, including its added features, Support services, |
|
19 | 19 | # and proprietary license terms, please see https://rhodecode.com/licenses/ |
|
20 | 20 | |
|
21 | 21 | """ |
|
22 | 22 | Database creation, and setup module for RhodeCode Enterprise. Used for creation |
|
23 | 23 | of database as well as for migration operations |
|
24 | 24 | """ |
|
25 | 25 | |
|
26 | 26 | import os |
|
27 | 27 | import sys |
|
28 | 28 | import time |
|
29 | 29 | import uuid |
|
30 | 30 | import logging |
|
31 | 31 | import getpass |
|
32 | 32 | from os.path import dirname as dn, join as jn |
|
33 | 33 | |
|
34 | 34 | from sqlalchemy.engine import create_engine |
|
35 | 35 | |
|
36 | 36 | from rhodecode import __dbversion__ |
|
37 | 37 | from rhodecode.model import init_model |
|
38 | 38 | from rhodecode.model.user import UserModel |
|
39 | 39 | from rhodecode.model.db import ( |
|
40 | 40 | User, Permission, RhodeCodeUi, RhodeCodeSetting, UserToPerm, |
|
41 | 41 | DbMigrateVersion, RepoGroup, UserRepoGroupToPerm, CacheKey, Repository) |
|
42 | 42 | from rhodecode.model.meta import Session, Base |
|
43 | 43 | from rhodecode.model.permission import PermissionModel |
|
44 | 44 | from rhodecode.model.repo import RepoModel |
|
45 | 45 | from rhodecode.model.repo_group import RepoGroupModel |
|
46 | 46 | from rhodecode.model.settings import SettingsModel |
|
47 | 47 | |
|
48 | 48 | |
|
49 | 49 | log = logging.getLogger(__name__) |
|
50 | 50 | |
|
51 | 51 | |
|
52 | 52 | def notify(msg): |
|
53 | 53 | """ |
|
54 | 54 | Notification for migrations messages |
|
55 | 55 | """ |
|
56 | 56 | ml = len(msg) + (4 * 2) |
|
57 | 57 | print(('\n%s\n*** %s ***\n%s' % ('*' * ml, msg, '*' * ml)).upper()) |
|
58 | 58 | |
|
59 | 59 | |
|
60 | 60 | class DbManage(object): |
|
61 | 61 | |
|
62 | 62 | def __init__(self, log_sql, dbconf, root, tests=False, |
|
63 | 63 | SESSION=None, cli_args=None): |
|
64 | 64 | self.dbname = dbconf.split('/')[-1] |
|
65 | 65 | self.tests = tests |
|
66 | 66 | self.root = root |
|
67 | 67 | self.dburi = dbconf |
|
68 | 68 | self.log_sql = log_sql |
|
69 | 69 | self.db_exists = False |
|
70 | 70 | self.cli_args = cli_args or {} |
|
71 | 71 | self.init_db(SESSION=SESSION) |
|
72 | 72 | self.ask_ok = self.get_ask_ok_func(self.cli_args.get('force_ask')) |
|
73 | 73 | |
|
74 | 74 | def get_ask_ok_func(self, param): |
|
75 | 75 | if param not in [None]: |
|
76 | 76 | # return a function lambda that has a default set to param |
|
77 | 77 | return lambda *args, **kwargs: param |
|
78 | 78 | else: |
|
79 | 79 | from rhodecode.lib.utils import ask_ok |
|
80 | 80 | return ask_ok |
|
81 | 81 | |
|
82 | 82 | def init_db(self, SESSION=None): |
|
83 | 83 | if SESSION: |
|
84 | 84 | self.sa = SESSION |
|
85 | 85 | else: |
|
86 | 86 | # init new sessions |
|
87 | 87 | engine = create_engine(self.dburi, echo=self.log_sql) |
|
88 | 88 | init_model(engine) |
|
89 | 89 | self.sa = Session() |
|
90 | 90 | |
|
91 | 91 | def create_tables(self, override=False): |
|
92 | 92 | """ |
|
93 | 93 | Create a auth database |
|
94 | 94 | """ |
|
95 | 95 | |
|
96 | 96 | log.info("Existing database with the same name is going to be destroyed.") |
|
97 | 97 | log.info("Setup command will run DROP ALL command on that database.") |
|
98 | 98 | if self.tests: |
|
99 | 99 | destroy = True |
|
100 | 100 | else: |
|
101 | 101 | destroy = self.ask_ok('Are you sure that you want to destroy the old database? [y/n]') |
|
102 | 102 | if not destroy: |
|
103 | 103 | log.info('Nothing done.') |
|
104 | 104 | sys.exit(0) |
|
105 | 105 | if destroy: |
|
106 | 106 | Base.metadata.drop_all() |
|
107 | 107 | |
|
108 | 108 | checkfirst = not override |
|
109 | 109 | Base.metadata.create_all(checkfirst=checkfirst) |
|
110 | 110 | log.info('Created tables for %s', self.dbname) |
|
111 | 111 | |
|
112 | 112 | def set_db_version(self): |
|
113 | 113 | ver = DbMigrateVersion() |
|
114 | 114 | ver.version = __dbversion__ |
|
115 | 115 | ver.repository_id = 'rhodecode_db_migrations' |
|
116 | 116 | ver.repository_path = 'versions' |
|
117 | 117 | self.sa.add(ver) |
|
118 | 118 | log.info('db version set to: %s', __dbversion__) |
|
119 | 119 | |
|
120 | 120 | def run_post_migration_tasks(self): |
|
121 | 121 | """ |
|
122 | 122 | Run various tasks before actually doing migrations |
|
123 | 123 | """ |
|
124 | 124 | # delete cache keys on each upgrade |
|
125 | 125 | total = CacheKey.query().count() |
|
126 | 126 | log.info("Deleting (%s) cache keys now...", total) |
|
127 | 127 | CacheKey.delete_all_cache() |
|
128 | 128 | |
|
129 | 129 | def upgrade(self, version=None): |
|
130 | 130 | """ |
|
131 | 131 | Upgrades given database schema to given revision following |
|
132 | 132 | all needed steps, to perform the upgrade |
|
133 | 133 | |
|
134 | 134 | """ |
|
135 | 135 | |
|
136 | 136 | from rhodecode.lib.dbmigrate.migrate.versioning import api |
|
137 | 137 | from rhodecode.lib.dbmigrate.migrate.exceptions import \ |
|
138 | 138 | DatabaseNotControlledError |
|
139 | 139 | |
|
140 | 140 | if 'sqlite' in self.dburi: |
|
141 | 141 | print( |
|
142 | 142 | '********************** WARNING **********************\n' |
|
143 | 143 | 'Make sure your version of sqlite is at least 3.7.X. \n' |
|
144 | 144 | 'Earlier versions are known to fail on some migrations\n' |
|
145 | 145 | '*****************************************************\n') |
|
146 | 146 | |
|
147 | 147 | upgrade = self.ask_ok( |
|
148 | 148 | 'You are about to perform a database upgrade. Make ' |
|
149 | 149 | 'sure you have backed up your database. ' |
|
150 | 150 | 'Continue ? [y/n]') |
|
151 | 151 | if not upgrade: |
|
152 | 152 | log.info('No upgrade performed') |
|
153 | 153 | sys.exit(0) |
|
154 | 154 | |
|
155 | 155 | repository_path = jn(dn(dn(dn(os.path.realpath(__file__)))), |
|
156 | 156 | 'rhodecode/lib/dbmigrate') |
|
157 | 157 | db_uri = self.dburi |
|
158 | 158 | |
|
159 | 159 | if version: |
|
160 | 160 | DbMigrateVersion.set_version(version) |
|
161 | 161 | |
|
162 | 162 | try: |
|
163 | 163 | curr_version = api.db_version(db_uri, repository_path) |
|
164 | 164 | msg = ('Found current database db_uri under version ' |
|
165 | 165 | 'control with version {}'.format(curr_version)) |
|
166 | 166 | |
|
167 | 167 | except (RuntimeError, DatabaseNotControlledError): |
|
168 | 168 | curr_version = 1 |
|
169 | 169 | msg = ('Current database is not under version control. Setting ' |
|
170 | 170 | 'as version %s' % curr_version) |
|
171 | 171 | api.version_control(db_uri, repository_path, curr_version) |
|
172 | 172 | |
|
173 | 173 | notify(msg) |
|
174 | 174 | |
|
175 | 175 | |
|
176 | 176 | if curr_version == __dbversion__: |
|
177 | 177 | log.info('This database is already at the newest version') |
|
178 | 178 | sys.exit(0) |
|
179 | 179 | |
|
180 | 180 | upgrade_steps = range(curr_version + 1, __dbversion__ + 1) |
|
181 | 181 | notify('attempting to upgrade database from ' |
|
182 | 182 | 'version %s to version %s' % (curr_version, __dbversion__)) |
|
183 | 183 | |
|
184 | 184 | # CALL THE PROPER ORDER OF STEPS TO PERFORM FULL UPGRADE |
|
185 | 185 | _step = None |
|
186 | 186 | for step in upgrade_steps: |
|
187 | 187 | notify('performing upgrade step %s' % step) |
|
188 | 188 | time.sleep(0.5) |
|
189 | 189 | |
|
190 | 190 | api.upgrade(db_uri, repository_path, step) |
|
191 | 191 | self.sa.rollback() |
|
192 | 192 | notify('schema upgrade for step %s completed' % (step,)) |
|
193 | 193 | |
|
194 | 194 | _step = step |
|
195 | 195 | |
|
196 | 196 | self.run_post_migration_tasks() |
|
197 | 197 | notify('upgrade to version %s successful' % _step) |
|
198 | 198 | |
|
199 | 199 | def fix_repo_paths(self): |
|
200 | 200 | """ |
|
201 | 201 | Fixes an old RhodeCode version path into new one without a '*' |
|
202 | 202 | """ |
|
203 | 203 | |
|
204 | 204 | paths = self.sa.query(RhodeCodeUi)\ |
|
205 | 205 | .filter(RhodeCodeUi.ui_key == '/')\ |
|
206 | 206 | .scalar() |
|
207 | 207 | |
|
208 | 208 | paths.ui_value = paths.ui_value.replace('*', '') |
|
209 | 209 | |
|
210 | 210 | try: |
|
211 | 211 | self.sa.add(paths) |
|
212 | 212 | self.sa.commit() |
|
213 | 213 | except Exception: |
|
214 | 214 | self.sa.rollback() |
|
215 | 215 | raise |
|
216 | 216 | |
|
217 | 217 | def fix_default_user(self): |
|
218 | 218 | """ |
|
219 | 219 | Fixes an old default user with some 'nicer' default values, |
|
220 | 220 | used mostly for anonymous access |
|
221 | 221 | """ |
|
222 | 222 | def_user = self.sa.query(User)\ |
|
223 | 223 | .filter(User.username == User.DEFAULT_USER)\ |
|
224 | 224 | .one() |
|
225 | 225 | |
|
226 | 226 | def_user.name = 'Anonymous' |
|
227 | 227 | def_user.lastname = 'User' |
|
228 | 228 | def_user.email = User.DEFAULT_USER_EMAIL |
|
229 | 229 | |
|
230 | 230 | try: |
|
231 | 231 | self.sa.add(def_user) |
|
232 | 232 | self.sa.commit() |
|
233 | 233 | except Exception: |
|
234 | 234 | self.sa.rollback() |
|
235 | 235 | raise |
|
236 | 236 | |
|
237 | 237 | def fix_settings(self): |
|
238 | 238 | """ |
|
239 | 239 | Fixes rhodecode settings and adds ga_code key for google analytics |
|
240 | 240 | """ |
|
241 | 241 | |
|
242 | 242 | hgsettings3 = RhodeCodeSetting('ga_code', '') |
|
243 | 243 | |
|
244 | 244 | try: |
|
245 | 245 | self.sa.add(hgsettings3) |
|
246 | 246 | self.sa.commit() |
|
247 | 247 | except Exception: |
|
248 | 248 | self.sa.rollback() |
|
249 | 249 | raise |
|
250 | 250 | |
|
251 | 251 | def create_admin_and_prompt(self): |
|
252 | 252 | |
|
253 | 253 | # defaults |
|
254 | 254 | defaults = self.cli_args |
|
255 | 255 | username = defaults.get('username') |
|
256 | 256 | password = defaults.get('password') |
|
257 | 257 | email = defaults.get('email') |
|
258 | 258 | |
|
259 | 259 | if username is None: |
|
260 | 260 | username = raw_input('Specify admin username:') |
|
261 | 261 | if password is None: |
|
262 | 262 | password = self._get_admin_password() |
|
263 | 263 | if not password: |
|
264 | 264 | # second try |
|
265 | 265 | password = self._get_admin_password() |
|
266 | 266 | if not password: |
|
267 | 267 | sys.exit() |
|
268 | 268 | if email is None: |
|
269 | 269 | email = raw_input('Specify admin email:') |
|
270 | 270 | api_key = self.cli_args.get('api_key') |
|
271 | 271 | self.create_user(username, password, email, True, |
|
272 | 272 | strict_creation_check=False, |
|
273 | 273 | api_key=api_key) |
|
274 | 274 | |
|
275 | 275 | def _get_admin_password(self): |
|
276 | 276 | password = getpass.getpass('Specify admin password ' |
|
277 | 277 | '(min 6 chars):') |
|
278 | 278 | confirm = getpass.getpass('Confirm password:') |
|
279 | 279 | |
|
280 | 280 | if password != confirm: |
|
281 | 281 | log.error('passwords mismatch') |
|
282 | 282 | return False |
|
283 | 283 | if len(password) < 6: |
|
284 | 284 | log.error('password is too short - use at least 6 characters') |
|
285 | 285 | return False |
|
286 | 286 | |
|
287 | 287 | return password |
|
288 | 288 | |
|
289 | 289 | def create_test_admin_and_users(self): |
|
290 | 290 | log.info('creating admin and regular test users') |
|
291 | 291 | from rhodecode.tests import TEST_USER_ADMIN_LOGIN, \ |
|
292 | 292 | TEST_USER_ADMIN_PASS, TEST_USER_ADMIN_EMAIL, \ |
|
293 | 293 | TEST_USER_REGULAR_LOGIN, TEST_USER_REGULAR_PASS, \ |
|
294 | 294 | TEST_USER_REGULAR_EMAIL, TEST_USER_REGULAR2_LOGIN, \ |
|
295 | 295 | TEST_USER_REGULAR2_PASS, TEST_USER_REGULAR2_EMAIL |
|
296 | 296 | |
|
297 | 297 | self.create_user(TEST_USER_ADMIN_LOGIN, TEST_USER_ADMIN_PASS, |
|
298 | 298 | TEST_USER_ADMIN_EMAIL, True, api_key=True) |
|
299 | 299 | |
|
300 | 300 | self.create_user(TEST_USER_REGULAR_LOGIN, TEST_USER_REGULAR_PASS, |
|
301 | 301 | TEST_USER_REGULAR_EMAIL, False, api_key=True) |
|
302 | 302 | |
|
303 | 303 | self.create_user(TEST_USER_REGULAR2_LOGIN, TEST_USER_REGULAR2_PASS, |
|
304 | 304 | TEST_USER_REGULAR2_EMAIL, False, api_key=True) |
|
305 | 305 | |
|
306 | 306 | def create_ui_settings(self, repo_store_path): |
|
307 | 307 | """ |
|
308 | 308 | Creates ui settings, fills out hooks |
|
309 | 309 | and disables dotencode |
|
310 | 310 | """ |
|
311 | 311 | settings_model = SettingsModel(sa=self.sa) |
|
312 | 312 | from rhodecode.lib.vcs.backends.hg import largefiles_store |
|
313 | 313 | from rhodecode.lib.vcs.backends.git import lfs_store |
|
314 | 314 | |
|
315 | 315 | # Build HOOKS |
|
316 | 316 | hooks = [ |
|
317 | 317 | (RhodeCodeUi.HOOK_REPO_SIZE, 'python:vcsserver.hooks.repo_size'), |
|
318 | 318 | |
|
319 | 319 | # HG |
|
320 | 320 | (RhodeCodeUi.HOOK_PRE_PULL, 'python:vcsserver.hooks.pre_pull'), |
|
321 | 321 | (RhodeCodeUi.HOOK_PULL, 'python:vcsserver.hooks.log_pull_action'), |
|
322 | 322 | (RhodeCodeUi.HOOK_PRE_PUSH, 'python:vcsserver.hooks.pre_push'), |
|
323 | 323 | (RhodeCodeUi.HOOK_PRETX_PUSH, 'python:vcsserver.hooks.pre_push'), |
|
324 | 324 | (RhodeCodeUi.HOOK_PUSH, 'python:vcsserver.hooks.log_push_action'), |
|
325 | 325 | (RhodeCodeUi.HOOK_PUSH_KEY, 'python:vcsserver.hooks.key_push'), |
|
326 | 326 | |
|
327 | 327 | ] |
|
328 | 328 | |
|
329 | 329 | for key, value in hooks: |
|
330 | 330 | hook_obj = settings_model.get_ui_by_key(key) |
|
331 | 331 | hooks2 = hook_obj if hook_obj else RhodeCodeUi() |
|
332 | 332 | hooks2.ui_section = 'hooks' |
|
333 | 333 | hooks2.ui_key = key |
|
334 | 334 | hooks2.ui_value = value |
|
335 | 335 | self.sa.add(hooks2) |
|
336 | 336 | |
|
337 | 337 | # enable largefiles |
|
338 | 338 | largefiles = RhodeCodeUi() |
|
339 | 339 | largefiles.ui_section = 'extensions' |
|
340 | 340 | largefiles.ui_key = 'largefiles' |
|
341 | 341 | largefiles.ui_value = '' |
|
342 | 342 | self.sa.add(largefiles) |
|
343 | 343 | |
|
344 | 344 | # set default largefiles cache dir, defaults to |
|
345 | 345 | # /repo_store_location/.cache/largefiles |
|
346 | 346 | largefiles = RhodeCodeUi() |
|
347 | 347 | largefiles.ui_section = 'largefiles' |
|
348 | 348 | largefiles.ui_key = 'usercache' |
|
349 | 349 | largefiles.ui_value = largefiles_store(repo_store_path) |
|
350 | 350 | |
|
351 | 351 | self.sa.add(largefiles) |
|
352 | 352 | |
|
353 | 353 | # set default lfs cache dir, defaults to |
|
354 | 354 | # /repo_store_location/.cache/lfs_store |
|
355 | 355 | lfsstore = RhodeCodeUi() |
|
356 | 356 | lfsstore.ui_section = 'vcs_git_lfs' |
|
357 | 357 | lfsstore.ui_key = 'store_location' |
|
358 | 358 | lfsstore.ui_value = lfs_store(repo_store_path) |
|
359 | 359 | |
|
360 | 360 | self.sa.add(lfsstore) |
|
361 | 361 | |
|
362 | 362 | # enable hgsubversion disabled by default |
|
363 | 363 | hgsubversion = RhodeCodeUi() |
|
364 | 364 | hgsubversion.ui_section = 'extensions' |
|
365 | 365 | hgsubversion.ui_key = 'hgsubversion' |
|
366 | 366 | hgsubversion.ui_value = '' |
|
367 | 367 | hgsubversion.ui_active = False |
|
368 | 368 | self.sa.add(hgsubversion) |
|
369 | 369 | |
|
370 | 370 | # enable hgevolve disabled by default |
|
371 | 371 | hgevolve = RhodeCodeUi() |
|
372 | 372 | hgevolve.ui_section = 'extensions' |
|
373 | 373 | hgevolve.ui_key = 'evolve' |
|
374 | 374 | hgevolve.ui_value = '' |
|
375 | 375 | hgevolve.ui_active = False |
|
376 | 376 | self.sa.add(hgevolve) |
|
377 | 377 | |
|
378 | 378 | hgevolve = RhodeCodeUi() |
|
379 | 379 | hgevolve.ui_section = 'experimental' |
|
380 | 380 | hgevolve.ui_key = 'evolution' |
|
381 | 381 | hgevolve.ui_value = '' |
|
382 | 382 | hgevolve.ui_active = False |
|
383 | 383 | self.sa.add(hgevolve) |
|
384 | 384 | |
|
385 | 385 | hgevolve = RhodeCodeUi() |
|
386 | 386 | hgevolve.ui_section = 'experimental' |
|
387 | 387 | hgevolve.ui_key = 'evolution.exchange' |
|
388 | 388 | hgevolve.ui_value = '' |
|
389 | 389 | hgevolve.ui_active = False |
|
390 | 390 | self.sa.add(hgevolve) |
|
391 | 391 | |
|
392 | 392 | hgevolve = RhodeCodeUi() |
|
393 | 393 | hgevolve.ui_section = 'extensions' |
|
394 | 394 | hgevolve.ui_key = 'topic' |
|
395 | 395 | hgevolve.ui_value = '' |
|
396 | 396 | hgevolve.ui_active = False |
|
397 | 397 | self.sa.add(hgevolve) |
|
398 | 398 | |
|
399 | 399 | # enable hggit disabled by default |
|
400 | 400 | hggit = RhodeCodeUi() |
|
401 | 401 | hggit.ui_section = 'extensions' |
|
402 | 402 | hggit.ui_key = 'hggit' |
|
403 | 403 | hggit.ui_value = '' |
|
404 | 404 | hggit.ui_active = False |
|
405 | 405 | self.sa.add(hggit) |
|
406 | 406 | |
|
407 | 407 | # set svn branch defaults |
|
408 | 408 | branches = ["/branches/*", "/trunk"] |
|
409 | 409 | tags = ["/tags/*"] |
|
410 | 410 | |
|
411 | 411 | for branch in branches: |
|
412 | 412 | settings_model.create_ui_section_value( |
|
413 | 413 | RhodeCodeUi.SVN_BRANCH_ID, branch) |
|
414 | 414 | |
|
415 | 415 | for tag in tags: |
|
416 | 416 | settings_model.create_ui_section_value(RhodeCodeUi.SVN_TAG_ID, tag) |
|
417 | 417 | |
|
418 | 418 | def create_auth_plugin_options(self, skip_existing=False): |
|
419 | 419 | """ |
|
420 | 420 | Create default auth plugin settings, and make it active |
|
421 | 421 | |
|
422 | 422 | :param skip_existing: |
|
423 | 423 | """ |
|
424 | defaults = [ | |
|
425 | ('auth_plugins', | |
|
426 | 'egg:rhodecode-enterprise-ce#token,egg:rhodecode-enterprise-ce#rhodecode', | |
|
427 | 'list'), | |
|
424 | 428 | |
|
425 | for k, v, t in [('auth_plugins', 'egg:rhodecode-enterprise-ce#rhodecode', 'list'), | |
|
426 | ('auth_rhodecode_enabled', 'True', 'bool')]: | |
|
429 | ('auth_authtoken_enabled', | |
|
430 | 'True', | |
|
431 | 'bool'), | |
|
432 | ||
|
433 | ('auth_rhodecode_enabled', | |
|
434 | 'True', | |
|
435 | 'bool'), | |
|
436 | ] | |
|
437 | for k, v, t in defaults: | |
|
427 | 438 | if (skip_existing and |
|
428 | 439 | SettingsModel().get_setting_by_name(k) is not None): |
|
429 | 440 | log.debug('Skipping option %s', k) |
|
430 | 441 | continue |
|
431 | 442 | setting = RhodeCodeSetting(k, v, t) |
|
432 | 443 | self.sa.add(setting) |
|
433 | 444 | |
|
434 | 445 | def create_default_options(self, skip_existing=False): |
|
435 | 446 | """Creates default settings""" |
|
436 | 447 | |
|
437 | 448 | for k, v, t in [ |
|
438 | 449 | ('default_repo_enable_locking', False, 'bool'), |
|
439 | 450 | ('default_repo_enable_downloads', False, 'bool'), |
|
440 | 451 | ('default_repo_enable_statistics', False, 'bool'), |
|
441 | 452 | ('default_repo_private', False, 'bool'), |
|
442 | 453 | ('default_repo_type', 'hg', 'unicode')]: |
|
443 | 454 | |
|
444 | 455 | if (skip_existing and |
|
445 | 456 | SettingsModel().get_setting_by_name(k) is not None): |
|
446 | 457 | log.debug('Skipping option %s', k) |
|
447 | 458 | continue |
|
448 | 459 | setting = RhodeCodeSetting(k, v, t) |
|
449 | 460 | self.sa.add(setting) |
|
450 | 461 | |
|
451 | 462 | def fixup_groups(self): |
|
452 | 463 | def_usr = User.get_default_user() |
|
453 | 464 | for g in RepoGroup.query().all(): |
|
454 | 465 | g.group_name = g.get_new_name(g.name) |
|
455 | 466 | self.sa.add(g) |
|
456 | 467 | # get default perm |
|
457 | 468 | default = UserRepoGroupToPerm.query()\ |
|
458 | 469 | .filter(UserRepoGroupToPerm.group == g)\ |
|
459 | 470 | .filter(UserRepoGroupToPerm.user == def_usr)\ |
|
460 | 471 | .scalar() |
|
461 | 472 | |
|
462 | 473 | if default is None: |
|
463 | 474 | log.debug('missing default permission for group %s adding', g) |
|
464 | 475 | perm_obj = RepoGroupModel()._create_default_perms(g) |
|
465 | 476 | self.sa.add(perm_obj) |
|
466 | 477 | |
|
467 | 478 | def reset_permissions(self, username): |
|
468 | 479 | """ |
|
469 | 480 | Resets permissions to default state, useful when old systems had |
|
470 | 481 | bad permissions, we must clean them up |
|
471 | 482 | |
|
472 | 483 | :param username: |
|
473 | 484 | """ |
|
474 | 485 | default_user = User.get_by_username(username) |
|
475 | 486 | if not default_user: |
|
476 | 487 | return |
|
477 | 488 | |
|
478 | 489 | u2p = UserToPerm.query()\ |
|
479 | 490 | .filter(UserToPerm.user == default_user).all() |
|
480 | 491 | fixed = False |
|
481 | 492 | if len(u2p) != len(Permission.DEFAULT_USER_PERMISSIONS): |
|
482 | 493 | for p in u2p: |
|
483 | 494 | Session().delete(p) |
|
484 | 495 | fixed = True |
|
485 | 496 | self.populate_default_permissions() |
|
486 | 497 | return fixed |
|
487 | 498 | |
|
488 | 499 | def config_prompt(self, test_repo_path='', retries=3): |
|
489 | 500 | defaults = self.cli_args |
|
490 | 501 | _path = defaults.get('repos_location') |
|
491 | 502 | if retries == 3: |
|
492 | 503 | log.info('Setting up repositories config') |
|
493 | 504 | |
|
494 | 505 | if _path is not None: |
|
495 | 506 | path = _path |
|
496 | 507 | elif not self.tests and not test_repo_path: |
|
497 | 508 | path = raw_input( |
|
498 | 509 | 'Enter a valid absolute path to store repositories. ' |
|
499 | 510 | 'All repositories in that path will be added automatically:' |
|
500 | 511 | ) |
|
501 | 512 | else: |
|
502 | 513 | path = test_repo_path |
|
503 | 514 | path_ok = True |
|
504 | 515 | |
|
505 | 516 | # check proper dir |
|
506 | 517 | if not os.path.isdir(path): |
|
507 | 518 | path_ok = False |
|
508 | 519 | log.error('Given path %s is not a valid directory', path) |
|
509 | 520 | |
|
510 | 521 | elif not os.path.isabs(path): |
|
511 | 522 | path_ok = False |
|
512 | 523 | log.error('Given path %s is not an absolute path', path) |
|
513 | 524 | |
|
514 | 525 | # check if path is at least readable. |
|
515 | 526 | if not os.access(path, os.R_OK): |
|
516 | 527 | path_ok = False |
|
517 | 528 | log.error('Given path %s is not readable', path) |
|
518 | 529 | |
|
519 | 530 | # check write access, warn user about non writeable paths |
|
520 | 531 | elif not os.access(path, os.W_OK) and path_ok: |
|
521 | 532 | log.warning('No write permission to given path %s', path) |
|
522 | 533 | |
|
523 | 534 | q = ('Given path %s is not writeable, do you want to ' |
|
524 | 535 | 'continue with read only mode ? [y/n]' % (path,)) |
|
525 | 536 | if not self.ask_ok(q): |
|
526 | 537 | log.error('Canceled by user') |
|
527 | 538 | sys.exit(-1) |
|
528 | 539 | |
|
529 | 540 | if retries == 0: |
|
530 | 541 | sys.exit('max retries reached') |
|
531 | 542 | if not path_ok: |
|
532 | 543 | retries -= 1 |
|
533 | 544 | return self.config_prompt(test_repo_path, retries) |
|
534 | 545 | |
|
535 | 546 | real_path = os.path.normpath(os.path.realpath(path)) |
|
536 | 547 | |
|
537 | 548 | if real_path != os.path.normpath(path): |
|
538 | 549 | q = ('Path looks like a symlink, RhodeCode Enterprise will store ' |
|
539 | 550 | 'given path as %s ? [y/n]') % (real_path,) |
|
540 | 551 | if not self.ask_ok(q): |
|
541 | 552 | log.error('Canceled by user') |
|
542 | 553 | sys.exit(-1) |
|
543 | 554 | |
|
544 | 555 | return real_path |
|
545 | 556 | |
|
546 | 557 | def create_settings(self, path): |
|
547 | 558 | |
|
548 | 559 | self.create_ui_settings(path) |
|
549 | 560 | |
|
550 | 561 | ui_config = [ |
|
551 | 562 | ('web', 'push_ssl', 'False'), |
|
552 | 563 | ('web', 'allow_archive', 'gz zip bz2'), |
|
553 | 564 | ('web', 'allow_push', '*'), |
|
554 | 565 | ('web', 'baseurl', '/'), |
|
555 | 566 | ('paths', '/', path), |
|
556 | 567 | ('phases', 'publish', 'True') |
|
557 | 568 | ] |
|
558 | 569 | for section, key, value in ui_config: |
|
559 | 570 | ui_conf = RhodeCodeUi() |
|
560 | 571 | setattr(ui_conf, 'ui_section', section) |
|
561 | 572 | setattr(ui_conf, 'ui_key', key) |
|
562 | 573 | setattr(ui_conf, 'ui_value', value) |
|
563 | 574 | self.sa.add(ui_conf) |
|
564 | 575 | |
|
565 | 576 | # rhodecode app settings |
|
566 | 577 | settings = [ |
|
567 | 578 | ('realm', 'RhodeCode', 'unicode'), |
|
568 | 579 | ('title', '', 'unicode'), |
|
569 | 580 | ('pre_code', '', 'unicode'), |
|
570 | 581 | ('post_code', '', 'unicode'), |
|
582 | ||
|
583 | # Visual | |
|
571 | 584 | ('show_public_icon', True, 'bool'), |
|
572 | 585 | ('show_private_icon', True, 'bool'), |
|
573 | 586 | ('stylify_metatags', False, 'bool'), |
|
574 | 587 | ('dashboard_items', 100, 'int'), |
|
575 | 588 | ('admin_grid_items', 25, 'int'), |
|
589 | ||
|
590 | ('markup_renderer', 'markdown', 'unicode'), | |
|
591 | ||
|
576 | 592 | ('show_version', True, 'bool'), |
|
593 | ('show_revision_number', True, 'bool'), | |
|
594 | ('show_sha_length', 12, 'int'), | |
|
595 | ||
|
577 | 596 | ('use_gravatar', False, 'bool'), |
|
578 | 597 | ('gravatar_url', User.DEFAULT_GRAVATAR_URL, 'unicode'), |
|
598 | ||
|
579 | 599 | ('clone_uri_tmpl', Repository.DEFAULT_CLONE_URI, 'unicode'), |
|
600 | ('clone_uri_ssh_tmpl', Repository.DEFAULT_CLONE_URI_SSH, 'unicode'), | |
|
580 | 601 | ('support_url', '', 'unicode'), |
|
581 | 602 | ('update_url', RhodeCodeSetting.DEFAULT_UPDATE_URL, 'unicode'), |
|
582 | ('show_revision_number', True, 'bool'), | |
|
583 | ('show_sha_length', 12, 'int'), | |
|
603 | ||
|
604 | # VCS Settings | |
|
605 | ('pr_merge_enabled', True, 'bool'), | |
|
606 | ('use_outdated_comments', True, 'bool'), | |
|
607 | ('diff_cache', True, 'bool'), | |
|
584 | 608 | ] |
|
585 | 609 | |
|
586 | 610 | for key, val, type_ in settings: |
|
587 | 611 | sett = RhodeCodeSetting(key, val, type_) |
|
588 | 612 | self.sa.add(sett) |
|
589 | 613 | |
|
590 | 614 | self.create_auth_plugin_options() |
|
591 | 615 | self.create_default_options() |
|
592 | 616 | |
|
593 | 617 | log.info('created ui config') |
|
594 | 618 | |
|
595 | 619 | def create_user(self, username, password, email='', admin=False, |
|
596 | 620 | strict_creation_check=True, api_key=None): |
|
597 | 621 | log.info('creating user `%s`', username) |
|
598 | 622 | user = UserModel().create_or_update( |
|
599 | 623 | username, password, email, firstname=u'RhodeCode', lastname=u'Admin', |
|
600 | 624 | active=True, admin=admin, extern_type="rhodecode", |
|
601 | 625 | strict_creation_check=strict_creation_check) |
|
602 | 626 | |
|
603 | 627 | if api_key: |
|
604 | 628 | log.info('setting a new default auth token for user `%s`', username) |
|
605 | 629 | UserModel().add_auth_token( |
|
606 | 630 | user=user, lifetime_minutes=-1, |
|
607 | 631 | role=UserModel.auth_token_role.ROLE_ALL, |
|
608 | 632 | description=u'BUILTIN TOKEN') |
|
609 | 633 | |
|
610 | 634 | def create_default_user(self): |
|
611 | 635 | log.info('creating default user') |
|
612 | 636 | # create default user for handling default permissions. |
|
613 | 637 | user = UserModel().create_or_update(username=User.DEFAULT_USER, |
|
614 | 638 | password=str(uuid.uuid1())[:20], |
|
615 | 639 | email=User.DEFAULT_USER_EMAIL, |
|
616 | 640 | firstname=u'Anonymous', |
|
617 | 641 | lastname=u'User', |
|
618 | 642 | strict_creation_check=False) |
|
619 | 643 | # based on configuration options activate/de-activate this user which |
|
620 | 644 | # controlls anonymous access |
|
621 | 645 | if self.cli_args.get('public_access') is False: |
|
622 | 646 | log.info('Public access disabled') |
|
623 | 647 | user.active = False |
|
624 | 648 | Session().add(user) |
|
625 | 649 | Session().commit() |
|
626 | 650 | |
|
627 | 651 | def create_permissions(self): |
|
628 | 652 | """ |
|
629 | 653 | Creates all permissions defined in the system |
|
630 | 654 | """ |
|
631 | 655 | # module.(access|create|change|delete)_[name] |
|
632 | 656 | # module.(none|read|write|admin) |
|
633 | 657 | log.info('creating permissions') |
|
634 | 658 | PermissionModel(self.sa).create_permissions() |
|
635 | 659 | |
|
636 | 660 | def populate_default_permissions(self): |
|
637 | 661 | """ |
|
638 | 662 | Populate default permissions. It will create only the default |
|
639 | 663 | permissions that are missing, and not alter already defined ones |
|
640 | 664 | """ |
|
641 | 665 | log.info('creating default user permissions') |
|
642 | 666 | PermissionModel(self.sa).create_default_user_permissions(user=User.DEFAULT_USER) |
General Comments 0
You need to be logged in to leave comments.
Login now