##// END OF EJS Templates
fixed path issues in upgrade script
marcink -
r843:3b21ba5f beta
parent child Browse files
Show More
@@ -1,476 +1,477 b''
1 # -*- coding: utf-8 -*-
1 # -*- coding: utf-8 -*-
2 """
2 """
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-2010 Marcin Kuzminski <marcin@python-works.com>
11 :copyright: (C) 2009-2010 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
15 # modify it under the terms of the GNU General Public License
15 # modify it under the terms of the GNU General Public License
16 # as published by the Free Software Foundation; version 2
16 # as published by the Free Software Foundation; version 2
17 # of the License or (at your opinion) any later version of the license.
17 # of the License or (at your opinion) any later version of the license.
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, write to the Free Software
26 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
26 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
27 # MA 02110-1301, USA.
27 # MA 02110-1301, USA.
28
28
29 import os
29 import os
30 import sys
30 import sys
31 import uuid
31 import uuid
32 import logging
32 import logging
33 from os.path import dirname as dn, join as jn
33 from os.path import dirname as dn, join as jn
34
34
35 from rhodecode import __dbversion__
35 from rhodecode import __dbversion__
36 from rhodecode.model import meta
36 from rhodecode.model import meta
37
37
38 from rhodecode.lib.auth import get_crypt_password
38 from rhodecode.lib.auth import get_crypt_password
39 from rhodecode.lib.utils import ask_ok
39 from rhodecode.lib.utils import ask_ok
40 from rhodecode.model import init_model
40 from rhodecode.model import init_model
41 from rhodecode.model.db import User, Permission, RhodeCodeUi, RhodeCodeSettings, \
41 from rhodecode.model.db import User, Permission, RhodeCodeUi, RhodeCodeSettings, \
42 UserToPerm, DbMigrateVersion
42 UserToPerm, DbMigrateVersion
43
43
44 from sqlalchemy.engine import create_engine
44 from sqlalchemy.engine import create_engine
45
45
46 log = logging.getLogger(__name__)
46 log = logging.getLogger(__name__)
47
47
48 class DbManage(object):
48 class DbManage(object):
49 def __init__(self, log_sql, dbconf, root, tests=False):
49 def __init__(self, log_sql, dbconf, root, tests=False):
50 self.dbname = dbconf.split('/')[-1]
50 self.dbname = dbconf.split('/')[-1]
51 self.tests = tests
51 self.tests = tests
52 self.root = root
52 self.root = root
53 self.dburi = dbconf
53 self.dburi = dbconf
54 engine = create_engine(self.dburi, echo=log_sql)
54 engine = create_engine(self.dburi, echo=log_sql)
55 init_model(engine)
55 init_model(engine)
56 self.sa = meta.Session()
56 self.sa = meta.Session()
57 self.db_exists = False
57 self.db_exists = False
58
58
59 def check_for_db(self, override):
59 def check_for_db(self, override):
60 db_path = jn(self.root, self.dbname)
60 db_path = jn(self.root, self.dbname)
61 if self.dburi.startswith('sqlite'):
61 if self.dburi.startswith('sqlite'):
62 log.info('checking for existing db in %s', db_path)
62 log.info('checking for existing db in %s', db_path)
63 if os.path.isfile(db_path):
63 if os.path.isfile(db_path):
64
64
65 self.db_exists = True
65 self.db_exists = True
66 if not override:
66 if not override:
67 raise Exception('database already exists')
67 raise Exception('database already exists')
68
68
69 def create_tables(self, override=False):
69 def create_tables(self, override=False):
70 """Create a auth database
70 """Create a auth database
71 """
71 """
72
72
73 self.check_for_db(override)
73 self.check_for_db(override)
74 if self.db_exists:
74 if self.db_exists:
75 log.info("database exist and it's going to be destroyed")
75 log.info("database exist and it's going to be destroyed")
76 if self.tests:
76 if self.tests:
77 destroy = True
77 destroy = True
78 else:
78 else:
79 destroy = ask_ok('Are you sure to destroy old database ? [y/n]')
79 destroy = ask_ok('Are you sure to destroy old database ? [y/n]')
80 if not destroy:
80 if not destroy:
81 sys.exit()
81 sys.exit()
82 if self.db_exists and destroy:
82 if self.db_exists and destroy:
83 os.remove(jn(self.root, self.dbname))
83 os.remove(jn(self.root, self.dbname))
84 checkfirst = not override
84 checkfirst = not override
85 meta.Base.metadata.create_all(checkfirst=checkfirst)
85 meta.Base.metadata.create_all(checkfirst=checkfirst)
86 log.info('Created tables for %s', self.dbname)
86 log.info('Created tables for %s', self.dbname)
87
87
88
88
89
89
90 def set_db_version(self):
90 def set_db_version(self):
91 try:
91 try:
92 ver = DbMigrateVersion()
92 ver = DbMigrateVersion()
93 ver.version = __dbversion__
93 ver.version = __dbversion__
94 ver.repository_id = 'rhodecode_db_migrations'
94 ver.repository_id = 'rhodecode_db_migrations'
95 ver.repository_path = 'versions'
95 ver.repository_path = 'versions'
96 self.sa.add(ver)
96 self.sa.add(ver)
97 self.sa.commit()
97 self.sa.commit()
98 except:
98 except:
99 self.sa.rollback()
99 self.sa.rollback()
100 raise
100 raise
101 log.info('db version set to: %s', __dbversion__)
101 log.info('db version set to: %s', __dbversion__)
102
102
103
103
104 def upgrade(self):
104 def upgrade(self):
105 """Upgrades given database schema to given revision following
105 """Upgrades given database schema to given revision following
106 all needed steps,
106 all needed steps,
107
107
108 :param revision: revision to upgrade to
108 :param revision: revision to upgrade to
109 """
109 """
110
110
111 from rhodecode.lib.dbmigrate.migrate.versioning import api
111 from rhodecode.lib.dbmigrate.migrate.versioning import api
112 from rhodecode.lib.dbmigrate.migrate.exceptions import \
112 from rhodecode.lib.dbmigrate.migrate.exceptions import \
113 DatabaseNotControlledError
113 DatabaseNotControlledError
114
114
115 upgrade = ask_ok('You are about to perform database upgrade, make '
115 upgrade = ask_ok('You are about to perform database upgrade, make '
116 'sure You backed up your database before. '
116 'sure You backed up your database before. '
117 'Continue ? [y/n]')
117 'Continue ? [y/n]')
118 if not upgrade:
118 if not upgrade:
119 sys.exit('Nothing done')
119 sys.exit('Nothing done')
120
120
121 repository_path = 'rhodecode/lib/dbmigrate'
121 repository_path = jn(dn(dn(dn(os.path.realpath(__file__)))),
122 'rhodecode/lib/dbmigrate')
122 db_uri = self.dburi
123 db_uri = self.dburi
123
124
124 try:
125 try:
125 curr_version = api.db_version(db_uri, repository_path)
126 curr_version = api.db_version(db_uri, repository_path)
126 msg = ('Found current database under version'
127 msg = ('Found current database under version'
127 ' control with version %s' % curr_version)
128 ' control with version %s' % curr_version)
128
129
129 except (RuntimeError, DatabaseNotControlledError), e:
130 except (RuntimeError, DatabaseNotControlledError), e:
130 curr_version = 1
131 curr_version = 1
131 msg = ('Current database is not under version control. Setting'
132 msg = ('Current database is not under version control. Setting'
132 ' as version %s' % curr_version)
133 ' as version %s' % curr_version)
133 api.version_control(db_uri, repository_path, curr_version)
134 api.version_control(db_uri, repository_path, curr_version)
134
135
135 print (msg)
136 print (msg)
136
137
137 if curr_version == __dbversion__:
138 if curr_version == __dbversion__:
138 sys.exit('This database is already at the newest version')
139 sys.exit('This database is already at the newest version')
139
140
140 #======================================================================
141 #======================================================================
141 # UPGRADE STEPS
142 # UPGRADE STEPS
142 #======================================================================
143 #======================================================================
143 class UpgradeSteps(object):
144 class UpgradeSteps(object):
144
145
145 def __init__(self, klass):
146 def __init__(self, klass):
146 self.klass = klass
147 self.klass = klass
147
148
148 def step_0(self):
149 def step_0(self):
149 #step 0 is the schema upgrade, and than follow proper upgrades
150 #step 0 is the schema upgrade, and than follow proper upgrades
150 print ('attempting to do database upgrade to version %s' \
151 print ('attempting to do database upgrade to version %s' \
151 % __dbversion__)
152 % __dbversion__)
152 api.upgrade(db_uri, repository_path, __dbversion__)
153 api.upgrade(db_uri, repository_path, __dbversion__)
153 print ('Schema upgrade completed')
154 print ('Schema upgrade completed')
154
155
155 def step_1(self):
156 def step_1(self):
156 pass
157 pass
157
158
158 def step_2(self):
159 def step_2(self):
159 print ('Patching repo paths for newer version of RhodeCode')
160 print ('Patching repo paths for newer version of RhodeCode')
160 self.klass.fix_repo_paths()
161 self.klass.fix_repo_paths()
161
162
162 print ('Patching default user of RhodeCode')
163 print ('Patching default user of RhodeCode')
163 self.klass.fix_default_user()
164 self.klass.fix_default_user()
164
165
165 log.info('Changing ui settings')
166 log.info('Changing ui settings')
166 self.klass.create_ui_settings()
167 self.klass.create_ui_settings()
167
168
168
169
169 upgrade_steps = [0] + range(curr_version + 1, __dbversion__ + 1)
170 upgrade_steps = [0] + range(curr_version + 1, __dbversion__ + 1)
170
171
171 #CALL THE PROPER ORDER OF STEPS TO PERFORM FULL UPGRADE
172 #CALL THE PROPER ORDER OF STEPS TO PERFORM FULL UPGRADE
172 for step in upgrade_steps:
173 for step in upgrade_steps:
173 print ('performing upgrade step %s' % step)
174 print ('performing upgrade step %s' % step)
174 callable = getattr(UpgradeSteps(self), 'step_%s' % step)()
175 callable = getattr(UpgradeSteps(self), 'step_%s' % step)()
175
176
176
177
177
178
178 def fix_repo_paths(self):
179 def fix_repo_paths(self):
179 """Fixes a old rhodecode version path into new one without a '*'
180 """Fixes a old rhodecode version path into new one without a '*'
180 """
181 """
181
182
182 paths = self.sa.query(RhodeCodeUi)\
183 paths = self.sa.query(RhodeCodeUi)\
183 .filter(RhodeCodeUi.ui_key == '/')\
184 .filter(RhodeCodeUi.ui_key == '/')\
184 .scalar()
185 .scalar()
185
186
186 paths.ui_value = paths.ui_value.replace('*', '')
187 paths.ui_value = paths.ui_value.replace('*', '')
187
188
188 try:
189 try:
189 self.sa.add(paths)
190 self.sa.add(paths)
190 self.sa.commit()
191 self.sa.commit()
191 except:
192 except:
192 self.sa.rollback()
193 self.sa.rollback()
193 raise
194 raise
194
195
195 def fix_default_user(self):
196 def fix_default_user(self):
196 """Fixes a old default user with some 'nicer' default values,
197 """Fixes a old default user with some 'nicer' default values,
197 used mostly for anonymous access
198 used mostly for anonymous access
198 """
199 """
199 def_user = self.sa.query(User)\
200 def_user = self.sa.query(User)\
200 .filter(User.username == 'default')\
201 .filter(User.username == 'default')\
201 .one()
202 .one()
202
203
203 def_user.name = 'Anonymous'
204 def_user.name = 'Anonymous'
204 def_user.lastname = 'User'
205 def_user.lastname = 'User'
205 def_user.email = 'anonymous@rhodecode.org'
206 def_user.email = 'anonymous@rhodecode.org'
206
207
207 try:
208 try:
208 self.sa.add(def_user)
209 self.sa.add(def_user)
209 self.sa.commit()
210 self.sa.commit()
210 except:
211 except:
211 self.sa.rollback()
212 self.sa.rollback()
212 raise
213 raise
213
214
214
215
215
216
216 def admin_prompt(self, second=False):
217 def admin_prompt(self, second=False):
217 if not self.tests:
218 if not self.tests:
218 import getpass
219 import getpass
219
220
220
221
221 def get_password():
222 def get_password():
222 password = getpass.getpass('Specify admin password (min 6 chars):')
223 password = getpass.getpass('Specify admin password (min 6 chars):')
223 confirm = getpass.getpass('Confirm password:')
224 confirm = getpass.getpass('Confirm password:')
224
225
225 if password != confirm:
226 if password != confirm:
226 log.error('passwords mismatch')
227 log.error('passwords mismatch')
227 return False
228 return False
228 if len(password) < 6:
229 if len(password) < 6:
229 log.error('password is to short use at least 6 characters')
230 log.error('password is to short use at least 6 characters')
230 return False
231 return False
231
232
232 return password
233 return password
233
234
234 username = raw_input('Specify admin username:')
235 username = raw_input('Specify admin username:')
235
236
236 password = get_password()
237 password = get_password()
237 if not password:
238 if not password:
238 #second try
239 #second try
239 password = get_password()
240 password = get_password()
240 if not password:
241 if not password:
241 sys.exit()
242 sys.exit()
242
243
243 email = raw_input('Specify admin email:')
244 email = raw_input('Specify admin email:')
244 self.create_user(username, password, email, True)
245 self.create_user(username, password, email, True)
245 else:
246 else:
246 log.info('creating admin and regular test users')
247 log.info('creating admin and regular test users')
247 self.create_user('test_admin', 'test12', 'test_admin@mail.com', True)
248 self.create_user('test_admin', 'test12', 'test_admin@mail.com', True)
248 self.create_user('test_regular', 'test12', 'test_regular@mail.com', False)
249 self.create_user('test_regular', 'test12', 'test_regular@mail.com', False)
249 self.create_user('test_regular2', 'test12', 'test_regular2@mail.com', False)
250 self.create_user('test_regular2', 'test12', 'test_regular2@mail.com', False)
250
251
251 def create_ui_settings(self):
252 def create_ui_settings(self):
252 """Creates ui settings, fills out hooks
253 """Creates ui settings, fills out hooks
253 and disables dotencode
254 and disables dotencode
254
255
255 """
256 """
256 #HOOKS
257 #HOOKS
257 hooks1_key = 'changegroup.update'
258 hooks1_key = 'changegroup.update'
258 hooks1_ = self.sa.query(RhodeCodeUi)\
259 hooks1_ = self.sa.query(RhodeCodeUi)\
259 .filter(RhodeCodeUi.ui_key == hooks1_key).scalar()
260 .filter(RhodeCodeUi.ui_key == hooks1_key).scalar()
260
261
261 hooks1 = RhodeCodeUi() if hooks1_ is None else hooks1_
262 hooks1 = RhodeCodeUi() if hooks1_ is None else hooks1_
262 hooks1.ui_section = 'hooks'
263 hooks1.ui_section = 'hooks'
263 hooks1.ui_key = hooks1_key
264 hooks1.ui_key = hooks1_key
264 hooks1.ui_value = 'hg update >&2'
265 hooks1.ui_value = 'hg update >&2'
265 hooks1.ui_active = False
266 hooks1.ui_active = False
266
267
267 hooks2_key = 'changegroup.repo_size'
268 hooks2_key = 'changegroup.repo_size'
268 hooks2_ = self.sa.query(RhodeCodeUi)\
269 hooks2_ = self.sa.query(RhodeCodeUi)\
269 .filter(RhodeCodeUi.ui_key == hooks2_key).scalar()
270 .filter(RhodeCodeUi.ui_key == hooks2_key).scalar()
270
271
271 hooks2 = RhodeCodeUi() if hooks2_ is None else hooks2_
272 hooks2 = RhodeCodeUi() if hooks2_ is None else hooks2_
272 hooks2.ui_section = 'hooks'
273 hooks2.ui_section = 'hooks'
273 hooks2.ui_key = hooks2_key
274 hooks2.ui_key = hooks2_key
274 hooks2.ui_value = 'python:rhodecode.lib.hooks.repo_size'
275 hooks2.ui_value = 'python:rhodecode.lib.hooks.repo_size'
275
276
276 hooks3 = RhodeCodeUi()
277 hooks3 = RhodeCodeUi()
277 hooks3.ui_section = 'hooks'
278 hooks3.ui_section = 'hooks'
278 hooks3.ui_key = 'pretxnchangegroup.push_logger'
279 hooks3.ui_key = 'pretxnchangegroup.push_logger'
279 hooks3.ui_value = 'python:rhodecode.lib.hooks.log_push_action'
280 hooks3.ui_value = 'python:rhodecode.lib.hooks.log_push_action'
280
281
281 hooks4 = RhodeCodeUi()
282 hooks4 = RhodeCodeUi()
282 hooks4.ui_section = 'hooks'
283 hooks4.ui_section = 'hooks'
283 hooks4.ui_key = 'preoutgoing.pull_logger'
284 hooks4.ui_key = 'preoutgoing.pull_logger'
284 hooks4.ui_value = 'python:rhodecode.lib.hooks.log_pull_action'
285 hooks4.ui_value = 'python:rhodecode.lib.hooks.log_pull_action'
285
286
286 #For mercurial 1.7 set backward comapatibility with format
287 #For mercurial 1.7 set backward comapatibility with format
287 dotencode_disable = RhodeCodeUi()
288 dotencode_disable = RhodeCodeUi()
288 dotencode_disable.ui_section = 'format'
289 dotencode_disable.ui_section = 'format'
289 dotencode_disable.ui_key = 'dotencode'
290 dotencode_disable.ui_key = 'dotencode'
290 dotencode_disable.ui_value = 'false'
291 dotencode_disable.ui_value = 'false'
291
292
292 try:
293 try:
293 self.sa.add(hooks1)
294 self.sa.add(hooks1)
294 self.sa.add(hooks2)
295 self.sa.add(hooks2)
295 self.sa.add(hooks3)
296 self.sa.add(hooks3)
296 self.sa.add(hooks4)
297 self.sa.add(hooks4)
297 self.sa.add(dotencode_disable)
298 self.sa.add(dotencode_disable)
298 self.sa.commit()
299 self.sa.commit()
299 except:
300 except:
300 self.sa.rollback()
301 self.sa.rollback()
301 raise
302 raise
302
303
303
304
304 def create_ldap_options(self):
305 def create_ldap_options(self):
305 """Creates ldap settings"""
306 """Creates ldap settings"""
306
307
307 try:
308 try:
308 for k in ['ldap_active', 'ldap_host', 'ldap_port', 'ldap_ldaps',
309 for k in ['ldap_active', 'ldap_host', 'ldap_port', 'ldap_ldaps',
309 'ldap_dn_user', 'ldap_dn_pass', 'ldap_base_dn']:
310 'ldap_dn_user', 'ldap_dn_pass', 'ldap_base_dn']:
310
311
311 setting = RhodeCodeSettings(k, '')
312 setting = RhodeCodeSettings(k, '')
312 self.sa.add(setting)
313 self.sa.add(setting)
313 self.sa.commit()
314 self.sa.commit()
314 except:
315 except:
315 self.sa.rollback()
316 self.sa.rollback()
316 raise
317 raise
317
318
318 def config_prompt(self, test_repo_path=''):
319 def config_prompt(self, test_repo_path=''):
319 log.info('Setting up repositories config')
320 log.info('Setting up repositories config')
320
321
321 if not self.tests and not test_repo_path:
322 if not self.tests and not test_repo_path:
322 path = raw_input('Specify valid full path to your repositories'
323 path = raw_input('Specify valid full path to your repositories'
323 ' you can change this later in application settings:')
324 ' you can change this later in application settings:')
324 else:
325 else:
325 path = test_repo_path
326 path = test_repo_path
326
327
327 if not os.path.isdir(path):
328 if not os.path.isdir(path):
328 log.error('You entered wrong path: %s', path)
329 log.error('You entered wrong path: %s', path)
329 sys.exit()
330 sys.exit()
330
331
331 self.create_ui_settings()
332 self.create_ui_settings()
332
333
333 #HG UI OPTIONS
334 #HG UI OPTIONS
334 web1 = RhodeCodeUi()
335 web1 = RhodeCodeUi()
335 web1.ui_section = 'web'
336 web1.ui_section = 'web'
336 web1.ui_key = 'push_ssl'
337 web1.ui_key = 'push_ssl'
337 web1.ui_value = 'false'
338 web1.ui_value = 'false'
338
339
339 web2 = RhodeCodeUi()
340 web2 = RhodeCodeUi()
340 web2.ui_section = 'web'
341 web2.ui_section = 'web'
341 web2.ui_key = 'allow_archive'
342 web2.ui_key = 'allow_archive'
342 web2.ui_value = 'gz zip bz2'
343 web2.ui_value = 'gz zip bz2'
343
344
344 web3 = RhodeCodeUi()
345 web3 = RhodeCodeUi()
345 web3.ui_section = 'web'
346 web3.ui_section = 'web'
346 web3.ui_key = 'allow_push'
347 web3.ui_key = 'allow_push'
347 web3.ui_value = '*'
348 web3.ui_value = '*'
348
349
349 web4 = RhodeCodeUi()
350 web4 = RhodeCodeUi()
350 web4.ui_section = 'web'
351 web4.ui_section = 'web'
351 web4.ui_key = 'baseurl'
352 web4.ui_key = 'baseurl'
352 web4.ui_value = '/'
353 web4.ui_value = '/'
353
354
354 paths = RhodeCodeUi()
355 paths = RhodeCodeUi()
355 paths.ui_section = 'paths'
356 paths.ui_section = 'paths'
356 paths.ui_key = '/'
357 paths.ui_key = '/'
357 paths.ui_value = path
358 paths.ui_value = path
358
359
359
360
360 hgsettings1 = RhodeCodeSettings('realm', 'RhodeCode authentication')
361 hgsettings1 = RhodeCodeSettings('realm', 'RhodeCode authentication')
361 hgsettings2 = RhodeCodeSettings('title', 'RhodeCode')
362 hgsettings2 = RhodeCodeSettings('title', 'RhodeCode')
362
363
363
364
364 try:
365 try:
365 self.sa.add(web1)
366 self.sa.add(web1)
366 self.sa.add(web2)
367 self.sa.add(web2)
367 self.sa.add(web3)
368 self.sa.add(web3)
368 self.sa.add(web4)
369 self.sa.add(web4)
369 self.sa.add(paths)
370 self.sa.add(paths)
370 self.sa.add(hgsettings1)
371 self.sa.add(hgsettings1)
371 self.sa.add(hgsettings2)
372 self.sa.add(hgsettings2)
372
373
373 self.sa.commit()
374 self.sa.commit()
374 except:
375 except:
375 self.sa.rollback()
376 self.sa.rollback()
376 raise
377 raise
377
378
378 self.create_ldap_options()
379 self.create_ldap_options()
379
380
380 log.info('created ui config')
381 log.info('created ui config')
381
382
382 def create_user(self, username, password, email='', admin=False):
383 def create_user(self, username, password, email='', admin=False):
383 log.info('creating administrator user %s', username)
384 log.info('creating administrator user %s', username)
384 new_user = User()
385 new_user = User()
385 new_user.username = username
386 new_user.username = username
386 new_user.password = get_crypt_password(password)
387 new_user.password = get_crypt_password(password)
387 new_user.name = 'RhodeCode'
388 new_user.name = 'RhodeCode'
388 new_user.lastname = 'Admin'
389 new_user.lastname = 'Admin'
389 new_user.email = email
390 new_user.email = email
390 new_user.admin = admin
391 new_user.admin = admin
391 new_user.active = True
392 new_user.active = True
392
393
393 try:
394 try:
394 self.sa.add(new_user)
395 self.sa.add(new_user)
395 self.sa.commit()
396 self.sa.commit()
396 except:
397 except:
397 self.sa.rollback()
398 self.sa.rollback()
398 raise
399 raise
399
400
400 def create_default_user(self):
401 def create_default_user(self):
401 log.info('creating default user')
402 log.info('creating default user')
402 #create default user for handling default permissions.
403 #create default user for handling default permissions.
403 def_user = User()
404 def_user = User()
404 def_user.username = 'default'
405 def_user.username = 'default'
405 def_user.password = get_crypt_password(str(uuid.uuid1())[:8])
406 def_user.password = get_crypt_password(str(uuid.uuid1())[:8])
406 def_user.name = 'Anonymous'
407 def_user.name = 'Anonymous'
407 def_user.lastname = 'User'
408 def_user.lastname = 'User'
408 def_user.email = 'anonymous@rhodecode.org'
409 def_user.email = 'anonymous@rhodecode.org'
409 def_user.admin = False
410 def_user.admin = False
410 def_user.active = False
411 def_user.active = False
411 try:
412 try:
412 self.sa.add(def_user)
413 self.sa.add(def_user)
413 self.sa.commit()
414 self.sa.commit()
414 except:
415 except:
415 self.sa.rollback()
416 self.sa.rollback()
416 raise
417 raise
417
418
418 def create_permissions(self):
419 def create_permissions(self):
419 #module.(access|create|change|delete)_[name]
420 #module.(access|create|change|delete)_[name]
420 #module.(read|write|owner)
421 #module.(read|write|owner)
421 perms = [('repository.none', 'Repository no access'),
422 perms = [('repository.none', 'Repository no access'),
422 ('repository.read', 'Repository read access'),
423 ('repository.read', 'Repository read access'),
423 ('repository.write', 'Repository write access'),
424 ('repository.write', 'Repository write access'),
424 ('repository.admin', 'Repository admin access'),
425 ('repository.admin', 'Repository admin access'),
425 ('hg.admin', 'Hg Administrator'),
426 ('hg.admin', 'Hg Administrator'),
426 ('hg.create.repository', 'Repository create'),
427 ('hg.create.repository', 'Repository create'),
427 ('hg.create.none', 'Repository creation disabled'),
428 ('hg.create.none', 'Repository creation disabled'),
428 ('hg.register.none', 'Register disabled'),
429 ('hg.register.none', 'Register disabled'),
429 ('hg.register.manual_activate', 'Register new user with rhodecode without manual activation'),
430 ('hg.register.manual_activate', 'Register new user with rhodecode without manual activation'),
430 ('hg.register.auto_activate', 'Register new user with rhodecode without auto activation'),
431 ('hg.register.auto_activate', 'Register new user with rhodecode without auto activation'),
431 ]
432 ]
432
433
433 for p in perms:
434 for p in perms:
434 new_perm = Permission()
435 new_perm = Permission()
435 new_perm.permission_name = p[0]
436 new_perm.permission_name = p[0]
436 new_perm.permission_longname = p[1]
437 new_perm.permission_longname = p[1]
437 try:
438 try:
438 self.sa.add(new_perm)
439 self.sa.add(new_perm)
439 self.sa.commit()
440 self.sa.commit()
440 except:
441 except:
441 self.sa.rollback()
442 self.sa.rollback()
442 raise
443 raise
443
444
444 def populate_default_permissions(self):
445 def populate_default_permissions(self):
445 log.info('creating default user permissions')
446 log.info('creating default user permissions')
446
447
447 default_user = self.sa.query(User)\
448 default_user = self.sa.query(User)\
448 .filter(User.username == 'default').scalar()
449 .filter(User.username == 'default').scalar()
449
450
450 reg_perm = UserToPerm()
451 reg_perm = UserToPerm()
451 reg_perm.user = default_user
452 reg_perm.user = default_user
452 reg_perm.permission = self.sa.query(Permission)\
453 reg_perm.permission = self.sa.query(Permission)\
453 .filter(Permission.permission_name == 'hg.register.manual_activate')\
454 .filter(Permission.permission_name == 'hg.register.manual_activate')\
454 .scalar()
455 .scalar()
455
456
456 create_repo_perm = UserToPerm()
457 create_repo_perm = UserToPerm()
457 create_repo_perm.user = default_user
458 create_repo_perm.user = default_user
458 create_repo_perm.permission = self.sa.query(Permission)\
459 create_repo_perm.permission = self.sa.query(Permission)\
459 .filter(Permission.permission_name == 'hg.create.repository')\
460 .filter(Permission.permission_name == 'hg.create.repository')\
460 .scalar()
461 .scalar()
461
462
462 default_repo_perm = UserToPerm()
463 default_repo_perm = UserToPerm()
463 default_repo_perm.user = default_user
464 default_repo_perm.user = default_user
464 default_repo_perm.permission = self.sa.query(Permission)\
465 default_repo_perm.permission = self.sa.query(Permission)\
465 .filter(Permission.permission_name == 'repository.read')\
466 .filter(Permission.permission_name == 'repository.read')\
466 .scalar()
467 .scalar()
467
468
468 try:
469 try:
469 self.sa.add(reg_perm)
470 self.sa.add(reg_perm)
470 self.sa.add(create_repo_perm)
471 self.sa.add(create_repo_perm)
471 self.sa.add(default_repo_perm)
472 self.sa.add(default_repo_perm)
472 self.sa.commit()
473 self.sa.commit()
473 except:
474 except:
474 self.sa.rollback()
475 self.sa.rollback()
475 raise
476 raise
476
477
General Comments 0
You need to be logged in to leave comments. Login now