Show More
@@ -1,69 +1,78 b'' | |||
|
1 | 1 | # -*- coding: utf-8 -*- |
|
2 | 2 | |
|
3 | 3 | # Copyright (C) 2010-2016 RhodeCode GmbH |
|
4 | 4 | # |
|
5 | 5 | # This program is free software: you can redistribute it and/or modify |
|
6 | 6 | # it under the terms of the GNU Affero General Public License, version 3 |
|
7 | 7 | # (only), as published by the Free Software Foundation. |
|
8 | 8 | # |
|
9 | 9 | # This program is distributed in the hope that it will be useful, |
|
10 | 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 | 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 | 12 | # GNU General Public License for more details. |
|
13 | 13 | # |
|
14 | 14 | # You should have received a copy of the GNU Affero General Public License |
|
15 | 15 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
16 | 16 | # |
|
17 | 17 | # This program is dual-licensed. If you wish to learn more about the |
|
18 | 18 | # RhodeCode Enterprise Edition, including its added features, Support services, |
|
19 | 19 | # and proprietary license terms, please see https://rhodecode.com/licenses/ |
|
20 | 20 | |
|
21 | 21 | |
|
22 | 22 | import pytest |
|
23 | 23 | |
|
24 | 24 | from rhodecode.model.scm import ScmModel |
|
25 | 25 | from rhodecode.api.tests.utils import build_data, api_call, assert_ok |
|
26 | 26 | |
|
27 | 27 | |
|
28 | 28 | @pytest.fixture |
|
29 | 29 | def http_host_stub(): |
|
30 | 30 | """ |
|
31 | 31 | To ensure that we can get an IP address, this test shall run with a |
|
32 | 32 | hostname set to "localhost". |
|
33 | 33 | """ |
|
34 | 34 | return 'localhost:80' |
|
35 | 35 | |
|
36 | 36 | |
|
37 | 37 | @pytest.mark.usefixtures("testuser_api", "app") |
|
38 | 38 | class TestGetServerInfo(object): |
|
39 | 39 | def test_api_get_server_info(self): |
|
40 | 40 | id_, params = build_data(self.apikey, 'get_server_info') |
|
41 | 41 | response = api_call(self.app, params) |
|
42 | 42 | resp = response.json |
|
43 | 43 | expected = ScmModel().get_server_info() |
|
44 | 44 | expected['memory'] = resp['result']['memory'] |
|
45 | 45 | expected['uptime'] = resp['result']['uptime'] |
|
46 | 46 | expected['load'] = resp['result']['load'] |
|
47 | 47 | expected['cpu'] = resp['result']['cpu'] |
|
48 | 48 | expected['storage'] = resp['result']['storage'] |
|
49 | 49 | expected['storage_temp'] = resp['result']['storage_temp'] |
|
50 | 50 | expected['storage_inodes'] = resp['result']['storage_inodes'] |
|
51 | 51 | expected['server'] = resp['result']['server'] |
|
52 | 52 | |
|
53 | 53 | assert_ok(id_, expected, given=response.body) |
|
54 | 54 | |
|
55 | 55 | def test_api_get_server_info_ip(self): |
|
56 | 56 | id_, params = build_data(self.apikey, 'get_server_info') |
|
57 | 57 | response = api_call(self.app, params) |
|
58 | 58 | resp = response.json |
|
59 | 59 | expected = ScmModel().get_server_info({'SERVER_NAME': 'unknown'}) |
|
60 | 60 | expected['memory'] = resp['result']['memory'] |
|
61 | 61 | expected['uptime'] = resp['result']['uptime'] |
|
62 | 62 | expected['load'] = resp['result']['load'] |
|
63 | 63 | expected['cpu'] = resp['result']['cpu'] |
|
64 | 64 | expected['storage'] = resp['result']['storage'] |
|
65 | 65 | expected['storage_temp'] = resp['result']['storage_temp'] |
|
66 | 66 | expected['storage_inodes'] = resp['result']['storage_inodes'] |
|
67 | 67 | expected['server'] = resp['result']['server'] |
|
68 | 68 | |
|
69 | 69 | assert_ok(id_, expected, given=response.body) |
|
70 | ||
|
71 | def test_api_get_server_info_data_for_search_index_build(self): | |
|
72 | id_, params = build_data(self.apikey, 'get_server_info') | |
|
73 | response = api_call(self.app, params) | |
|
74 | resp = response.json | |
|
75 | ||
|
76 | # required by indexer | |
|
77 | assert resp['result']['index_storage'] | |
|
78 | assert resp['result']['storage'] |
@@ -1,172 +1,178 b'' | |||
|
1 | 1 | # -*- coding: utf-8 -*- |
|
2 | 2 | |
|
3 | 3 | # Copyright (C) 2011-2016 RhodeCode GmbH |
|
4 | 4 | # |
|
5 | 5 | # This program is free software: you can redistribute it and/or modify |
|
6 | 6 | # it under the terms of the GNU Affero General Public License, version 3 |
|
7 | 7 | # (only), as published by the Free Software Foundation. |
|
8 | 8 | # |
|
9 | 9 | # This program is distributed in the hope that it will be useful, |
|
10 | 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 | 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 | 12 | # GNU General Public License for more details. |
|
13 | 13 | # |
|
14 | 14 | # You should have received a copy of the GNU Affero General Public License |
|
15 | 15 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
16 | 16 | # |
|
17 | 17 | # This program is dual-licensed. If you wish to learn more about the |
|
18 | 18 | # RhodeCode Enterprise Edition, including its added features, Support services, |
|
19 | 19 | # and proprietary license terms, please see https://rhodecode.com/licenses/ |
|
20 | 20 | |
|
21 | 21 | |
|
22 | 22 | import logging |
|
23 | 23 | |
|
24 | 24 | from rhodecode.api import jsonrpc_method, JSONRPCError, JSONRPCForbidden |
|
25 | 25 | |
|
26 | 26 | from rhodecode.api.utils import ( |
|
27 | 27 | Optional, OAttr, has_superadmin_permission, get_user_or_error) |
|
28 | 28 | from rhodecode.lib.utils import repo2db_mapper |
|
29 | 29 | from rhodecode.model.db import UserIpMap |
|
30 | 30 | from rhodecode.model.scm import ScmModel |
|
31 | 31 | |
|
32 | 32 | log = logging.getLogger(__name__) |
|
33 | 33 | |
|
34 | 34 | |
|
35 | 35 | @jsonrpc_method() |
|
36 | 36 | def get_server_info(request, apiuser): |
|
37 | 37 | """ |
|
38 | 38 | Returns the |RCE| server information. |
|
39 | 39 | |
|
40 | 40 | This includes the running version of |RCE| and all installed |
|
41 | 41 | packages. This command takes the following options: |
|
42 | 42 | |
|
43 | 43 | :param apiuser: This is filled automatically from the |authtoken|. |
|
44 | 44 | :type apiuser: AuthUser |
|
45 | 45 | |
|
46 | 46 | Example output: |
|
47 | 47 | |
|
48 | 48 | .. code-block:: bash |
|
49 | 49 | |
|
50 | 50 | id : <id_given_in_input> |
|
51 | 51 | result : { |
|
52 | 52 | 'modules': [<module name>,...] |
|
53 | 53 | 'py_version': <python version>, |
|
54 | 54 | 'platform': <platform type>, |
|
55 | 55 | 'rhodecode_version': <rhodecode version> |
|
56 | 56 | } |
|
57 | 57 | error : null |
|
58 | 58 | """ |
|
59 | 59 | |
|
60 | 60 | if not has_superadmin_permission(apiuser): |
|
61 | 61 | raise JSONRPCForbidden() |
|
62 | 62 | |
|
63 |
re |
|
|
63 | server_info = ScmModel().get_server_info(request.environ) | |
|
64 | # rhodecode-index requires those | |
|
65 | ||
|
66 | server_info['index_storage'] = server_info['search']['value']['location'] | |
|
67 | server_info['storage'] = server_info['storage']['value']['path'] | |
|
68 | ||
|
69 | return server_info | |
|
64 | 70 | |
|
65 | 71 | |
|
66 | 72 | @jsonrpc_method() |
|
67 | 73 | def get_ip(request, apiuser, userid=Optional(OAttr('apiuser'))): |
|
68 | 74 | """ |
|
69 | 75 | Displays the IP Address as seen from the |RCE| server. |
|
70 | 76 | |
|
71 | 77 | * This command displays the IP Address, as well as all the defined IP |
|
72 | 78 | addresses for the specified user. If the ``userid`` is not set, the |
|
73 | 79 | data returned is for the user calling the method. |
|
74 | 80 | |
|
75 | 81 | This command can only be run using an |authtoken| with admin rights to |
|
76 | 82 | the specified repository. |
|
77 | 83 | |
|
78 | 84 | This command takes the following options: |
|
79 | 85 | |
|
80 | 86 | :param apiuser: This is filled automatically from |authtoken|. |
|
81 | 87 | :type apiuser: AuthUser |
|
82 | 88 | :param userid: Sets the userid for which associated IP Address data |
|
83 | 89 | is returned. |
|
84 | 90 | :type userid: Optional(str or int) |
|
85 | 91 | |
|
86 | 92 | Example output: |
|
87 | 93 | |
|
88 | 94 | .. code-block:: bash |
|
89 | 95 | |
|
90 | 96 | id : <id_given_in_input> |
|
91 | 97 | result : { |
|
92 | 98 | "server_ip_addr": "<ip_from_clien>", |
|
93 | 99 | "user_ips": [ |
|
94 | 100 | { |
|
95 | 101 | "ip_addr": "<ip_with_mask>", |
|
96 | 102 | "ip_range": ["<start_ip>", "<end_ip>"], |
|
97 | 103 | }, |
|
98 | 104 | ... |
|
99 | 105 | ] |
|
100 | 106 | } |
|
101 | 107 | |
|
102 | 108 | """ |
|
103 | 109 | if not has_superadmin_permission(apiuser): |
|
104 | 110 | raise JSONRPCForbidden() |
|
105 | 111 | |
|
106 | 112 | userid = Optional.extract(userid, evaluate_locals=locals()) |
|
107 | 113 | userid = getattr(userid, 'user_id', userid) |
|
108 | 114 | |
|
109 | 115 | user = get_user_or_error(userid) |
|
110 | 116 | ips = UserIpMap.query().filter(UserIpMap.user == user).all() |
|
111 | 117 | return { |
|
112 | 118 | 'server_ip_addr': request.rpc_ip_addr, |
|
113 | 119 | 'user_ips': ips |
|
114 | 120 | } |
|
115 | 121 | |
|
116 | 122 | |
|
117 | 123 | @jsonrpc_method() |
|
118 | 124 | def rescan_repos(request, apiuser, remove_obsolete=Optional(False)): |
|
119 | 125 | """ |
|
120 | 126 | Triggers a rescan of the specified repositories. |
|
121 | 127 | |
|
122 | 128 | * If the ``remove_obsolete`` option is set, it also deletes repositories |
|
123 | 129 | that are found in the database but not on the file system, so called |
|
124 | 130 | "clean zombies". |
|
125 | 131 | |
|
126 | 132 | This command can only be run using an |authtoken| with admin rights to |
|
127 | 133 | the specified repository. |
|
128 | 134 | |
|
129 | 135 | This command takes the following options: |
|
130 | 136 | |
|
131 | 137 | :param apiuser: This is filled automatically from the |authtoken|. |
|
132 | 138 | :type apiuser: AuthUser |
|
133 | 139 | :param remove_obsolete: Deletes repositories from the database that |
|
134 | 140 | are not found on the filesystem. |
|
135 | 141 | :type remove_obsolete: Optional(``True`` | ``False``) |
|
136 | 142 | |
|
137 | 143 | Example output: |
|
138 | 144 | |
|
139 | 145 | .. code-block:: bash |
|
140 | 146 | |
|
141 | 147 | id : <id_given_in_input> |
|
142 | 148 | result : { |
|
143 | 149 | 'added': [<added repository name>,...] |
|
144 | 150 | 'removed': [<removed repository name>,...] |
|
145 | 151 | } |
|
146 | 152 | error : null |
|
147 | 153 | |
|
148 | 154 | Example error output: |
|
149 | 155 | |
|
150 | 156 | .. code-block:: bash |
|
151 | 157 | |
|
152 | 158 | id : <id_given_in_input> |
|
153 | 159 | result : null |
|
154 | 160 | error : { |
|
155 | 161 | 'Error occurred during rescan repositories action' |
|
156 | 162 | } |
|
157 | 163 | |
|
158 | 164 | """ |
|
159 | 165 | if not has_superadmin_permission(apiuser): |
|
160 | 166 | raise JSONRPCForbidden() |
|
161 | 167 | |
|
162 | 168 | try: |
|
163 | 169 | rm_obsolete = Optional.extract(remove_obsolete) |
|
164 | 170 | added, removed = repo2db_mapper(ScmModel().repo_scan(), |
|
165 | 171 | remove_obsolete=rm_obsolete) |
|
166 | 172 | return {'added': added, 'removed': removed} |
|
167 | 173 | except Exception: |
|
168 | 174 | log.exception('Failed to run repo rescann') |
|
169 | 175 | raise JSONRPCError( |
|
170 | 176 | 'Error occurred during rescan repositories action' |
|
171 | 177 | ) |
|
172 | 178 |
General Comments 0
You need to be logged in to leave comments.
Login now