Show More
@@ -1,163 +1,171 b'' | |||||
1 | # -*- coding: utf-8 -*- |
|
1 | # -*- coding: utf-8 -*- | |
2 |
|
2 | |||
3 | # Copyright (C) 2016-2018 RhodeCode GmbH |
|
3 | # Copyright (C) 2016-2018 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 logging |
|
21 | import logging | |
22 |
|
22 | |||
23 | import psutil |
|
23 | import psutil | |
24 | import signal |
|
24 | import signal | |
25 | from pyramid.view import view_config |
|
25 | from pyramid.view import view_config | |
26 |
|
26 | |||
27 | from rhodecode.apps._base import BaseAppView |
|
27 | from rhodecode.apps._base import BaseAppView | |
28 | from rhodecode.apps.admin.navigation import navigation_list |
|
28 | from rhodecode.apps.admin.navigation import navigation_list | |
29 | from rhodecode.lib.auth import ( |
|
29 | from rhodecode.lib.auth import ( | |
30 | LoginRequired, HasPermissionAllDecorator, CSRFRequired) |
|
30 | LoginRequired, HasPermissionAllDecorator, CSRFRequired) | |
31 | from rhodecode.lib.utils2 import safe_int, StrictAttributeDict |
|
31 | from rhodecode.lib.utils2 import safe_int, StrictAttributeDict | |
32 |
|
32 | |||
33 | log = logging.getLogger(__name__) |
|
33 | log = logging.getLogger(__name__) | |
34 |
|
34 | |||
35 |
|
35 | |||
36 | class AdminProcessManagementView(BaseAppView): |
|
36 | class AdminProcessManagementView(BaseAppView): | |
37 | def load_default_context(self): |
|
37 | def load_default_context(self): | |
38 | c = self._get_local_tmpl_context() |
|
38 | c = self._get_local_tmpl_context() | |
39 | return c |
|
39 | return c | |
40 |
|
40 | |||
41 | def _format_proc(self, proc, with_children=False): |
|
41 | def _format_proc(self, proc, with_children=False): | |
42 | try: |
|
42 | try: | |
43 | mem = proc.memory_info() |
|
43 | mem = proc.memory_info() | |
44 | proc_formatted = StrictAttributeDict({ |
|
44 | proc_formatted = StrictAttributeDict({ | |
45 | 'pid': proc.pid, |
|
45 | 'pid': proc.pid, | |
46 | 'name': proc.name(), |
|
46 | 'name': proc.name(), | |
47 | 'mem_rss': mem.rss, |
|
47 | 'mem_rss': mem.rss, | |
48 | 'mem_vms': mem.vms, |
|
48 | 'mem_vms': mem.vms, | |
49 | 'cpu_percent': proc.cpu_percent(), |
|
49 | 'cpu_percent': proc.cpu_percent(), | |
50 | 'create_time': proc.create_time(), |
|
50 | 'create_time': proc.create_time(), | |
51 | 'cmd': ' '.join(proc.cmdline()), |
|
51 | 'cmd': ' '.join(proc.cmdline()), | |
52 | }) |
|
52 | }) | |
53 |
|
53 | |||
54 | if with_children: |
|
54 | if with_children: | |
55 | proc_formatted.update({ |
|
55 | proc_formatted.update({ | |
56 | 'children': [self._format_proc(x) |
|
56 | 'children': [self._format_proc(x) | |
57 | for x in proc.children(recursive=True)] |
|
57 | for x in proc.children(recursive=True)] | |
58 | }) |
|
58 | }) | |
59 | except Exception: |
|
59 | except Exception: | |
60 | log.exception('Failed to load proc') |
|
60 | log.exception('Failed to load proc') | |
61 | proc_formatted = None |
|
61 | proc_formatted = None | |
62 | return proc_formatted |
|
62 | return proc_formatted | |
63 |
|
63 | |||
64 | def get_processes(self): |
|
64 | def get_processes(self): | |
65 | proc_list = [] |
|
65 | proc_list = [] | |
66 | for p in psutil.process_iter(): |
|
66 | for p in psutil.process_iter(): | |
67 | if 'gunicorn' in p.name(): |
|
67 | if 'gunicorn' in p.name(): | |
68 | proc = self._format_proc(p, with_children=True) |
|
68 | proc = self._format_proc(p, with_children=True) | |
69 | if proc: |
|
69 | if proc: | |
70 | proc_list.append(proc) |
|
70 | proc_list.append(proc) | |
71 |
|
71 | |||
72 | return proc_list |
|
72 | return proc_list | |
73 |
|
73 | |||
74 | @LoginRequired() |
|
74 | @LoginRequired() | |
75 | @HasPermissionAllDecorator('hg.admin') |
|
75 | @HasPermissionAllDecorator('hg.admin') | |
76 | @view_config( |
|
76 | @view_config( | |
77 | route_name='admin_settings_process_management', request_method='GET', |
|
77 | route_name='admin_settings_process_management', request_method='GET', | |
78 | renderer='rhodecode:templates/admin/settings/settings.mako') |
|
78 | renderer='rhodecode:templates/admin/settings/settings.mako') | |
79 | def process_management(self): |
|
79 | def process_management(self): | |
80 | _ = self.request.translate |
|
80 | _ = self.request.translate | |
81 | c = self.load_default_context() |
|
81 | c = self.load_default_context() | |
82 |
|
82 | |||
83 | c.active = 'process_management' |
|
83 | c.active = 'process_management' | |
84 | c.navlist = navigation_list(self.request) |
|
84 | c.navlist = navigation_list(self.request) | |
85 | c.gunicorn_processes = self.get_processes() |
|
85 | c.gunicorn_processes = self.get_processes() | |
86 | return self._get_template_context(c) |
|
86 | return self._get_template_context(c) | |
87 |
|
87 | |||
88 | @LoginRequired() |
|
88 | @LoginRequired() | |
89 | @HasPermissionAllDecorator('hg.admin') |
|
89 | @HasPermissionAllDecorator('hg.admin') | |
90 | @view_config( |
|
90 | @view_config( | |
91 | route_name='admin_settings_process_management_data', request_method='GET', |
|
91 | route_name='admin_settings_process_management_data', request_method='GET', | |
92 | renderer='rhodecode:templates/admin/settings/settings_process_management_data.mako') |
|
92 | renderer='rhodecode:templates/admin/settings/settings_process_management_data.mako') | |
93 | def process_management_data(self): |
|
93 | def process_management_data(self): | |
94 | _ = self.request.translate |
|
94 | _ = self.request.translate | |
95 | c = self.load_default_context() |
|
95 | c = self.load_default_context() | |
96 | c.gunicorn_processes = self.get_processes() |
|
96 | c.gunicorn_processes = self.get_processes() | |
97 | return self._get_template_context(c) |
|
97 | return self._get_template_context(c) | |
98 |
|
98 | |||
99 | @LoginRequired() |
|
99 | @LoginRequired() | |
100 | @HasPermissionAllDecorator('hg.admin') |
|
100 | @HasPermissionAllDecorator('hg.admin') | |
101 | @CSRFRequired() |
|
101 | @CSRFRequired() | |
102 | @view_config( |
|
102 | @view_config( | |
103 | route_name='admin_settings_process_management_signal', |
|
103 | route_name='admin_settings_process_management_signal', | |
104 | request_method='POST', renderer='json_ext') |
|
104 | request_method='POST', renderer='json_ext') | |
105 | def process_management_signal(self): |
|
105 | def process_management_signal(self): | |
106 | pids = self.request.json.get('pids', []) |
|
106 | pids = self.request.json.get('pids', []) | |
107 | result = [] |
|
107 | result = [] | |
|
108 | ||||
108 | def on_terminate(proc): |
|
109 | def on_terminate(proc): | |
109 | msg = "process `PID:{}` terminated with exit code {}".format( |
|
110 | msg = "process `PID:{}` terminated with exit code {}".format( | |
110 | proc.pid, proc.returncode) |
|
111 | proc.pid, proc.returncode or 0) | |
111 | result.append(msg) |
|
112 | result.append(msg) | |
112 |
|
113 | |||
113 | procs = [] |
|
114 | procs = [] | |
114 | for pid in pids: |
|
115 | for pid in pids: | |
115 | pid = safe_int(pid) |
|
116 | pid = safe_int(pid) | |
116 | if pid: |
|
117 | if pid: | |
117 | try: |
|
118 | try: | |
118 | proc = psutil.Process(pid) |
|
119 | proc = psutil.Process(pid) | |
119 | except psutil.NoSuchProcess: |
|
120 | except psutil.NoSuchProcess: | |
120 | continue |
|
121 | continue | |
121 |
|
122 | |||
122 | children = proc.children(recursive=True) |
|
123 | children = proc.children(recursive=True) | |
123 | if children: |
|
124 | if children: | |
124 |
|
|
125 | log.warning('Wont kill Master Process') | |
125 | else: |
|
126 | else: | |
126 | procs.append(proc) |
|
127 | procs.append(proc) | |
127 |
|
128 | |||
128 | for p in procs: |
|
129 | for p in procs: | |
129 |
|
|
130 | try: | |
|
131 | p.terminate() | |||
|
132 | except psutil.AccessDenied as e: | |||
|
133 | log.warning('Access denied: {}'.format(e)) | |||
|
134 | ||||
130 | gone, alive = psutil.wait_procs(procs, timeout=10, callback=on_terminate) |
|
135 | gone, alive = psutil.wait_procs(procs, timeout=10, callback=on_terminate) | |
131 | for p in alive: |
|
136 | for p in alive: | |
132 |
|
|
137 | try: | |
|
138 | p.kill() | |||
|
139 | except psutil.AccessDenied as e: | |||
|
140 | log.warning('Access denied: {}'.format(e)) | |||
133 |
|
141 | |||
134 | return {'result': result} |
|
142 | return {'result': result} | |
135 |
|
143 | |||
136 | @LoginRequired() |
|
144 | @LoginRequired() | |
137 | @HasPermissionAllDecorator('hg.admin') |
|
145 | @HasPermissionAllDecorator('hg.admin') | |
138 | @CSRFRequired() |
|
146 | @CSRFRequired() | |
139 | @view_config( |
|
147 | @view_config( | |
140 | route_name='admin_settings_process_management_master_signal', |
|
148 | route_name='admin_settings_process_management_master_signal', | |
141 | request_method='POST', renderer='json_ext') |
|
149 | request_method='POST', renderer='json_ext') | |
142 | def process_management_master_signal(self): |
|
150 | def process_management_master_signal(self): | |
143 | pid_data = self.request.json.get('pid_data', {}) |
|
151 | pid_data = self.request.json.get('pid_data', {}) | |
144 | pid = safe_int(pid_data['pid']) |
|
152 | pid = safe_int(pid_data['pid']) | |
145 | action = pid_data['action'] |
|
153 | action = pid_data['action'] | |
146 | if pid: |
|
154 | if pid: | |
147 | try: |
|
155 | try: | |
148 | proc = psutil.Process(pid) |
|
156 | proc = psutil.Process(pid) | |
149 | except psutil.NoSuchProcess: |
|
157 | except psutil.NoSuchProcess: | |
150 | return {'result': 'failure_no_such_process'} |
|
158 | return {'result': 'failure_no_such_process'} | |
151 |
|
159 | |||
152 | children = proc.children(recursive=True) |
|
160 | children = proc.children(recursive=True) | |
153 | if children: |
|
161 | if children: | |
154 | # master process |
|
162 | # master process | |
155 | if action == '+' and len(children) <= 20: |
|
163 | if action == '+' and len(children) <= 20: | |
156 | proc.send_signal(signal.SIGTTIN) |
|
164 | proc.send_signal(signal.SIGTTIN) | |
157 | elif action == '-' and len(children) >= 2: |
|
165 | elif action == '-' and len(children) >= 2: | |
158 | proc.send_signal(signal.SIGTTOU) |
|
166 | proc.send_signal(signal.SIGTTOU) | |
159 | else: |
|
167 | else: | |
160 | return {'result': 'failure_wrong_action'} |
|
168 | return {'result': 'failure_wrong_action'} | |
161 | return {'result': 'success'} |
|
169 | return {'result': 'success'} | |
162 |
|
170 | |||
163 | return {'result': 'failure_not_master'} |
|
171 | return {'result': 'failure_not_master'} |
General Comments 0
You need to be logged in to leave comments.
Login now