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