Show More
@@ -1,244 +1,245 b'' | |||||
1 | # -*- coding: utf-8 -*- |
|
1 | # -*- coding: utf-8 -*- | |
2 |
|
2 | |||
3 | # Copyright (C) 2011-2017 RhodeCode GmbH |
|
3 | # Copyright (C) 2011-2017 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.lib import system_info |
|
29 | from rhodecode.lib import system_info | |
30 | from rhodecode.lib import user_sessions |
|
30 | from rhodecode.lib import user_sessions | |
|
31 | from rhodecode.lib.utils2 import safe_int | |||
31 | from rhodecode.model.db import UserIpMap |
|
32 | from rhodecode.model.db import UserIpMap | |
32 | from rhodecode.model.scm import ScmModel |
|
33 | from rhodecode.model.scm import ScmModel | |
33 |
|
34 | |||
34 | log = logging.getLogger(__name__) |
|
35 | log = logging.getLogger(__name__) | |
35 |
|
36 | |||
36 |
|
37 | |||
37 | @jsonrpc_method() |
|
38 | @jsonrpc_method() | |
38 | def get_server_info(request, apiuser): |
|
39 | def get_server_info(request, apiuser): | |
39 | """ |
|
40 | """ | |
40 | Returns the |RCE| server information. |
|
41 | Returns the |RCE| server information. | |
41 |
|
42 | |||
42 | This includes the running version of |RCE| and all installed |
|
43 | This includes the running version of |RCE| and all installed | |
43 | packages. This command takes the following options: |
|
44 | packages. This command takes the following options: | |
44 |
|
45 | |||
45 | :param apiuser: This is filled automatically from the |authtoken|. |
|
46 | :param apiuser: This is filled automatically from the |authtoken|. | |
46 | :type apiuser: AuthUser |
|
47 | :type apiuser: AuthUser | |
47 |
|
48 | |||
48 | Example output: |
|
49 | Example output: | |
49 |
|
50 | |||
50 | .. code-block:: bash |
|
51 | .. code-block:: bash | |
51 |
|
52 | |||
52 | id : <id_given_in_input> |
|
53 | id : <id_given_in_input> | |
53 | result : { |
|
54 | result : { | |
54 | 'modules': [<module name>,...] |
|
55 | 'modules': [<module name>,...] | |
55 | 'py_version': <python version>, |
|
56 | 'py_version': <python version>, | |
56 | 'platform': <platform type>, |
|
57 | 'platform': <platform type>, | |
57 | 'rhodecode_version': <rhodecode version> |
|
58 | 'rhodecode_version': <rhodecode version> | |
58 | } |
|
59 | } | |
59 | error : null |
|
60 | error : null | |
60 | """ |
|
61 | """ | |
61 |
|
62 | |||
62 | if not has_superadmin_permission(apiuser): |
|
63 | if not has_superadmin_permission(apiuser): | |
63 | raise JSONRPCForbidden() |
|
64 | raise JSONRPCForbidden() | |
64 |
|
65 | |||
65 | server_info = ScmModel().get_server_info(request.environ) |
|
66 | server_info = ScmModel().get_server_info(request.environ) | |
66 | # rhodecode-index requires those |
|
67 | # rhodecode-index requires those | |
67 |
|
68 | |||
68 | server_info['index_storage'] = server_info['search']['value']['location'] |
|
69 | server_info['index_storage'] = server_info['search']['value']['location'] | |
69 | server_info['storage'] = server_info['storage']['value']['path'] |
|
70 | server_info['storage'] = server_info['storage']['value']['path'] | |
70 |
|
71 | |||
71 | return server_info |
|
72 | return server_info | |
72 |
|
73 | |||
73 |
|
74 | |||
74 | @jsonrpc_method() |
|
75 | @jsonrpc_method() | |
75 | def get_ip(request, apiuser, userid=Optional(OAttr('apiuser'))): |
|
76 | def get_ip(request, apiuser, userid=Optional(OAttr('apiuser'))): | |
76 | """ |
|
77 | """ | |
77 | Displays the IP Address as seen from the |RCE| server. |
|
78 | Displays the IP Address as seen from the |RCE| server. | |
78 |
|
79 | |||
79 | * This command displays the IP Address, as well as all the defined IP |
|
80 | * This command displays the IP Address, as well as all the defined IP | |
80 | addresses for the specified user. If the ``userid`` is not set, the |
|
81 | addresses for the specified user. If the ``userid`` is not set, the | |
81 | data returned is for the user calling the method. |
|
82 | data returned is for the user calling the method. | |
82 |
|
83 | |||
83 | This command can only be run using an |authtoken| with admin rights to |
|
84 | This command can only be run using an |authtoken| with admin rights to | |
84 | the specified repository. |
|
85 | the specified repository. | |
85 |
|
86 | |||
86 | This command takes the following options: |
|
87 | This command takes the following options: | |
87 |
|
88 | |||
88 | :param apiuser: This is filled automatically from |authtoken|. |
|
89 | :param apiuser: This is filled automatically from |authtoken|. | |
89 | :type apiuser: AuthUser |
|
90 | :type apiuser: AuthUser | |
90 | :param userid: Sets the userid for which associated IP Address data |
|
91 | :param userid: Sets the userid for which associated IP Address data | |
91 | is returned. |
|
92 | is returned. | |
92 | :type userid: Optional(str or int) |
|
93 | :type userid: Optional(str or int) | |
93 |
|
94 | |||
94 | Example output: |
|
95 | Example output: | |
95 |
|
96 | |||
96 | .. code-block:: bash |
|
97 | .. code-block:: bash | |
97 |
|
98 | |||
98 | id : <id_given_in_input> |
|
99 | id : <id_given_in_input> | |
99 | result : { |
|
100 | result : { | |
100 | "server_ip_addr": "<ip_from_clien>", |
|
101 | "server_ip_addr": "<ip_from_clien>", | |
101 | "user_ips": [ |
|
102 | "user_ips": [ | |
102 | { |
|
103 | { | |
103 | "ip_addr": "<ip_with_mask>", |
|
104 | "ip_addr": "<ip_with_mask>", | |
104 | "ip_range": ["<start_ip>", "<end_ip>"], |
|
105 | "ip_range": ["<start_ip>", "<end_ip>"], | |
105 | }, |
|
106 | }, | |
106 | ... |
|
107 | ... | |
107 | ] |
|
108 | ] | |
108 | } |
|
109 | } | |
109 |
|
110 | |||
110 | """ |
|
111 | """ | |
111 | if not has_superadmin_permission(apiuser): |
|
112 | if not has_superadmin_permission(apiuser): | |
112 | raise JSONRPCForbidden() |
|
113 | raise JSONRPCForbidden() | |
113 |
|
114 | |||
114 | userid = Optional.extract(userid, evaluate_locals=locals()) |
|
115 | userid = Optional.extract(userid, evaluate_locals=locals()) | |
115 | userid = getattr(userid, 'user_id', userid) |
|
116 | userid = getattr(userid, 'user_id', userid) | |
116 |
|
117 | |||
117 | user = get_user_or_error(userid) |
|
118 | user = get_user_or_error(userid) | |
118 | ips = UserIpMap.query().filter(UserIpMap.user == user).all() |
|
119 | ips = UserIpMap.query().filter(UserIpMap.user == user).all() | |
119 | return { |
|
120 | return { | |
120 | 'server_ip_addr': request.rpc_ip_addr, |
|
121 | 'server_ip_addr': request.rpc_ip_addr, | |
121 | 'user_ips': ips |
|
122 | 'user_ips': ips | |
122 | } |
|
123 | } | |
123 |
|
124 | |||
124 |
|
125 | |||
125 | @jsonrpc_method() |
|
126 | @jsonrpc_method() | |
126 | def rescan_repos(request, apiuser, remove_obsolete=Optional(False)): |
|
127 | def rescan_repos(request, apiuser, remove_obsolete=Optional(False)): | |
127 | """ |
|
128 | """ | |
128 | Triggers a rescan of the specified repositories. |
|
129 | Triggers a rescan of the specified repositories. | |
129 |
|
130 | |||
130 | * If the ``remove_obsolete`` option is set, it also deletes repositories |
|
131 | * If the ``remove_obsolete`` option is set, it also deletes repositories | |
131 | that are found in the database but not on the file system, so called |
|
132 | that are found in the database but not on the file system, so called | |
132 | "clean zombies". |
|
133 | "clean zombies". | |
133 |
|
134 | |||
134 | This command can only be run using an |authtoken| with admin rights to |
|
135 | This command can only be run using an |authtoken| with admin rights to | |
135 | the specified repository. |
|
136 | the specified repository. | |
136 |
|
137 | |||
137 | This command takes the following options: |
|
138 | This command takes the following options: | |
138 |
|
139 | |||
139 | :param apiuser: This is filled automatically from the |authtoken|. |
|
140 | :param apiuser: This is filled automatically from the |authtoken|. | |
140 | :type apiuser: AuthUser |
|
141 | :type apiuser: AuthUser | |
141 | :param remove_obsolete: Deletes repositories from the database that |
|
142 | :param remove_obsolete: Deletes repositories from the database that | |
142 | are not found on the filesystem. |
|
143 | are not found on the filesystem. | |
143 | :type remove_obsolete: Optional(``True`` | ``False``) |
|
144 | :type remove_obsolete: Optional(``True`` | ``False``) | |
144 |
|
145 | |||
145 | Example output: |
|
146 | Example output: | |
146 |
|
147 | |||
147 | .. code-block:: bash |
|
148 | .. code-block:: bash | |
148 |
|
149 | |||
149 | id : <id_given_in_input> |
|
150 | id : <id_given_in_input> | |
150 | result : { |
|
151 | result : { | |
151 | 'added': [<added repository name>,...] |
|
152 | 'added': [<added repository name>,...] | |
152 | 'removed': [<removed repository name>,...] |
|
153 | 'removed': [<removed repository name>,...] | |
153 | } |
|
154 | } | |
154 | error : null |
|
155 | error : null | |
155 |
|
156 | |||
156 | Example error output: |
|
157 | Example error output: | |
157 |
|
158 | |||
158 | .. code-block:: bash |
|
159 | .. code-block:: bash | |
159 |
|
160 | |||
160 | id : <id_given_in_input> |
|
161 | id : <id_given_in_input> | |
161 | result : null |
|
162 | result : null | |
162 | error : { |
|
163 | error : { | |
163 | 'Error occurred during rescan repositories action' |
|
164 | 'Error occurred during rescan repositories action' | |
164 | } |
|
165 | } | |
165 |
|
166 | |||
166 | """ |
|
167 | """ | |
167 | if not has_superadmin_permission(apiuser): |
|
168 | if not has_superadmin_permission(apiuser): | |
168 | raise JSONRPCForbidden() |
|
169 | raise JSONRPCForbidden() | |
169 |
|
170 | |||
170 | try: |
|
171 | try: | |
171 | rm_obsolete = Optional.extract(remove_obsolete) |
|
172 | rm_obsolete = Optional.extract(remove_obsolete) | |
172 | added, removed = repo2db_mapper(ScmModel().repo_scan(), |
|
173 | added, removed = repo2db_mapper(ScmModel().repo_scan(), | |
173 | remove_obsolete=rm_obsolete) |
|
174 | remove_obsolete=rm_obsolete) | |
174 | return {'added': added, 'removed': removed} |
|
175 | return {'added': added, 'removed': removed} | |
175 | except Exception: |
|
176 | except Exception: | |
176 | log.exception('Failed to run repo rescann') |
|
177 | log.exception('Failed to run repo rescann') | |
177 | raise JSONRPCError( |
|
178 | raise JSONRPCError( | |
178 | 'Error occurred during rescan repositories action' |
|
179 | 'Error occurred during rescan repositories action' | |
179 | ) |
|
180 | ) | |
180 |
|
181 | |||
181 |
|
182 | |||
182 | @jsonrpc_method() |
|
183 | @jsonrpc_method() | |
183 | def cleanup_sessions(request, apiuser, older_then=Optional(60)): |
|
184 | def cleanup_sessions(request, apiuser, older_then=Optional(60)): | |
184 | """ |
|
185 | """ | |
185 | Triggers a session cleanup action. |
|
186 | Triggers a session cleanup action. | |
186 |
|
187 | |||
187 | If the ``older_then`` option is set, only sessions that hasn't been |
|
188 | If the ``older_then`` option is set, only sessions that hasn't been | |
188 | accessed in the given number of days will be removed. |
|
189 | accessed in the given number of days will be removed. | |
189 |
|
190 | |||
190 | This command can only be run using an |authtoken| with admin rights to |
|
191 | This command can only be run using an |authtoken| with admin rights to | |
191 | the specified repository. |
|
192 | the specified repository. | |
192 |
|
193 | |||
193 | This command takes the following options: |
|
194 | This command takes the following options: | |
194 |
|
195 | |||
195 | :param apiuser: This is filled automatically from the |authtoken|. |
|
196 | :param apiuser: This is filled automatically from the |authtoken|. | |
196 | :type apiuser: AuthUser |
|
197 | :type apiuser: AuthUser | |
197 | :param older_then: Deletes session that hasn't been accessed |
|
198 | :param older_then: Deletes session that hasn't been accessed | |
198 | in given number of days. |
|
199 | in given number of days. | |
199 | :type older_then: Optional(int) |
|
200 | :type older_then: Optional(int) | |
200 |
|
201 | |||
201 | Example output: |
|
202 | Example output: | |
202 |
|
203 | |||
203 | .. code-block:: bash |
|
204 | .. code-block:: bash | |
204 |
|
205 | |||
205 | id : <id_given_in_input> |
|
206 | id : <id_given_in_input> | |
206 | result: { |
|
207 | result: { | |
207 | "backend": "<type of backend>", |
|
208 | "backend": "<type of backend>", | |
208 | "sessions_removed": <number_of_removed_sessions> |
|
209 | "sessions_removed": <number_of_removed_sessions> | |
209 | } |
|
210 | } | |
210 | error : null |
|
211 | error : null | |
211 |
|
212 | |||
212 | Example error output: |
|
213 | Example error output: | |
213 |
|
214 | |||
214 | .. code-block:: bash |
|
215 | .. code-block:: bash | |
215 |
|
216 | |||
216 | id : <id_given_in_input> |
|
217 | id : <id_given_in_input> | |
217 | result : null |
|
218 | result : null | |
218 | error : { |
|
219 | error : { | |
219 | 'Error occurred during session cleanup' |
|
220 | 'Error occurred during session cleanup' | |
220 | } |
|
221 | } | |
221 |
|
222 | |||
222 | """ |
|
223 | """ | |
223 | if not has_superadmin_permission(apiuser): |
|
224 | if not has_superadmin_permission(apiuser): | |
224 | raise JSONRPCForbidden() |
|
225 | raise JSONRPCForbidden() | |
225 |
|
226 | |||
226 | older_then = Optional.extract(older_then) |
|
227 | older_then = safe_int(Optional.extract(older_then)) or 60 | |
227 | older_than_seconds = 60 * 60 * 24 * older_then |
|
228 | older_than_seconds = 60 * 60 * 24 * older_then | |
228 |
|
229 | |||
229 | config = system_info.rhodecode_config().get_value()['value']['config'] |
|
230 | config = system_info.rhodecode_config().get_value()['value']['config'] | |
230 | session_model = user_sessions.get_session_handler( |
|
231 | session_model = user_sessions.get_session_handler( | |
231 | config.get('beaker.session.type', 'memory'))(config) |
|
232 | config.get('beaker.session.type', 'memory'))(config) | |
232 |
|
233 | |||
233 | backend = session_model.SESSION_TYPE |
|
234 | backend = session_model.SESSION_TYPE | |
234 | try: |
|
235 | try: | |
235 | cleaned = session_model.clean_sessions( |
|
236 | cleaned = session_model.clean_sessions( | |
236 | older_than_seconds=older_than_seconds) |
|
237 | older_than_seconds=older_than_seconds) | |
237 | return {'sessions_removed': cleaned, 'backend': backend} |
|
238 | return {'sessions_removed': cleaned, 'backend': backend} | |
238 | except user_sessions.CleanupCommand as msg: |
|
239 | except user_sessions.CleanupCommand as msg: | |
239 | return {'cleanup_command': msg.message, 'backend': backend} |
|
240 | return {'cleanup_command': msg.message, 'backend': backend} | |
240 | except Exception as e: |
|
241 | except Exception as e: | |
241 | log.exception('Failed session cleanup') |
|
242 | log.exception('Failed session cleanup') | |
242 | raise JSONRPCError( |
|
243 | raise JSONRPCError( | |
243 | 'Error occurred during session cleanup' |
|
244 | 'Error occurred during session cleanup' | |
244 | ) |
|
245 | ) |
General Comments 0
You need to be logged in to leave comments.
Login now