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