Show More
@@ -1,582 +1,583 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__ |
|
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 sqlalchemy.schema import MetaData |
|
44 | 44 | from rhodecode.model.repos_group import ReposGroupModel |
|
45 | 45 | #from rhodecode.model import meta |
|
46 | 46 | from rhodecode.model.meta import Session, Base |
|
47 | 47 | |
|
48 | 48 | |
|
49 | 49 | log = logging.getLogger(__name__) |
|
50 | 50 | |
|
51 | 51 | |
|
52 | 52 | class DbManage(object): |
|
53 | 53 | def __init__(self, log_sql, dbconf, root, tests=False): |
|
54 | 54 | self.dbname = dbconf.split('/')[-1] |
|
55 | 55 | self.tests = tests |
|
56 | 56 | self.root = root |
|
57 | 57 | self.dburi = dbconf |
|
58 | 58 | self.log_sql = log_sql |
|
59 | 59 | self.db_exists = False |
|
60 | 60 | self.init_db() |
|
61 | 61 | |
|
62 | 62 | def init_db(self): |
|
63 | 63 | engine = create_engine(self.dburi, echo=self.log_sql) |
|
64 | 64 | init_model(engine) |
|
65 | 65 | self.sa = Session() |
|
66 | 66 | |
|
67 | 67 | def create_tables(self, override=False, defaults={}): |
|
68 | 68 | """ |
|
69 | 69 | Create a auth database |
|
70 | 70 | """ |
|
71 | 71 | quiet = defaults.get('quiet') |
|
72 | 72 | log.info("Any existing database is going to be destroyed") |
|
73 | 73 | if self.tests or quiet: |
|
74 | 74 | destroy = True |
|
75 | 75 | else: |
|
76 | 76 | destroy = ask_ok('Are you sure to destroy old database ? [y/n]') |
|
77 | 77 | if not destroy: |
|
78 | 78 | sys.exit() |
|
79 | 79 | if destroy: |
|
80 | 80 | Base.metadata.drop_all() |
|
81 | 81 | |
|
82 | 82 | checkfirst = not override |
|
83 | 83 | Base.metadata.create_all(checkfirst=checkfirst) |
|
84 | 84 | log.info('Created tables for %s' % self.dbname) |
|
85 | 85 | |
|
86 | 86 | def set_db_version(self): |
|
87 | 87 | ver = DbMigrateVersion() |
|
88 | 88 | ver.version = __dbversion__ |
|
89 | 89 | ver.repository_id = 'rhodecode_db_migrations' |
|
90 | 90 | ver.repository_path = 'versions' |
|
91 | 91 | self.sa.add(ver) |
|
92 | 92 | log.info('db version set to: %s' % __dbversion__) |
|
93 | 93 | |
|
94 | 94 | def upgrade(self): |
|
95 | 95 | """ |
|
96 | 96 | Upgrades given database schema to given revision following |
|
97 | 97 | all needed steps, to perform the upgrade |
|
98 | 98 | |
|
99 | 99 | """ |
|
100 | 100 | |
|
101 | 101 | from rhodecode.lib.dbmigrate.migrate.versioning import api |
|
102 | 102 | from rhodecode.lib.dbmigrate.migrate.exceptions import \ |
|
103 | 103 | DatabaseNotControlledError |
|
104 | 104 | |
|
105 | 105 | if 'sqlite' in self.dburi: |
|
106 | 106 | print ( |
|
107 | 107 | '********************** WARNING **********************\n' |
|
108 | 108 | 'Make sure your version of sqlite is at least 3.7.X. \n' |
|
109 | 109 | 'Earlier versions are known to fail on some migrations\n' |
|
110 | 110 | '*****************************************************\n' |
|
111 | 111 | ) |
|
112 | 112 | upgrade = ask_ok('You are about to perform database upgrade, make ' |
|
113 | 113 | 'sure You backed up your database before. ' |
|
114 | 114 | 'Continue ? [y/n]') |
|
115 | 115 | if not upgrade: |
|
116 | 116 | sys.exit('Nothing done') |
|
117 | 117 | |
|
118 | 118 | repository_path = jn(dn(dn(dn(os.path.realpath(__file__)))), |
|
119 | 119 | 'rhodecode/lib/dbmigrate') |
|
120 | 120 | db_uri = self.dburi |
|
121 | 121 | |
|
122 | 122 | try: |
|
123 | 123 | curr_version = api.db_version(db_uri, repository_path) |
|
124 | 124 | msg = ('Found current database under version' |
|
125 | 125 | ' control with version %s' % curr_version) |
|
126 | 126 | |
|
127 | 127 | except (RuntimeError, DatabaseNotControlledError): |
|
128 | 128 | curr_version = 1 |
|
129 | 129 | msg = ('Current database is not under version control. Setting' |
|
130 | 130 | ' as version %s' % curr_version) |
|
131 | 131 | api.version_control(db_uri, repository_path, curr_version) |
|
132 | 132 | |
|
133 | 133 | print (msg) |
|
134 | 134 | |
|
135 | 135 | if curr_version == __dbversion__: |
|
136 | 136 | sys.exit('This database is already at the newest version') |
|
137 | 137 | |
|
138 | 138 | #====================================================================== |
|
139 | 139 | # UPGRADE STEPS |
|
140 | 140 | #====================================================================== |
|
141 | 141 | class UpgradeSteps(object): |
|
142 | 142 | """ |
|
143 | 143 | Those steps follow schema versions so for example schema |
|
144 | 144 | for example schema with seq 002 == step_2 and so on. |
|
145 | 145 | """ |
|
146 | 146 | |
|
147 | 147 | def __init__(self, klass): |
|
148 | 148 | self.klass = klass |
|
149 | 149 | |
|
150 | 150 | def step_0(self): |
|
151 | 151 | # step 0 is the schema upgrade, and than follow proper upgrades |
|
152 | 152 | print ('attempting to do database upgrade to version %s' \ |
|
153 | 153 | % __dbversion__) |
|
154 | 154 | api.upgrade(db_uri, repository_path, __dbversion__) |
|
155 | 155 | print ('Schema upgrade completed') |
|
156 | 156 | |
|
157 | 157 | def step_1(self): |
|
158 | 158 | pass |
|
159 | 159 | |
|
160 | 160 | def step_2(self): |
|
161 | 161 | print ('Patching repo paths for newer version of RhodeCode') |
|
162 | 162 | self.klass.fix_repo_paths() |
|
163 | 163 | |
|
164 | 164 | print ('Patching default user of RhodeCode') |
|
165 | 165 | self.klass.fix_default_user() |
|
166 | 166 | |
|
167 | 167 | log.info('Changing ui settings') |
|
168 | 168 | self.klass.create_ui_settings() |
|
169 | 169 | |
|
170 | 170 | def step_3(self): |
|
171 | 171 | print ('Adding additional settings into RhodeCode db') |
|
172 | 172 | self.klass.fix_settings() |
|
173 | 173 | print ('Adding ldap defaults') |
|
174 | 174 | self.klass.create_ldap_options(skip_existing=True) |
|
175 | 175 | |
|
176 | 176 | def step_4(self): |
|
177 | 177 | print ('create permissions and fix groups') |
|
178 | 178 | self.klass.create_permissions() |
|
179 | 179 | self.klass.fixup_groups() |
|
180 | 180 | |
|
181 | 181 | def step_5(self): |
|
182 | 182 | pass |
|
183 | 183 | |
|
184 | 184 | def step_6(self): |
|
185 | 185 | print ('re-checking permissions') |
|
186 | 186 | self.klass.create_permissions() |
|
187 | 187 | |
|
188 | 188 | print ('installing new hooks') |
|
189 | 189 | hooks4 = RhodeCodeUi() |
|
190 | hooks4.ui_section = 'hooks' | |
|
190 | 191 | hooks4.ui_key = RhodeCodeUi.HOOK_PRE_PUSH |
|
191 | 192 | hooks4.ui_value = 'python:rhodecode.lib.hooks.pre_push' |
|
192 | 193 | Session().add(hooks4) |
|
193 | 194 | |
|
194 | 195 | hooks6 = RhodeCodeUi() |
|
195 | 196 | hooks6.ui_section = 'hooks' |
|
196 | 197 | hooks6.ui_key = RhodeCodeUi.HOOK_PRE_PULL |
|
197 | 198 | hooks6.ui_value = 'python:rhodecode.lib.hooks.pre_pull' |
|
198 | 199 | Session().add(hooks6) |
|
199 | 200 | |
|
200 | 201 | print ('installing hgsubversion option') |
|
201 | 202 | # enable hgsubversion disabled by default |
|
202 | 203 | hgsubversion = RhodeCodeUi() |
|
203 | 204 | hgsubversion.ui_section = 'extensions' |
|
204 | 205 | hgsubversion.ui_key = 'hgsubversion' |
|
205 | 206 | hgsubversion.ui_value = '' |
|
206 | 207 | hgsubversion.ui_active = False |
|
207 | 208 | Session().add(hgsubversion) |
|
208 | 209 | |
|
209 | 210 | print ('installing hg git option') |
|
210 | 211 | # enable hggit disabled by default |
|
211 | 212 | hggit = RhodeCodeUi() |
|
212 | 213 | hggit.ui_section = 'extensions' |
|
213 | 214 | hggit.ui_key = 'hggit' |
|
214 | 215 | hggit.ui_value = '' |
|
215 | 216 | hggit.ui_active = False |
|
216 | 217 | Session().add(hggit) |
|
217 | 218 | |
|
218 | 219 | print ('re-check default permissions') |
|
219 | 220 | self.klass.populate_default_permissions() |
|
220 | 221 | |
|
221 | 222 | upgrade_steps = [0] + range(curr_version + 1, __dbversion__ + 1) |
|
222 | 223 | |
|
223 | 224 | # CALL THE PROPER ORDER OF STEPS TO PERFORM FULL UPGRADE |
|
224 | 225 | for step in upgrade_steps: |
|
225 | 226 | print ('performing upgrade step %s' % step) |
|
226 | 227 | getattr(UpgradeSteps(self), 'step_%s' % step)() |
|
227 | 228 | self.sa.commit() |
|
228 | 229 | |
|
229 | 230 | def fix_repo_paths(self): |
|
230 | 231 | """ |
|
231 | 232 | Fixes a old rhodecode version path into new one without a '*' |
|
232 | 233 | """ |
|
233 | 234 | |
|
234 | 235 | paths = self.sa.query(RhodeCodeUi)\ |
|
235 | 236 | .filter(RhodeCodeUi.ui_key == '/')\ |
|
236 | 237 | .scalar() |
|
237 | 238 | |
|
238 | 239 | paths.ui_value = paths.ui_value.replace('*', '') |
|
239 | 240 | |
|
240 | 241 | try: |
|
241 | 242 | self.sa.add(paths) |
|
242 | 243 | self.sa.commit() |
|
243 | 244 | except: |
|
244 | 245 | self.sa.rollback() |
|
245 | 246 | raise |
|
246 | 247 | |
|
247 | 248 | def fix_default_user(self): |
|
248 | 249 | """ |
|
249 | 250 | Fixes a old default user with some 'nicer' default values, |
|
250 | 251 | used mostly for anonymous access |
|
251 | 252 | """ |
|
252 | 253 | def_user = self.sa.query(User)\ |
|
253 | 254 | .filter(User.username == 'default')\ |
|
254 | 255 | .one() |
|
255 | 256 | |
|
256 | 257 | def_user.name = 'Anonymous' |
|
257 | 258 | def_user.lastname = 'User' |
|
258 | 259 | def_user.email = 'anonymous@rhodecode.org' |
|
259 | 260 | |
|
260 | 261 | try: |
|
261 | 262 | self.sa.add(def_user) |
|
262 | 263 | self.sa.commit() |
|
263 | 264 | except: |
|
264 | 265 | self.sa.rollback() |
|
265 | 266 | raise |
|
266 | 267 | |
|
267 | 268 | def fix_settings(self): |
|
268 | 269 | """ |
|
269 | 270 | Fixes rhodecode settings adds ga_code key for google analytics |
|
270 | 271 | """ |
|
271 | 272 | |
|
272 | 273 | hgsettings3 = RhodeCodeSetting('ga_code', '') |
|
273 | 274 | |
|
274 | 275 | try: |
|
275 | 276 | self.sa.add(hgsettings3) |
|
276 | 277 | self.sa.commit() |
|
277 | 278 | except: |
|
278 | 279 | self.sa.rollback() |
|
279 | 280 | raise |
|
280 | 281 | |
|
281 | 282 | def admin_prompt(self, second=False, defaults={}): |
|
282 | 283 | if not self.tests: |
|
283 | 284 | import getpass |
|
284 | 285 | |
|
285 | 286 | # defaults |
|
286 | 287 | username = defaults.get('username') |
|
287 | 288 | password = defaults.get('password') |
|
288 | 289 | email = defaults.get('email') |
|
289 | 290 | |
|
290 | 291 | def get_password(): |
|
291 | 292 | password = getpass.getpass('Specify admin password ' |
|
292 | 293 | '(min 6 chars):') |
|
293 | 294 | confirm = getpass.getpass('Confirm password:') |
|
294 | 295 | |
|
295 | 296 | if password != confirm: |
|
296 | 297 | log.error('passwords mismatch') |
|
297 | 298 | return False |
|
298 | 299 | if len(password) < 6: |
|
299 | 300 | log.error('password is to short use at least 6 characters') |
|
300 | 301 | return False |
|
301 | 302 | |
|
302 | 303 | return password |
|
303 | 304 | if username is None: |
|
304 | 305 | username = raw_input('Specify admin username:') |
|
305 | 306 | if password is None: |
|
306 | 307 | password = get_password() |
|
307 | 308 | if not password: |
|
308 | 309 | #second try |
|
309 | 310 | password = get_password() |
|
310 | 311 | if not password: |
|
311 | 312 | sys.exit() |
|
312 | 313 | if email is None: |
|
313 | 314 | email = raw_input('Specify admin email:') |
|
314 | 315 | self.create_user(username, password, email, True) |
|
315 | 316 | else: |
|
316 | 317 | log.info('creating admin and regular test users') |
|
317 | 318 | from rhodecode.tests import TEST_USER_ADMIN_LOGIN, \ |
|
318 | 319 | TEST_USER_ADMIN_PASS, TEST_USER_ADMIN_EMAIL, \ |
|
319 | 320 | TEST_USER_REGULAR_LOGIN, TEST_USER_REGULAR_PASS, \ |
|
320 | 321 | TEST_USER_REGULAR_EMAIL, TEST_USER_REGULAR2_LOGIN, \ |
|
321 | 322 | TEST_USER_REGULAR2_PASS, TEST_USER_REGULAR2_EMAIL |
|
322 | 323 | |
|
323 | 324 | self.create_user(TEST_USER_ADMIN_LOGIN, TEST_USER_ADMIN_PASS, |
|
324 | 325 | TEST_USER_ADMIN_EMAIL, True) |
|
325 | 326 | |
|
326 | 327 | self.create_user(TEST_USER_REGULAR_LOGIN, TEST_USER_REGULAR_PASS, |
|
327 | 328 | TEST_USER_REGULAR_EMAIL, False) |
|
328 | 329 | |
|
329 | 330 | self.create_user(TEST_USER_REGULAR2_LOGIN, TEST_USER_REGULAR2_PASS, |
|
330 | 331 | TEST_USER_REGULAR2_EMAIL, False) |
|
331 | 332 | |
|
332 | 333 | def create_ui_settings(self): |
|
333 | 334 | """ |
|
334 | 335 | Creates ui settings, fills out hooks |
|
335 | 336 | and disables dotencode |
|
336 | 337 | """ |
|
337 | 338 | |
|
338 | 339 | #HOOKS |
|
339 | 340 | hooks1_key = RhodeCodeUi.HOOK_UPDATE |
|
340 | 341 | hooks1_ = self.sa.query(RhodeCodeUi)\ |
|
341 | 342 | .filter(RhodeCodeUi.ui_key == hooks1_key).scalar() |
|
342 | 343 | |
|
343 | 344 | hooks1 = RhodeCodeUi() if hooks1_ is None else hooks1_ |
|
344 | 345 | hooks1.ui_section = 'hooks' |
|
345 | 346 | hooks1.ui_key = hooks1_key |
|
346 | 347 | hooks1.ui_value = 'hg update >&2' |
|
347 | 348 | hooks1.ui_active = False |
|
348 | 349 | self.sa.add(hooks1) |
|
349 | 350 | |
|
350 | 351 | hooks2_key = RhodeCodeUi.HOOK_REPO_SIZE |
|
351 | 352 | hooks2_ = self.sa.query(RhodeCodeUi)\ |
|
352 | 353 | .filter(RhodeCodeUi.ui_key == hooks2_key).scalar() |
|
353 | 354 | hooks2 = RhodeCodeUi() if hooks2_ is None else hooks2_ |
|
354 | 355 | hooks2.ui_section = 'hooks' |
|
355 | 356 | hooks2.ui_key = hooks2_key |
|
356 | 357 | hooks2.ui_value = 'python:rhodecode.lib.hooks.repo_size' |
|
357 | 358 | self.sa.add(hooks2) |
|
358 | 359 | |
|
359 | 360 | hooks3 = RhodeCodeUi() |
|
360 | 361 | hooks3.ui_section = 'hooks' |
|
361 | 362 | hooks3.ui_key = RhodeCodeUi.HOOK_PUSH |
|
362 | 363 | hooks3.ui_value = 'python:rhodecode.lib.hooks.log_push_action' |
|
363 | 364 | self.sa.add(hooks3) |
|
364 | 365 | |
|
365 | 366 | hooks4 = RhodeCodeUi() |
|
366 | 367 | hooks4.ui_section = 'hooks' |
|
367 | 368 | hooks4.ui_key = RhodeCodeUi.HOOK_PRE_PUSH |
|
368 | 369 | hooks4.ui_value = 'python:rhodecode.lib.hooks.pre_push' |
|
369 | 370 | self.sa.add(hooks4) |
|
370 | 371 | |
|
371 | 372 | hooks5 = RhodeCodeUi() |
|
372 | 373 | hooks5.ui_section = 'hooks' |
|
373 | 374 | hooks5.ui_key = RhodeCodeUi.HOOK_PULL |
|
374 | 375 | hooks5.ui_value = 'python:rhodecode.lib.hooks.log_pull_action' |
|
375 | 376 | self.sa.add(hooks5) |
|
376 | 377 | |
|
377 | 378 | hooks6 = RhodeCodeUi() |
|
378 | 379 | hooks6.ui_section = 'hooks' |
|
379 | 380 | hooks6.ui_key = RhodeCodeUi.HOOK_PRE_PULL |
|
380 | 381 | hooks6.ui_value = 'python:rhodecode.lib.hooks.pre_pull' |
|
381 | 382 | self.sa.add(hooks6) |
|
382 | 383 | |
|
383 | 384 | # enable largefiles |
|
384 | 385 | largefiles = RhodeCodeUi() |
|
385 | 386 | largefiles.ui_section = 'extensions' |
|
386 | 387 | largefiles.ui_key = 'largefiles' |
|
387 | 388 | largefiles.ui_value = '' |
|
388 | 389 | self.sa.add(largefiles) |
|
389 | 390 | |
|
390 | 391 | # enable hgsubversion disabled by default |
|
391 | 392 | hgsubversion = RhodeCodeUi() |
|
392 | 393 | hgsubversion.ui_section = 'extensions' |
|
393 | 394 | hgsubversion.ui_key = 'hgsubversion' |
|
394 | 395 | hgsubversion.ui_value = '' |
|
395 | 396 | hgsubversion.ui_active = False |
|
396 | 397 | self.sa.add(hgsubversion) |
|
397 | 398 | |
|
398 | 399 | # enable hggit disabled by default |
|
399 | 400 | hggit = RhodeCodeUi() |
|
400 | 401 | hggit.ui_section = 'extensions' |
|
401 | 402 | hggit.ui_key = 'hggit' |
|
402 | 403 | hggit.ui_value = '' |
|
403 | 404 | hggit.ui_active = False |
|
404 | 405 | self.sa.add(hggit) |
|
405 | 406 | |
|
406 | 407 | def create_ldap_options(self, skip_existing=False): |
|
407 | 408 | """Creates ldap settings""" |
|
408 | 409 | |
|
409 | 410 | for k, v in [('ldap_active', 'false'), ('ldap_host', ''), |
|
410 | 411 | ('ldap_port', '389'), ('ldap_tls_kind', 'PLAIN'), |
|
411 | 412 | ('ldap_tls_reqcert', ''), ('ldap_dn_user', ''), |
|
412 | 413 | ('ldap_dn_pass', ''), ('ldap_base_dn', ''), |
|
413 | 414 | ('ldap_filter', ''), ('ldap_search_scope', ''), |
|
414 | 415 | ('ldap_attr_login', ''), ('ldap_attr_firstname', ''), |
|
415 | 416 | ('ldap_attr_lastname', ''), ('ldap_attr_email', '')]: |
|
416 | 417 | |
|
417 | 418 | if skip_existing and RhodeCodeSetting.get_by_name(k) != None: |
|
418 | 419 | log.debug('Skipping option %s' % k) |
|
419 | 420 | continue |
|
420 | 421 | setting = RhodeCodeSetting(k, v) |
|
421 | 422 | self.sa.add(setting) |
|
422 | 423 | |
|
423 | 424 | def fixup_groups(self): |
|
424 | 425 | def_usr = User.get_by_username('default') |
|
425 | 426 | for g in RepoGroup.query().all(): |
|
426 | 427 | g.group_name = g.get_new_name(g.name) |
|
427 | 428 | self.sa.add(g) |
|
428 | 429 | # get default perm |
|
429 | 430 | default = UserRepoGroupToPerm.query()\ |
|
430 | 431 | .filter(UserRepoGroupToPerm.group == g)\ |
|
431 | 432 | .filter(UserRepoGroupToPerm.user == def_usr)\ |
|
432 | 433 | .scalar() |
|
433 | 434 | |
|
434 | 435 | if default is None: |
|
435 | 436 | log.debug('missing default permission for group %s adding' % g) |
|
436 | 437 | ReposGroupModel()._create_default_perms(g) |
|
437 | 438 | |
|
438 | 439 | def config_prompt(self, test_repo_path='', retries=3, defaults={}): |
|
439 | 440 | _path = defaults.get('repos_location') |
|
440 | 441 | if retries == 3: |
|
441 | 442 | log.info('Setting up repositories config') |
|
442 | 443 | |
|
443 | 444 | if _path is not None: |
|
444 | 445 | path = _path |
|
445 | 446 | elif not self.tests and not test_repo_path: |
|
446 | 447 | path = raw_input( |
|
447 | 448 | 'Enter a valid absolute path to store repositories. ' |
|
448 | 449 | 'All repositories in that path will be added automatically:' |
|
449 | 450 | ) |
|
450 | 451 | else: |
|
451 | 452 | path = test_repo_path |
|
452 | 453 | path_ok = True |
|
453 | 454 | |
|
454 | 455 | # check proper dir |
|
455 | 456 | if not os.path.isdir(path): |
|
456 | 457 | path_ok = False |
|
457 | 458 | log.error('Given path %s is not a valid directory' % path) |
|
458 | 459 | |
|
459 | 460 | elif not os.path.isabs(path): |
|
460 | 461 | path_ok = False |
|
461 | 462 | log.error('Given path %s is not an absolute path' % path) |
|
462 | 463 | |
|
463 | 464 | # check write access |
|
464 | 465 | elif not os.access(path, os.W_OK) and path_ok: |
|
465 | 466 | path_ok = False |
|
466 | 467 | log.error('No write permission to given path %s' % path) |
|
467 | 468 | |
|
468 | 469 | if retries == 0: |
|
469 | 470 | sys.exit('max retries reached') |
|
470 | 471 | if path_ok is False: |
|
471 | 472 | retries -= 1 |
|
472 | 473 | return self.config_prompt(test_repo_path, retries) |
|
473 | 474 | |
|
474 | 475 | return path |
|
475 | 476 | |
|
476 | 477 | def create_settings(self, path): |
|
477 | 478 | |
|
478 | 479 | self.create_ui_settings() |
|
479 | 480 | |
|
480 | 481 | #HG UI OPTIONS |
|
481 | 482 | web1 = RhodeCodeUi() |
|
482 | 483 | web1.ui_section = 'web' |
|
483 | 484 | web1.ui_key = 'push_ssl' |
|
484 | 485 | web1.ui_value = 'false' |
|
485 | 486 | |
|
486 | 487 | web2 = RhodeCodeUi() |
|
487 | 488 | web2.ui_section = 'web' |
|
488 | 489 | web2.ui_key = 'allow_archive' |
|
489 | 490 | web2.ui_value = 'gz zip bz2' |
|
490 | 491 | |
|
491 | 492 | web3 = RhodeCodeUi() |
|
492 | 493 | web3.ui_section = 'web' |
|
493 | 494 | web3.ui_key = 'allow_push' |
|
494 | 495 | web3.ui_value = '*' |
|
495 | 496 | |
|
496 | 497 | web4 = RhodeCodeUi() |
|
497 | 498 | web4.ui_section = 'web' |
|
498 | 499 | web4.ui_key = 'baseurl' |
|
499 | 500 | web4.ui_value = '/' |
|
500 | 501 | |
|
501 | 502 | paths = RhodeCodeUi() |
|
502 | 503 | paths.ui_section = 'paths' |
|
503 | 504 | paths.ui_key = '/' |
|
504 | 505 | paths.ui_value = path |
|
505 | 506 | |
|
506 | 507 | phases = RhodeCodeUi() |
|
507 | 508 | phases.ui_section = 'phases' |
|
508 | 509 | phases.ui_key = 'publish' |
|
509 | 510 | phases.ui_value = False |
|
510 | 511 | |
|
511 | 512 | sett1 = RhodeCodeSetting('realm', 'RhodeCode authentication') |
|
512 | 513 | sett2 = RhodeCodeSetting('title', 'RhodeCode') |
|
513 | 514 | sett3 = RhodeCodeSetting('ga_code', '') |
|
514 | 515 | |
|
515 | 516 | sett4 = RhodeCodeSetting('show_public_icon', True) |
|
516 | 517 | sett5 = RhodeCodeSetting('show_private_icon', True) |
|
517 | 518 | sett6 = RhodeCodeSetting('stylify_metatags', False) |
|
518 | 519 | |
|
519 | 520 | self.sa.add(web1) |
|
520 | 521 | self.sa.add(web2) |
|
521 | 522 | self.sa.add(web3) |
|
522 | 523 | self.sa.add(web4) |
|
523 | 524 | self.sa.add(paths) |
|
524 | 525 | self.sa.add(sett1) |
|
525 | 526 | self.sa.add(sett2) |
|
526 | 527 | self.sa.add(sett3) |
|
527 | 528 | self.sa.add(sett4) |
|
528 | 529 | self.sa.add(sett5) |
|
529 | 530 | self.sa.add(sett6) |
|
530 | 531 | |
|
531 | 532 | self.create_ldap_options() |
|
532 | 533 | |
|
533 | 534 | log.info('created ui config') |
|
534 | 535 | |
|
535 | 536 | def create_user(self, username, password, email='', admin=False): |
|
536 | 537 | log.info('creating user %s' % username) |
|
537 | 538 | UserModel().create_or_update(username, password, email, |
|
538 | 539 | firstname='RhodeCode', lastname='Admin', |
|
539 | 540 | active=True, admin=admin) |
|
540 | 541 | |
|
541 | 542 | def create_default_user(self): |
|
542 | 543 | log.info('creating default user') |
|
543 | 544 | # create default user for handling default permissions. |
|
544 | 545 | UserModel().create_or_update(username='default', |
|
545 | 546 | password=str(uuid.uuid1())[:8], |
|
546 | 547 | email='anonymous@rhodecode.org', |
|
547 | 548 | firstname='Anonymous', lastname='User') |
|
548 | 549 | |
|
549 | 550 | def create_permissions(self): |
|
550 | 551 | # module.(access|create|change|delete)_[name] |
|
551 | 552 | # module.(none|read|write|admin) |
|
552 | 553 | |
|
553 | 554 | for p in Permission.PERMS: |
|
554 | 555 | if not Permission.get_by_key(p[0]): |
|
555 | 556 | new_perm = Permission() |
|
556 | 557 | new_perm.permission_name = p[0] |
|
557 | 558 | new_perm.permission_longname = p[0] |
|
558 | 559 | self.sa.add(new_perm) |
|
559 | 560 | |
|
560 | 561 | def populate_default_permissions(self): |
|
561 | 562 | log.info('creating default user permissions') |
|
562 | 563 | |
|
563 | 564 | default_user = User.get_by_username('default') |
|
564 | 565 | |
|
565 | 566 | for def_perm in ['hg.register.manual_activate', 'hg.create.repository', |
|
566 | 567 | 'hg.fork.repository', 'repository.read']: |
|
567 | 568 | |
|
568 | 569 | perm = self.sa.query(Permission)\ |
|
569 | 570 | .filter(Permission.permission_name == def_perm)\ |
|
570 | 571 | .scalar() |
|
571 | 572 | if not perm: |
|
572 | 573 | raise Exception( |
|
573 | 574 | 'CRITICAL: permission %s not found inside database !!' |
|
574 | 575 | % def_perm |
|
575 | 576 | ) |
|
576 | 577 | if not UserToPerm.query()\ |
|
577 | 578 | .filter(UserToPerm.permission == perm)\ |
|
578 | 579 | .filter(UserToPerm.user == default_user).scalar(): |
|
579 | 580 | reg_perm = UserToPerm() |
|
580 | 581 | reg_perm.user = default_user |
|
581 | 582 | reg_perm.permission = perm |
|
582 | 583 | self.sa.add(reg_perm) |
@@ -1,162 +1,173 b'' | |||
|
1 | 1 | import logging |
|
2 | 2 | import datetime |
|
3 | 3 | |
|
4 | 4 | from sqlalchemy import * |
|
5 | 5 | from sqlalchemy.exc import DatabaseError |
|
6 | 6 | from sqlalchemy.orm import relation, backref, class_mapper |
|
7 | 7 | from sqlalchemy.orm.session import Session |
|
8 | 8 | from sqlalchemy.ext.declarative import declarative_base |
|
9 | 9 | |
|
10 | 10 | from rhodecode.lib.dbmigrate.migrate import * |
|
11 | 11 | from rhodecode.lib.dbmigrate.migrate.changeset import * |
|
12 | 12 | |
|
13 | 13 | from rhodecode.model.meta import Base |
|
14 | 14 | from rhodecode.model import meta |
|
15 | 15 | |
|
16 | 16 | log = logging.getLogger(__name__) |
|
17 | 17 | |
|
18 | 18 | |
|
19 | 19 | def upgrade(migrate_engine): |
|
20 | 20 | """ |
|
21 | 21 | Upgrade operations go here. |
|
22 | 22 | Don't create your own engine; bind migrate_engine to your metadata |
|
23 | 23 | """ |
|
24 | 24 | |
|
25 | 25 | #========================================================================== |
|
26 | 26 | # USEREMAILMAP |
|
27 | 27 | #========================================================================== |
|
28 | 28 | from rhodecode.lib.dbmigrate.schema.db_1_4_0 import UserEmailMap |
|
29 | 29 | tbl = UserEmailMap.__table__ |
|
30 | 30 | tbl.create() |
|
31 | 31 | #========================================================================== |
|
32 | 32 | # PULL REQUEST |
|
33 | 33 | #========================================================================== |
|
34 | 34 | from rhodecode.lib.dbmigrate.schema.db_1_4_0 import PullRequest |
|
35 | 35 | tbl = PullRequest.__table__ |
|
36 | 36 | tbl.create() |
|
37 | 37 | |
|
38 | 38 | #========================================================================== |
|
39 | 39 | # PULL REQUEST REVIEWERS |
|
40 | 40 | #========================================================================== |
|
41 | 41 | from rhodecode.lib.dbmigrate.schema.db_1_4_0 import PullRequestReviewers |
|
42 | 42 | tbl = PullRequestReviewers.__table__ |
|
43 | 43 | tbl.create() |
|
44 | 44 | |
|
45 | 45 | #========================================================================== |
|
46 | 46 | # CHANGESET STATUS |
|
47 | 47 | #========================================================================== |
|
48 | 48 | from rhodecode.lib.dbmigrate.schema.db_1_4_0 import ChangesetStatus |
|
49 | 49 | tbl = ChangesetStatus.__table__ |
|
50 | 50 | tbl.create() |
|
51 | 51 | |
|
52 | 52 | ## RESET COMPLETLY THE metadata for sqlalchemy to use the 1_3_0 Base |
|
53 | 53 | Base = declarative_base() |
|
54 | 54 | Base.metadata.clear() |
|
55 | 55 | Base.metadata = MetaData() |
|
56 | 56 | Base.metadata.bind = migrate_engine |
|
57 | 57 | meta.Base = Base |
|
58 | 58 | |
|
59 | 59 | #========================================================================== |
|
60 | 60 | # USERS TABLE |
|
61 | 61 | #========================================================================== |
|
62 | 62 | from rhodecode.lib.dbmigrate.schema.db_1_3_0 import User |
|
63 | 63 | tbl = User.__table__ |
|
64 | 64 | |
|
65 | 65 | # change column name -> firstname |
|
66 | 66 | col = User.__table__.columns.name |
|
67 | 67 | col.alter(index=Index('u_username_idx', 'username')) |
|
68 | 68 | col.alter(index=Index('u_email_idx', 'email')) |
|
69 | 69 | col.alter(name="firstname", table=tbl) |
|
70 | 70 | |
|
71 | 71 | inherit_default_permissions = Column("inherit_default_permissions", |
|
72 | 72 | Boolean(), nullable=True, unique=None, |
|
73 | 73 | default=True) |
|
74 | 74 | inherit_default_permissions.create(table=tbl) |
|
75 | 75 | inherit_default_permissions.alter(nullable=False, default=True, table=tbl) |
|
76 | 76 | |
|
77 | 77 | #========================================================================== |
|
78 | # GROUPS TABLE | |
|
79 | #========================================================================== | |
|
80 | from rhodecode.lib.dbmigrate.schema.db_1_3_0 import RepoGroup | |
|
81 | tbl = RepoGroup.__table__ | |
|
82 | inherit_default_permissions = Column("inherit_default_permissions", | |
|
83 | Boolean(), nullable=True, unique=None, | |
|
84 | default=True) | |
|
85 | inherit_default_permissions.create(table=tbl) | |
|
86 | inherit_default_permissions.alter(nullable=False, default=True, table=tbl) | |
|
87 | ||
|
88 | #========================================================================== | |
|
78 | 89 | # REPOSITORIES |
|
79 | 90 | #========================================================================== |
|
80 | 91 | from rhodecode.lib.dbmigrate.schema.db_1_3_0 import Repository |
|
81 | 92 | tbl = Repository.__table__ |
|
82 | 93 | |
|
83 | 94 | enable_locking = Column("enable_locking", Boolean(), nullable=True, |
|
84 | 95 | unique=None, default=False) |
|
85 | 96 | enable_locking.create(table=tbl) |
|
86 | 97 | enable_locking.alter(nullable=False, default=False, table=tbl) |
|
87 | 98 | |
|
88 | 99 | _locked = Column("locked", String(255), nullable=True, unique=False, |
|
89 | 100 | default=None) |
|
90 | 101 | _locked.create(table=tbl) |
|
91 | 102 | |
|
92 | 103 | landing_rev = Column("landing_revision", String(255), nullable=True, |
|
93 | 104 | unique=False, default='tip') |
|
94 | 105 | landing_rev.create(table=tbl) |
|
95 | 106 | landing_rev.alter(nullable=False, default='tip', table=tbl) |
|
96 | 107 | |
|
97 | 108 | #========================================================================== |
|
98 | 109 | # GROUPS |
|
99 | 110 | #========================================================================== |
|
100 | 111 | from rhodecode.lib.dbmigrate.schema.db_1_3_0 import RepoGroup |
|
101 | 112 | tbl = RepoGroup.__table__ |
|
102 | 113 | enable_locking = Column("enable_locking", Boolean(), nullable=True, |
|
103 | 114 | unique=None, default=False) |
|
104 | 115 | enable_locking.create(table=tbl) |
|
105 | 116 | enable_locking.alter(nullable=False, default=False) |
|
106 | 117 | |
|
107 | 118 | #========================================================================== |
|
108 | 119 | # CACHE INVALIDATION |
|
109 | 120 | #========================================================================== |
|
110 | 121 | from rhodecode.lib.dbmigrate.schema.db_1_3_0 import CacheInvalidation |
|
111 | 122 | tbl = CacheInvalidation.__table__ |
|
112 | 123 | |
|
113 | 124 | # change column name -> firstname |
|
114 | 125 | col = CacheInvalidation.__table__.columns.cache_key |
|
115 | 126 | col.alter(index=Index('key_idx', 'cache_key')) |
|
116 | 127 | |
|
117 | 128 | #========================================================================== |
|
118 | 129 | # NOTIFICATION |
|
119 | 130 | #========================================================================== |
|
120 | 131 | from rhodecode.lib.dbmigrate.schema.db_1_3_0 import Notification |
|
121 | 132 | tbl = Notification.__table__ |
|
122 | 133 | |
|
123 | 134 | # change column name -> firstname |
|
124 | 135 | col = Notification.__table__.columns.type |
|
125 | 136 | col.alter(index=Index('notification_type_idx', 'type'),) |
|
126 | 137 | |
|
127 | 138 | #========================================================================== |
|
128 | 139 | # CHANGESET_COMMENTS |
|
129 | 140 | #========================================================================== |
|
130 | 141 | from rhodecode.lib.dbmigrate.schema.db_1_3_0 import ChangesetComment |
|
131 | 142 | |
|
132 | 143 | tbl = ChangesetComment.__table__ |
|
133 | 144 | |
|
134 | 145 | col = ChangesetComment.__table__.columns.revision |
|
135 | 146 | col.alter(index=Index('cc_revision_idx', 'revision'),) |
|
136 | 147 | |
|
137 | 148 | hl_lines = Column('hl_lines', Unicode(512), nullable=True) |
|
138 | 149 | hl_lines.create(table=tbl) |
|
139 | 150 | |
|
140 | 151 | created_on = Column('created_on', DateTime(timezone=False), nullable=True, |
|
141 | 152 | default=datetime.datetime.now) |
|
142 | 153 | created_on.create(table=tbl) |
|
143 | 154 | created_on.alter(nullable=False, default=datetime.datetime.now) |
|
144 | 155 | modified_at = Column('modified_at', DateTime(timezone=False), nullable=False, |
|
145 | 156 | default=datetime.datetime.now) |
|
146 | 157 | modified_at.alter(type=DateTime(timezone=False), table=tbl) |
|
147 | 158 | |
|
148 | 159 | pull_request_id = Column("pull_request_id", Integer(), |
|
149 | 160 | ForeignKey('pull_requests.pull_request_id'), |
|
150 | 161 | nullable=True) |
|
151 | 162 | pull_request_id.create(table=tbl) |
|
152 | 163 | ## RESET COMPLETLY THE metadata for sqlalchemy back after using 1_3_0 |
|
153 | 164 | Base = declarative_base() |
|
154 | 165 | Base.metadata.clear() |
|
155 | 166 | Base.metadata = MetaData() |
|
156 | 167 | Base.metadata.bind = migrate_engine |
|
157 | 168 | meta.Base = Base |
|
158 | 169 | |
|
159 | 170 | |
|
160 | 171 | def downgrade(migrate_engine): |
|
161 | 172 | meta = MetaData() |
|
162 | 173 | meta.bind = migrate_engine |
General Comments 0
You need to be logged in to leave comments.
Login now