]> git.proxmox.com Git - ceph.git/blob - ceph/src/pybind/mgr/dashboard/plugins/debug.py
import quincy beta 17.1.0
[ceph.git] / ceph / src / pybind / mgr / dashboard / plugins / debug.py
1 # -*- coding: utf-8 -*-
2
3
4 import json
5 from enum import Enum
6 from typing import no_type_check
7
8 from . import PLUGIN_MANAGER as PM
9 from . import interfaces as I # noqa: E741,N812
10 from .plugin import SimplePlugin as SP
11
12
13 class Actions(Enum):
14 ENABLE = 'enable'
15 DISABLE = 'disable'
16 STATUS = 'status'
17
18
19 @PM.add_plugin # pylint: disable=too-many-ancestors
20 class Debug(SP, I.CanCherrypy, I.ConfiguresCherryPy, # pylint: disable=too-many-ancestors
21 I.Setupable, I.ConfigNotify):
22 NAME = 'debug'
23
24 OPTIONS = [
25 SP.Option(
26 name=NAME,
27 default=False,
28 type='bool',
29 desc="Enable/disable debug options"
30 )
31 ]
32
33 @no_type_check # https://github.com/python/mypy/issues/7806
34 def _refresh_health_checks(self):
35 debug = self.get_option(self.NAME)
36 if debug:
37 self.mgr.health_checks.update({'DASHBOARD_DEBUG': {
38 'severity': 'warning',
39 'summary': 'Dashboard debug mode is enabled',
40 'detail': [
41 'Please disable debug mode in production environments using '
42 '"ceph dashboard {} {}"'.format(self.NAME, Actions.DISABLE.value)
43 ]
44 }})
45 else:
46 self.mgr.health_checks.pop('DASHBOARD_DEBUG', None)
47 self.mgr.refresh_health_checks()
48
49 @PM.add_hook
50 def setup(self):
51 self._refresh_health_checks()
52
53 @no_type_check
54 def handler(self, action: Actions):
55 '''
56 Control and report debug status in Ceph-Dashboard
57 '''
58 ret = 0
59 msg = ''
60 if action in [Actions.ENABLE, Actions.DISABLE]:
61 self.set_option(self.NAME, action == Actions.ENABLE)
62 self.mgr.update_cherrypy_config({})
63 self._refresh_health_checks()
64 else:
65 debug = self.get_option(self.NAME)
66 msg = "Debug: '{}'".format('enabled' if debug else 'disabled')
67 return ret, msg, None
68
69 COMMANDS = [
70 SP.Command(
71 prefix="dashboard {name}".format(name=NAME),
72 handler=handler
73 )
74 ]
75
76 def custom_error_response(self, status, message, traceback, version):
77 self.response.headers['Content-Type'] = 'application/json'
78 error_response = dict(status=status, detail=message, request_id=str(self.request.unique_id))
79
80 if self.get_option(self.NAME):
81 error_response.update(dict(traceback=traceback, version=version))
82
83 return json.dumps(error_response)
84
85 @PM.add_hook
86 def configure_cherrypy(self, config):
87 config.update({
88 'environment': 'test_suite' if self.get_option(self.NAME) else 'production',
89 'error_page.default': self.custom_error_response,
90 })
91
92 @PM.add_hook
93 def config_notify(self):
94 self._refresh_health_checks()