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