##// END OF EJS Templates
Added group permission autofix for older version of rhodecode which didn't have default permissions for repos groups
marcink -
r1985:40f04021 beta
parent child Browse files
Show More
@@ -1,500 +1,517 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) 2010-2012 Marcin Kuzminski <marcin@python-works.com>
11 :copyright: (C) 2010-2012 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, RepoGroup,\
41 UserRepoGroupToPerm
41
42
42 from sqlalchemy.engine import create_engine
43 from sqlalchemy.engine import create_engine
44 from rhodecode.model.repos_group import ReposGroupModel
43
45
44 log = logging.getLogger(__name__)
46 log = logging.getLogger(__name__)
45
47
46
48
47 class DbManage(object):
49 class DbManage(object):
48 def __init__(self, log_sql, dbconf, root, tests=False):
50 def __init__(self, log_sql, dbconf, root, tests=False):
49 self.dbname = dbconf.split('/')[-1]
51 self.dbname = dbconf.split('/')[-1]
50 self.tests = tests
52 self.tests = tests
51 self.root = root
53 self.root = root
52 self.dburi = dbconf
54 self.dburi = dbconf
53 self.log_sql = log_sql
55 self.log_sql = log_sql
54 self.db_exists = False
56 self.db_exists = False
55 self.init_db()
57 self.init_db()
56
58
57 def init_db(self):
59 def init_db(self):
58 engine = create_engine(self.dburi, echo=self.log_sql)
60 engine = create_engine(self.dburi, echo=self.log_sql)
59 init_model(engine)
61 init_model(engine)
60 self.sa = meta.Session
62 self.sa = meta.Session
61
63
62 def create_tables(self, override=False):
64 def create_tables(self, override=False):
63 """
65 """
64 Create a auth database
66 Create a auth database
65 """
67 """
66
68
67 log.info("Any existing database is going to be destroyed")
69 log.info("Any existing database is going to be destroyed")
68 if self.tests:
70 if self.tests:
69 destroy = True
71 destroy = True
70 else:
72 else:
71 destroy = ask_ok('Are you sure to destroy old database ? [y/n]')
73 destroy = ask_ok('Are you sure to destroy old database ? [y/n]')
72 if not destroy:
74 if not destroy:
73 sys.exit()
75 sys.exit()
74 if destroy:
76 if destroy:
75 meta.Base.metadata.drop_all()
77 meta.Base.metadata.drop_all()
76
78
77 checkfirst = not override
79 checkfirst = not override
78 meta.Base.metadata.create_all(checkfirst=checkfirst)
80 meta.Base.metadata.create_all(checkfirst=checkfirst)
79 log.info('Created tables for %s' % self.dbname)
81 log.info('Created tables for %s' % self.dbname)
80
82
81 def set_db_version(self):
83 def set_db_version(self):
82 ver = DbMigrateVersion()
84 ver = DbMigrateVersion()
83 ver.version = __dbversion__
85 ver.version = __dbversion__
84 ver.repository_id = 'rhodecode_db_migrations'
86 ver.repository_id = 'rhodecode_db_migrations'
85 ver.repository_path = 'versions'
87 ver.repository_path = 'versions'
86 self.sa.add(ver)
88 self.sa.add(ver)
87 log.info('db version set to: %s' % __dbversion__)
89 log.info('db version set to: %s' % __dbversion__)
88
90
89 def upgrade(self):
91 def upgrade(self):
90 """
92 """
91 Upgrades given database schema to given revision following
93 Upgrades given database schema to given revision following
92 all needed steps, to perform the upgrade
94 all needed steps, to perform the upgrade
93
95
94 """
96 """
95
97
96 from rhodecode.lib.dbmigrate.migrate.versioning import api
98 from rhodecode.lib.dbmigrate.migrate.versioning import api
97 from rhodecode.lib.dbmigrate.migrate.exceptions import \
99 from rhodecode.lib.dbmigrate.migrate.exceptions import \
98 DatabaseNotControlledError
100 DatabaseNotControlledError
99
101
100 if 'sqlite' in self.dburi:
102 if 'sqlite' in self.dburi:
101 print (
103 print (
102 '********************** WARNING **********************\n'
104 '********************** WARNING **********************\n'
103 'Make sure your version of sqlite is at least 3.7.X. \n'
105 'Make sure your version of sqlite is at least 3.7.X. \n'
104 'Earlier versions are known to fail on some migrations\n'
106 'Earlier versions are known to fail on some migrations\n'
105 '*****************************************************\n'
107 '*****************************************************\n'
106 )
108 )
107 upgrade = ask_ok('You are about to perform database upgrade, make '
109 upgrade = ask_ok('You are about to perform database upgrade, make '
108 'sure You backed up your database before. '
110 'sure You backed up your database before. '
109 'Continue ? [y/n]')
111 'Continue ? [y/n]')
110 if not upgrade:
112 if not upgrade:
111 sys.exit('Nothing done')
113 sys.exit('Nothing done')
112
114
113 repository_path = jn(dn(dn(dn(os.path.realpath(__file__)))),
115 repository_path = jn(dn(dn(dn(os.path.realpath(__file__)))),
114 'rhodecode/lib/dbmigrate')
116 'rhodecode/lib/dbmigrate')
115 db_uri = self.dburi
117 db_uri = self.dburi
116
118
117 try:
119 try:
118 curr_version = api.db_version(db_uri, repository_path)
120 curr_version = api.db_version(db_uri, repository_path)
119 msg = ('Found current database under version'
121 msg = ('Found current database under version'
120 ' control with version %s' % curr_version)
122 ' control with version %s' % curr_version)
121
123
122 except (RuntimeError, DatabaseNotControlledError):
124 except (RuntimeError, DatabaseNotControlledError):
123 curr_version = 1
125 curr_version = 1
124 msg = ('Current database is not under version control. Setting'
126 msg = ('Current database is not under version control. Setting'
125 ' as version %s' % curr_version)
127 ' as version %s' % curr_version)
126 api.version_control(db_uri, repository_path, curr_version)
128 api.version_control(db_uri, repository_path, curr_version)
127
129
128 print (msg)
130 print (msg)
129
131
130 if curr_version == __dbversion__:
132 if curr_version == __dbversion__:
131 sys.exit('This database is already at the newest version')
133 sys.exit('This database is already at the newest version')
132
134
133 #======================================================================
135 #======================================================================
134 # UPGRADE STEPS
136 # UPGRADE STEPS
135 #======================================================================
137 #======================================================================
136 class UpgradeSteps(object):
138 class UpgradeSteps(object):
137 """
139 """
138 Those steps follow schema versions so for example schema
140 Those steps follow schema versions so for example schema
139 for example schema with seq 002 == step_2 and so on.
141 for example schema with seq 002 == step_2 and so on.
140 """
142 """
141
143
142 def __init__(self, klass):
144 def __init__(self, klass):
143 self.klass = klass
145 self.klass = klass
144
146
145 def step_0(self):
147 def step_0(self):
146 # step 0 is the schema upgrade, and than follow proper upgrades
148 # step 0 is the schema upgrade, and than follow proper upgrades
147 print ('attempting to do database upgrade to version %s' \
149 print ('attempting to do database upgrade to version %s' \
148 % __dbversion__)
150 % __dbversion__)
149 api.upgrade(db_uri, repository_path, __dbversion__)
151 api.upgrade(db_uri, repository_path, __dbversion__)
150 print ('Schema upgrade completed')
152 print ('Schema upgrade completed')
151
153
152 def step_1(self):
154 def step_1(self):
153 pass
155 pass
154
156
155 def step_2(self):
157 def step_2(self):
156 print ('Patching repo paths for newer version of RhodeCode')
158 print ('Patching repo paths for newer version of RhodeCode')
157 self.klass.fix_repo_paths()
159 self.klass.fix_repo_paths()
158
160
159 print ('Patching default user of RhodeCode')
161 print ('Patching default user of RhodeCode')
160 self.klass.fix_default_user()
162 self.klass.fix_default_user()
161
163
162 log.info('Changing ui settings')
164 log.info('Changing ui settings')
163 self.klass.create_ui_settings()
165 self.klass.create_ui_settings()
164
166
165 def step_3(self):
167 def step_3(self):
166 print ('Adding additional settings into RhodeCode db')
168 print ('Adding additional settings into RhodeCode db')
167 self.klass.fix_settings()
169 self.klass.fix_settings()
168 print ('Adding ldap defaults')
170 print ('Adding ldap defaults')
169 self.klass.create_ldap_options(skip_existing=True)
171 self.klass.create_ldap_options(skip_existing=True)
170
172
171 def step_4(self):
173 def step_4(self):
172 print ('TODO:')
174 print ('TODO:')
173 raise NotImplementedError()
175 self.klass.fixup_groups()
174
176
175 upgrade_steps = [0] + range(curr_version + 1, __dbversion__ + 1)
177 upgrade_steps = [0] + range(curr_version + 1, __dbversion__ + 1)
176
178
177 # CALL THE PROPER ORDER OF STEPS TO PERFORM FULL UPGRADE
179 # CALL THE PROPER ORDER OF STEPS TO PERFORM FULL UPGRADE
178 for step in upgrade_steps:
180 for step in upgrade_steps:
179 print ('performing upgrade step %s' % step)
181 print ('performing upgrade step %s' % step)
180 getattr(UpgradeSteps(self), 'step_%s' % step)()
182 getattr(UpgradeSteps(self), 'step_%s' % step)()
181
183
182 def fix_repo_paths(self):
184 def fix_repo_paths(self):
183 """
185 """
184 Fixes a old rhodecode version path into new one without a '*'
186 Fixes a old rhodecode version path into new one without a '*'
185 """
187 """
186
188
187 paths = self.sa.query(RhodeCodeUi)\
189 paths = self.sa.query(RhodeCodeUi)\
188 .filter(RhodeCodeUi.ui_key == '/')\
190 .filter(RhodeCodeUi.ui_key == '/')\
189 .scalar()
191 .scalar()
190
192
191 paths.ui_value = paths.ui_value.replace('*', '')
193 paths.ui_value = paths.ui_value.replace('*', '')
192
194
193 try:
195 try:
194 self.sa.add(paths)
196 self.sa.add(paths)
195 self.sa.commit()
197 self.sa.commit()
196 except:
198 except:
197 self.sa.rollback()
199 self.sa.rollback()
198 raise
200 raise
199
201
200 def fix_default_user(self):
202 def fix_default_user(self):
201 """
203 """
202 Fixes a old default user with some 'nicer' default values,
204 Fixes a old default user with some 'nicer' default values,
203 used mostly for anonymous access
205 used mostly for anonymous access
204 """
206 """
205 def_user = self.sa.query(User)\
207 def_user = self.sa.query(User)\
206 .filter(User.username == 'default')\
208 .filter(User.username == 'default')\
207 .one()
209 .one()
208
210
209 def_user.name = 'Anonymous'
211 def_user.name = 'Anonymous'
210 def_user.lastname = 'User'
212 def_user.lastname = 'User'
211 def_user.email = 'anonymous@rhodecode.org'
213 def_user.email = 'anonymous@rhodecode.org'
212
214
213 try:
215 try:
214 self.sa.add(def_user)
216 self.sa.add(def_user)
215 self.sa.commit()
217 self.sa.commit()
216 except:
218 except:
217 self.sa.rollback()
219 self.sa.rollback()
218 raise
220 raise
219
221
220 def fix_settings(self):
222 def fix_settings(self):
221 """
223 """
222 Fixes rhodecode settings adds ga_code key for google analytics
224 Fixes rhodecode settings adds ga_code key for google analytics
223 """
225 """
224
226
225 hgsettings3 = RhodeCodeSetting('ga_code', '')
227 hgsettings3 = RhodeCodeSetting('ga_code', '')
226
228
227 try:
229 try:
228 self.sa.add(hgsettings3)
230 self.sa.add(hgsettings3)
229 self.sa.commit()
231 self.sa.commit()
230 except:
232 except:
231 self.sa.rollback()
233 self.sa.rollback()
232 raise
234 raise
233
235
234 def admin_prompt(self, second=False):
236 def admin_prompt(self, second=False):
235 if not self.tests:
237 if not self.tests:
236 import getpass
238 import getpass
237
239
238 def get_password():
240 def get_password():
239 password = getpass.getpass('Specify admin password '
241 password = getpass.getpass('Specify admin password '
240 '(min 6 chars):')
242 '(min 6 chars):')
241 confirm = getpass.getpass('Confirm password:')
243 confirm = getpass.getpass('Confirm password:')
242
244
243 if password != confirm:
245 if password != confirm:
244 log.error('passwords mismatch')
246 log.error('passwords mismatch')
245 return False
247 return False
246 if len(password) < 6:
248 if len(password) < 6:
247 log.error('password is to short use at least 6 characters')
249 log.error('password is to short use at least 6 characters')
248 return False
250 return False
249
251
250 return password
252 return password
251
253
252 username = raw_input('Specify admin username:')
254 username = raw_input('Specify admin username:')
253
255
254 password = get_password()
256 password = get_password()
255 if not password:
257 if not password:
256 #second try
258 #second try
257 password = get_password()
259 password = get_password()
258 if not password:
260 if not password:
259 sys.exit()
261 sys.exit()
260
262
261 email = raw_input('Specify admin email:')
263 email = raw_input('Specify admin email:')
262 self.create_user(username, password, email, True)
264 self.create_user(username, password, email, True)
263 else:
265 else:
264 log.info('creating admin and regular test users')
266 log.info('creating admin and regular test users')
265 from rhodecode.tests import TEST_USER_ADMIN_LOGIN,\
267 from rhodecode.tests import TEST_USER_ADMIN_LOGIN,\
266 TEST_USER_ADMIN_PASS, TEST_USER_ADMIN_EMAIL,\
268 TEST_USER_ADMIN_PASS, TEST_USER_ADMIN_EMAIL,\
267 TEST_USER_REGULAR_LOGIN, TEST_USER_REGULAR_PASS,\
269 TEST_USER_REGULAR_LOGIN, TEST_USER_REGULAR_PASS,\
268 TEST_USER_REGULAR_EMAIL, TEST_USER_REGULAR2_LOGIN, \
270 TEST_USER_REGULAR_EMAIL, TEST_USER_REGULAR2_LOGIN, \
269 TEST_USER_REGULAR2_PASS, TEST_USER_REGULAR2_EMAIL
271 TEST_USER_REGULAR2_PASS, TEST_USER_REGULAR2_EMAIL
270
272
271 self.create_user(TEST_USER_ADMIN_LOGIN, TEST_USER_ADMIN_PASS,
273 self.create_user(TEST_USER_ADMIN_LOGIN, TEST_USER_ADMIN_PASS,
272 TEST_USER_ADMIN_EMAIL, True)
274 TEST_USER_ADMIN_EMAIL, True)
273
275
274 self.create_user(TEST_USER_REGULAR_LOGIN, TEST_USER_REGULAR_PASS,
276 self.create_user(TEST_USER_REGULAR_LOGIN, TEST_USER_REGULAR_PASS,
275 TEST_USER_REGULAR_EMAIL, False)
277 TEST_USER_REGULAR_EMAIL, False)
276
278
277 self.create_user(TEST_USER_REGULAR2_LOGIN, TEST_USER_REGULAR2_PASS,
279 self.create_user(TEST_USER_REGULAR2_LOGIN, TEST_USER_REGULAR2_PASS,
278 TEST_USER_REGULAR2_EMAIL, False)
280 TEST_USER_REGULAR2_EMAIL, False)
279
281
280 def create_ui_settings(self):
282 def create_ui_settings(self):
281 """
283 """
282 Creates ui settings, fills out hooks
284 Creates ui settings, fills out hooks
283 and disables dotencode
285 and disables dotencode
284 """
286 """
285
287
286 #HOOKS
288 #HOOKS
287 hooks1_key = RhodeCodeUi.HOOK_UPDATE
289 hooks1_key = RhodeCodeUi.HOOK_UPDATE
288 hooks1_ = self.sa.query(RhodeCodeUi)\
290 hooks1_ = self.sa.query(RhodeCodeUi)\
289 .filter(RhodeCodeUi.ui_key == hooks1_key).scalar()
291 .filter(RhodeCodeUi.ui_key == hooks1_key).scalar()
290
292
291 hooks1 = RhodeCodeUi() if hooks1_ is None else hooks1_
293 hooks1 = RhodeCodeUi() if hooks1_ is None else hooks1_
292 hooks1.ui_section = 'hooks'
294 hooks1.ui_section = 'hooks'
293 hooks1.ui_key = hooks1_key
295 hooks1.ui_key = hooks1_key
294 hooks1.ui_value = 'hg update >&2'
296 hooks1.ui_value = 'hg update >&2'
295 hooks1.ui_active = False
297 hooks1.ui_active = False
296
298
297 hooks2_key = RhodeCodeUi.HOOK_REPO_SIZE
299 hooks2_key = RhodeCodeUi.HOOK_REPO_SIZE
298 hooks2_ = self.sa.query(RhodeCodeUi)\
300 hooks2_ = self.sa.query(RhodeCodeUi)\
299 .filter(RhodeCodeUi.ui_key == hooks2_key).scalar()
301 .filter(RhodeCodeUi.ui_key == hooks2_key).scalar()
300
302
301 hooks2 = RhodeCodeUi() if hooks2_ is None else hooks2_
303 hooks2 = RhodeCodeUi() if hooks2_ is None else hooks2_
302 hooks2.ui_section = 'hooks'
304 hooks2.ui_section = 'hooks'
303 hooks2.ui_key = hooks2_key
305 hooks2.ui_key = hooks2_key
304 hooks2.ui_value = 'python:rhodecode.lib.hooks.repo_size'
306 hooks2.ui_value = 'python:rhodecode.lib.hooks.repo_size'
305
307
306 hooks3 = RhodeCodeUi()
308 hooks3 = RhodeCodeUi()
307 hooks3.ui_section = 'hooks'
309 hooks3.ui_section = 'hooks'
308 hooks3.ui_key = RhodeCodeUi.HOOK_PUSH
310 hooks3.ui_key = RhodeCodeUi.HOOK_PUSH
309 hooks3.ui_value = 'python:rhodecode.lib.hooks.log_push_action'
311 hooks3.ui_value = 'python:rhodecode.lib.hooks.log_push_action'
310
312
311 hooks4 = RhodeCodeUi()
313 hooks4 = RhodeCodeUi()
312 hooks4.ui_section = 'hooks'
314 hooks4.ui_section = 'hooks'
313 hooks4.ui_key = RhodeCodeUi.HOOK_PULL
315 hooks4.ui_key = RhodeCodeUi.HOOK_PULL
314 hooks4.ui_value = 'python:rhodecode.lib.hooks.log_pull_action'
316 hooks4.ui_value = 'python:rhodecode.lib.hooks.log_pull_action'
315
317
316 # For mercurial 1.7 set backward comapatibility with format
318 # For mercurial 1.7 set backward comapatibility with format
317 dotencode_disable = RhodeCodeUi()
319 dotencode_disable = RhodeCodeUi()
318 dotencode_disable.ui_section = 'format'
320 dotencode_disable.ui_section = 'format'
319 dotencode_disable.ui_key = 'dotencode'
321 dotencode_disable.ui_key = 'dotencode'
320 dotencode_disable.ui_value = 'false'
322 dotencode_disable.ui_value = 'false'
321
323
322 # enable largefiles
324 # enable largefiles
323 largefiles = RhodeCodeUi()
325 largefiles = RhodeCodeUi()
324 largefiles.ui_section = 'extensions'
326 largefiles.ui_section = 'extensions'
325 largefiles.ui_key = 'largefiles'
327 largefiles.ui_key = 'largefiles'
326 largefiles.ui_value = ''
328 largefiles.ui_value = ''
327
329
328 self.sa.add(hooks1)
330 self.sa.add(hooks1)
329 self.sa.add(hooks2)
331 self.sa.add(hooks2)
330 self.sa.add(hooks3)
332 self.sa.add(hooks3)
331 self.sa.add(hooks4)
333 self.sa.add(hooks4)
332 self.sa.add(largefiles)
334 self.sa.add(largefiles)
333
335
334 def create_ldap_options(self, skip_existing=False):
336 def create_ldap_options(self, skip_existing=False):
335 """Creates ldap settings"""
337 """Creates ldap settings"""
336
338
337 for k, v in [('ldap_active', 'false'), ('ldap_host', ''),
339 for k, v in [('ldap_active', 'false'), ('ldap_host', ''),
338 ('ldap_port', '389'), ('ldap_tls_kind', 'PLAIN'),
340 ('ldap_port', '389'), ('ldap_tls_kind', 'PLAIN'),
339 ('ldap_tls_reqcert', ''), ('ldap_dn_user', ''),
341 ('ldap_tls_reqcert', ''), ('ldap_dn_user', ''),
340 ('ldap_dn_pass', ''), ('ldap_base_dn', ''),
342 ('ldap_dn_pass', ''), ('ldap_base_dn', ''),
341 ('ldap_filter', ''), ('ldap_search_scope', ''),
343 ('ldap_filter', ''), ('ldap_search_scope', ''),
342 ('ldap_attr_login', ''), ('ldap_attr_firstname', ''),
344 ('ldap_attr_login', ''), ('ldap_attr_firstname', ''),
343 ('ldap_attr_lastname', ''), ('ldap_attr_email', '')]:
345 ('ldap_attr_lastname', ''), ('ldap_attr_email', '')]:
344
346
345 if skip_existing and RhodeCodeSetting.get_by_name(k) != None:
347 if skip_existing and RhodeCodeSetting.get_by_name(k) != None:
346 log.debug('Skipping option %s' % k)
348 log.debug('Skipping option %s' % k)
347 continue
349 continue
348 setting = RhodeCodeSetting(k, v)
350 setting = RhodeCodeSetting(k, v)
349 self.sa.add(setting)
351 self.sa.add(setting)
350
352
353 def fixup_groups(self):
354 def_usr = User.get_by_username('default')
355 for g in RepoGroup.query().all():
356 g.group_name = g.get_new_name(g.name)
357 self.sa.add(g)
358 # get default perm
359 default = UserRepoGroupToPerm.query()\
360 .filter(UserRepoGroupToPerm.group == g)\
361 .filter(UserRepoGroupToPerm.user == def_usr)\
362 .scalar()
363
364 if default is None:
365 log.debug('missing default permission for group %s adding' % g)
366 ReposGroupModel()._create_default_perms(g)
367
351 def config_prompt(self, test_repo_path='', retries=3):
368 def config_prompt(self, test_repo_path='', retries=3):
352 if retries == 3:
369 if retries == 3:
353 log.info('Setting up repositories config')
370 log.info('Setting up repositories config')
354
371
355 if not self.tests and not test_repo_path:
372 if not self.tests and not test_repo_path:
356 path = raw_input(
373 path = raw_input(
357 'Enter a valid path to store repositories. '
374 'Enter a valid path to store repositories. '
358 'All repositories in that path will be added automatically:'
375 'All repositories in that path will be added automatically:'
359 )
376 )
360 else:
377 else:
361 path = test_repo_path
378 path = test_repo_path
362 path_ok = True
379 path_ok = True
363
380
364 # check proper dir
381 # check proper dir
365 if not os.path.isdir(path):
382 if not os.path.isdir(path):
366 path_ok = False
383 path_ok = False
367 log.error('Given path %s is not a valid directory' % path)
384 log.error('Given path %s is not a valid directory' % path)
368
385
369 # check write access
386 # check write access
370 if not os.access(path, os.W_OK) and path_ok:
387 if not os.access(path, os.W_OK) and path_ok:
371 path_ok = False
388 path_ok = False
372 log.error('No write permission to given path %s' % path)
389 log.error('No write permission to given path %s' % path)
373
390
374 if retries == 0:
391 if retries == 0:
375 sys.exit('max retries reached')
392 sys.exit('max retries reached')
376 if path_ok is False:
393 if path_ok is False:
377 retries -= 1
394 retries -= 1
378 return self.config_prompt(test_repo_path, retries)
395 return self.config_prompt(test_repo_path, retries)
379
396
380 return path
397 return path
381
398
382 def create_settings(self, path):
399 def create_settings(self, path):
383
400
384 self.create_ui_settings()
401 self.create_ui_settings()
385
402
386 #HG UI OPTIONS
403 #HG UI OPTIONS
387 web1 = RhodeCodeUi()
404 web1 = RhodeCodeUi()
388 web1.ui_section = 'web'
405 web1.ui_section = 'web'
389 web1.ui_key = 'push_ssl'
406 web1.ui_key = 'push_ssl'
390 web1.ui_value = 'false'
407 web1.ui_value = 'false'
391
408
392 web2 = RhodeCodeUi()
409 web2 = RhodeCodeUi()
393 web2.ui_section = 'web'
410 web2.ui_section = 'web'
394 web2.ui_key = 'allow_archive'
411 web2.ui_key = 'allow_archive'
395 web2.ui_value = 'gz zip bz2'
412 web2.ui_value = 'gz zip bz2'
396
413
397 web3 = RhodeCodeUi()
414 web3 = RhodeCodeUi()
398 web3.ui_section = 'web'
415 web3.ui_section = 'web'
399 web3.ui_key = 'allow_push'
416 web3.ui_key = 'allow_push'
400 web3.ui_value = '*'
417 web3.ui_value = '*'
401
418
402 web4 = RhodeCodeUi()
419 web4 = RhodeCodeUi()
403 web4.ui_section = 'web'
420 web4.ui_section = 'web'
404 web4.ui_key = 'baseurl'
421 web4.ui_key = 'baseurl'
405 web4.ui_value = '/'
422 web4.ui_value = '/'
406
423
407 paths = RhodeCodeUi()
424 paths = RhodeCodeUi()
408 paths.ui_section = 'paths'
425 paths.ui_section = 'paths'
409 paths.ui_key = '/'
426 paths.ui_key = '/'
410 paths.ui_value = path
427 paths.ui_value = path
411
428
412 hgsettings1 = RhodeCodeSetting('realm', 'RhodeCode authentication')
429 hgsettings1 = RhodeCodeSetting('realm', 'RhodeCode authentication')
413 hgsettings2 = RhodeCodeSetting('title', 'RhodeCode')
430 hgsettings2 = RhodeCodeSetting('title', 'RhodeCode')
414 hgsettings3 = RhodeCodeSetting('ga_code', '')
431 hgsettings3 = RhodeCodeSetting('ga_code', '')
415
432
416 self.sa.add(web1)
433 self.sa.add(web1)
417 self.sa.add(web2)
434 self.sa.add(web2)
418 self.sa.add(web3)
435 self.sa.add(web3)
419 self.sa.add(web4)
436 self.sa.add(web4)
420 self.sa.add(paths)
437 self.sa.add(paths)
421 self.sa.add(hgsettings1)
438 self.sa.add(hgsettings1)
422 self.sa.add(hgsettings2)
439 self.sa.add(hgsettings2)
423 self.sa.add(hgsettings3)
440 self.sa.add(hgsettings3)
424
441
425 self.create_ldap_options()
442 self.create_ldap_options()
426
443
427 log.info('created ui config')
444 log.info('created ui config')
428
445
429 def create_user(self, username, password, email='', admin=False):
446 def create_user(self, username, password, email='', admin=False):
430 log.info('creating user %s' % username)
447 log.info('creating user %s' % username)
431 UserModel().create_or_update(username, password, email,
448 UserModel().create_or_update(username, password, email,
432 name='RhodeCode', lastname='Admin',
449 name='RhodeCode', lastname='Admin',
433 active=True, admin=admin)
450 active=True, admin=admin)
434
451
435 def create_default_user(self):
452 def create_default_user(self):
436 log.info('creating default user')
453 log.info('creating default user')
437 # create default user for handling default permissions.
454 # create default user for handling default permissions.
438 UserModel().create_or_update(username='default',
455 UserModel().create_or_update(username='default',
439 password=str(uuid.uuid1())[:8],
456 password=str(uuid.uuid1())[:8],
440 email='anonymous@rhodecode.org',
457 email='anonymous@rhodecode.org',
441 name='Anonymous', lastname='User')
458 name='Anonymous', lastname='User')
442
459
443 def create_permissions(self):
460 def create_permissions(self):
444 # module.(access|create|change|delete)_[name]
461 # module.(access|create|change|delete)_[name]
445 # module.(none|read|write|admin)
462 # module.(none|read|write|admin)
446 perms = [
463 perms = [
447 ('repository.none', 'Repository no access'),
464 ('repository.none', 'Repository no access'),
448 ('repository.read', 'Repository read access'),
465 ('repository.read', 'Repository read access'),
449 ('repository.write', 'Repository write access'),
466 ('repository.write', 'Repository write access'),
450 ('repository.admin', 'Repository admin access'),
467 ('repository.admin', 'Repository admin access'),
451
468
452 ('group.none', 'Repositories Group no access'),
469 ('group.none', 'Repositories Group no access'),
453 ('group.read', 'Repositories Group read access'),
470 ('group.read', 'Repositories Group read access'),
454 ('group.write', 'Repositories Group write access'),
471 ('group.write', 'Repositories Group write access'),
455 ('group.admin', 'Repositories Group admin access'),
472 ('group.admin', 'Repositories Group admin access'),
456
473
457 ('hg.admin', 'Hg Administrator'),
474 ('hg.admin', 'Hg Administrator'),
458 ('hg.create.repository', 'Repository create'),
475 ('hg.create.repository', 'Repository create'),
459 ('hg.create.none', 'Repository creation disabled'),
476 ('hg.create.none', 'Repository creation disabled'),
460 ('hg.register.none', 'Register disabled'),
477 ('hg.register.none', 'Register disabled'),
461 ('hg.register.manual_activate', 'Register new user with RhodeCode '
478 ('hg.register.manual_activate', 'Register new user with RhodeCode '
462 'without manual activation'),
479 'without manual activation'),
463
480
464 ('hg.register.auto_activate', 'Register new user with RhodeCode '
481 ('hg.register.auto_activate', 'Register new user with RhodeCode '
465 'without auto activation'),
482 'without auto activation'),
466 ]
483 ]
467
484
468 for p in perms:
485 for p in perms:
469 new_perm = Permission()
486 new_perm = Permission()
470 new_perm.permission_name = p[0]
487 new_perm.permission_name = p[0]
471 new_perm.permission_longname = p[1]
488 new_perm.permission_longname = p[1]
472 self.sa.add(new_perm)
489 self.sa.add(new_perm)
473
490
474 def populate_default_permissions(self):
491 def populate_default_permissions(self):
475 log.info('creating default user permissions')
492 log.info('creating default user permissions')
476
493
477 default_user = self.sa.query(User)\
494 default_user = self.sa.query(User)\
478 .filter(User.username == 'default').scalar()
495 .filter(User.username == 'default').scalar()
479
496
480 reg_perm = UserToPerm()
497 reg_perm = UserToPerm()
481 reg_perm.user = default_user
498 reg_perm.user = default_user
482 reg_perm.permission = self.sa.query(Permission)\
499 reg_perm.permission = self.sa.query(Permission)\
483 .filter(Permission.permission_name == 'hg.register.manual_activate')\
500 .filter(Permission.permission_name == 'hg.register.manual_activate')\
484 .scalar()
501 .scalar()
485
502
486 create_repo_perm = UserToPerm()
503 create_repo_perm = UserToPerm()
487 create_repo_perm.user = default_user
504 create_repo_perm.user = default_user
488 create_repo_perm.permission = self.sa.query(Permission)\
505 create_repo_perm.permission = self.sa.query(Permission)\
489 .filter(Permission.permission_name == 'hg.create.repository')\
506 .filter(Permission.permission_name == 'hg.create.repository')\
490 .scalar()
507 .scalar()
491
508
492 default_repo_perm = UserToPerm()
509 default_repo_perm = UserToPerm()
493 default_repo_perm.user = default_user
510 default_repo_perm.user = default_user
494 default_repo_perm.permission = self.sa.query(Permission)\
511 default_repo_perm.permission = self.sa.query(Permission)\
495 .filter(Permission.permission_name == 'repository.read')\
512 .filter(Permission.permission_name == 'repository.read')\
496 .scalar()
513 .scalar()
497
514
498 self.sa.add(reg_perm)
515 self.sa.add(reg_perm)
499 self.sa.add(create_repo_perm)
516 self.sa.add(create_repo_perm)
500 self.sa.add(default_repo_perm)
517 self.sa.add(default_repo_perm)
@@ -1,616 +1,633 b''
1 # -*- coding: utf-8 -*-
1 # -*- coding: utf-8 -*-
2 """
2 """
3 rhodecode.lib.utils
3 rhodecode.lib.utils
4 ~~~~~~~~~~~~~~~~~~~
4 ~~~~~~~~~~~~~~~~~~~
5
5
6 Utilities library for RhodeCode
6 Utilities library for RhodeCode
7
7
8 :created_on: Apr 18, 2010
8 :created_on: Apr 18, 2010
9 :author: marcink
9 :author: marcink
10 :copyright: (C) 2010-2012 Marcin Kuzminski <marcin@python-works.com>
10 :copyright: (C) 2010-2012 Marcin Kuzminski <marcin@python-works.com>
11 :license: GPLv3, see COPYING for more details.
11 :license: GPLv3, see COPYING for more details.
12 """
12 """
13 # This program is free software: you can redistribute it and/or modify
13 # This program is free software: you can redistribute it and/or modify
14 # it under the terms of the GNU General Public License as published by
14 # it under the terms of the GNU General Public License as published by
15 # the Free Software Foundation, either version 3 of the License, or
15 # the Free Software Foundation, either version 3 of the License, or
16 # (at your option) any later version.
16 # (at your option) any later version.
17 #
17 #
18 # This program is distributed in the hope that it will be useful,
18 # This program is distributed in the hope that it will be useful,
19 # but WITHOUT ANY WARRANTY; without even the implied warranty of
19 # but WITHOUT ANY WARRANTY; without even the implied warranty of
20 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 # GNU General Public License for more details.
21 # GNU General Public License for more details.
22 #
22 #
23 # You should have received a copy of the GNU General Public License
23 # You should have received a copy of the GNU General Public License
24 # along with this program. If not, see <http://www.gnu.org/licenses/>.
24 # along with this program. If not, see <http://www.gnu.org/licenses/>.
25
25
26 import os
26 import os
27 import logging
27 import logging
28 import datetime
28 import datetime
29 import traceback
29 import traceback
30 import paste
30 import paste
31 import beaker
31 import beaker
32 import tarfile
32 import tarfile
33 import shutil
33 import shutil
34 from os.path import abspath
34 from os.path import abspath
35 from os.path import dirname as dn, join as jn
35 from os.path import dirname as dn, join as jn
36
36
37 from paste.script.command import Command, BadCommand
37 from paste.script.command import Command, BadCommand
38
38
39 from mercurial import ui, config
39 from mercurial import ui, config
40
40
41 from webhelpers.text import collapse, remove_formatting, strip_tags
41 from webhelpers.text import collapse, remove_formatting, strip_tags
42
42
43 from vcs import get_backend
43 from vcs import get_backend
44 from vcs.backends.base import BaseChangeset
44 from vcs.backends.base import BaseChangeset
45 from vcs.utils.lazy import LazyProperty
45 from vcs.utils.lazy import LazyProperty
46 from vcs.utils.helpers import get_scm
46 from vcs.utils.helpers import get_scm
47 from vcs.exceptions import VCSError
47 from vcs.exceptions import VCSError
48
48
49 from rhodecode.lib.caching_query import FromCache
49 from rhodecode.lib.caching_query import FromCache
50
50
51 from rhodecode.model import meta
51 from rhodecode.model import meta
52 from rhodecode.model.db import Repository, User, RhodeCodeUi, \
52 from rhodecode.model.db import Repository, User, RhodeCodeUi, \
53 UserLog, RepoGroup, RhodeCodeSetting
53 UserLog, RepoGroup, RhodeCodeSetting, UserRepoGroupToPerm
54 from rhodecode.model.meta import Session
54 from rhodecode.model.meta import Session
55 from rhodecode.model.repos_group import ReposGroupModel
55 from rhodecode.model.repos_group import ReposGroupModel
56
56
57 log = logging.getLogger(__name__)
57 log = logging.getLogger(__name__)
58
58
59
59
60 def recursive_replace(str_, replace=' '):
60 def recursive_replace(str_, replace=' '):
61 """Recursive replace of given sign to just one instance
61 """Recursive replace of given sign to just one instance
62
62
63 :param str_: given string
63 :param str_: given string
64 :param replace: char to find and replace multiple instances
64 :param replace: char to find and replace multiple instances
65
65
66 Examples::
66 Examples::
67 >>> recursive_replace("Mighty---Mighty-Bo--sstones",'-')
67 >>> recursive_replace("Mighty---Mighty-Bo--sstones",'-')
68 'Mighty-Mighty-Bo-sstones'
68 'Mighty-Mighty-Bo-sstones'
69 """
69 """
70
70
71 if str_.find(replace * 2) == -1:
71 if str_.find(replace * 2) == -1:
72 return str_
72 return str_
73 else:
73 else:
74 str_ = str_.replace(replace * 2, replace)
74 str_ = str_.replace(replace * 2, replace)
75 return recursive_replace(str_, replace)
75 return recursive_replace(str_, replace)
76
76
77
77
78 def repo_name_slug(value):
78 def repo_name_slug(value):
79 """Return slug of name of repository
79 """Return slug of name of repository
80 This function is called on each creation/modification
80 This function is called on each creation/modification
81 of repository to prevent bad names in repo
81 of repository to prevent bad names in repo
82 """
82 """
83
83
84 slug = remove_formatting(value)
84 slug = remove_formatting(value)
85 slug = strip_tags(slug)
85 slug = strip_tags(slug)
86
86
87 for c in """=[]\;'"<>,/~!@#$%^&*()+{}|: """:
87 for c in """=[]\;'"<>,/~!@#$%^&*()+{}|: """:
88 slug = slug.replace(c, '-')
88 slug = slug.replace(c, '-')
89 slug = recursive_replace(slug, '-')
89 slug = recursive_replace(slug, '-')
90 slug = collapse(slug, '-')
90 slug = collapse(slug, '-')
91 return slug
91 return slug
92
92
93
93
94 def get_repo_slug(request):
94 def get_repo_slug(request):
95 return request.environ['pylons.routes_dict'].get('repo_name')
95 return request.environ['pylons.routes_dict'].get('repo_name')
96
96
97
97
98 def get_repos_group_slug(request):
98 def get_repos_group_slug(request):
99 return request.environ['pylons.routes_dict'].get('group_name')
99 return request.environ['pylons.routes_dict'].get('group_name')
100
100
101
101
102 def action_logger(user, action, repo, ipaddr='', sa=None, commit=False):
102 def action_logger(user, action, repo, ipaddr='', sa=None, commit=False):
103 """
103 """
104 Action logger for various actions made by users
104 Action logger for various actions made by users
105
105
106 :param user: user that made this action, can be a unique username string or
106 :param user: user that made this action, can be a unique username string or
107 object containing user_id attribute
107 object containing user_id attribute
108 :param action: action to log, should be on of predefined unique actions for
108 :param action: action to log, should be on of predefined unique actions for
109 easy translations
109 easy translations
110 :param repo: string name of repository or object containing repo_id,
110 :param repo: string name of repository or object containing repo_id,
111 that action was made on
111 that action was made on
112 :param ipaddr: optional ip address from what the action was made
112 :param ipaddr: optional ip address from what the action was made
113 :param sa: optional sqlalchemy session
113 :param sa: optional sqlalchemy session
114
114
115 """
115 """
116
116
117 if not sa:
117 if not sa:
118 sa = meta.Session
118 sa = meta.Session
119
119
120 try:
120 try:
121 if hasattr(user, 'user_id'):
121 if hasattr(user, 'user_id'):
122 user_obj = user
122 user_obj = user
123 elif isinstance(user, basestring):
123 elif isinstance(user, basestring):
124 user_obj = User.get_by_username(user)
124 user_obj = User.get_by_username(user)
125 else:
125 else:
126 raise Exception('You have to provide user object or username')
126 raise Exception('You have to provide user object or username')
127
127
128 if hasattr(repo, 'repo_id'):
128 if hasattr(repo, 'repo_id'):
129 repo_obj = Repository.get(repo.repo_id)
129 repo_obj = Repository.get(repo.repo_id)
130 repo_name = repo_obj.repo_name
130 repo_name = repo_obj.repo_name
131 elif isinstance(repo, basestring):
131 elif isinstance(repo, basestring):
132 repo_name = repo.lstrip('/')
132 repo_name = repo.lstrip('/')
133 repo_obj = Repository.get_by_repo_name(repo_name)
133 repo_obj = Repository.get_by_repo_name(repo_name)
134 else:
134 else:
135 raise Exception('You have to provide repository to action logger')
135 raise Exception('You have to provide repository to action logger')
136
136
137 user_log = UserLog()
137 user_log = UserLog()
138 user_log.user_id = user_obj.user_id
138 user_log.user_id = user_obj.user_id
139 user_log.action = action
139 user_log.action = action
140
140
141 user_log.repository_id = repo_obj.repo_id
141 user_log.repository_id = repo_obj.repo_id
142 user_log.repository_name = repo_name
142 user_log.repository_name = repo_name
143
143
144 user_log.action_date = datetime.datetime.now()
144 user_log.action_date = datetime.datetime.now()
145 user_log.user_ip = ipaddr
145 user_log.user_ip = ipaddr
146 sa.add(user_log)
146 sa.add(user_log)
147
147
148 log.info('Adding user %s, action %s on %s' % (user_obj, action, repo))
148 log.info('Adding user %s, action %s on %s' % (user_obj, action, repo))
149 if commit:
149 if commit:
150 sa.commit()
150 sa.commit()
151 except:
151 except:
152 log.error(traceback.format_exc())
152 log.error(traceback.format_exc())
153 raise
153 raise
154
154
155
155
156 def get_repos(path, recursive=False):
156 def get_repos(path, recursive=False):
157 """
157 """
158 Scans given path for repos and return (name,(type,path)) tuple
158 Scans given path for repos and return (name,(type,path)) tuple
159
159
160 :param path: path to scan for repositories
160 :param path: path to scan for repositories
161 :param recursive: recursive search and return names with subdirs in front
161 :param recursive: recursive search and return names with subdirs in front
162 """
162 """
163
163
164 # remove ending slash for better results
164 # remove ending slash for better results
165 path = path.rstrip(os.sep)
165 path = path.rstrip(os.sep)
166
166
167 def _get_repos(p):
167 def _get_repos(p):
168 if not os.access(p, os.W_OK):
168 if not os.access(p, os.W_OK):
169 return
169 return
170 for dirpath in os.listdir(p):
170 for dirpath in os.listdir(p):
171 if os.path.isfile(os.path.join(p, dirpath)):
171 if os.path.isfile(os.path.join(p, dirpath)):
172 continue
172 continue
173 cur_path = os.path.join(p, dirpath)
173 cur_path = os.path.join(p, dirpath)
174 try:
174 try:
175 scm_info = get_scm(cur_path)
175 scm_info = get_scm(cur_path)
176 yield scm_info[1].split(path, 1)[-1].lstrip(os.sep), scm_info
176 yield scm_info[1].split(path, 1)[-1].lstrip(os.sep), scm_info
177 except VCSError:
177 except VCSError:
178 if not recursive:
178 if not recursive:
179 continue
179 continue
180 #check if this dir containts other repos for recursive scan
180 #check if this dir containts other repos for recursive scan
181 rec_path = os.path.join(p, dirpath)
181 rec_path = os.path.join(p, dirpath)
182 if os.path.isdir(rec_path):
182 if os.path.isdir(rec_path):
183 for inner_scm in _get_repos(rec_path):
183 for inner_scm in _get_repos(rec_path):
184 yield inner_scm
184 yield inner_scm
185
185
186 return _get_repos(path)
186 return _get_repos(path)
187
187
188
188
189 def is_valid_repo(repo_name, base_path):
189 def is_valid_repo(repo_name, base_path):
190 """
190 """
191 Returns True if given path is a valid repository False otherwise
191 Returns True if given path is a valid repository False otherwise
192 :param repo_name:
192 :param repo_name:
193 :param base_path:
193 :param base_path:
194
194
195 :return True: if given path is a valid repository
195 :return True: if given path is a valid repository
196 """
196 """
197 full_path = os.path.join(base_path, repo_name)
197 full_path = os.path.join(base_path, repo_name)
198
198
199 try:
199 try:
200 get_scm(full_path)
200 get_scm(full_path)
201 return True
201 return True
202 except VCSError:
202 except VCSError:
203 return False
203 return False
204
204
205
205
206 def is_valid_repos_group(repos_group_name, base_path):
206 def is_valid_repos_group(repos_group_name, base_path):
207 """
207 """
208 Returns True if given path is a repos group False otherwise
208 Returns True if given path is a repos group False otherwise
209
209
210 :param repo_name:
210 :param repo_name:
211 :param base_path:
211 :param base_path:
212 """
212 """
213 full_path = os.path.join(base_path, repos_group_name)
213 full_path = os.path.join(base_path, repos_group_name)
214
214
215 # check if it's not a repo
215 # check if it's not a repo
216 if is_valid_repo(repos_group_name, base_path):
216 if is_valid_repo(repos_group_name, base_path):
217 return False
217 return False
218
218
219 # check if it's a valid path
219 # check if it's a valid path
220 if os.path.isdir(full_path):
220 if os.path.isdir(full_path):
221 return True
221 return True
222
222
223 return False
223 return False
224
224
225
225
226 def ask_ok(prompt, retries=4, complaint='Yes or no, please!'):
226 def ask_ok(prompt, retries=4, complaint='Yes or no, please!'):
227 while True:
227 while True:
228 ok = raw_input(prompt)
228 ok = raw_input(prompt)
229 if ok in ('y', 'ye', 'yes'):
229 if ok in ('y', 'ye', 'yes'):
230 return True
230 return True
231 if ok in ('n', 'no', 'nop', 'nope'):
231 if ok in ('n', 'no', 'nop', 'nope'):
232 return False
232 return False
233 retries = retries - 1
233 retries = retries - 1
234 if retries < 0:
234 if retries < 0:
235 raise IOError
235 raise IOError
236 print complaint
236 print complaint
237
237
238 #propagated from mercurial documentation
238 #propagated from mercurial documentation
239 ui_sections = ['alias', 'auth',
239 ui_sections = ['alias', 'auth',
240 'decode/encode', 'defaults',
240 'decode/encode', 'defaults',
241 'diff', 'email',
241 'diff', 'email',
242 'extensions', 'format',
242 'extensions', 'format',
243 'merge-patterns', 'merge-tools',
243 'merge-patterns', 'merge-tools',
244 'hooks', 'http_proxy',
244 'hooks', 'http_proxy',
245 'smtp', 'patch',
245 'smtp', 'patch',
246 'paths', 'profiling',
246 'paths', 'profiling',
247 'server', 'trusted',
247 'server', 'trusted',
248 'ui', 'web', ]
248 'ui', 'web', ]
249
249
250
250
251 def make_ui(read_from='file', path=None, checkpaths=True):
251 def make_ui(read_from='file', path=None, checkpaths=True):
252 """A function that will read python rc files or database
252 """A function that will read python rc files or database
253 and make an mercurial ui object from read options
253 and make an mercurial ui object from read options
254
254
255 :param path: path to mercurial config file
255 :param path: path to mercurial config file
256 :param checkpaths: check the path
256 :param checkpaths: check the path
257 :param read_from: read from 'file' or 'db'
257 :param read_from: read from 'file' or 'db'
258 """
258 """
259
259
260 baseui = ui.ui()
260 baseui = ui.ui()
261
261
262 #clean the baseui object
262 #clean the baseui object
263 baseui._ocfg = config.config()
263 baseui._ocfg = config.config()
264 baseui._ucfg = config.config()
264 baseui._ucfg = config.config()
265 baseui._tcfg = config.config()
265 baseui._tcfg = config.config()
266
266
267 if read_from == 'file':
267 if read_from == 'file':
268 if not os.path.isfile(path):
268 if not os.path.isfile(path):
269 log.warning('Unable to read config file %s' % path)
269 log.warning('Unable to read config file %s' % path)
270 return False
270 return False
271 log.debug('reading hgrc from %s' % path)
271 log.debug('reading hgrc from %s' % path)
272 cfg = config.config()
272 cfg = config.config()
273 cfg.read(path)
273 cfg.read(path)
274 for section in ui_sections:
274 for section in ui_sections:
275 for k, v in cfg.items(section):
275 for k, v in cfg.items(section):
276 log.debug('settings ui from file[%s]%s:%s' % (section, k, v))
276 log.debug('settings ui from file[%s]%s:%s' % (section, k, v))
277 baseui.setconfig(section, k, v)
277 baseui.setconfig(section, k, v)
278
278
279 elif read_from == 'db':
279 elif read_from == 'db':
280 sa = meta.Session
280 sa = meta.Session
281 ret = sa.query(RhodeCodeUi)\
281 ret = sa.query(RhodeCodeUi)\
282 .options(FromCache("sql_cache_short",
282 .options(FromCache("sql_cache_short",
283 "get_hg_ui_settings")).all()
283 "get_hg_ui_settings")).all()
284
284
285 hg_ui = ret
285 hg_ui = ret
286 for ui_ in hg_ui:
286 for ui_ in hg_ui:
287 if ui_.ui_active:
287 if ui_.ui_active:
288 log.debug('settings ui from db[%s]%s:%s', ui_.ui_section,
288 log.debug('settings ui from db[%s]%s:%s', ui_.ui_section,
289 ui_.ui_key, ui_.ui_value)
289 ui_.ui_key, ui_.ui_value)
290 baseui.setconfig(ui_.ui_section, ui_.ui_key, ui_.ui_value)
290 baseui.setconfig(ui_.ui_section, ui_.ui_key, ui_.ui_value)
291
291
292 meta.Session.remove()
292 meta.Session.remove()
293 return baseui
293 return baseui
294
294
295
295
296 def set_rhodecode_config(config):
296 def set_rhodecode_config(config):
297 """
297 """
298 Updates pylons config with new settings from database
298 Updates pylons config with new settings from database
299
299
300 :param config:
300 :param config:
301 """
301 """
302 hgsettings = RhodeCodeSetting.get_app_settings()
302 hgsettings = RhodeCodeSetting.get_app_settings()
303
303
304 for k, v in hgsettings.items():
304 for k, v in hgsettings.items():
305 config[k] = v
305 config[k] = v
306
306
307
307
308 def invalidate_cache(cache_key, *args):
308 def invalidate_cache(cache_key, *args):
309 """
309 """
310 Puts cache invalidation task into db for
310 Puts cache invalidation task into db for
311 further global cache invalidation
311 further global cache invalidation
312 """
312 """
313
313
314 from rhodecode.model.scm import ScmModel
314 from rhodecode.model.scm import ScmModel
315
315
316 if cache_key.startswith('get_repo_cached_'):
316 if cache_key.startswith('get_repo_cached_'):
317 name = cache_key.split('get_repo_cached_')[-1]
317 name = cache_key.split('get_repo_cached_')[-1]
318 ScmModel().mark_for_invalidation(name)
318 ScmModel().mark_for_invalidation(name)
319
319
320
320
321 class EmptyChangeset(BaseChangeset):
321 class EmptyChangeset(BaseChangeset):
322 """
322 """
323 An dummy empty changeset. It's possible to pass hash when creating
323 An dummy empty changeset. It's possible to pass hash when creating
324 an EmptyChangeset
324 an EmptyChangeset
325 """
325 """
326
326
327 def __init__(self, cs='0' * 40, repo=None, requested_revision=None,
327 def __init__(self, cs='0' * 40, repo=None, requested_revision=None,
328 alias=None):
328 alias=None):
329 self._empty_cs = cs
329 self._empty_cs = cs
330 self.revision = -1
330 self.revision = -1
331 self.message = ''
331 self.message = ''
332 self.author = ''
332 self.author = ''
333 self.date = ''
333 self.date = ''
334 self.repository = repo
334 self.repository = repo
335 self.requested_revision = requested_revision
335 self.requested_revision = requested_revision
336 self.alias = alias
336 self.alias = alias
337
337
338 @LazyProperty
338 @LazyProperty
339 def raw_id(self):
339 def raw_id(self):
340 """
340 """
341 Returns raw string identifying this changeset, useful for web
341 Returns raw string identifying this changeset, useful for web
342 representation.
342 representation.
343 """
343 """
344
344
345 return self._empty_cs
345 return self._empty_cs
346
346
347 @LazyProperty
347 @LazyProperty
348 def branch(self):
348 def branch(self):
349 return get_backend(self.alias).DEFAULT_BRANCH_NAME
349 return get_backend(self.alias).DEFAULT_BRANCH_NAME
350
350
351 @LazyProperty
351 @LazyProperty
352 def short_id(self):
352 def short_id(self):
353 return self.raw_id[:12]
353 return self.raw_id[:12]
354
354
355 def get_file_changeset(self, path):
355 def get_file_changeset(self, path):
356 return self
356 return self
357
357
358 def get_file_content(self, path):
358 def get_file_content(self, path):
359 return u''
359 return u''
360
360
361 def get_file_size(self, path):
361 def get_file_size(self, path):
362 return 0
362 return 0
363
363
364
364
365 def map_groups(groups):
365 def map_groups(groups):
366 """
366 """
367 Checks for groups existence, and creates groups structures.
367 Checks for groups existence, and creates groups structures.
368 It returns last group in structure
368 It returns last group in structure
369
369
370 :param groups: list of groups structure
370 :param groups: list of groups structure
371 """
371 """
372 sa = meta.Session
372 sa = meta.Session
373
373
374 parent = None
374 parent = None
375 group = None
375 group = None
376
376
377 # last element is repo in nested groups structure
377 # last element is repo in nested groups structure
378 groups = groups[:-1]
378 groups = groups[:-1]
379 rgm = ReposGroupModel(sa)
379 rgm = ReposGroupModel(sa)
380 for lvl, group_name in enumerate(groups):
380 for lvl, group_name in enumerate(groups):
381 log.debug('creating group level: %s group_name: %s' % (lvl, group_name))
381 log.debug('creating group level: %s group_name: %s' % (lvl, group_name))
382 group_name = '/'.join(groups[:lvl] + [group_name])
382 group_name = '/'.join(groups[:lvl] + [group_name])
383 group = RepoGroup.get_by_group_name(group_name)
383 group = RepoGroup.get_by_group_name(group_name)
384 desc = '%s group' % group_name
384 desc = '%s group' % group_name
385
385
386 # # WTF that doesn't work !?
386 # # WTF that doesn't work !?
387 # if group is None:
387 # if group is None:
388 # group = rgm.create(group_name, desc, parent, just_db=True)
388 # group = rgm.create(group_name, desc, parent, just_db=True)
389 # sa.commit()
389 # sa.commit()
390
390
391 if group is None:
391 if group is None:
392 group = RepoGroup(group_name, parent)
392 group = RepoGroup(group_name, parent)
393 group.group_description = desc
393 group.group_description = desc
394 sa.add(group)
394 sa.add(group)
395 rgm._create_default_perms(group)
395 rgm._create_default_perms(group)
396 sa.commit()
396 sa.commit()
397 parent = group
397 parent = group
398 return group
398 return group
399
399
400
400
401 def repo2db_mapper(initial_repo_list, remove_obsolete=False):
401 def repo2db_mapper(initial_repo_list, remove_obsolete=False):
402 """
402 """
403 maps all repos given in initial_repo_list, non existing repositories
403 maps all repos given in initial_repo_list, non existing repositories
404 are created, if remove_obsolete is True it also check for db entries
404 are created, if remove_obsolete is True it also check for db entries
405 that are not in initial_repo_list and removes them.
405 that are not in initial_repo_list and removes them.
406
406
407 :param initial_repo_list: list of repositories found by scanning methods
407 :param initial_repo_list: list of repositories found by scanning methods
408 :param remove_obsolete: check for obsolete entries in database
408 :param remove_obsolete: check for obsolete entries in database
409 """
409 """
410 from rhodecode.model.repo import RepoModel
410 from rhodecode.model.repo import RepoModel
411 sa = meta.Session
411 sa = meta.Session
412 rm = RepoModel()
412 rm = RepoModel()
413 user = sa.query(User).filter(User.admin == True).first()
413 user = sa.query(User).filter(User.admin == True).first()
414 if user is None:
414 if user is None:
415 raise Exception('Missing administrative account !')
415 raise Exception('Missing administrative account !')
416 added = []
416 added = []
417
417
418 # fixup groups paths to new format on the fly. Helps with migration from
419 # old rhodecode versions also set permissions if they are not present !
420 # TODO: remove this in future, before release
421 def_usr = User.get_by_username('default')
422 for g in RepoGroup.query().all():
423 g.group_name = g.get_new_name(g.name)
424 sa.add(g)
425 # get default perm
426 default = UserRepoGroupToPerm.query()\
427 .filter(UserRepoGroupToPerm.group == g)\
428 .filter(UserRepoGroupToPerm.user == def_usr)\
429 .scalar()
430
431 if default is None:
432 log.debug('missing default permission for group %s adding' % g)
433 ReposGroupModel()._create_default_perms(g)
434
418 for name, repo in initial_repo_list.items():
435 for name, repo in initial_repo_list.items():
419 group = map_groups(name.split(Repository.url_sep()))
436 group = map_groups(name.split(Repository.url_sep()))
420 if not rm.get_by_repo_name(name, cache=False):
437 if not rm.get_by_repo_name(name, cache=False):
421 log.info('repository %s not found creating default' % name)
438 log.info('repository %s not found creating default' % name)
422 added.append(name)
439 added.append(name)
423 form_data = {
440 form_data = {
424 'repo_name': name,
441 'repo_name': name,
425 'repo_name_full': name,
442 'repo_name_full': name,
426 'repo_type': repo.alias,
443 'repo_type': repo.alias,
427 'description': repo.description \
444 'description': repo.description \
428 if repo.description != 'unknown' else '%s repository' % name,
445 if repo.description != 'unknown' else '%s repository' % name,
429 'private': False,
446 'private': False,
430 'group_id': getattr(group, 'group_id', None)
447 'group_id': getattr(group, 'group_id', None)
431 }
448 }
432 rm.create(form_data, user, just_db=True)
449 rm.create(form_data, user, just_db=True)
433 sa.commit()
450 sa.commit()
434 removed = []
451 removed = []
435 if remove_obsolete:
452 if remove_obsolete:
436 #remove from database those repositories that are not in the filesystem
453 #remove from database those repositories that are not in the filesystem
437 for repo in sa.query(Repository).all():
454 for repo in sa.query(Repository).all():
438 if repo.repo_name not in initial_repo_list.keys():
455 if repo.repo_name not in initial_repo_list.keys():
439 removed.append(repo.repo_name)
456 removed.append(repo.repo_name)
440 sa.delete(repo)
457 sa.delete(repo)
441 sa.commit()
458 sa.commit()
442
459
443 return added, removed
460 return added, removed
444
461
445
462
446 # set cache regions for beaker so celery can utilise it
463 # set cache regions for beaker so celery can utilise it
447 def add_cache(settings):
464 def add_cache(settings):
448 cache_settings = {'regions': None}
465 cache_settings = {'regions': None}
449 for key in settings.keys():
466 for key in settings.keys():
450 for prefix in ['beaker.cache.', 'cache.']:
467 for prefix in ['beaker.cache.', 'cache.']:
451 if key.startswith(prefix):
468 if key.startswith(prefix):
452 name = key.split(prefix)[1].strip()
469 name = key.split(prefix)[1].strip()
453 cache_settings[name] = settings[key].strip()
470 cache_settings[name] = settings[key].strip()
454 if cache_settings['regions']:
471 if cache_settings['regions']:
455 for region in cache_settings['regions'].split(','):
472 for region in cache_settings['regions'].split(','):
456 region = region.strip()
473 region = region.strip()
457 region_settings = {}
474 region_settings = {}
458 for key, value in cache_settings.items():
475 for key, value in cache_settings.items():
459 if key.startswith(region):
476 if key.startswith(region):
460 region_settings[key.split('.')[1]] = value
477 region_settings[key.split('.')[1]] = value
461 region_settings['expire'] = int(region_settings.get('expire',
478 region_settings['expire'] = int(region_settings.get('expire',
462 60))
479 60))
463 region_settings.setdefault('lock_dir',
480 region_settings.setdefault('lock_dir',
464 cache_settings.get('lock_dir'))
481 cache_settings.get('lock_dir'))
465 region_settings.setdefault('data_dir',
482 region_settings.setdefault('data_dir',
466 cache_settings.get('data_dir'))
483 cache_settings.get('data_dir'))
467
484
468 if 'type' not in region_settings:
485 if 'type' not in region_settings:
469 region_settings['type'] = cache_settings.get('type',
486 region_settings['type'] = cache_settings.get('type',
470 'memory')
487 'memory')
471 beaker.cache.cache_regions[region] = region_settings
488 beaker.cache.cache_regions[region] = region_settings
472
489
473
490
474 #==============================================================================
491 #==============================================================================
475 # TEST FUNCTIONS AND CREATORS
492 # TEST FUNCTIONS AND CREATORS
476 #==============================================================================
493 #==============================================================================
477 def create_test_index(repo_location, config, full_index):
494 def create_test_index(repo_location, config, full_index):
478 """
495 """
479 Makes default test index
496 Makes default test index
480
497
481 :param config: test config
498 :param config: test config
482 :param full_index:
499 :param full_index:
483 """
500 """
484
501
485 from rhodecode.lib.indexers.daemon import WhooshIndexingDaemon
502 from rhodecode.lib.indexers.daemon import WhooshIndexingDaemon
486 from rhodecode.lib.pidlock import DaemonLock, LockHeld
503 from rhodecode.lib.pidlock import DaemonLock, LockHeld
487
504
488 repo_location = repo_location
505 repo_location = repo_location
489
506
490 index_location = os.path.join(config['app_conf']['index_dir'])
507 index_location = os.path.join(config['app_conf']['index_dir'])
491 if not os.path.exists(index_location):
508 if not os.path.exists(index_location):
492 os.makedirs(index_location)
509 os.makedirs(index_location)
493
510
494 try:
511 try:
495 l = DaemonLock(file_=jn(dn(index_location), 'make_index.lock'))
512 l = DaemonLock(file_=jn(dn(index_location), 'make_index.lock'))
496 WhooshIndexingDaemon(index_location=index_location,
513 WhooshIndexingDaemon(index_location=index_location,
497 repo_location=repo_location)\
514 repo_location=repo_location)\
498 .run(full_index=full_index)
515 .run(full_index=full_index)
499 l.release()
516 l.release()
500 except LockHeld:
517 except LockHeld:
501 pass
518 pass
502
519
503
520
504 def create_test_env(repos_test_path, config):
521 def create_test_env(repos_test_path, config):
505 """
522 """
506 Makes a fresh database and
523 Makes a fresh database and
507 install test repository into tmp dir
524 install test repository into tmp dir
508 """
525 """
509 from rhodecode.lib.db_manage import DbManage
526 from rhodecode.lib.db_manage import DbManage
510 from rhodecode.tests import HG_REPO, TESTS_TMP_PATH
527 from rhodecode.tests import HG_REPO, TESTS_TMP_PATH
511
528
512 # PART ONE create db
529 # PART ONE create db
513 dbconf = config['sqlalchemy.db1.url']
530 dbconf = config['sqlalchemy.db1.url']
514 log.debug('making test db %s' % dbconf)
531 log.debug('making test db %s' % dbconf)
515
532
516 # create test dir if it doesn't exist
533 # create test dir if it doesn't exist
517 if not os.path.isdir(repos_test_path):
534 if not os.path.isdir(repos_test_path):
518 log.debug('Creating testdir %s' % repos_test_path)
535 log.debug('Creating testdir %s' % repos_test_path)
519 os.makedirs(repos_test_path)
536 os.makedirs(repos_test_path)
520
537
521 dbmanage = DbManage(log_sql=True, dbconf=dbconf, root=config['here'],
538 dbmanage = DbManage(log_sql=True, dbconf=dbconf, root=config['here'],
522 tests=True)
539 tests=True)
523 dbmanage.create_tables(override=True)
540 dbmanage.create_tables(override=True)
524 dbmanage.create_settings(dbmanage.config_prompt(repos_test_path))
541 dbmanage.create_settings(dbmanage.config_prompt(repos_test_path))
525 dbmanage.create_default_user()
542 dbmanage.create_default_user()
526 dbmanage.admin_prompt()
543 dbmanage.admin_prompt()
527 dbmanage.create_permissions()
544 dbmanage.create_permissions()
528 dbmanage.populate_default_permissions()
545 dbmanage.populate_default_permissions()
529 Session.commit()
546 Session.commit()
530 # PART TWO make test repo
547 # PART TWO make test repo
531 log.debug('making test vcs repositories')
548 log.debug('making test vcs repositories')
532
549
533 idx_path = config['app_conf']['index_dir']
550 idx_path = config['app_conf']['index_dir']
534 data_path = config['app_conf']['cache_dir']
551 data_path = config['app_conf']['cache_dir']
535
552
536 #clean index and data
553 #clean index and data
537 if idx_path and os.path.exists(idx_path):
554 if idx_path and os.path.exists(idx_path):
538 log.debug('remove %s' % idx_path)
555 log.debug('remove %s' % idx_path)
539 shutil.rmtree(idx_path)
556 shutil.rmtree(idx_path)
540
557
541 if data_path and os.path.exists(data_path):
558 if data_path and os.path.exists(data_path):
542 log.debug('remove %s' % data_path)
559 log.debug('remove %s' % data_path)
543 shutil.rmtree(data_path)
560 shutil.rmtree(data_path)
544
561
545 #CREATE DEFAULT HG REPOSITORY
562 #CREATE DEFAULT HG REPOSITORY
546 cur_dir = dn(dn(abspath(__file__)))
563 cur_dir = dn(dn(abspath(__file__)))
547 tar = tarfile.open(jn(cur_dir, 'tests', "vcs_test_hg.tar.gz"))
564 tar = tarfile.open(jn(cur_dir, 'tests', "vcs_test_hg.tar.gz"))
548 tar.extractall(jn(TESTS_TMP_PATH, HG_REPO))
565 tar.extractall(jn(TESTS_TMP_PATH, HG_REPO))
549 tar.close()
566 tar.close()
550
567
551
568
552 #==============================================================================
569 #==============================================================================
553 # PASTER COMMANDS
570 # PASTER COMMANDS
554 #==============================================================================
571 #==============================================================================
555 class BasePasterCommand(Command):
572 class BasePasterCommand(Command):
556 """
573 """
557 Abstract Base Class for paster commands.
574 Abstract Base Class for paster commands.
558
575
559 The celery commands are somewhat aggressive about loading
576 The celery commands are somewhat aggressive about loading
560 celery.conf, and since our module sets the `CELERY_LOADER`
577 celery.conf, and since our module sets the `CELERY_LOADER`
561 environment variable to our loader, we have to bootstrap a bit and
578 environment variable to our loader, we have to bootstrap a bit and
562 make sure we've had a chance to load the pylons config off of the
579 make sure we've had a chance to load the pylons config off of the
563 command line, otherwise everything fails.
580 command line, otherwise everything fails.
564 """
581 """
565 min_args = 1
582 min_args = 1
566 min_args_error = "Please provide a paster config file as an argument."
583 min_args_error = "Please provide a paster config file as an argument."
567 takes_config_file = 1
584 takes_config_file = 1
568 requires_config_file = True
585 requires_config_file = True
569
586
570 def notify_msg(self, msg, log=False):
587 def notify_msg(self, msg, log=False):
571 """Make a notification to user, additionally if logger is passed
588 """Make a notification to user, additionally if logger is passed
572 it logs this action using given logger
589 it logs this action using given logger
573
590
574 :param msg: message that will be printed to user
591 :param msg: message that will be printed to user
575 :param log: logging instance, to use to additionally log this message
592 :param log: logging instance, to use to additionally log this message
576
593
577 """
594 """
578 if log and isinstance(log, logging):
595 if log and isinstance(log, logging):
579 log(msg)
596 log(msg)
580
597
581 def run(self, args):
598 def run(self, args):
582 """
599 """
583 Overrides Command.run
600 Overrides Command.run
584
601
585 Checks for a config file argument and loads it.
602 Checks for a config file argument and loads it.
586 """
603 """
587 if len(args) < self.min_args:
604 if len(args) < self.min_args:
588 raise BadCommand(
605 raise BadCommand(
589 self.min_args_error % {'min_args': self.min_args,
606 self.min_args_error % {'min_args': self.min_args,
590 'actual_args': len(args)})
607 'actual_args': len(args)})
591
608
592 # Decrement because we're going to lob off the first argument.
609 # Decrement because we're going to lob off the first argument.
593 # @@ This is hacky
610 # @@ This is hacky
594 self.min_args -= 1
611 self.min_args -= 1
595 self.bootstrap_config(args[0])
612 self.bootstrap_config(args[0])
596 self.update_parser()
613 self.update_parser()
597 return super(BasePasterCommand, self).run(args[1:])
614 return super(BasePasterCommand, self).run(args[1:])
598
615
599 def update_parser(self):
616 def update_parser(self):
600 """
617 """
601 Abstract method. Allows for the class's parser to be updated
618 Abstract method. Allows for the class's parser to be updated
602 before the superclass's `run` method is called. Necessary to
619 before the superclass's `run` method is called. Necessary to
603 allow options/arguments to be passed through to the underlying
620 allow options/arguments to be passed through to the underlying
604 celery command.
621 celery command.
605 """
622 """
606 raise NotImplementedError("Abstract Method.")
623 raise NotImplementedError("Abstract Method.")
607
624
608 def bootstrap_config(self, conf):
625 def bootstrap_config(self, conf):
609 """
626 """
610 Loads the pylons configuration.
627 Loads the pylons configuration.
611 """
628 """
612 from pylons import config as pylonsconfig
629 from pylons import config as pylonsconfig
613
630
614 path_to_ini_file = os.path.realpath(conf)
631 path_to_ini_file = os.path.realpath(conf)
615 conf = paste.deploy.appconfig('config:' + path_to_ini_file)
632 conf = paste.deploy.appconfig('config:' + path_to_ini_file)
616 pylonsconfig.init_app(conf.global_conf, conf.local_conf)
633 pylonsconfig.init_app(conf.global_conf, conf.local_conf)
General Comments 0
You need to be logged in to leave comments. Login now