##// END OF EJS Templates
ops: make ping view return orderedDict, and make the platform-id expose variables
super-admin -
r4851:98843d15 default
parent child Browse files
Show More
@@ -1,97 +1,96 b''
1 # -*- coding: utf-8 -*-
1 # -*- coding: utf-8 -*-
2
2
3 # Copyright (C) 2016-2020 RhodeCode GmbH
3 # Copyright (C) 2016-2020 RhodeCode GmbH
4 #
4 #
5 # This program is free software: you can redistribute it and/or modify
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU Affero General Public License, version 3
6 # it under the terms of the GNU Affero General Public License, version 3
7 # (only), as published by the Free Software Foundation.
7 # (only), as published by the Free Software Foundation.
8 #
8 #
9 # This program is distributed in the hope that it will be useful,
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
12 # GNU General Public License for more details.
13 #
13 #
14 # You should have received a copy of the GNU Affero General Public License
14 # You should have received a copy of the GNU Affero General Public License
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
16 #
16 #
17 # This program is dual-licensed. If you wish to learn more about the
17 # This program is dual-licensed. If you wish to learn more about the
18 # RhodeCode Enterprise Edition, including its added features, Support services,
18 # RhodeCode Enterprise Edition, including its added features, Support services,
19 # and proprietary license terms, please see https://rhodecode.com/licenses/
19 # and proprietary license terms, please see https://rhodecode.com/licenses/
20
20
21 import time
21 import time
22 import logging
22 import logging
23
23
24
24
25 from pyramid.httpexceptions import HTTPFound
25 from pyramid.httpexceptions import HTTPFound
26
26
27 from rhodecode.apps._base import BaseAppView
27 from rhodecode.apps._base import BaseAppView
28 from rhodecode.lib import helpers as h
28 from rhodecode.lib import helpers as h
29 from rhodecode.lib.auth import LoginRequired
29 from rhodecode.lib.auth import LoginRequired
30 from rhodecode.lib.compat import OrderedDict
30 from rhodecode.model.db import UserApiKeys
31 from rhodecode.model.db import UserApiKeys
31
32
32 log = logging.getLogger(__name__)
33 log = logging.getLogger(__name__)
33
34
34
35
35 class OpsView(BaseAppView):
36 class OpsView(BaseAppView):
36
37
37 def load_default_context(self):
38 def load_default_context(self):
38 c = self._get_local_tmpl_context()
39 c = self._get_local_tmpl_context()
39 c.user = c.auth_user.get_instance()
40 c.user = c.auth_user.get_instance()
40
41
41 return c
42 return c
42
43
43 def ops_ping(self):
44 def ops_ping(self):
44 data = {
45 data = OrderedDict()
45 'instance': self.request.registry.settings.get('instance_id'),
46 data['instance'] = self.request.registry.settings.get('instance_id')
46 }
47
47 if getattr(self.request, 'user'):
48 if getattr(self.request, 'user'):
48 caller_name = 'anonymous'
49 caller_name = 'anonymous'
49 if self.request.user.user_id:
50 if self.request.user.user_id:
50 caller_name = self.request.user.username
51 caller_name = self.request.user.username
51
52
52 data.update({
53 data['caller_ip'] = self.request.user.ip_addr
53 'caller_ip': self.request.user.ip_addr,
54 data['caller_name'] = caller_name
54 'caller_name': caller_name,
55
55 })
56 return {'ok': data}
56 return {'ok': data}
57
57
58 def ops_error_test(self):
58 def ops_error_test(self):
59 """
59 """
60 Test exception handling and emails on errors
60 Test exception handling and emails on errors
61 """
61 """
62
62
63 class TestException(Exception):
63 class TestException(Exception):
64 pass
64 pass
65 # add timeout so we add some sort of rate limiter
65 # add timeout so we add some sort of rate limiter
66 time.sleep(2)
66 time.sleep(2)
67 msg = ('RhodeCode Enterprise test exception. '
67 msg = ('RhodeCode Enterprise test exception. '
68 'Client:{}. Generation time: {}.'.format(self.request.user, time.time()))
68 'Client:{}. Generation time: {}.'.format(self.request.user, time.time()))
69 raise TestException(msg)
69 raise TestException(msg)
70
70
71 def ops_redirect_test(self):
71 def ops_redirect_test(self):
72 """
72 """
73 Test redirect handling
73 Test redirect handling
74 """
74 """
75 redirect_to = self.request.GET.get('to') or h.route_path('home')
75 redirect_to = self.request.GET.get('to') or h.route_path('home')
76 raise HTTPFound(redirect_to)
76 raise HTTPFound(redirect_to)
77
77
78 @LoginRequired(auth_token_access=[UserApiKeys.ROLE_HTTP])
78 @LoginRequired(auth_token_access=[UserApiKeys.ROLE_HTTP])
79 def ops_healthcheck(self):
79 def ops_healthcheck(self):
80 from rhodecode.lib.system_info import load_system_info
80 from rhodecode.lib.system_info import load_system_info
81
81
82 vcsserver_info = load_system_info('vcs_server')
82 vcsserver_info = load_system_info('vcs_server')
83 if vcsserver_info:
83 if vcsserver_info:
84 vcsserver_info = vcsserver_info['human_value']
84 vcsserver_info = vcsserver_info['human_value']
85
85
86 db_info = load_system_info('database_info')
86 db_info = load_system_info('database_info')
87 if db_info:
87 if db_info:
88 db_info = db_info['human_value']
88 db_info = db_info['human_value']
89
89
90 health_spec = {
90 health_spec = {
91 'caller_ip': self.request.user.ip_addr,
91 'caller_ip': self.request.user.ip_addr,
92 'vcsserver': vcsserver_info,
92 'vcsserver': vcsserver_info,
93 'db': db_info,
93 'db': db_info,
94 }
94 }
95
95
96 return {'healthcheck': health_spec}
96 return {'healthcheck': health_spec}
97
@@ -1,101 +1,104 b''
1 # -*- coding: utf-8 -*-
1 # -*- coding: utf-8 -*-
2
2
3 # Copyright (C) 2010-2020 RhodeCode GmbH
3 # Copyright (C) 2010-2020 RhodeCode GmbH
4 #
4 #
5 # This program is free software: you can redistribute it and/or modify
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU Affero General Public License, version 3
6 # it under the terms of the GNU Affero General Public License, version 3
7 # (only), as published by the Free Software Foundation.
7 # (only), as published by the Free Software Foundation.
8 #
8 #
9 # This program is distributed in the hope that it will be useful,
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
12 # GNU General Public License for more details.
13 #
13 #
14 # You should have received a copy of the GNU Affero General Public License
14 # You should have received a copy of the GNU Affero General Public License
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
16 #
16 #
17 # This program is dual-licensed. If you wish to learn more about the
17 # This program is dual-licensed. If you wish to learn more about the
18 # RhodeCode Enterprise Edition, including its added features, Support services,
18 # RhodeCode Enterprise Edition, including its added features, Support services,
19 # and proprietary license terms, please see https://rhodecode.com/licenses/
19 # and proprietary license terms, please see https://rhodecode.com/licenses/
20
20
21 import os
21 import os
22 import platform
22 import platform
23
23
24 from rhodecode.model import init_model
24 from rhodecode.model import init_model
25
25
26
26
27 def configure_vcs(config):
27 def configure_vcs(config):
28 """
28 """
29 Patch VCS config with some RhodeCode specific stuff
29 Patch VCS config with some RhodeCode specific stuff
30 """
30 """
31 from rhodecode.lib.vcs import conf
31 from rhodecode.lib.vcs import conf
32 import rhodecode.lib.vcs.conf.settings
32 import rhodecode.lib.vcs.conf.settings
33
33
34 conf.settings.BACKENDS = {
34 conf.settings.BACKENDS = {
35 'hg': 'rhodecode.lib.vcs.backends.hg.MercurialRepository',
35 'hg': 'rhodecode.lib.vcs.backends.hg.MercurialRepository',
36 'git': 'rhodecode.lib.vcs.backends.git.GitRepository',
36 'git': 'rhodecode.lib.vcs.backends.git.GitRepository',
37 'svn': 'rhodecode.lib.vcs.backends.svn.SubversionRepository',
37 'svn': 'rhodecode.lib.vcs.backends.svn.SubversionRepository',
38 }
38 }
39
39
40 conf.settings.HOOKS_PROTOCOL = config['vcs.hooks.protocol']
40 conf.settings.HOOKS_PROTOCOL = config['vcs.hooks.protocol']
41 conf.settings.HOOKS_HOST = config['vcs.hooks.host']
41 conf.settings.HOOKS_HOST = config['vcs.hooks.host']
42 conf.settings.HOOKS_DIRECT_CALLS = config['vcs.hooks.direct_calls']
42 conf.settings.HOOKS_DIRECT_CALLS = config['vcs.hooks.direct_calls']
43 conf.settings.DEFAULT_ENCODINGS = config['default_encoding']
43 conf.settings.DEFAULT_ENCODINGS = config['default_encoding']
44 conf.settings.ALIASES[:] = config['vcs.backends']
44 conf.settings.ALIASES[:] = config['vcs.backends']
45 conf.settings.SVN_COMPATIBLE_VERSION = config['vcs.svn.compatible_version']
45 conf.settings.SVN_COMPATIBLE_VERSION = config['vcs.svn.compatible_version']
46
46
47
47
48 def initialize_database(config):
48 def initialize_database(config):
49 from rhodecode.lib.utils2 import engine_from_config, get_encryption_key
49 from rhodecode.lib.utils2 import engine_from_config, get_encryption_key
50 engine = engine_from_config(config, 'sqlalchemy.db1.')
50 engine = engine_from_config(config, 'sqlalchemy.db1.')
51 init_model(engine, encryption_key=get_encryption_key(config))
51 init_model(engine, encryption_key=get_encryption_key(config))
52
52
53
53
54 def initialize_test_environment(settings, test_env=None):
54 def initialize_test_environment(settings, test_env=None):
55 if test_env is None:
55 if test_env is None:
56 test_env = not int(os.environ.get('RC_NO_TMP_PATH', 0))
56 test_env = not int(os.environ.get('RC_NO_TMP_PATH', 0))
57
57
58 from rhodecode.lib.utils import (
58 from rhodecode.lib.utils import (
59 create_test_directory, create_test_database, create_test_repositories,
59 create_test_directory, create_test_database, create_test_repositories,
60 create_test_index)
60 create_test_index)
61 from rhodecode.tests import TESTS_TMP_PATH
61 from rhodecode.tests import TESTS_TMP_PATH
62 from rhodecode.lib.vcs.backends.hg import largefiles_store
62 from rhodecode.lib.vcs.backends.hg import largefiles_store
63 from rhodecode.lib.vcs.backends.git import lfs_store
63 from rhodecode.lib.vcs.backends.git import lfs_store
64
64
65 # test repos
65 # test repos
66 if test_env:
66 if test_env:
67 create_test_directory(TESTS_TMP_PATH)
67 create_test_directory(TESTS_TMP_PATH)
68 # large object stores
68 # large object stores
69 create_test_directory(largefiles_store(TESTS_TMP_PATH))
69 create_test_directory(largefiles_store(TESTS_TMP_PATH))
70 create_test_directory(lfs_store(TESTS_TMP_PATH))
70 create_test_directory(lfs_store(TESTS_TMP_PATH))
71
71
72 create_test_database(TESTS_TMP_PATH, settings)
72 create_test_database(TESTS_TMP_PATH, settings)
73 create_test_repositories(TESTS_TMP_PATH, settings)
73 create_test_repositories(TESTS_TMP_PATH, settings)
74 create_test_index(TESTS_TMP_PATH, settings)
74 create_test_index(TESTS_TMP_PATH, settings)
75
75
76
76
77 def get_vcs_server_protocol(config):
77 def get_vcs_server_protocol(config):
78 return config['vcs.server.protocol']
78 return config['vcs.server.protocol']
79
79
80
80
81 def set_instance_id(config):
81 def set_instance_id(config):
82 """
82 """
83 Sets a dynamic generated config['instance_id'] if missing or '*'
83 Sets a dynamic generated config['instance_id'] if missing or '*'
84 E.g instance_id = *cluster-1 or instance_id = *
84 E.g instance_id = *cluster-1 or instance_id = *
85 """
85 """
86
86
87 config['instance_id'] = config.get('instance_id') or ''
87 config['instance_id'] = config.get('instance_id') or ''
88 instance_id = config['instance_id']
88 instance_id = config['instance_id']
89 if instance_id.startswith('*') or not instance_id:
89 if instance_id.startswith('*') or not instance_id:
90 prefix = instance_id.lstrip('*')
90 prefix = instance_id.lstrip('*')
91 _platform_id = platform.uname()[1] or 'instance'
91 _platform_id = platform.uname()[1] or 'instance'
92 config['instance_id'] = '%s%s-%s' % (prefix, _platform_id, os.getpid())
92 config['instance_id'] = '{prefix}uname:{platform}-pid:{pid}'.format(
93 prefix=prefix,
94 platform=_platform_id,
95 pid=os.getpid())
93
96
94
97
95 def get_default_user_id():
98 def get_default_user_id():
96 from rhodecode.model.db import User, Session
99 from rhodecode.model.db import User, Session
97 user_id = Session()\
100 user_id = Session()\
98 .query(User.user_id)\
101 .query(User.user_id)\
99 .filter(User.username == User.DEFAULT_USER)\
102 .filter(User.username == User.DEFAULT_USER)\
100 .scalar()
103 .scalar()
101 return user_id
104 return user_id
General Comments 0
You need to be logged in to leave comments. Login now