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