Show More
@@ -1,679 +1,679 b'' | |||
|
1 | 1 | # Copyright (C) 2010-2023 RhodeCode GmbH |
|
2 | 2 | # |
|
3 | 3 | # This program is free software: you can redistribute it and/or modify |
|
4 | 4 | # it under the terms of the GNU Affero General Public License, version 3 |
|
5 | 5 | # (only), as published by the Free Software Foundation. |
|
6 | 6 | # |
|
7 | 7 | # This program is distributed in the hope that it will be useful, |
|
8 | 8 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
9 | 9 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
10 | 10 | # GNU General Public License for more details. |
|
11 | 11 | # |
|
12 | 12 | # You should have received a copy of the GNU Affero General Public License |
|
13 | 13 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
14 | 14 | # |
|
15 | 15 | # This program is dual-licensed. If you wish to learn more about the |
|
16 | 16 | # RhodeCode Enterprise Edition, including its added features, Support services, |
|
17 | 17 | # and proprietary license terms, please see https://rhodecode.com/licenses/ |
|
18 | 18 | |
|
19 | 19 | """ |
|
20 | 20 | Database creation, and setup module for RhodeCode Enterprise. Used for creation |
|
21 | 21 | of database as well as for migration operations |
|
22 | 22 | """ |
|
23 | 23 | |
|
24 | 24 | import os |
|
25 | 25 | import sys |
|
26 | 26 | import time |
|
27 | 27 | import uuid |
|
28 | 28 | import logging |
|
29 | 29 | import getpass |
|
30 | 30 | from os.path import dirname as dn, join as jn |
|
31 | 31 | |
|
32 | 32 | from sqlalchemy.engine import create_engine |
|
33 | 33 | |
|
34 | 34 | from rhodecode import __dbversion__ |
|
35 | 35 | from rhodecode.model import init_model |
|
36 | 36 | from rhodecode.model.user import UserModel |
|
37 | 37 | from rhodecode.model.db import ( |
|
38 | 38 | User, Permission, RhodeCodeUi, RhodeCodeSetting, UserToPerm, |
|
39 | 39 | DbMigrateVersion, RepoGroup, UserRepoGroupToPerm, CacheKey, Repository) |
|
40 | 40 | from rhodecode.model.meta import Session, Base |
|
41 | 41 | from rhodecode.model.permission import PermissionModel |
|
42 | 42 | from rhodecode.model.repo import RepoModel |
|
43 | 43 | from rhodecode.model.repo_group import RepoGroupModel |
|
44 | 44 | from rhodecode.model.settings import SettingsModel |
|
45 | 45 | |
|
46 | 46 | |
|
47 | 47 | log = logging.getLogger(__name__) |
|
48 | 48 | |
|
49 | 49 | |
|
50 | 50 | def notify(msg): |
|
51 | 51 | """ |
|
52 | 52 | Notification for migrations messages |
|
53 | 53 | """ |
|
54 | 54 | ml = len(msg) + (4 * 2) |
|
55 | 55 | print((('\n%s\n*** %s ***\n%s' % ('*' * ml, msg, '*' * ml)).upper())) |
|
56 | 56 | |
|
57 | 57 | |
|
58 | 58 | class DbManage(object): |
|
59 | 59 | |
|
60 | 60 | def __init__(self, log_sql, dbconf, root, tests=False, |
|
61 | 61 | SESSION=None, cli_args=None, enc_key=b''): |
|
62 | 62 | |
|
63 | 63 | self.dbname = dbconf.split('/')[-1] |
|
64 | 64 | self.tests = tests |
|
65 | 65 | self.root = root |
|
66 | 66 | self.dburi = dbconf |
|
67 | 67 | self.log_sql = log_sql |
|
68 | 68 | self.cli_args = cli_args or {} |
|
69 | 69 | self.sa = None |
|
70 | 70 | self.engine = None |
|
71 | 71 | self.enc_key = enc_key |
|
72 | 72 | # sets .sa .engine |
|
73 | 73 | self.init_db(SESSION=SESSION) |
|
74 | 74 | |
|
75 | 75 | self.ask_ok = self.get_ask_ok_func(self.cli_args.get('force_ask')) |
|
76 | 76 | |
|
77 | 77 | def db_exists(self): |
|
78 | 78 | if not self.sa: |
|
79 | 79 | self.init_db() |
|
80 | 80 | try: |
|
81 | 81 | self.sa.query(RhodeCodeUi)\ |
|
82 | 82 | .filter(RhodeCodeUi.ui_key == '/')\ |
|
83 | 83 | .scalar() |
|
84 | 84 | return True |
|
85 | 85 | except Exception: |
|
86 | 86 | return False |
|
87 | 87 | finally: |
|
88 | 88 | self.sa.rollback() |
|
89 | 89 | |
|
90 | 90 | def get_ask_ok_func(self, param): |
|
91 | 91 | if param not in [None]: |
|
92 | 92 | # return a function lambda that has a default set to param |
|
93 | 93 | return lambda *args, **kwargs: param |
|
94 | 94 | else: |
|
95 | 95 | from rhodecode.lib.utils import ask_ok |
|
96 | 96 | return ask_ok |
|
97 | 97 | |
|
98 | 98 | def init_db(self, SESSION=None): |
|
99 | 99 | |
|
100 | 100 | if SESSION: |
|
101 | 101 | self.sa = SESSION |
|
102 | 102 | self.engine = SESSION.bind |
|
103 | 103 | else: |
|
104 | 104 | # init new sessions |
|
105 | 105 | engine = create_engine(self.dburi, echo=self.log_sql) |
|
106 | 106 | init_model(engine, encryption_key=self.enc_key) |
|
107 | 107 | self.sa = Session() |
|
108 | 108 | self.engine = engine |
|
109 | 109 | |
|
110 | 110 | def create_tables(self, override=False): |
|
111 | 111 | """ |
|
112 | 112 | Create a auth database |
|
113 | 113 | """ |
|
114 | 114 | |
|
115 | 115 | log.info("Existing database with the same name is going to be destroyed.") |
|
116 | 116 | log.info("Setup command will run DROP ALL command on that database.") |
|
117 | 117 | engine = self.engine |
|
118 | 118 | |
|
119 | 119 | if self.tests: |
|
120 | 120 | destroy = True |
|
121 | 121 | else: |
|
122 | 122 | destroy = self.ask_ok('Are you sure that you want to destroy the old database? [y/n]') |
|
123 | 123 | if not destroy: |
|
124 | 124 | log.info('db tables bootstrap: Nothing done.') |
|
125 | 125 | sys.exit(0) |
|
126 | 126 | if destroy: |
|
127 | 127 | Base.metadata.drop_all(bind=engine) |
|
128 | 128 | |
|
129 | 129 | checkfirst = not override |
|
130 | 130 | Base.metadata.create_all(bind=engine, checkfirst=checkfirst) |
|
131 | 131 | log.info('Created tables for %s', self.dbname) |
|
132 | 132 | |
|
133 | 133 | def set_db_version(self): |
|
134 | 134 | ver = DbMigrateVersion() |
|
135 | 135 | ver.version = __dbversion__ |
|
136 | 136 | ver.repository_id = 'rhodecode_db_migrations' |
|
137 | 137 | ver.repository_path = 'versions' |
|
138 | 138 | self.sa.add(ver) |
|
139 | 139 | log.info('db version set to: %s', __dbversion__) |
|
140 | 140 | |
|
141 | 141 | def run_post_migration_tasks(self): |
|
142 | 142 | """ |
|
143 | 143 | Run various tasks before actually doing migrations |
|
144 | 144 | """ |
|
145 | 145 | # delete cache keys on each upgrade |
|
146 | 146 | total = CacheKey.query().count() |
|
147 | 147 | log.info("Deleting (%s) cache keys now...", total) |
|
148 | 148 | CacheKey.delete_all_cache() |
|
149 | 149 | |
|
150 | 150 | def upgrade(self, version=None): |
|
151 | 151 | """ |
|
152 | 152 | Upgrades given database schema to given revision following |
|
153 | 153 | all needed steps, to perform the upgrade |
|
154 | 154 | |
|
155 | 155 | """ |
|
156 | 156 | |
|
157 | 157 | from rhodecode.lib.dbmigrate.migrate.versioning import api |
|
158 | 158 | from rhodecode.lib.dbmigrate.migrate.exceptions import DatabaseNotControlledError |
|
159 | 159 | |
|
160 | 160 | if 'sqlite' in self.dburi: |
|
161 | 161 | print( |
|
162 | 162 | '********************** WARNING **********************\n' |
|
163 | 163 | 'Make sure your version of sqlite is at least 3.7.X. \n' |
|
164 | 164 | 'Earlier versions are known to fail on some migrations\n' |
|
165 | 165 | '*****************************************************\n') |
|
166 | 166 | |
|
167 | 167 | upgrade = self.ask_ok( |
|
168 | 168 | 'You are about to perform a database upgrade. Make ' |
|
169 | 169 | 'sure you have backed up your database. ' |
|
170 | 170 | 'Continue ? [y/n]') |
|
171 | 171 | if not upgrade: |
|
172 | 172 | log.info('No upgrade performed') |
|
173 | 173 | sys.exit(0) |
|
174 | 174 | |
|
175 | 175 | repository_path = jn(dn(dn(dn(os.path.realpath(__file__)))), |
|
176 | 176 | 'rhodecode/lib/dbmigrate') |
|
177 | 177 | db_uri = self.dburi |
|
178 | 178 | |
|
179 | 179 | if version: |
|
180 | 180 | DbMigrateVersion.set_version(version) |
|
181 | 181 | |
|
182 | 182 | try: |
|
183 | 183 | curr_version = api.db_version(db_uri, repository_path) |
|
184 | 184 | msg = (f'Found current database db_uri under version ' |
|
185 | 185 | f'control with version {curr_version}') |
|
186 | 186 | |
|
187 | 187 | except (RuntimeError, DatabaseNotControlledError): |
|
188 | 188 | curr_version = 1 |
|
189 | 189 | msg = f'Current database is not under version control. ' \ |
|
190 | 190 | f'Setting as version {curr_version}' |
|
191 | 191 | api.version_control(db_uri, repository_path, curr_version) |
|
192 | 192 | |
|
193 | 193 | notify(msg) |
|
194 | 194 | |
|
195 | 195 | if curr_version == __dbversion__: |
|
196 | 196 | log.info('This database is already at the newest version') |
|
197 | 197 | sys.exit(0) |
|
198 | 198 | |
|
199 | 199 | upgrade_steps = list(range(curr_version + 1, __dbversion__ + 1)) |
|
200 | 200 | notify(f'attempting to upgrade database from ' |
|
201 | 201 | f'version {curr_version} to version {__dbversion__}') |
|
202 | 202 | |
|
203 | 203 | # CALL THE PROPER ORDER OF STEPS TO PERFORM FULL UPGRADE |
|
204 |
_step = |
|
|
204 | final_step = 'latest' | |
|
205 | 205 | for step in upgrade_steps: |
|
206 | 206 | notify(f'performing upgrade step {step}') |
|
207 | 207 | time.sleep(0.5) |
|
208 | 208 | |
|
209 | 209 | api.upgrade(db_uri, repository_path, step) |
|
210 | 210 | self.sa.rollback() |
|
211 | 211 | notify(f'schema upgrade for step {step} completed') |
|
212 | 212 | |
|
213 | _step = step | |
|
213 | final_step = step | |
|
214 | 214 | |
|
215 | 215 | self.run_post_migration_tasks() |
|
216 | notify(f'upgrade to version {step} successful') | |
|
216 | notify(f'upgrade to version {final_step} successful') | |
|
217 | 217 | |
|
218 | 218 | def fix_repo_paths(self): |
|
219 | 219 | """ |
|
220 | 220 | Fixes an old RhodeCode version path into new one without a '*' |
|
221 | 221 | """ |
|
222 | 222 | |
|
223 | 223 | paths = self.sa.query(RhodeCodeUi)\ |
|
224 | 224 | .filter(RhodeCodeUi.ui_key == '/')\ |
|
225 | 225 | .scalar() |
|
226 | 226 | |
|
227 | 227 | paths.ui_value = paths.ui_value.replace('*', '') |
|
228 | 228 | |
|
229 | 229 | try: |
|
230 | 230 | self.sa.add(paths) |
|
231 | 231 | self.sa.commit() |
|
232 | 232 | except Exception: |
|
233 | 233 | self.sa.rollback() |
|
234 | 234 | raise |
|
235 | 235 | |
|
236 | 236 | def fix_default_user(self): |
|
237 | 237 | """ |
|
238 | 238 | Fixes an old default user with some 'nicer' default values, |
|
239 | 239 | used mostly for anonymous access |
|
240 | 240 | """ |
|
241 | 241 | def_user = self.sa.query(User)\ |
|
242 | 242 | .filter(User.username == User.DEFAULT_USER)\ |
|
243 | 243 | .one() |
|
244 | 244 | |
|
245 | 245 | def_user.name = 'Anonymous' |
|
246 | 246 | def_user.lastname = 'User' |
|
247 | 247 | def_user.email = User.DEFAULT_USER_EMAIL |
|
248 | 248 | |
|
249 | 249 | try: |
|
250 | 250 | self.sa.add(def_user) |
|
251 | 251 | self.sa.commit() |
|
252 | 252 | except Exception: |
|
253 | 253 | self.sa.rollback() |
|
254 | 254 | raise |
|
255 | 255 | |
|
256 | 256 | def fix_settings(self): |
|
257 | 257 | """ |
|
258 | 258 | Fixes rhodecode settings and adds ga_code key for google analytics |
|
259 | 259 | """ |
|
260 | 260 | |
|
261 | 261 | hgsettings3 = RhodeCodeSetting('ga_code', '') |
|
262 | 262 | |
|
263 | 263 | try: |
|
264 | 264 | self.sa.add(hgsettings3) |
|
265 | 265 | self.sa.commit() |
|
266 | 266 | except Exception: |
|
267 | 267 | self.sa.rollback() |
|
268 | 268 | raise |
|
269 | 269 | |
|
270 | 270 | def create_admin_and_prompt(self): |
|
271 | 271 | |
|
272 | 272 | # defaults |
|
273 | 273 | defaults = self.cli_args |
|
274 | 274 | username = defaults.get('username') |
|
275 | 275 | password = defaults.get('password') |
|
276 | 276 | email = defaults.get('email') |
|
277 | 277 | |
|
278 | 278 | if username is None: |
|
279 | 279 | username = input('Specify admin username:') |
|
280 | 280 | if password is None: |
|
281 | 281 | password = self._get_admin_password() |
|
282 | 282 | if not password: |
|
283 | 283 | # second try |
|
284 | 284 | password = self._get_admin_password() |
|
285 | 285 | if not password: |
|
286 | 286 | sys.exit() |
|
287 | 287 | if email is None: |
|
288 | 288 | email = input('Specify admin email:') |
|
289 | 289 | api_key = self.cli_args.get('api_key') |
|
290 | 290 | self.create_user(username, password, email, True, |
|
291 | 291 | strict_creation_check=False, |
|
292 | 292 | api_key=api_key) |
|
293 | 293 | |
|
294 | 294 | def _get_admin_password(self): |
|
295 | 295 | password = getpass.getpass('Specify admin password ' |
|
296 | 296 | '(min 6 chars):') |
|
297 | 297 | confirm = getpass.getpass('Confirm password:') |
|
298 | 298 | |
|
299 | 299 | if password != confirm: |
|
300 | 300 | log.error('passwords mismatch') |
|
301 | 301 | return False |
|
302 | 302 | if len(password) < 6: |
|
303 | 303 | log.error('password is too short - use at least 6 characters') |
|
304 | 304 | return False |
|
305 | 305 | |
|
306 | 306 | return password |
|
307 | 307 | |
|
308 | 308 | def create_test_admin_and_users(self): |
|
309 | 309 | log.info('creating admin and regular test users') |
|
310 | 310 | from rhodecode.tests import TEST_USER_ADMIN_LOGIN, \ |
|
311 | 311 | TEST_USER_ADMIN_PASS, TEST_USER_ADMIN_EMAIL, \ |
|
312 | 312 | TEST_USER_REGULAR_LOGIN, TEST_USER_REGULAR_PASS, \ |
|
313 | 313 | TEST_USER_REGULAR_EMAIL, TEST_USER_REGULAR2_LOGIN, \ |
|
314 | 314 | TEST_USER_REGULAR2_PASS, TEST_USER_REGULAR2_EMAIL |
|
315 | 315 | |
|
316 | 316 | self.create_user(TEST_USER_ADMIN_LOGIN, TEST_USER_ADMIN_PASS, |
|
317 | 317 | TEST_USER_ADMIN_EMAIL, True, api_key=True) |
|
318 | 318 | |
|
319 | 319 | self.create_user(TEST_USER_REGULAR_LOGIN, TEST_USER_REGULAR_PASS, |
|
320 | 320 | TEST_USER_REGULAR_EMAIL, False, api_key=True) |
|
321 | 321 | |
|
322 | 322 | self.create_user(TEST_USER_REGULAR2_LOGIN, TEST_USER_REGULAR2_PASS, |
|
323 | 323 | TEST_USER_REGULAR2_EMAIL, False, api_key=True) |
|
324 | 324 | |
|
325 | 325 | def create_ui_settings(self, repo_store_path): |
|
326 | 326 | """ |
|
327 | 327 | Creates ui settings, fills out hooks |
|
328 | 328 | and disables dotencode |
|
329 | 329 | """ |
|
330 | 330 | settings_model = SettingsModel(sa=self.sa) |
|
331 | 331 | from rhodecode.lib.vcs.backends.hg import largefiles_store |
|
332 | 332 | from rhodecode.lib.vcs.backends.git import lfs_store |
|
333 | 333 | |
|
334 | 334 | # Build HOOKS |
|
335 | 335 | hooks = [ |
|
336 | 336 | (RhodeCodeUi.HOOK_REPO_SIZE, 'python:vcsserver.hooks.repo_size'), |
|
337 | 337 | |
|
338 | 338 | # HG |
|
339 | 339 | (RhodeCodeUi.HOOK_PRE_PULL, 'python:vcsserver.hooks.pre_pull'), |
|
340 | 340 | (RhodeCodeUi.HOOK_PULL, 'python:vcsserver.hooks.log_pull_action'), |
|
341 | 341 | (RhodeCodeUi.HOOK_PRE_PUSH, 'python:vcsserver.hooks.pre_push'), |
|
342 | 342 | (RhodeCodeUi.HOOK_PRETX_PUSH, 'python:vcsserver.hooks.pre_push'), |
|
343 | 343 | (RhodeCodeUi.HOOK_PUSH, 'python:vcsserver.hooks.log_push_action'), |
|
344 | 344 | (RhodeCodeUi.HOOK_PUSH_KEY, 'python:vcsserver.hooks.key_push'), |
|
345 | 345 | |
|
346 | 346 | ] |
|
347 | 347 | |
|
348 | 348 | for key, value in hooks: |
|
349 | 349 | hook_obj = settings_model.get_ui_by_key(key) |
|
350 | 350 | hooks2 = hook_obj if hook_obj else RhodeCodeUi() |
|
351 | 351 | hooks2.ui_section = 'hooks' |
|
352 | 352 | hooks2.ui_key = key |
|
353 | 353 | hooks2.ui_value = value |
|
354 | 354 | self.sa.add(hooks2) |
|
355 | 355 | |
|
356 | 356 | # enable largefiles |
|
357 | 357 | largefiles = RhodeCodeUi() |
|
358 | 358 | largefiles.ui_section = 'extensions' |
|
359 | 359 | largefiles.ui_key = 'largefiles' |
|
360 | 360 | largefiles.ui_value = '' |
|
361 | 361 | self.sa.add(largefiles) |
|
362 | 362 | |
|
363 | 363 | # set default largefiles cache dir, defaults to |
|
364 | 364 | # /repo_store_location/.cache/largefiles |
|
365 | 365 | largefiles = RhodeCodeUi() |
|
366 | 366 | largefiles.ui_section = 'largefiles' |
|
367 | 367 | largefiles.ui_key = 'usercache' |
|
368 | 368 | largefiles.ui_value = largefiles_store(repo_store_path) |
|
369 | 369 | |
|
370 | 370 | self.sa.add(largefiles) |
|
371 | 371 | |
|
372 | 372 | # set default lfs cache dir, defaults to |
|
373 | 373 | # /repo_store_location/.cache/lfs_store |
|
374 | 374 | lfsstore = RhodeCodeUi() |
|
375 | 375 | lfsstore.ui_section = 'vcs_git_lfs' |
|
376 | 376 | lfsstore.ui_key = 'store_location' |
|
377 | 377 | lfsstore.ui_value = lfs_store(repo_store_path) |
|
378 | 378 | |
|
379 | 379 | self.sa.add(lfsstore) |
|
380 | 380 | |
|
381 | 381 | # enable hgevolve disabled by default |
|
382 | 382 | hgevolve = RhodeCodeUi() |
|
383 | 383 | hgevolve.ui_section = 'extensions' |
|
384 | 384 | hgevolve.ui_key = 'evolve' |
|
385 | 385 | hgevolve.ui_value = '' |
|
386 | 386 | hgevolve.ui_active = False |
|
387 | 387 | self.sa.add(hgevolve) |
|
388 | 388 | |
|
389 | 389 | hgevolve = RhodeCodeUi() |
|
390 | 390 | hgevolve.ui_section = 'experimental' |
|
391 | 391 | hgevolve.ui_key = 'evolution' |
|
392 | 392 | hgevolve.ui_value = '' |
|
393 | 393 | hgevolve.ui_active = False |
|
394 | 394 | self.sa.add(hgevolve) |
|
395 | 395 | |
|
396 | 396 | hgevolve = RhodeCodeUi() |
|
397 | 397 | hgevolve.ui_section = 'experimental' |
|
398 | 398 | hgevolve.ui_key = 'evolution.exchange' |
|
399 | 399 | hgevolve.ui_value = '' |
|
400 | 400 | hgevolve.ui_active = False |
|
401 | 401 | self.sa.add(hgevolve) |
|
402 | 402 | |
|
403 | 403 | hgevolve = RhodeCodeUi() |
|
404 | 404 | hgevolve.ui_section = 'extensions' |
|
405 | 405 | hgevolve.ui_key = 'topic' |
|
406 | 406 | hgevolve.ui_value = '' |
|
407 | 407 | hgevolve.ui_active = False |
|
408 | 408 | self.sa.add(hgevolve) |
|
409 | 409 | |
|
410 | 410 | # enable hggit disabled by default |
|
411 | 411 | hggit = RhodeCodeUi() |
|
412 | 412 | hggit.ui_section = 'extensions' |
|
413 | 413 | hggit.ui_key = 'hggit' |
|
414 | 414 | hggit.ui_value = '' |
|
415 | 415 | hggit.ui_active = False |
|
416 | 416 | self.sa.add(hggit) |
|
417 | 417 | |
|
418 | 418 | # set svn branch defaults |
|
419 | 419 | branches = ["/branches/*", "/trunk"] |
|
420 | 420 | tags = ["/tags/*"] |
|
421 | 421 | |
|
422 | 422 | for branch in branches: |
|
423 | 423 | settings_model.create_ui_section_value( |
|
424 | 424 | RhodeCodeUi.SVN_BRANCH_ID, branch) |
|
425 | 425 | |
|
426 | 426 | for tag in tags: |
|
427 | 427 | settings_model.create_ui_section_value(RhodeCodeUi.SVN_TAG_ID, tag) |
|
428 | 428 | |
|
429 | 429 | def create_auth_plugin_options(self, skip_existing=False): |
|
430 | 430 | """ |
|
431 | 431 | Create default auth plugin settings, and make it active |
|
432 | 432 | |
|
433 | 433 | :param skip_existing: |
|
434 | 434 | """ |
|
435 | 435 | defaults = [ |
|
436 | 436 | ('auth_plugins', |
|
437 | 437 | 'egg:rhodecode-enterprise-ce#token,egg:rhodecode-enterprise-ce#rhodecode', |
|
438 | 438 | 'list'), |
|
439 | 439 | |
|
440 | 440 | ('auth_authtoken_enabled', |
|
441 | 441 | 'True', |
|
442 | 442 | 'bool'), |
|
443 | 443 | |
|
444 | 444 | ('auth_rhodecode_enabled', |
|
445 | 445 | 'True', |
|
446 | 446 | 'bool'), |
|
447 | 447 | ] |
|
448 | 448 | for k, v, t in defaults: |
|
449 | 449 | if (skip_existing and |
|
450 | 450 | SettingsModel().get_setting_by_name(k) is not None): |
|
451 | 451 | log.debug('Skipping option %s', k) |
|
452 | 452 | continue |
|
453 | 453 | setting = RhodeCodeSetting(k, v, t) |
|
454 | 454 | self.sa.add(setting) |
|
455 | 455 | |
|
456 | 456 | def create_default_options(self, skip_existing=False): |
|
457 | 457 | """Creates default settings""" |
|
458 | 458 | |
|
459 | 459 | for k, v, t in [ |
|
460 | 460 | ('default_repo_enable_locking', False, 'bool'), |
|
461 | 461 | ('default_repo_enable_downloads', False, 'bool'), |
|
462 | 462 | ('default_repo_enable_statistics', False, 'bool'), |
|
463 | 463 | ('default_repo_private', False, 'bool'), |
|
464 | 464 | ('default_repo_type', 'hg', 'unicode')]: |
|
465 | 465 | |
|
466 | 466 | if (skip_existing and |
|
467 | 467 | SettingsModel().get_setting_by_name(k) is not None): |
|
468 | 468 | log.debug('Skipping option %s', k) |
|
469 | 469 | continue |
|
470 | 470 | setting = RhodeCodeSetting(k, v, t) |
|
471 | 471 | self.sa.add(setting) |
|
472 | 472 | |
|
473 | 473 | def fixup_groups(self): |
|
474 | 474 | def_usr = User.get_default_user() |
|
475 | 475 | for g in RepoGroup.query().all(): |
|
476 | 476 | g.group_name = g.get_new_name(g.name) |
|
477 | 477 | self.sa.add(g) |
|
478 | 478 | # get default perm |
|
479 | 479 | default = UserRepoGroupToPerm.query()\ |
|
480 | 480 | .filter(UserRepoGroupToPerm.group == g)\ |
|
481 | 481 | .filter(UserRepoGroupToPerm.user == def_usr)\ |
|
482 | 482 | .scalar() |
|
483 | 483 | |
|
484 | 484 | if default is None: |
|
485 | 485 | log.debug('missing default permission for group %s adding', g) |
|
486 | 486 | perm_obj = RepoGroupModel()._create_default_perms(g) |
|
487 | 487 | self.sa.add(perm_obj) |
|
488 | 488 | |
|
489 | 489 | def reset_permissions(self, username): |
|
490 | 490 | """ |
|
491 | 491 | Resets permissions to default state, useful when old systems had |
|
492 | 492 | bad permissions, we must clean them up |
|
493 | 493 | |
|
494 | 494 | :param username: |
|
495 | 495 | """ |
|
496 | 496 | default_user = User.get_by_username(username) |
|
497 | 497 | if not default_user: |
|
498 | 498 | return |
|
499 | 499 | |
|
500 | 500 | u2p = UserToPerm.query()\ |
|
501 | 501 | .filter(UserToPerm.user == default_user).all() |
|
502 | 502 | fixed = False |
|
503 | 503 | if len(u2p) != len(Permission.DEFAULT_USER_PERMISSIONS): |
|
504 | 504 | for p in u2p: |
|
505 | 505 | Session().delete(p) |
|
506 | 506 | fixed = True |
|
507 | 507 | self.populate_default_permissions() |
|
508 | 508 | return fixed |
|
509 | 509 | |
|
510 | 510 | def config_prompt(self, test_repo_path='', retries=3): |
|
511 | 511 | defaults = self.cli_args |
|
512 | 512 | _path = defaults.get('repos_location') |
|
513 | 513 | if retries == 3: |
|
514 | 514 | log.info('Setting up repositories config') |
|
515 | 515 | |
|
516 | 516 | if _path is not None: |
|
517 | 517 | path = _path |
|
518 | 518 | elif not self.tests and not test_repo_path: |
|
519 | 519 | path = input( |
|
520 | 520 | 'Enter a valid absolute path to store repositories. ' |
|
521 | 521 | 'All repositories in that path will be added automatically:' |
|
522 | 522 | ) |
|
523 | 523 | else: |
|
524 | 524 | path = test_repo_path |
|
525 | 525 | path_ok = True |
|
526 | 526 | |
|
527 | 527 | # check proper dir |
|
528 | 528 | if not os.path.isdir(path): |
|
529 | 529 | path_ok = False |
|
530 | 530 | log.error('Given path %s is not a valid directory', path) |
|
531 | 531 | |
|
532 | 532 | elif not os.path.isabs(path): |
|
533 | 533 | path_ok = False |
|
534 | 534 | log.error('Given path %s is not an absolute path', path) |
|
535 | 535 | |
|
536 | 536 | # check if path is at least readable. |
|
537 | 537 | if not os.access(path, os.R_OK): |
|
538 | 538 | path_ok = False |
|
539 | 539 | log.error('Given path %s is not readable', path) |
|
540 | 540 | |
|
541 | 541 | # check write access, warn user about non writeable paths |
|
542 | 542 | elif not os.access(path, os.W_OK) and path_ok: |
|
543 | 543 | log.warning('No write permission to given path %s', path) |
|
544 | 544 | |
|
545 | 545 | q = (f'Given path {path} is not writeable, do you want to ' |
|
546 | 546 | f'continue with read only mode ? [y/n]') |
|
547 | 547 | if not self.ask_ok(q): |
|
548 | 548 | log.error('Canceled by user') |
|
549 | 549 | sys.exit(-1) |
|
550 | 550 | |
|
551 | 551 | if retries == 0: |
|
552 | 552 | sys.exit('max retries reached') |
|
553 | 553 | if not path_ok: |
|
554 | 554 | retries -= 1 |
|
555 | 555 | return self.config_prompt(test_repo_path, retries) |
|
556 | 556 | |
|
557 | 557 | real_path = os.path.normpath(os.path.realpath(path)) |
|
558 | 558 | |
|
559 | 559 | if real_path != os.path.normpath(path): |
|
560 | 560 | q = (f'Path looks like a symlink, RhodeCode Enterprise will store ' |
|
561 | 561 | f'given path as {real_path} ? [y/n]') |
|
562 | 562 | if not self.ask_ok(q): |
|
563 | 563 | log.error('Canceled by user') |
|
564 | 564 | sys.exit(-1) |
|
565 | 565 | |
|
566 | 566 | return real_path |
|
567 | 567 | |
|
568 | 568 | def create_settings(self, path): |
|
569 | 569 | |
|
570 | 570 | self.create_ui_settings(path) |
|
571 | 571 | |
|
572 | 572 | ui_config = [ |
|
573 | 573 | ('web', 'push_ssl', 'False'), |
|
574 | 574 | ('web', 'allow_archive', 'gz zip bz2'), |
|
575 | 575 | ('web', 'allow_push', '*'), |
|
576 | 576 | ('web', 'baseurl', '/'), |
|
577 | 577 | ('paths', '/', path), |
|
578 | 578 | ('phases', 'publish', 'True') |
|
579 | 579 | ] |
|
580 | 580 | for section, key, value in ui_config: |
|
581 | 581 | ui_conf = RhodeCodeUi() |
|
582 | 582 | setattr(ui_conf, 'ui_section', section) |
|
583 | 583 | setattr(ui_conf, 'ui_key', key) |
|
584 | 584 | setattr(ui_conf, 'ui_value', value) |
|
585 | 585 | self.sa.add(ui_conf) |
|
586 | 586 | |
|
587 | 587 | # rhodecode app settings |
|
588 | 588 | settings = [ |
|
589 | 589 | ('realm', 'RhodeCode', 'unicode'), |
|
590 | 590 | ('title', '', 'unicode'), |
|
591 | 591 | ('pre_code', '', 'unicode'), |
|
592 | 592 | ('post_code', '', 'unicode'), |
|
593 | 593 | |
|
594 | 594 | # Visual |
|
595 | 595 | ('show_public_icon', True, 'bool'), |
|
596 | 596 | ('show_private_icon', True, 'bool'), |
|
597 | 597 | ('stylify_metatags', True, 'bool'), |
|
598 | 598 | ('dashboard_items', 100, 'int'), |
|
599 | 599 | ('admin_grid_items', 25, 'int'), |
|
600 | 600 | |
|
601 | 601 | ('markup_renderer', 'markdown', 'unicode'), |
|
602 | 602 | |
|
603 | 603 | ('repository_fields', True, 'bool'), |
|
604 | 604 | ('show_version', True, 'bool'), |
|
605 | 605 | ('show_revision_number', True, 'bool'), |
|
606 | 606 | ('show_sha_length', 12, 'int'), |
|
607 | 607 | |
|
608 | 608 | ('use_gravatar', False, 'bool'), |
|
609 | 609 | ('gravatar_url', User.DEFAULT_GRAVATAR_URL, 'unicode'), |
|
610 | 610 | |
|
611 | 611 | ('clone_uri_tmpl', Repository.DEFAULT_CLONE_URI, 'unicode'), |
|
612 | 612 | ('clone_uri_id_tmpl', Repository.DEFAULT_CLONE_URI_ID, 'unicode'), |
|
613 | 613 | ('clone_uri_ssh_tmpl', Repository.DEFAULT_CLONE_URI_SSH, 'unicode'), |
|
614 | 614 | ('support_url', '', 'unicode'), |
|
615 | 615 | ('update_url', RhodeCodeSetting.DEFAULT_UPDATE_URL, 'unicode'), |
|
616 | 616 | |
|
617 | 617 | # VCS Settings |
|
618 | 618 | ('pr_merge_enabled', True, 'bool'), |
|
619 | 619 | ('use_outdated_comments', True, 'bool'), |
|
620 | 620 | ('diff_cache', True, 'bool'), |
|
621 | 621 | ] |
|
622 | 622 | |
|
623 | 623 | for key, val, type_ in settings: |
|
624 | 624 | sett = RhodeCodeSetting(key, val, type_) |
|
625 | 625 | self.sa.add(sett) |
|
626 | 626 | |
|
627 | 627 | self.create_auth_plugin_options() |
|
628 | 628 | self.create_default_options() |
|
629 | 629 | |
|
630 | 630 | log.info('created ui config') |
|
631 | 631 | |
|
632 | 632 | def create_user(self, username, password, email='', admin=False, |
|
633 | 633 | strict_creation_check=True, api_key=None): |
|
634 | 634 | log.info('creating user `%s`', username) |
|
635 | 635 | user = UserModel().create_or_update( |
|
636 | 636 | username, password, email, firstname='RhodeCode', lastname='Admin', |
|
637 | 637 | active=True, admin=admin, extern_type="rhodecode", |
|
638 | 638 | strict_creation_check=strict_creation_check) |
|
639 | 639 | |
|
640 | 640 | if api_key: |
|
641 | 641 | log.info('setting a new default auth token for user `%s`', username) |
|
642 | 642 | UserModel().add_auth_token( |
|
643 | 643 | user=user, lifetime_minutes=-1, |
|
644 | 644 | role=UserModel.auth_token_role.ROLE_ALL, |
|
645 | 645 | description='BUILTIN TOKEN') |
|
646 | 646 | |
|
647 | 647 | def create_default_user(self): |
|
648 | 648 | log.info('creating default user') |
|
649 | 649 | # create default user for handling default permissions. |
|
650 | 650 | user = UserModel().create_or_update(username=User.DEFAULT_USER, |
|
651 | 651 | password=str(uuid.uuid1())[:20], |
|
652 | 652 | email=User.DEFAULT_USER_EMAIL, |
|
653 | 653 | firstname='Anonymous', |
|
654 | 654 | lastname='User', |
|
655 | 655 | strict_creation_check=False) |
|
656 | 656 | # based on configuration options activate/de-activate this user which |
|
657 | 657 | # controls anonymous access |
|
658 | 658 | if self.cli_args.get('public_access') is False: |
|
659 | 659 | log.info('Public access disabled') |
|
660 | 660 | user.active = False |
|
661 | 661 | Session().add(user) |
|
662 | 662 | Session().commit() |
|
663 | 663 | |
|
664 | 664 | def create_permissions(self): |
|
665 | 665 | """ |
|
666 | 666 | Creates all permissions defined in the system |
|
667 | 667 | """ |
|
668 | 668 | # module.(access|create|change|delete)_[name] |
|
669 | 669 | # module.(none|read|write|admin) |
|
670 | 670 | log.info('creating permissions') |
|
671 | 671 | PermissionModel(self.sa).create_permissions() |
|
672 | 672 | |
|
673 | 673 | def populate_default_permissions(self): |
|
674 | 674 | """ |
|
675 | 675 | Populate default permissions. It will create only the default |
|
676 | 676 | permissions that are missing, and not alter already defined ones |
|
677 | 677 | """ |
|
678 | 678 | log.info('creating default user permissions') |
|
679 | 679 | PermissionModel(self.sa).create_default_user_permissions(user=User.DEFAULT_USER) |
General Comments 0
You need to be logged in to leave comments.
Login now