comparison rhodecode/lib/db_manage.py @ 1216:8363b0d20c41

fixes for stable
author Marcin Kuzminski <marcin@python-works.com>
date Tue, 05 Apr 2011 18:01:19 +0200
parents 93b980ebee55
children bf263968da47
comparison
equal deleted inserted replaced
1215:8a153dba7033 1216:8363b0d20c41
3 rhodecode.lib.db_manage 3 rhodecode.lib.db_manage
4 ~~~~~~~~~~~~~~~~~~~~~~~ 4 ~~~~~~~~~~~~~~~~~~~~~~~
5 5
6 Database creation, and setup module for RhodeCode. Used for creation 6 Database creation, and setup module for RhodeCode. Used for creation
7 of database as well as for migration operations 7 of database as well as for migration operations
8 8
9 :created_on: Apr 10, 2010 9 :created_on: Apr 10, 2010
10 :author: marcink 10 :author: marcink
11 :copyright: (C) 2009-2011 Marcin Kuzminski <marcin@python-works.com> 11 :copyright: (C) 2009-2011 Marcin Kuzminski <marcin@python-works.com>
12 :license: GPLv3, see COPYING for more details. 12 :license: GPLv3, see COPYING for more details.
13 """ 13 """
14 # This program is free software; you can redistribute it and/or 14 # This program is free software: you can redistribute it and/or modify
15 # modify it under the terms of the GNU General Public License 15 # it under the terms of the GNU General Public License as published by
16 # as published by the Free Software Foundation; version 2 16 # the Free Software Foundation, either version 3 of the License, or
17 # of the License or (at your opinion) any later version of the license. 17 # (at your option) any later version.
18 # 18 #
19 # This program is distributed in the hope that it will be useful, 19 # This program is distributed in the hope that it will be useful,
20 # but WITHOUT ANY WARRANTY; without even the implied warranty of 20 # but WITHOUT ANY WARRANTY; without even the implied warranty of
21 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 21 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 # GNU General Public License for more details. 22 # GNU General Public License for more details.
23 # 23 #
24 # You should have received a copy of the GNU General Public License 24 # You should have received a copy of the GNU General Public License
25 # along with this program; if not, write to the Free Software 25 # along with this program. If not, see <http://www.gnu.org/licenses/>.
26 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
27 # MA 02110-1301, USA.
28 26
29 import os 27 import os
30 import sys 28 import sys
31 import uuid 29 import uuid
32 import logging 30 import logging
36 from rhodecode.model import meta 34 from rhodecode.model import meta
37 35
38 from rhodecode.lib.auth import get_crypt_password 36 from rhodecode.lib.auth import get_crypt_password
39 from rhodecode.lib.utils import ask_ok 37 from rhodecode.lib.utils import ask_ok
40 from rhodecode.model import init_model 38 from rhodecode.model import init_model
41 from rhodecode.model.db import User, Permission, RhodeCodeUi, RhodeCodeSettings, \ 39 from rhodecode.model.db import User, Permission, RhodeCodeUi, \
42 UserToPerm, DbMigrateVersion 40 RhodeCodeSettings, UserToPerm, DbMigrateVersion
43 41
44 from sqlalchemy.engine import create_engine 42 from sqlalchemy.engine import create_engine
45 43
46 log = logging.getLogger(__name__) 44 log = logging.getLogger(__name__)
45
47 46
48 class DbManage(object): 47 class DbManage(object):
49 def __init__(self, log_sql, dbconf, root, tests=False): 48 def __init__(self, log_sql, dbconf, root, tests=False):
50 self.dbname = dbconf.split('/')[-1] 49 self.dbname = dbconf.split('/')[-1]
51 self.tests = tests 50 self.tests = tests
76 75
77 checkfirst = not override 76 checkfirst = not override
78 meta.Base.metadata.create_all(checkfirst=checkfirst) 77 meta.Base.metadata.create_all(checkfirst=checkfirst)
79 log.info('Created tables for %s', self.dbname) 78 log.info('Created tables for %s', self.dbname)
80 79
81
82
83 def set_db_version(self): 80 def set_db_version(self):
84 try: 81 try:
85 ver = DbMigrateVersion() 82 ver = DbMigrateVersion()
86 ver.version = __dbversion__ 83 ver.version = __dbversion__
87 ver.repository_id = 'rhodecode_db_migrations' 84 ver.repository_id = 'rhodecode_db_migrations'
91 except: 88 except:
92 self.sa.rollback() 89 self.sa.rollback()
93 raise 90 raise
94 log.info('db version set to: %s', __dbversion__) 91 log.info('db version set to: %s', __dbversion__)
95 92
96
97 def upgrade(self): 93 def upgrade(self):
98 """Upgrades given database schema to given revision following 94 """Upgrades given database schema to given revision following
99 all needed steps, to perform the upgrade 95 all needed steps, to perform the upgrade
100 96
101 :param revision: revision to upgrade to
102 """ 97 """
103 98
104 from rhodecode.lib.dbmigrate.migrate.versioning import api 99 from rhodecode.lib.dbmigrate.migrate.versioning import api
105 from rhodecode.lib.dbmigrate.migrate.exceptions import \ 100 from rhodecode.lib.dbmigrate.migrate.exceptions import \
106 DatabaseNotControlledError 101 DatabaseNotControlledError
133 128
134 #====================================================================== 129 #======================================================================
135 # UPGRADE STEPS 130 # UPGRADE STEPS
136 #====================================================================== 131 #======================================================================
137 class UpgradeSteps(object): 132 class UpgradeSteps(object):
138 """Those steps follow schema versions so for example schema 133 """Those steps follow schema versions so for example schema
139 for example schema with seq 002 == step_2 and so on. 134 for example schema with seq 002 == step_2 and so on.
140 """ 135 """
141 136
142 def __init__(self, klass): 137 def __init__(self, klass):
143 self.klass = klass 138 self.klass = klass
160 self.klass.fix_default_user() 155 self.klass.fix_default_user()
161 156
162 log.info('Changing ui settings') 157 log.info('Changing ui settings')
163 self.klass.create_ui_settings() 158 self.klass.create_ui_settings()
164 159
165
166 upgrade_steps = [0] + range(curr_version + 1, __dbversion__ + 1) 160 upgrade_steps = [0] + range(curr_version + 1, __dbversion__ + 1)
167 161
168 #CALL THE PROPER ORDER OF STEPS TO PERFORM FULL UPGRADE 162 #CALL THE PROPER ORDER OF STEPS TO PERFORM FULL UPGRADE
169 for step in upgrade_steps: 163 for step in upgrade_steps:
170 print ('performing upgrade step %s' % step) 164 print ('performing upgrade step %s' % step)
171 callable = getattr(UpgradeSteps(self), 'step_%s' % step)() 165 callable = getattr(UpgradeSteps(self), 'step_%s' % step)()
172
173
174 166
175 def fix_repo_paths(self): 167 def fix_repo_paths(self):
176 """Fixes a old rhodecode version path into new one without a '*' 168 """Fixes a old rhodecode version path into new one without a '*'
177 """ 169 """
178 170
206 self.sa.commit() 198 self.sa.commit()
207 except: 199 except:
208 self.sa.rollback() 200 self.sa.rollback()
209 raise 201 raise
210 202
211
212
213 def admin_prompt(self, second=False): 203 def admin_prompt(self, second=False):
214 if not self.tests: 204 if not self.tests:
215 import getpass 205 import getpass
216 206
217
218 def get_password(): 207 def get_password():
219 password = getpass.getpass('Specify admin password (min 6 chars):') 208 password = getpass.getpass('Specify admin password '
209 '(min 6 chars):')
220 confirm = getpass.getpass('Confirm password:') 210 confirm = getpass.getpass('Confirm password:')
221 211
222 if password != confirm: 212 if password != confirm:
223 log.error('passwords mismatch') 213 log.error('passwords mismatch')
224 return False 214 return False
239 229
240 email = raw_input('Specify admin email:') 230 email = raw_input('Specify admin email:')
241 self.create_user(username, password, email, True) 231 self.create_user(username, password, email, True)
242 else: 232 else:
243 log.info('creating admin and regular test users') 233 log.info('creating admin and regular test users')
244 self.create_user('test_admin', 'test12', 'test_admin@mail.com', True) 234 self.create_user('test_admin', 'test12',
245 self.create_user('test_regular', 'test12', 'test_regular@mail.com', False) 235 'test_admin@mail.com', True)
246 self.create_user('test_regular2', 'test12', 'test_regular2@mail.com', False) 236 self.create_user('test_regular', 'test12',
237 'test_regular@mail.com', False)
238 self.create_user('test_regular2', 'test12',
239 'test_regular2@mail.com', False)
247 240
248 def create_ui_settings(self): 241 def create_ui_settings(self):
249 """Creates ui settings, fills out hooks 242 """Creates ui settings, fills out hooks
250 and disables dotencode 243 and disables dotencode
251 244
252 """ 245 """
253 #HOOKS 246 #HOOKS
254 hooks1_key = 'changegroup.update' 247 hooks1_key = 'changegroup.update'
255 hooks1_ = self.sa.query(RhodeCodeUi)\ 248 hooks1_ = self.sa.query(RhodeCodeUi)\
256 .filter(RhodeCodeUi.ui_key == hooks1_key).scalar() 249 .filter(RhodeCodeUi.ui_key == hooks1_key).scalar()
294 self.sa.add(dotencode_disable) 287 self.sa.add(dotencode_disable)
295 self.sa.commit() 288 self.sa.commit()
296 except: 289 except:
297 self.sa.rollback() 290 self.sa.rollback()
298 raise 291 raise
299
300 292
301 def create_ldap_options(self): 293 def create_ldap_options(self):
302 """Creates ldap settings""" 294 """Creates ldap settings"""
303 295
304 try: 296 try:
314 self.sa.commit() 306 self.sa.commit()
315 except: 307 except:
316 self.sa.rollback() 308 self.sa.rollback()
317 raise 309 raise
318 310
319 def config_prompt(self, test_repo_path=''): 311 def config_prompt(self, test_repo_path='', retries=3):
320 log.info('Setting up repositories config') 312 if retries == 3:
313 log.info('Setting up repositories config')
321 314
322 if not self.tests and not test_repo_path: 315 if not self.tests and not test_repo_path:
323 path = raw_input('Specify valid full path to your repositories' 316 path = raw_input('Specify valid full path to your repositories'
324 ' you can change this later in application settings:') 317 ' you can change this later in application settings:')
325 else: 318 else:
326 path = test_repo_path 319 path = test_repo_path
327 320 path_ok = True
321
322 #check proper dir
328 if not os.path.isdir(path): 323 if not os.path.isdir(path):
329 log.error('You entered wrong path: %s', path) 324 path_ok = False
325 log.error('Entered path is not a valid directory: %s [%s/3]',
326 path, retries)
327
328 #check write access
329 if not os.access(path, os.W_OK):
330 path_ok = False
331
332 log.error('No write permission to given path: %s [%s/3]',
333 path, retries)
334
335 if retries == 0:
330 sys.exit() 336 sys.exit()
337 if path_ok is False:
338 retries -= 1
339 return self.config_prompt(test_repo_path, retries)
340
341 return path
342
343 def create_settings(self, path):
331 344
332 self.create_ui_settings() 345 self.create_ui_settings()
333 346
334 #HG UI OPTIONS 347 #HG UI OPTIONS
335 web1 = RhodeCodeUi() 348 web1 = RhodeCodeUi()
355 paths = RhodeCodeUi() 368 paths = RhodeCodeUi()
356 paths.ui_section = 'paths' 369 paths.ui_section = 'paths'
357 paths.ui_key = '/' 370 paths.ui_key = '/'
358 paths.ui_value = path 371 paths.ui_value = path
359 372
360
361 hgsettings1 = RhodeCodeSettings('realm', 'RhodeCode authentication') 373 hgsettings1 = RhodeCodeSettings('realm', 'RhodeCode authentication')
362 hgsettings2 = RhodeCodeSettings('title', 'RhodeCode') 374 hgsettings2 = RhodeCodeSettings('title', 'RhodeCode')
363
364 375
365 try: 376 try:
366 self.sa.add(web1) 377 self.sa.add(web1)
367 self.sa.add(web2) 378 self.sa.add(web2)
368 self.sa.add(web3) 379 self.sa.add(web3)
425 ('repository.admin', 'Repository admin access'), 436 ('repository.admin', 'Repository admin access'),
426 ('hg.admin', 'Hg Administrator'), 437 ('hg.admin', 'Hg Administrator'),
427 ('hg.create.repository', 'Repository create'), 438 ('hg.create.repository', 'Repository create'),
428 ('hg.create.none', 'Repository creation disabled'), 439 ('hg.create.none', 'Repository creation disabled'),
429 ('hg.register.none', 'Register disabled'), 440 ('hg.register.none', 'Register disabled'),
430 ('hg.register.manual_activate', 'Register new user with rhodecode without manual activation'), 441 ('hg.register.manual_activate', ('Register new user with '
431 ('hg.register.auto_activate', 'Register new user with rhodecode without auto activation'), 442 'RhodeCode without '
443 'manual activation')),
444 ('hg.register.auto_activate', ('Register new user with '
445 'RhodeCode without auto '
446 'activation')),
432 ] 447 ]
433 448
434 for p in perms: 449 for p in perms:
435 new_perm = Permission() 450 new_perm = Permission()
436 new_perm.permission_name = p[0] 451 new_perm.permission_name = p[0]
472 self.sa.add(default_repo_perm) 487 self.sa.add(default_repo_perm)
473 self.sa.commit() 488 self.sa.commit()
474 except: 489 except:
475 self.sa.rollback() 490 self.sa.rollback()
476 raise 491 raise
477