##// END OF EJS Templates
fix: startup, report nicer error in case DB is damaged or empty when starting services
super-admin -
r5462:6ef12567 default
parent child Browse files
Show More
@@ -1,107 +1,110 b''
1 # Copyright (C) 2010-2023 RhodeCode GmbH
1 # Copyright (C) 2010-2023 RhodeCode GmbH
2 #
2 #
3 # This program is free software: you can redistribute it and/or modify
3 # This program is free software: you can redistribute it and/or modify
4 # it under the terms of the GNU Affero General Public License, version 3
4 # it under the terms of the GNU Affero General Public License, version 3
5 # (only), as published by the Free Software Foundation.
5 # (only), as published by the Free Software Foundation.
6 #
6 #
7 # This program is distributed in the hope that it will be useful,
7 # This program is distributed in the hope that it will be useful,
8 # but WITHOUT ANY WARRANTY; without even the implied warranty of
8 # but WITHOUT ANY WARRANTY; without even the implied warranty of
9 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 # GNU General Public License for more details.
10 # GNU General Public License for more details.
11 #
11 #
12 # You should have received a copy of the GNU Affero General Public License
12 # You should have received a copy of the GNU Affero General Public License
13 # along with this program. If not, see <http://www.gnu.org/licenses/>.
13 # along with this program. If not, see <http://www.gnu.org/licenses/>.
14 #
14 #
15 # This program is dual-licensed. If you wish to learn more about the
15 # This program is dual-licensed. If you wish to learn more about the
16 # RhodeCode Enterprise Edition, including its added features, Support services,
16 # RhodeCode Enterprise Edition, including its added features, Support services,
17 # and proprietary license terms, please see https://rhodecode.com/licenses/
17 # and proprietary license terms, please see https://rhodecode.com/licenses/
18
18
19 import os
19 import os
20 import platform
20 import platform
21
21
22 DEFAULT_USER = 'default'
22 DEFAULT_USER = 'default'
23
23
24
24
25 def configure_vcs(config):
25 def configure_vcs(config):
26 """
26 """
27 Patch VCS config with some RhodeCode specific stuff
27 Patch VCS config with some RhodeCode specific stuff
28 """
28 """
29 from rhodecode.lib.vcs import conf
29 from rhodecode.lib.vcs import conf
30 import rhodecode.lib.vcs.conf.settings
30 import rhodecode.lib.vcs.conf.settings
31
31
32 conf.settings.BACKENDS = {
32 conf.settings.BACKENDS = {
33 'hg': 'rhodecode.lib.vcs.backends.hg.MercurialRepository',
33 'hg': 'rhodecode.lib.vcs.backends.hg.MercurialRepository',
34 'git': 'rhodecode.lib.vcs.backends.git.GitRepository',
34 'git': 'rhodecode.lib.vcs.backends.git.GitRepository',
35 'svn': 'rhodecode.lib.vcs.backends.svn.SubversionRepository',
35 'svn': 'rhodecode.lib.vcs.backends.svn.SubversionRepository',
36 }
36 }
37
37
38 conf.settings.HOOKS_PROTOCOL = config['vcs.hooks.protocol']
38 conf.settings.HOOKS_PROTOCOL = config['vcs.hooks.protocol']
39 conf.settings.HOOKS_HOST = config['vcs.hooks.host']
39 conf.settings.HOOKS_HOST = config['vcs.hooks.host']
40 conf.settings.DEFAULT_ENCODINGS = config['default_encoding']
40 conf.settings.DEFAULT_ENCODINGS = config['default_encoding']
41 conf.settings.ALIASES[:] = config['vcs.backends']
41 conf.settings.ALIASES[:] = config['vcs.backends']
42 conf.settings.SVN_COMPATIBLE_VERSION = config['vcs.svn.compatible_version']
42 conf.settings.SVN_COMPATIBLE_VERSION = config['vcs.svn.compatible_version']
43
43
44
44
45 def initialize_database(config):
45 def initialize_database(config):
46 from rhodecode.lib.utils2 import engine_from_config, get_encryption_key
46 from rhodecode.lib.utils2 import engine_from_config, get_encryption_key
47 from rhodecode.model import init_model
47 from rhodecode.model import init_model
48 engine = engine_from_config(config, 'sqlalchemy.db1.')
48 engine = engine_from_config(config, 'sqlalchemy.db1.')
49 init_model(engine, encryption_key=get_encryption_key(config))
49 init_model(engine, encryption_key=get_encryption_key(config))
50
50
51
51
52 def initialize_test_environment(settings, test_env=None):
52 def initialize_test_environment(settings, test_env=None):
53 if test_env is None:
53 if test_env is None:
54 test_env = not int(os.environ.get('RC_NO_TMP_PATH', 0))
54 test_env = not int(os.environ.get('RC_NO_TMP_PATH', 0))
55
55
56 from rhodecode.lib.utils import (
56 from rhodecode.lib.utils import (
57 create_test_directory, create_test_database, create_test_repositories,
57 create_test_directory, create_test_database, create_test_repositories,
58 create_test_index)
58 create_test_index)
59 from rhodecode.tests import TESTS_TMP_PATH
59 from rhodecode.tests import TESTS_TMP_PATH
60 from rhodecode.lib.vcs.backends.hg import largefiles_store
60 from rhodecode.lib.vcs.backends.hg import largefiles_store
61 from rhodecode.lib.vcs.backends.git import lfs_store
61 from rhodecode.lib.vcs.backends.git import lfs_store
62
62
63 # test repos
63 # test repos
64 if test_env:
64 if test_env:
65 create_test_directory(TESTS_TMP_PATH)
65 create_test_directory(TESTS_TMP_PATH)
66 # large object stores
66 # large object stores
67 create_test_directory(largefiles_store(TESTS_TMP_PATH))
67 create_test_directory(largefiles_store(TESTS_TMP_PATH))
68 create_test_directory(lfs_store(TESTS_TMP_PATH))
68 create_test_directory(lfs_store(TESTS_TMP_PATH))
69
69
70 create_test_database(TESTS_TMP_PATH, settings)
70 create_test_database(TESTS_TMP_PATH, settings)
71 create_test_repositories(TESTS_TMP_PATH, settings)
71 create_test_repositories(TESTS_TMP_PATH, settings)
72 create_test_index(TESTS_TMP_PATH, settings)
72 create_test_index(TESTS_TMP_PATH, settings)
73
73
74
74
75 def get_vcs_server_protocol(config):
75 def get_vcs_server_protocol(config):
76 return config['vcs.server.protocol']
76 return config['vcs.server.protocol']
77
77
78
78
79 def set_instance_id(config):
79 def set_instance_id(config):
80 """
80 """
81 Sets a dynamic generated config['instance_id'] if missing or '*'
81 Sets a dynamic generated config['instance_id'] if missing or '*'
82 E.g instance_id = *cluster-1 or instance_id = *
82 E.g instance_id = *cluster-1 or instance_id = *
83 """
83 """
84
84
85 config['instance_id'] = config.get('instance_id') or ''
85 config['instance_id'] = config.get('instance_id') or ''
86 instance_id = config['instance_id']
86 instance_id = config['instance_id']
87 if instance_id.startswith('*') or not instance_id:
87 if instance_id.startswith('*') or not instance_id:
88 prefix = instance_id.lstrip('*')
88 prefix = instance_id.lstrip('*')
89 _platform_id = platform.uname()[1] or 'instance'
89 _platform_id = platform.uname()[1] or 'instance'
90 config['instance_id'] = '{prefix}uname:{platform}-pid:{pid}'.format(
90 config['instance_id'] = '{prefix}uname:{platform}-pid:{pid}'.format(
91 prefix=prefix,
91 prefix=prefix,
92 platform=_platform_id,
92 platform=_platform_id,
93 pid=os.getpid())
93 pid=os.getpid())
94
94
95
95
96 def get_default_user_id():
96 def get_default_user_id():
97 from sqlalchemy import text
97 from sqlalchemy import text
98 from rhodecode.model import meta
98 from rhodecode.model import meta
99
99
100 engine = meta.get_engine()
100 engine = meta.get_engine()
101 with meta.SA_Session(engine) as session:
101 with meta.SA_Session(engine) as session:
102 result = session.execute(text(
102 result = session.execute(text(
103 "SELECT user_id from users where username = :uname"
103 "SELECT user_id from users where username = :uname"
104 ), {'uname': DEFAULT_USER})
104 ), {'uname': DEFAULT_USER})
105 user_id = result.first()[0]
105 user = result.first()
106 if not user:
107 raise ValueError('Unable to retrieve default user data from DB')
108 user_id = user[0]
106
109
107 return user_id
110 return user_id
General Comments 0
You need to be logged in to leave comments. Login now