Show More
@@ -1,69 +1,78 b'' | |||||
1 | # -*- coding: utf-8 -*- |
|
1 | # -*- coding: utf-8 -*- | |
2 |
|
2 | |||
3 | # Copyright (C) 2010-2016 RhodeCode GmbH |
|
3 | # Copyright (C) 2010-2016 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 |
|
21 | |||
22 | import pytest |
|
22 | import pytest | |
23 |
|
23 | |||
24 | from rhodecode.model.scm import ScmModel |
|
24 | from rhodecode.model.scm import ScmModel | |
25 | from rhodecode.api.tests.utils import build_data, api_call, assert_ok |
|
25 | from rhodecode.api.tests.utils import build_data, api_call, assert_ok | |
26 |
|
26 | |||
27 |
|
27 | |||
28 | @pytest.fixture |
|
28 | @pytest.fixture | |
29 | def http_host_stub(): |
|
29 | def http_host_stub(): | |
30 | """ |
|
30 | """ | |
31 | To ensure that we can get an IP address, this test shall run with a |
|
31 | To ensure that we can get an IP address, this test shall run with a | |
32 | hostname set to "localhost". |
|
32 | hostname set to "localhost". | |
33 | """ |
|
33 | """ | |
34 | return 'localhost:80' |
|
34 | return 'localhost:80' | |
35 |
|
35 | |||
36 |
|
36 | |||
37 | @pytest.mark.usefixtures("testuser_api", "app") |
|
37 | @pytest.mark.usefixtures("testuser_api", "app") | |
38 | class TestGetServerInfo(object): |
|
38 | class TestGetServerInfo(object): | |
39 | def test_api_get_server_info(self): |
|
39 | def test_api_get_server_info(self): | |
40 | id_, params = build_data(self.apikey, 'get_server_info') |
|
40 | id_, params = build_data(self.apikey, 'get_server_info') | |
41 | response = api_call(self.app, params) |
|
41 | response = api_call(self.app, params) | |
42 | resp = response.json |
|
42 | resp = response.json | |
43 | expected = ScmModel().get_server_info() |
|
43 | expected = ScmModel().get_server_info() | |
44 | expected['memory'] = resp['result']['memory'] |
|
44 | expected['memory'] = resp['result']['memory'] | |
45 | expected['uptime'] = resp['result']['uptime'] |
|
45 | expected['uptime'] = resp['result']['uptime'] | |
46 | expected['load'] = resp['result']['load'] |
|
46 | expected['load'] = resp['result']['load'] | |
47 | expected['cpu'] = resp['result']['cpu'] |
|
47 | expected['cpu'] = resp['result']['cpu'] | |
48 | expected['storage'] = resp['result']['storage'] |
|
48 | expected['storage'] = resp['result']['storage'] | |
49 | expected['storage_temp'] = resp['result']['storage_temp'] |
|
49 | expected['storage_temp'] = resp['result']['storage_temp'] | |
50 | expected['storage_inodes'] = resp['result']['storage_inodes'] |
|
50 | expected['storage_inodes'] = resp['result']['storage_inodes'] | |
51 | expected['server'] = resp['result']['server'] |
|
51 | expected['server'] = resp['result']['server'] | |
52 |
|
52 | |||
53 | assert_ok(id_, expected, given=response.body) |
|
53 | assert_ok(id_, expected, given=response.body) | |
54 |
|
54 | |||
55 | def test_api_get_server_info_ip(self): |
|
55 | def test_api_get_server_info_ip(self): | |
56 | id_, params = build_data(self.apikey, 'get_server_info') |
|
56 | id_, params = build_data(self.apikey, 'get_server_info') | |
57 | response = api_call(self.app, params) |
|
57 | response = api_call(self.app, params) | |
58 | resp = response.json |
|
58 | resp = response.json | |
59 | expected = ScmModel().get_server_info({'SERVER_NAME': 'unknown'}) |
|
59 | expected = ScmModel().get_server_info({'SERVER_NAME': 'unknown'}) | |
60 | expected['memory'] = resp['result']['memory'] |
|
60 | expected['memory'] = resp['result']['memory'] | |
61 | expected['uptime'] = resp['result']['uptime'] |
|
61 | expected['uptime'] = resp['result']['uptime'] | |
62 | expected['load'] = resp['result']['load'] |
|
62 | expected['load'] = resp['result']['load'] | |
63 | expected['cpu'] = resp['result']['cpu'] |
|
63 | expected['cpu'] = resp['result']['cpu'] | |
64 | expected['storage'] = resp['result']['storage'] |
|
64 | expected['storage'] = resp['result']['storage'] | |
65 | expected['storage_temp'] = resp['result']['storage_temp'] |
|
65 | expected['storage_temp'] = resp['result']['storage_temp'] | |
66 | expected['storage_inodes'] = resp['result']['storage_inodes'] |
|
66 | expected['storage_inodes'] = resp['result']['storage_inodes'] | |
67 | expected['server'] = resp['result']['server'] |
|
67 | expected['server'] = resp['result']['server'] | |
68 |
|
68 | |||
69 | assert_ok(id_, expected, given=response.body) |
|
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 | # -*- coding: utf-8 -*- |
|
1 | # -*- coding: utf-8 -*- | |
2 |
|
2 | |||
3 | # Copyright (C) 2011-2016 RhodeCode GmbH |
|
3 | # Copyright (C) 2011-2016 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 |
|
21 | |||
22 | import logging |
|
22 | import logging | |
23 |
|
23 | |||
24 | from rhodecode.api import jsonrpc_method, JSONRPCError, JSONRPCForbidden |
|
24 | from rhodecode.api import jsonrpc_method, JSONRPCError, JSONRPCForbidden | |
25 |
|
25 | |||
26 | from rhodecode.api.utils import ( |
|
26 | from rhodecode.api.utils import ( | |
27 | Optional, OAttr, has_superadmin_permission, get_user_or_error) |
|
27 | Optional, OAttr, has_superadmin_permission, get_user_or_error) | |
28 | from rhodecode.lib.utils import repo2db_mapper |
|
28 | from rhodecode.lib.utils import repo2db_mapper | |
29 | from rhodecode.model.db import UserIpMap |
|
29 | from rhodecode.model.db import UserIpMap | |
30 | from rhodecode.model.scm import ScmModel |
|
30 | from rhodecode.model.scm import ScmModel | |
31 |
|
31 | |||
32 | log = logging.getLogger(__name__) |
|
32 | log = logging.getLogger(__name__) | |
33 |
|
33 | |||
34 |
|
34 | |||
35 | @jsonrpc_method() |
|
35 | @jsonrpc_method() | |
36 | def get_server_info(request, apiuser): |
|
36 | def get_server_info(request, apiuser): | |
37 | """ |
|
37 | """ | |
38 | Returns the |RCE| server information. |
|
38 | Returns the |RCE| server information. | |
39 |
|
39 | |||
40 | This includes the running version of |RCE| and all installed |
|
40 | This includes the running version of |RCE| and all installed | |
41 | packages. This command takes the following options: |
|
41 | packages. This command takes the following options: | |
42 |
|
42 | |||
43 | :param apiuser: This is filled automatically from the |authtoken|. |
|
43 | :param apiuser: This is filled automatically from the |authtoken|. | |
44 | :type apiuser: AuthUser |
|
44 | :type apiuser: AuthUser | |
45 |
|
45 | |||
46 | Example output: |
|
46 | Example output: | |
47 |
|
47 | |||
48 | .. code-block:: bash |
|
48 | .. code-block:: bash | |
49 |
|
49 | |||
50 | id : <id_given_in_input> |
|
50 | id : <id_given_in_input> | |
51 | result : { |
|
51 | result : { | |
52 | 'modules': [<module name>,...] |
|
52 | 'modules': [<module name>,...] | |
53 | 'py_version': <python version>, |
|
53 | 'py_version': <python version>, | |
54 | 'platform': <platform type>, |
|
54 | 'platform': <platform type>, | |
55 | 'rhodecode_version': <rhodecode version> |
|
55 | 'rhodecode_version': <rhodecode version> | |
56 | } |
|
56 | } | |
57 | error : null |
|
57 | error : null | |
58 | """ |
|
58 | """ | |
59 |
|
59 | |||
60 | if not has_superadmin_permission(apiuser): |
|
60 | if not has_superadmin_permission(apiuser): | |
61 | raise JSONRPCForbidden() |
|
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 | @jsonrpc_method() |
|
72 | @jsonrpc_method() | |
67 | def get_ip(request, apiuser, userid=Optional(OAttr('apiuser'))): |
|
73 | def get_ip(request, apiuser, userid=Optional(OAttr('apiuser'))): | |
68 | """ |
|
74 | """ | |
69 | Displays the IP Address as seen from the |RCE| server. |
|
75 | Displays the IP Address as seen from the |RCE| server. | |
70 |
|
76 | |||
71 | * This command displays the IP Address, as well as all the defined IP |
|
77 | * This command displays the IP Address, as well as all the defined IP | |
72 | addresses for the specified user. If the ``userid`` is not set, the |
|
78 | addresses for the specified user. If the ``userid`` is not set, the | |
73 | data returned is for the user calling the method. |
|
79 | data returned is for the user calling the method. | |
74 |
|
80 | |||
75 | This command can only be run using an |authtoken| with admin rights to |
|
81 | This command can only be run using an |authtoken| with admin rights to | |
76 | the specified repository. |
|
82 | the specified repository. | |
77 |
|
83 | |||
78 | This command takes the following options: |
|
84 | This command takes the following options: | |
79 |
|
85 | |||
80 | :param apiuser: This is filled automatically from |authtoken|. |
|
86 | :param apiuser: This is filled automatically from |authtoken|. | |
81 | :type apiuser: AuthUser |
|
87 | :type apiuser: AuthUser | |
82 | :param userid: Sets the userid for which associated IP Address data |
|
88 | :param userid: Sets the userid for which associated IP Address data | |
83 | is returned. |
|
89 | is returned. | |
84 | :type userid: Optional(str or int) |
|
90 | :type userid: Optional(str or int) | |
85 |
|
91 | |||
86 | Example output: |
|
92 | Example output: | |
87 |
|
93 | |||
88 | .. code-block:: bash |
|
94 | .. code-block:: bash | |
89 |
|
95 | |||
90 | id : <id_given_in_input> |
|
96 | id : <id_given_in_input> | |
91 | result : { |
|
97 | result : { | |
92 | "server_ip_addr": "<ip_from_clien>", |
|
98 | "server_ip_addr": "<ip_from_clien>", | |
93 | "user_ips": [ |
|
99 | "user_ips": [ | |
94 | { |
|
100 | { | |
95 | "ip_addr": "<ip_with_mask>", |
|
101 | "ip_addr": "<ip_with_mask>", | |
96 | "ip_range": ["<start_ip>", "<end_ip>"], |
|
102 | "ip_range": ["<start_ip>", "<end_ip>"], | |
97 | }, |
|
103 | }, | |
98 | ... |
|
104 | ... | |
99 | ] |
|
105 | ] | |
100 | } |
|
106 | } | |
101 |
|
107 | |||
102 | """ |
|
108 | """ | |
103 | if not has_superadmin_permission(apiuser): |
|
109 | if not has_superadmin_permission(apiuser): | |
104 | raise JSONRPCForbidden() |
|
110 | raise JSONRPCForbidden() | |
105 |
|
111 | |||
106 | userid = Optional.extract(userid, evaluate_locals=locals()) |
|
112 | userid = Optional.extract(userid, evaluate_locals=locals()) | |
107 | userid = getattr(userid, 'user_id', userid) |
|
113 | userid = getattr(userid, 'user_id', userid) | |
108 |
|
114 | |||
109 | user = get_user_or_error(userid) |
|
115 | user = get_user_or_error(userid) | |
110 | ips = UserIpMap.query().filter(UserIpMap.user == user).all() |
|
116 | ips = UserIpMap.query().filter(UserIpMap.user == user).all() | |
111 | return { |
|
117 | return { | |
112 | 'server_ip_addr': request.rpc_ip_addr, |
|
118 | 'server_ip_addr': request.rpc_ip_addr, | |
113 | 'user_ips': ips |
|
119 | 'user_ips': ips | |
114 | } |
|
120 | } | |
115 |
|
121 | |||
116 |
|
122 | |||
117 | @jsonrpc_method() |
|
123 | @jsonrpc_method() | |
118 | def rescan_repos(request, apiuser, remove_obsolete=Optional(False)): |
|
124 | def rescan_repos(request, apiuser, remove_obsolete=Optional(False)): | |
119 | """ |
|
125 | """ | |
120 | Triggers a rescan of the specified repositories. |
|
126 | Triggers a rescan of the specified repositories. | |
121 |
|
127 | |||
122 | * If the ``remove_obsolete`` option is set, it also deletes repositories |
|
128 | * If the ``remove_obsolete`` option is set, it also deletes repositories | |
123 | that are found in the database but not on the file system, so called |
|
129 | that are found in the database but not on the file system, so called | |
124 | "clean zombies". |
|
130 | "clean zombies". | |
125 |
|
131 | |||
126 | This command can only be run using an |authtoken| with admin rights to |
|
132 | This command can only be run using an |authtoken| with admin rights to | |
127 | the specified repository. |
|
133 | the specified repository. | |
128 |
|
134 | |||
129 | This command takes the following options: |
|
135 | This command takes the following options: | |
130 |
|
136 | |||
131 | :param apiuser: This is filled automatically from the |authtoken|. |
|
137 | :param apiuser: This is filled automatically from the |authtoken|. | |
132 | :type apiuser: AuthUser |
|
138 | :type apiuser: AuthUser | |
133 | :param remove_obsolete: Deletes repositories from the database that |
|
139 | :param remove_obsolete: Deletes repositories from the database that | |
134 | are not found on the filesystem. |
|
140 | are not found on the filesystem. | |
135 | :type remove_obsolete: Optional(``True`` | ``False``) |
|
141 | :type remove_obsolete: Optional(``True`` | ``False``) | |
136 |
|
142 | |||
137 | Example output: |
|
143 | Example output: | |
138 |
|
144 | |||
139 | .. code-block:: bash |
|
145 | .. code-block:: bash | |
140 |
|
146 | |||
141 | id : <id_given_in_input> |
|
147 | id : <id_given_in_input> | |
142 | result : { |
|
148 | result : { | |
143 | 'added': [<added repository name>,...] |
|
149 | 'added': [<added repository name>,...] | |
144 | 'removed': [<removed repository name>,...] |
|
150 | 'removed': [<removed repository name>,...] | |
145 | } |
|
151 | } | |
146 | error : null |
|
152 | error : null | |
147 |
|
153 | |||
148 | Example error output: |
|
154 | Example error output: | |
149 |
|
155 | |||
150 | .. code-block:: bash |
|
156 | .. code-block:: bash | |
151 |
|
157 | |||
152 | id : <id_given_in_input> |
|
158 | id : <id_given_in_input> | |
153 | result : null |
|
159 | result : null | |
154 | error : { |
|
160 | error : { | |
155 | 'Error occurred during rescan repositories action' |
|
161 | 'Error occurred during rescan repositories action' | |
156 | } |
|
162 | } | |
157 |
|
163 | |||
158 | """ |
|
164 | """ | |
159 | if not has_superadmin_permission(apiuser): |
|
165 | if not has_superadmin_permission(apiuser): | |
160 | raise JSONRPCForbidden() |
|
166 | raise JSONRPCForbidden() | |
161 |
|
167 | |||
162 | try: |
|
168 | try: | |
163 | rm_obsolete = Optional.extract(remove_obsolete) |
|
169 | rm_obsolete = Optional.extract(remove_obsolete) | |
164 | added, removed = repo2db_mapper(ScmModel().repo_scan(), |
|
170 | added, removed = repo2db_mapper(ScmModel().repo_scan(), | |
165 | remove_obsolete=rm_obsolete) |
|
171 | remove_obsolete=rm_obsolete) | |
166 | return {'added': added, 'removed': removed} |
|
172 | return {'added': added, 'removed': removed} | |
167 | except Exception: |
|
173 | except Exception: | |
168 | log.exception('Failed to run repo rescann') |
|
174 | log.exception('Failed to run repo rescann') | |
169 | raise JSONRPCError( |
|
175 | raise JSONRPCError( | |
170 | 'Error occurred during rescan repositories action' |
|
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