Show More
@@ -1,722 +1,721 b'' | |||
|
1 | 1 | # -*- coding: utf-8 -*- |
|
2 | 2 | """ |
|
3 | 3 | rhodecode.lib.db_manage |
|
4 | 4 | ~~~~~~~~~~~~~~~~~~~~~~~ |
|
5 | 5 | |
|
6 | 6 | Database creation, and setup module for RhodeCode. Used for creation |
|
7 | 7 | of database as well as for migration operations |
|
8 | 8 | |
|
9 | 9 | :created_on: Apr 10, 2010 |
|
10 | 10 | :author: marcink |
|
11 | 11 | :copyright: (C) 2010-2012 Marcin Kuzminski <marcin@python-works.com> |
|
12 | 12 | :license: GPLv3, see COPYING for more details. |
|
13 | 13 | """ |
|
14 | 14 | # This program is free software: you can redistribute it and/or modify |
|
15 | 15 | # it under the terms of the GNU General Public License as published by |
|
16 | 16 | # the Free Software Foundation, either version 3 of the License, or |
|
17 | 17 | # (at your option) any later version. |
|
18 | 18 | # |
|
19 | 19 | # This program is distributed in the hope that it will be useful, |
|
20 | 20 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
21 | 21 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
22 | 22 | # GNU General Public License for more details. |
|
23 | 23 | # |
|
24 | 24 | # You should have received a copy of the GNU General Public License |
|
25 | 25 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
26 | 26 | |
|
27 | 27 | import os |
|
28 | 28 | import sys |
|
29 | 29 | import uuid |
|
30 | 30 | import logging |
|
31 | 31 | from os.path import dirname as dn, join as jn |
|
32 | 32 | |
|
33 | 33 | from rhodecode import __dbversion__, __py_version__ |
|
34 | 34 | |
|
35 | 35 | from rhodecode.model.user import UserModel |
|
36 | 36 | from rhodecode.lib.utils import ask_ok |
|
37 | 37 | from rhodecode.model import init_model |
|
38 | 38 | from rhodecode.model.db import User, Permission, RhodeCodeUi, \ |
|
39 | 39 | RhodeCodeSetting, UserToPerm, DbMigrateVersion, RepoGroup, \ |
|
40 | 40 | UserRepoGroupToPerm |
|
41 | 41 | |
|
42 | 42 | from sqlalchemy.engine import create_engine |
|
43 | 43 | from rhodecode.model.repos_group import ReposGroupModel |
|
44 | 44 | #from rhodecode.model import meta |
|
45 | 45 | from rhodecode.model.meta import Session, Base |
|
46 | 46 | from rhodecode.model.repo import RepoModel |
|
47 | 47 | |
|
48 | 48 | |
|
49 | 49 | log = logging.getLogger(__name__) |
|
50 | 50 | |
|
51 | 51 | |
|
52 | 52 | def notify(msg): |
|
53 | 53 | """ |
|
54 | 54 | Notification for migrations messages |
|
55 | 55 | """ |
|
56 | 56 | ml = len(msg) + (4 * 2) |
|
57 | 57 | print >> sys.stdout, ('*** %s ***\n%s' % (msg, '*' * ml)).upper() |
|
58 | 58 | |
|
59 | 59 | |
|
60 | 60 | class DbManage(object): |
|
61 | 61 | def __init__(self, log_sql, dbconf, root, tests=False, cli_args={}): |
|
62 | 62 | self.dbname = dbconf.split('/')[-1] |
|
63 | 63 | self.tests = tests |
|
64 | 64 | self.root = root |
|
65 | 65 | self.dburi = dbconf |
|
66 | 66 | self.log_sql = log_sql |
|
67 | 67 | self.db_exists = False |
|
68 | 68 | self.cli_args = cli_args |
|
69 | 69 | self.init_db() |
|
70 | global ask_ok | |
|
71 | 70 | |
|
72 |
|
|
|
73 | ask_ok = lambda *args, **kwargs: True | |
|
74 | elif not self.cli_args.get('force_ask'): | |
|
75 |
ask_ok = lambda *args, **kwargs: |
|
|
71 | force_ask = self.cli_args.get('force_ask') | |
|
72 | if force_ask is not None: | |
|
73 | global ask_ok | |
|
74 | ask_ok = lambda *args, **kwargs: force_ask | |
|
76 | 75 | |
|
77 | 76 | def init_db(self): |
|
78 | 77 | engine = create_engine(self.dburi, echo=self.log_sql) |
|
79 | 78 | init_model(engine) |
|
80 | 79 | self.sa = Session() |
|
81 | 80 | |
|
82 | 81 | def create_tables(self, override=False): |
|
83 | 82 | """ |
|
84 | 83 | Create a auth database |
|
85 | 84 | """ |
|
86 | 85 | |
|
87 | 86 | log.info("Any existing database is going to be destroyed") |
|
88 | 87 | if self.tests: |
|
89 | 88 | destroy = True |
|
90 | 89 | else: |
|
91 | 90 | destroy = ask_ok('Are you sure to destroy old database ? [y/n]') |
|
92 | 91 | if not destroy: |
|
93 | 92 | sys.exit('Nothing done') |
|
94 | 93 | if destroy: |
|
95 | 94 | Base.metadata.drop_all() |
|
96 | 95 | |
|
97 | 96 | checkfirst = not override |
|
98 | 97 | Base.metadata.create_all(checkfirst=checkfirst) |
|
99 | 98 | log.info('Created tables for %s' % self.dbname) |
|
100 | 99 | |
|
101 | 100 | def set_db_version(self): |
|
102 | 101 | ver = DbMigrateVersion() |
|
103 | 102 | ver.version = __dbversion__ |
|
104 | 103 | ver.repository_id = 'rhodecode_db_migrations' |
|
105 | 104 | ver.repository_path = 'versions' |
|
106 | 105 | self.sa.add(ver) |
|
107 | 106 | log.info('db version set to: %s' % __dbversion__) |
|
108 | 107 | |
|
109 | 108 | def upgrade(self): |
|
110 | 109 | """ |
|
111 | 110 | Upgrades given database schema to given revision following |
|
112 | 111 | all needed steps, to perform the upgrade |
|
113 | 112 | |
|
114 | 113 | """ |
|
115 | 114 | |
|
116 | 115 | from rhodecode.lib.dbmigrate.migrate.versioning import api |
|
117 | 116 | from rhodecode.lib.dbmigrate.migrate.exceptions import \ |
|
118 | 117 | DatabaseNotControlledError |
|
119 | 118 | |
|
120 | 119 | if 'sqlite' in self.dburi: |
|
121 | 120 | print ( |
|
122 | 121 | '********************** WARNING **********************\n' |
|
123 | 122 | 'Make sure your version of sqlite is at least 3.7.X. \n' |
|
124 | 123 | 'Earlier versions are known to fail on some migrations\n' |
|
125 | 124 | '*****************************************************\n' |
|
126 | 125 | ) |
|
127 | 126 | upgrade = ask_ok('You are about to perform database upgrade, make ' |
|
128 | 127 | 'sure You backed up your database before. ' |
|
129 | 128 | 'Continue ? [y/n]') |
|
130 | 129 | if not upgrade: |
|
131 | 130 | sys.exit('Nothing done') |
|
132 | 131 | |
|
133 | 132 | repository_path = jn(dn(dn(dn(os.path.realpath(__file__)))), |
|
134 | 133 | 'rhodecode/lib/dbmigrate') |
|
135 | 134 | db_uri = self.dburi |
|
136 | 135 | |
|
137 | 136 | try: |
|
138 | 137 | curr_version = api.db_version(db_uri, repository_path) |
|
139 | 138 | msg = ('Found current database under version' |
|
140 | 139 | ' control with version %s' % curr_version) |
|
141 | 140 | |
|
142 | 141 | except (RuntimeError, DatabaseNotControlledError): |
|
143 | 142 | curr_version = 1 |
|
144 | 143 | msg = ('Current database is not under version control. Setting' |
|
145 | 144 | ' as version %s' % curr_version) |
|
146 | 145 | api.version_control(db_uri, repository_path, curr_version) |
|
147 | 146 | |
|
148 | 147 | notify(msg) |
|
149 | 148 | |
|
150 | 149 | if curr_version == __dbversion__: |
|
151 | 150 | sys.exit('This database is already at the newest version') |
|
152 | 151 | |
|
153 | 152 | #====================================================================== |
|
154 | 153 | # UPGRADE STEPS |
|
155 | 154 | #====================================================================== |
|
156 | 155 | |
|
157 | 156 | class UpgradeSteps(object): |
|
158 | 157 | """ |
|
159 | 158 | Those steps follow schema versions so for example schema |
|
160 | 159 | for example schema with seq 002 == step_2 and so on. |
|
161 | 160 | """ |
|
162 | 161 | |
|
163 | 162 | def __init__(self, klass): |
|
164 | 163 | self.klass = klass |
|
165 | 164 | |
|
166 | 165 | def step_0(self): |
|
167 | 166 | # step 0 is the schema upgrade, and than follow proper upgrades |
|
168 | 167 | notify('attempting to do database upgrade from ' |
|
169 | 168 | 'version %s to version %s' %(curr_version, __dbversion__)) |
|
170 | 169 | api.upgrade(db_uri, repository_path, __dbversion__) |
|
171 | 170 | notify('Schema upgrade completed') |
|
172 | 171 | |
|
173 | 172 | def step_1(self): |
|
174 | 173 | pass |
|
175 | 174 | |
|
176 | 175 | def step_2(self): |
|
177 | 176 | notify('Patching repo paths for newer version of RhodeCode') |
|
178 | 177 | self.klass.fix_repo_paths() |
|
179 | 178 | |
|
180 | 179 | notify('Patching default user of RhodeCode') |
|
181 | 180 | self.klass.fix_default_user() |
|
182 | 181 | |
|
183 | 182 | log.info('Changing ui settings') |
|
184 | 183 | self.klass.create_ui_settings() |
|
185 | 184 | |
|
186 | 185 | def step_3(self): |
|
187 | 186 | notify('Adding additional settings into RhodeCode db') |
|
188 | 187 | self.klass.fix_settings() |
|
189 | 188 | notify('Adding ldap defaults') |
|
190 | 189 | self.klass.create_ldap_options(skip_existing=True) |
|
191 | 190 | |
|
192 | 191 | def step_4(self): |
|
193 | 192 | notify('create permissions and fix groups') |
|
194 | 193 | self.klass.create_permissions() |
|
195 | 194 | self.klass.fixup_groups() |
|
196 | 195 | |
|
197 | 196 | def step_5(self): |
|
198 | 197 | pass |
|
199 | 198 | |
|
200 | 199 | def step_6(self): |
|
201 | 200 | |
|
202 | 201 | notify('re-checking permissions') |
|
203 | 202 | self.klass.create_permissions() |
|
204 | 203 | |
|
205 | 204 | notify('installing new UI options') |
|
206 | 205 | sett4 = RhodeCodeSetting('show_public_icon', True) |
|
207 | 206 | Session().add(sett4) |
|
208 | 207 | sett5 = RhodeCodeSetting('show_private_icon', True) |
|
209 | 208 | Session().add(sett5) |
|
210 | 209 | sett6 = RhodeCodeSetting('stylify_metatags', False) |
|
211 | 210 | Session().add(sett6) |
|
212 | 211 | |
|
213 | 212 | notify('fixing old PULL hook') |
|
214 | 213 | _pull = RhodeCodeUi.get_by_key('preoutgoing.pull_logger') |
|
215 | 214 | if _pull: |
|
216 | 215 | _pull.ui_key = RhodeCodeUi.HOOK_PULL |
|
217 | 216 | Session().add(_pull) |
|
218 | 217 | |
|
219 | 218 | notify('fixing old PUSH hook') |
|
220 | 219 | _push = RhodeCodeUi.get_by_key('pretxnchangegroup.push_logger') |
|
221 | 220 | if _push: |
|
222 | 221 | _push.ui_key = RhodeCodeUi.HOOK_PUSH |
|
223 | 222 | Session().add(_push) |
|
224 | 223 | |
|
225 | 224 | notify('installing new pre-push hook') |
|
226 | 225 | hooks4 = RhodeCodeUi() |
|
227 | 226 | hooks4.ui_section = 'hooks' |
|
228 | 227 | hooks4.ui_key = RhodeCodeUi.HOOK_PRE_PUSH |
|
229 | 228 | hooks4.ui_value = 'python:rhodecode.lib.hooks.pre_push' |
|
230 | 229 | Session().add(hooks4) |
|
231 | 230 | |
|
232 | 231 | notify('installing new pre-pull hook') |
|
233 | 232 | hooks6 = RhodeCodeUi() |
|
234 | 233 | hooks6.ui_section = 'hooks' |
|
235 | 234 | hooks6.ui_key = RhodeCodeUi.HOOK_PRE_PULL |
|
236 | 235 | hooks6.ui_value = 'python:rhodecode.lib.hooks.pre_pull' |
|
237 | 236 | Session().add(hooks6) |
|
238 | 237 | |
|
239 | 238 | notify('installing hgsubversion option') |
|
240 | 239 | # enable hgsubversion disabled by default |
|
241 | 240 | hgsubversion = RhodeCodeUi() |
|
242 | 241 | hgsubversion.ui_section = 'extensions' |
|
243 | 242 | hgsubversion.ui_key = 'hgsubversion' |
|
244 | 243 | hgsubversion.ui_value = '' |
|
245 | 244 | hgsubversion.ui_active = False |
|
246 | 245 | Session().add(hgsubversion) |
|
247 | 246 | |
|
248 | 247 | notify('installing hg git option') |
|
249 | 248 | # enable hggit disabled by default |
|
250 | 249 | hggit = RhodeCodeUi() |
|
251 | 250 | hggit.ui_section = 'extensions' |
|
252 | 251 | hggit.ui_key = 'hggit' |
|
253 | 252 | hggit.ui_value = '' |
|
254 | 253 | hggit.ui_active = False |
|
255 | 254 | Session().add(hggit) |
|
256 | 255 | |
|
257 | 256 | notify('re-check default permissions') |
|
258 | 257 | default_user = User.get_by_username(User.DEFAULT_USER) |
|
259 | 258 | perm = Permission.get_by_key('hg.fork.repository') |
|
260 | 259 | reg_perm = UserToPerm() |
|
261 | 260 | reg_perm.user = default_user |
|
262 | 261 | reg_perm.permission = perm |
|
263 | 262 | Session().add(reg_perm) |
|
264 | 263 | |
|
265 | 264 | def step_7(self): |
|
266 | 265 | perm_fixes = self.klass.reset_permissions(User.DEFAULT_USER) |
|
267 | 266 | Session().commit() |
|
268 | 267 | if perm_fixes: |
|
269 | 268 | notify('There was an inconsistent state of permissions ' |
|
270 | 269 | 'detected for default user. Permissions are now ' |
|
271 | 270 | 'reset to the default value for default user. ' |
|
272 | 271 | 'Please validate and check default permissions ' |
|
273 | 272 | 'in admin panel') |
|
274 | 273 | |
|
275 | 274 | def step_8(self): |
|
276 | 275 | self.klass.populate_default_permissions() |
|
277 | 276 | self.klass.create_default_options(skip_existing=True) |
|
278 | 277 | Session().commit() |
|
279 | 278 | |
|
280 | 279 | def step_9(self): |
|
281 | 280 | perm_fixes = self.klass.reset_permissions(User.DEFAULT_USER) |
|
282 | 281 | Session().commit() |
|
283 | 282 | if perm_fixes: |
|
284 | 283 | notify('There was an inconsistent state of permissions ' |
|
285 | 284 | 'detected for default user. Permissions are now ' |
|
286 | 285 | 'reset to the default value for default user. ' |
|
287 | 286 | 'Please validate and check default permissions ' |
|
288 | 287 | 'in admin panel') |
|
289 | 288 | |
|
290 | 289 | def step_10(self): |
|
291 | 290 | pass |
|
292 | 291 | |
|
293 | 292 | def step_11(self): |
|
294 | 293 | self.klass.update_repo_info() |
|
295 | 294 | |
|
296 | 295 | upgrade_steps = [0] + range(curr_version + 1, __dbversion__ + 1) |
|
297 | 296 | |
|
298 | 297 | # CALL THE PROPER ORDER OF STEPS TO PERFORM FULL UPGRADE |
|
299 | 298 | _step = None |
|
300 | 299 | for step in upgrade_steps: |
|
301 | 300 | notify('performing upgrade step %s' % step) |
|
302 | 301 | getattr(UpgradeSteps(self), 'step_%s' % step)() |
|
303 | 302 | self.sa.commit() |
|
304 | 303 | _step = step |
|
305 | 304 | |
|
306 | 305 | notify('upgrade to version %s successful' % _step) |
|
307 | 306 | |
|
308 | 307 | def fix_repo_paths(self): |
|
309 | 308 | """ |
|
310 | 309 | Fixes a old rhodecode version path into new one without a '*' |
|
311 | 310 | """ |
|
312 | 311 | |
|
313 | 312 | paths = self.sa.query(RhodeCodeUi)\ |
|
314 | 313 | .filter(RhodeCodeUi.ui_key == '/')\ |
|
315 | 314 | .scalar() |
|
316 | 315 | |
|
317 | 316 | paths.ui_value = paths.ui_value.replace('*', '') |
|
318 | 317 | |
|
319 | 318 | try: |
|
320 | 319 | self.sa.add(paths) |
|
321 | 320 | self.sa.commit() |
|
322 | 321 | except Exception: |
|
323 | 322 | self.sa.rollback() |
|
324 | 323 | raise |
|
325 | 324 | |
|
326 | 325 | def fix_default_user(self): |
|
327 | 326 | """ |
|
328 | 327 | Fixes a old default user with some 'nicer' default values, |
|
329 | 328 | used mostly for anonymous access |
|
330 | 329 | """ |
|
331 | 330 | def_user = self.sa.query(User)\ |
|
332 | 331 | .filter(User.username == 'default')\ |
|
333 | 332 | .one() |
|
334 | 333 | |
|
335 | 334 | def_user.name = 'Anonymous' |
|
336 | 335 | def_user.lastname = 'User' |
|
337 | 336 | def_user.email = 'anonymous@rhodecode.org' |
|
338 | 337 | |
|
339 | 338 | try: |
|
340 | 339 | self.sa.add(def_user) |
|
341 | 340 | self.sa.commit() |
|
342 | 341 | except Exception: |
|
343 | 342 | self.sa.rollback() |
|
344 | 343 | raise |
|
345 | 344 | |
|
346 | 345 | def fix_settings(self): |
|
347 | 346 | """ |
|
348 | 347 | Fixes rhodecode settings adds ga_code key for google analytics |
|
349 | 348 | """ |
|
350 | 349 | |
|
351 | 350 | hgsettings3 = RhodeCodeSetting('ga_code', '') |
|
352 | 351 | |
|
353 | 352 | try: |
|
354 | 353 | self.sa.add(hgsettings3) |
|
355 | 354 | self.sa.commit() |
|
356 | 355 | except Exception: |
|
357 | 356 | self.sa.rollback() |
|
358 | 357 | raise |
|
359 | 358 | |
|
360 | 359 | def admin_prompt(self, second=False): |
|
361 | 360 | if not self.tests: |
|
362 | 361 | import getpass |
|
363 | 362 | |
|
364 | 363 | # defaults |
|
365 | 364 | defaults = self.cli_args |
|
366 | 365 | username = defaults.get('username') |
|
367 | 366 | password = defaults.get('password') |
|
368 | 367 | email = defaults.get('email') |
|
369 | 368 | |
|
370 | 369 | def get_password(): |
|
371 | 370 | password = getpass.getpass('Specify admin password ' |
|
372 | 371 | '(min 6 chars):') |
|
373 | 372 | confirm = getpass.getpass('Confirm password:') |
|
374 | 373 | |
|
375 | 374 | if password != confirm: |
|
376 | 375 | log.error('passwords mismatch') |
|
377 | 376 | return False |
|
378 | 377 | if len(password) < 6: |
|
379 | 378 | log.error('password is to short use at least 6 characters') |
|
380 | 379 | return False |
|
381 | 380 | |
|
382 | 381 | return password |
|
383 | 382 | if username is None: |
|
384 | 383 | username = raw_input('Specify admin username:') |
|
385 | 384 | if password is None: |
|
386 | 385 | password = get_password() |
|
387 | 386 | if not password: |
|
388 | 387 | #second try |
|
389 | 388 | password = get_password() |
|
390 | 389 | if not password: |
|
391 | 390 | sys.exit() |
|
392 | 391 | if email is None: |
|
393 | 392 | email = raw_input('Specify admin email:') |
|
394 | 393 | self.create_user(username, password, email, True) |
|
395 | 394 | else: |
|
396 | 395 | log.info('creating admin and regular test users') |
|
397 | 396 | from rhodecode.tests import TEST_USER_ADMIN_LOGIN, \ |
|
398 | 397 | TEST_USER_ADMIN_PASS, TEST_USER_ADMIN_EMAIL, \ |
|
399 | 398 | TEST_USER_REGULAR_LOGIN, TEST_USER_REGULAR_PASS, \ |
|
400 | 399 | TEST_USER_REGULAR_EMAIL, TEST_USER_REGULAR2_LOGIN, \ |
|
401 | 400 | TEST_USER_REGULAR2_PASS, TEST_USER_REGULAR2_EMAIL |
|
402 | 401 | |
|
403 | 402 | self.create_user(TEST_USER_ADMIN_LOGIN, TEST_USER_ADMIN_PASS, |
|
404 | 403 | TEST_USER_ADMIN_EMAIL, True) |
|
405 | 404 | |
|
406 | 405 | self.create_user(TEST_USER_REGULAR_LOGIN, TEST_USER_REGULAR_PASS, |
|
407 | 406 | TEST_USER_REGULAR_EMAIL, False) |
|
408 | 407 | |
|
409 | 408 | self.create_user(TEST_USER_REGULAR2_LOGIN, TEST_USER_REGULAR2_PASS, |
|
410 | 409 | TEST_USER_REGULAR2_EMAIL, False) |
|
411 | 410 | |
|
412 | 411 | def create_ui_settings(self): |
|
413 | 412 | """ |
|
414 | 413 | Creates ui settings, fills out hooks |
|
415 | 414 | and disables dotencode |
|
416 | 415 | """ |
|
417 | 416 | |
|
418 | 417 | #HOOKS |
|
419 | 418 | hooks1_key = RhodeCodeUi.HOOK_UPDATE |
|
420 | 419 | hooks1_ = self.sa.query(RhodeCodeUi)\ |
|
421 | 420 | .filter(RhodeCodeUi.ui_key == hooks1_key).scalar() |
|
422 | 421 | |
|
423 | 422 | hooks1 = RhodeCodeUi() if hooks1_ is None else hooks1_ |
|
424 | 423 | hooks1.ui_section = 'hooks' |
|
425 | 424 | hooks1.ui_key = hooks1_key |
|
426 | 425 | hooks1.ui_value = 'hg update >&2' |
|
427 | 426 | hooks1.ui_active = False |
|
428 | 427 | self.sa.add(hooks1) |
|
429 | 428 | |
|
430 | 429 | hooks2_key = RhodeCodeUi.HOOK_REPO_SIZE |
|
431 | 430 | hooks2_ = self.sa.query(RhodeCodeUi)\ |
|
432 | 431 | .filter(RhodeCodeUi.ui_key == hooks2_key).scalar() |
|
433 | 432 | hooks2 = RhodeCodeUi() if hooks2_ is None else hooks2_ |
|
434 | 433 | hooks2.ui_section = 'hooks' |
|
435 | 434 | hooks2.ui_key = hooks2_key |
|
436 | 435 | hooks2.ui_value = 'python:rhodecode.lib.hooks.repo_size' |
|
437 | 436 | self.sa.add(hooks2) |
|
438 | 437 | |
|
439 | 438 | hooks3 = RhodeCodeUi() |
|
440 | 439 | hooks3.ui_section = 'hooks' |
|
441 | 440 | hooks3.ui_key = RhodeCodeUi.HOOK_PUSH |
|
442 | 441 | hooks3.ui_value = 'python:rhodecode.lib.hooks.log_push_action' |
|
443 | 442 | self.sa.add(hooks3) |
|
444 | 443 | |
|
445 | 444 | hooks4 = RhodeCodeUi() |
|
446 | 445 | hooks4.ui_section = 'hooks' |
|
447 | 446 | hooks4.ui_key = RhodeCodeUi.HOOK_PRE_PUSH |
|
448 | 447 | hooks4.ui_value = 'python:rhodecode.lib.hooks.pre_push' |
|
449 | 448 | self.sa.add(hooks4) |
|
450 | 449 | |
|
451 | 450 | hooks5 = RhodeCodeUi() |
|
452 | 451 | hooks5.ui_section = 'hooks' |
|
453 | 452 | hooks5.ui_key = RhodeCodeUi.HOOK_PULL |
|
454 | 453 | hooks5.ui_value = 'python:rhodecode.lib.hooks.log_pull_action' |
|
455 | 454 | self.sa.add(hooks5) |
|
456 | 455 | |
|
457 | 456 | hooks6 = RhodeCodeUi() |
|
458 | 457 | hooks6.ui_section = 'hooks' |
|
459 | 458 | hooks6.ui_key = RhodeCodeUi.HOOK_PRE_PULL |
|
460 | 459 | hooks6.ui_value = 'python:rhodecode.lib.hooks.pre_pull' |
|
461 | 460 | self.sa.add(hooks6) |
|
462 | 461 | |
|
463 | 462 | # enable largefiles |
|
464 | 463 | largefiles = RhodeCodeUi() |
|
465 | 464 | largefiles.ui_section = 'extensions' |
|
466 | 465 | largefiles.ui_key = 'largefiles' |
|
467 | 466 | largefiles.ui_value = '' |
|
468 | 467 | self.sa.add(largefiles) |
|
469 | 468 | |
|
470 | 469 | # enable hgsubversion disabled by default |
|
471 | 470 | hgsubversion = RhodeCodeUi() |
|
472 | 471 | hgsubversion.ui_section = 'extensions' |
|
473 | 472 | hgsubversion.ui_key = 'hgsubversion' |
|
474 | 473 | hgsubversion.ui_value = '' |
|
475 | 474 | hgsubversion.ui_active = False |
|
476 | 475 | self.sa.add(hgsubversion) |
|
477 | 476 | |
|
478 | 477 | # enable hggit disabled by default |
|
479 | 478 | hggit = RhodeCodeUi() |
|
480 | 479 | hggit.ui_section = 'extensions' |
|
481 | 480 | hggit.ui_key = 'hggit' |
|
482 | 481 | hggit.ui_value = '' |
|
483 | 482 | hggit.ui_active = False |
|
484 | 483 | self.sa.add(hggit) |
|
485 | 484 | |
|
486 | 485 | def create_ldap_options(self, skip_existing=False): |
|
487 | 486 | """Creates ldap settings""" |
|
488 | 487 | |
|
489 | 488 | for k, v in [('ldap_active', 'false'), ('ldap_host', ''), |
|
490 | 489 | ('ldap_port', '389'), ('ldap_tls_kind', 'PLAIN'), |
|
491 | 490 | ('ldap_tls_reqcert', ''), ('ldap_dn_user', ''), |
|
492 | 491 | ('ldap_dn_pass', ''), ('ldap_base_dn', ''), |
|
493 | 492 | ('ldap_filter', ''), ('ldap_search_scope', ''), |
|
494 | 493 | ('ldap_attr_login', ''), ('ldap_attr_firstname', ''), |
|
495 | 494 | ('ldap_attr_lastname', ''), ('ldap_attr_email', '')]: |
|
496 | 495 | |
|
497 | 496 | if skip_existing and RhodeCodeSetting.get_by_name(k) != None: |
|
498 | 497 | log.debug('Skipping option %s' % k) |
|
499 | 498 | continue |
|
500 | 499 | setting = RhodeCodeSetting(k, v) |
|
501 | 500 | self.sa.add(setting) |
|
502 | 501 | |
|
503 | 502 | def create_default_options(self, skip_existing=False): |
|
504 | 503 | """Creates default settings""" |
|
505 | 504 | |
|
506 | 505 | for k, v in [ |
|
507 | 506 | ('default_repo_enable_locking', False), |
|
508 | 507 | ('default_repo_enable_downloads', False), |
|
509 | 508 | ('default_repo_enable_statistics', False), |
|
510 | 509 | ('default_repo_private', False), |
|
511 | 510 | ('default_repo_type', 'hg')]: |
|
512 | 511 | |
|
513 | 512 | if skip_existing and RhodeCodeSetting.get_by_name(k) != None: |
|
514 | 513 | log.debug('Skipping option %s' % k) |
|
515 | 514 | continue |
|
516 | 515 | setting = RhodeCodeSetting(k, v) |
|
517 | 516 | self.sa.add(setting) |
|
518 | 517 | |
|
519 | 518 | def fixup_groups(self): |
|
520 | 519 | def_usr = User.get_by_username('default') |
|
521 | 520 | for g in RepoGroup.query().all(): |
|
522 | 521 | g.group_name = g.get_new_name(g.name) |
|
523 | 522 | self.sa.add(g) |
|
524 | 523 | # get default perm |
|
525 | 524 | default = UserRepoGroupToPerm.query()\ |
|
526 | 525 | .filter(UserRepoGroupToPerm.group == g)\ |
|
527 | 526 | .filter(UserRepoGroupToPerm.user == def_usr)\ |
|
528 | 527 | .scalar() |
|
529 | 528 | |
|
530 | 529 | if default is None: |
|
531 | 530 | log.debug('missing default permission for group %s adding' % g) |
|
532 | 531 | ReposGroupModel()._create_default_perms(g) |
|
533 | 532 | |
|
534 | 533 | def reset_permissions(self, username): |
|
535 | 534 | """ |
|
536 | 535 | Resets permissions to default state, usefull when old systems had |
|
537 | 536 | bad permissions, we must clean them up |
|
538 | 537 | |
|
539 | 538 | :param username: |
|
540 | 539 | :type username: |
|
541 | 540 | """ |
|
542 | 541 | default_user = User.get_by_username(username) |
|
543 | 542 | if not default_user: |
|
544 | 543 | return |
|
545 | 544 | |
|
546 | 545 | u2p = UserToPerm.query()\ |
|
547 | 546 | .filter(UserToPerm.user == default_user).all() |
|
548 | 547 | fixed = False |
|
549 | 548 | if len(u2p) != len(User.DEFAULT_PERMISSIONS): |
|
550 | 549 | for p in u2p: |
|
551 | 550 | Session().delete(p) |
|
552 | 551 | fixed = True |
|
553 | 552 | self.populate_default_permissions() |
|
554 | 553 | return fixed |
|
555 | 554 | |
|
556 | 555 | def update_repo_info(self): |
|
557 | 556 | RepoModel.update_repoinfo() |
|
558 | 557 | |
|
559 | 558 | def config_prompt(self, test_repo_path='', retries=3): |
|
560 | 559 | defaults = self.cli_args |
|
561 | 560 | _path = defaults.get('repos_location') |
|
562 | 561 | if retries == 3: |
|
563 | 562 | log.info('Setting up repositories config') |
|
564 | 563 | |
|
565 | 564 | if _path is not None: |
|
566 | 565 | path = _path |
|
567 | 566 | elif not self.tests and not test_repo_path: |
|
568 | 567 | path = raw_input( |
|
569 | 568 | 'Enter a valid absolute path to store repositories. ' |
|
570 | 569 | 'All repositories in that path will be added automatically:' |
|
571 | 570 | ) |
|
572 | 571 | else: |
|
573 | 572 | path = test_repo_path |
|
574 | 573 | path_ok = True |
|
575 | 574 | |
|
576 | 575 | # check proper dir |
|
577 | 576 | if not os.path.isdir(path): |
|
578 | 577 | path_ok = False |
|
579 | 578 | log.error('Given path %s is not a valid directory' % path) |
|
580 | 579 | |
|
581 | 580 | elif not os.path.isabs(path): |
|
582 | 581 | path_ok = False |
|
583 | 582 | log.error('Given path %s is not an absolute path' % path) |
|
584 | 583 | |
|
585 | 584 | # check write access |
|
586 | 585 | elif not os.access(path, os.W_OK) and path_ok: |
|
587 | 586 | path_ok = False |
|
588 | 587 | log.error('No write permission to given path %s' % path) |
|
589 | 588 | |
|
590 | 589 | if retries == 0: |
|
591 | 590 | sys.exit('max retries reached') |
|
592 | 591 | if not path_ok: |
|
593 | 592 | retries -= 1 |
|
594 | 593 | return self.config_prompt(test_repo_path, retries) |
|
595 | 594 | |
|
596 | 595 | real_path = os.path.normpath(os.path.realpath(path)) |
|
597 | 596 | |
|
598 | 597 | if real_path != os.path.normpath(path): |
|
599 | 598 | if not ask_ok(('Path looks like a symlink, Rhodecode will store ' |
|
600 | 599 | 'given path as %s ? [y/n]') % (real_path)): |
|
601 | 600 | log.error('Canceled by user') |
|
602 | 601 | sys.exit(-1) |
|
603 | 602 | |
|
604 | 603 | return real_path |
|
605 | 604 | |
|
606 | 605 | def create_settings(self, path): |
|
607 | 606 | |
|
608 | 607 | self.create_ui_settings() |
|
609 | 608 | |
|
610 | 609 | #HG UI OPTIONS |
|
611 | 610 | web1 = RhodeCodeUi() |
|
612 | 611 | web1.ui_section = 'web' |
|
613 | 612 | web1.ui_key = 'push_ssl' |
|
614 | 613 | web1.ui_value = 'false' |
|
615 | 614 | |
|
616 | 615 | web2 = RhodeCodeUi() |
|
617 | 616 | web2.ui_section = 'web' |
|
618 | 617 | web2.ui_key = 'allow_archive' |
|
619 | 618 | web2.ui_value = 'gz zip bz2' |
|
620 | 619 | |
|
621 | 620 | web3 = RhodeCodeUi() |
|
622 | 621 | web3.ui_section = 'web' |
|
623 | 622 | web3.ui_key = 'allow_push' |
|
624 | 623 | web3.ui_value = '*' |
|
625 | 624 | |
|
626 | 625 | web4 = RhodeCodeUi() |
|
627 | 626 | web4.ui_section = 'web' |
|
628 | 627 | web4.ui_key = 'baseurl' |
|
629 | 628 | web4.ui_value = '/' |
|
630 | 629 | |
|
631 | 630 | paths = RhodeCodeUi() |
|
632 | 631 | paths.ui_section = 'paths' |
|
633 | 632 | paths.ui_key = '/' |
|
634 | 633 | paths.ui_value = path |
|
635 | 634 | |
|
636 | 635 | phases = RhodeCodeUi() |
|
637 | 636 | phases.ui_section = 'phases' |
|
638 | 637 | phases.ui_key = 'publish' |
|
639 | 638 | phases.ui_value = False |
|
640 | 639 | |
|
641 | 640 | sett1 = RhodeCodeSetting('realm', 'RhodeCode authentication') |
|
642 | 641 | sett2 = RhodeCodeSetting('title', 'RhodeCode') |
|
643 | 642 | sett3 = RhodeCodeSetting('ga_code', '') |
|
644 | 643 | |
|
645 | 644 | sett4 = RhodeCodeSetting('show_public_icon', True) |
|
646 | 645 | sett5 = RhodeCodeSetting('show_private_icon', True) |
|
647 | 646 | sett6 = RhodeCodeSetting('stylify_metatags', False) |
|
648 | 647 | |
|
649 | 648 | self.sa.add(web1) |
|
650 | 649 | self.sa.add(web2) |
|
651 | 650 | self.sa.add(web3) |
|
652 | 651 | self.sa.add(web4) |
|
653 | 652 | self.sa.add(paths) |
|
654 | 653 | self.sa.add(sett1) |
|
655 | 654 | self.sa.add(sett2) |
|
656 | 655 | self.sa.add(sett3) |
|
657 | 656 | self.sa.add(sett4) |
|
658 | 657 | self.sa.add(sett5) |
|
659 | 658 | self.sa.add(sett6) |
|
660 | 659 | |
|
661 | 660 | self.create_ldap_options() |
|
662 | 661 | self.create_default_options() |
|
663 | 662 | |
|
664 | 663 | log.info('created ui config') |
|
665 | 664 | |
|
666 | 665 | def create_user(self, username, password, email='', admin=False): |
|
667 | 666 | log.info('creating user %s' % username) |
|
668 | 667 | UserModel().create_or_update(username, password, email, |
|
669 | 668 | firstname='RhodeCode', lastname='Admin', |
|
670 | 669 | active=True, admin=admin) |
|
671 | 670 | |
|
672 | 671 | def create_default_user(self): |
|
673 | 672 | log.info('creating default user') |
|
674 | 673 | # create default user for handling default permissions. |
|
675 | 674 | UserModel().create_or_update(username='default', |
|
676 | 675 | password=str(uuid.uuid1())[:8], |
|
677 | 676 | email='anonymous@rhodecode.org', |
|
678 | 677 | firstname='Anonymous', lastname='User') |
|
679 | 678 | |
|
680 | 679 | def create_permissions(self): |
|
681 | 680 | # module.(access|create|change|delete)_[name] |
|
682 | 681 | # module.(none|read|write|admin) |
|
683 | 682 | |
|
684 | 683 | for p in Permission.PERMS: |
|
685 | 684 | if not Permission.get_by_key(p[0]): |
|
686 | 685 | new_perm = Permission() |
|
687 | 686 | new_perm.permission_name = p[0] |
|
688 | 687 | new_perm.permission_longname = p[0] |
|
689 | 688 | self.sa.add(new_perm) |
|
690 | 689 | |
|
691 | 690 | def populate_default_permissions(self): |
|
692 | 691 | log.info('creating default user permissions') |
|
693 | 692 | |
|
694 | 693 | default_user = User.get_by_username('default') |
|
695 | 694 | |
|
696 | 695 | for def_perm in User.DEFAULT_PERMISSIONS: |
|
697 | 696 | |
|
698 | 697 | perm = self.sa.query(Permission)\ |
|
699 | 698 | .filter(Permission.permission_name == def_perm)\ |
|
700 | 699 | .scalar() |
|
701 | 700 | if not perm: |
|
702 | 701 | raise Exception( |
|
703 | 702 | 'CRITICAL: permission %s not found inside database !!' |
|
704 | 703 | % def_perm |
|
705 | 704 | ) |
|
706 | 705 | if not UserToPerm.query()\ |
|
707 | 706 | .filter(UserToPerm.permission == perm)\ |
|
708 | 707 | .filter(UserToPerm.user == default_user).scalar(): |
|
709 | 708 | reg_perm = UserToPerm() |
|
710 | 709 | reg_perm.user = default_user |
|
711 | 710 | reg_perm.permission = perm |
|
712 | 711 | self.sa.add(reg_perm) |
|
713 | 712 | |
|
714 | 713 | @staticmethod |
|
715 | 714 | def check_waitress(): |
|
716 | 715 | """ |
|
717 | 716 | Function executed at the end of setup |
|
718 | 717 | """ |
|
719 | 718 | if not __py_version__ >= (2, 6): |
|
720 | 719 | notify('Python2.5 detected, please switch ' |
|
721 | 720 | 'egg:waitress#main -> egg:Paste#http ' |
|
722 | 721 | 'in your .ini file') |
General Comments 0
You need to be logged in to leave comments.
Login now