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