]> git.proxmox.com Git - ceph.git/blob - ceph/src/pybind/mgr/dashboard/controllers/mgr_modules.py
import new upstream nautilus stable release 14.2.8
[ceph.git] / ceph / src / pybind / mgr / dashboard / controllers / mgr_modules.py
1 # -*- coding: utf-8 -*-
2 from __future__ import absolute_import
3
4 from . import ApiController, RESTController
5 from .. import mgr
6 from ..security import Scope
7 from ..services.ceph_service import CephService
8 from ..services.exception import handle_send_command_error
9 from ..tools import find_object_in_list, str_to_bool
10
11
12 @ApiController('/mgr/module', Scope.CONFIG_OPT)
13 class MgrModules(RESTController):
14 ignore_modules = ['selftest']
15
16 def list(self):
17 """
18 Get the list of managed modules.
19 :return: A list of objects with the fields 'enabled', 'name' and 'options'.
20 :rtype: list
21 """
22 result = []
23 mgr_map = mgr.get('mgr_map')
24 always_on_modules = mgr_map['always_on_modules'][mgr.release_name]
25 for module_config in mgr_map['available_modules']:
26 module_name = module_config['name']
27 if module_name not in self.ignore_modules:
28 always_on = module_name in always_on_modules
29 enabled = module_name in mgr_map['modules'] or always_on
30 result.append({
31 'name': module_name,
32 'enabled': enabled,
33 'always_on': always_on,
34 'options': self._convert_module_options(
35 module_config['module_options'])
36 })
37 return result
38
39 def get(self, module_name):
40 """
41 Retrieve the values of the persistent configuration settings.
42 :param module_name: The name of the Ceph Mgr module.
43 :type module_name: str
44 :return: The values of the module options.
45 :rtype: dict
46 """
47 assert self._is_module_managed(module_name)
48 options = self._get_module_options(module_name)
49 result = {}
50 for name, option in options.items():
51 result[name] = mgr.get_module_option_ex(module_name, name,
52 option['default_value'])
53 return result
54
55 @RESTController.Resource('PUT')
56 def set(self, module_name, config):
57 """
58 Set the values of the persistent configuration settings.
59 :param module_name: The name of the Ceph Mgr module.
60 :type module_name: str
61 :param config: The values of the module options to be stored.
62 :type config: dict
63 """
64 assert self._is_module_managed(module_name)
65 options = self._get_module_options(module_name)
66 for name in options.keys():
67 if name in config:
68 mgr.set_module_option_ex(module_name, name, config[name])
69
70 @RESTController.Resource('POST')
71 @handle_send_command_error('mgr_modules')
72 def enable(self, module_name):
73 """
74 Enable the specified Ceph Mgr module.
75 :param module_name: The name of the Ceph Mgr module.
76 :type module_name: str
77 """
78 assert self._is_module_managed(module_name)
79 CephService.send_command(
80 'mon', 'mgr module enable', module=module_name)
81
82 @RESTController.Resource('POST')
83 @handle_send_command_error('mgr_modules')
84 def disable(self, module_name):
85 """
86 Disable the specified Ceph Mgr module.
87 :param module_name: The name of the Ceph Mgr module.
88 :type module_name: str
89 """
90 assert self._is_module_managed(module_name)
91 CephService.send_command(
92 'mon', 'mgr module disable', module=module_name)
93
94 @RESTController.Resource('GET')
95 def options(self, module_name):
96 """
97 Get the module options of the specified Ceph Mgr module.
98 :param module_name: The name of the Ceph Mgr module.
99 :type module_name: str
100 :return: The module options as list of dicts.
101 :rtype: list
102 """
103 assert self._is_module_managed(module_name)
104 return self._get_module_options(module_name)
105
106 def _is_module_managed(self, module_name):
107 """
108 Check if the specified Ceph Mgr module is managed by this service.
109 :param module_name: The name of the Ceph Mgr module.
110 :type module_name: str
111 :return: Returns ``true`` if the Ceph Mgr module is managed by
112 this service, otherwise ``false``.
113 :rtype: bool
114 """
115 if module_name in self.ignore_modules:
116 return False
117 mgr_map = mgr.get('mgr_map')
118 for module_config in mgr_map['available_modules']:
119 if module_name == module_config['name']:
120 return True
121 return False
122
123 def _get_module_config(self, module_name):
124 """
125 Helper function to get detailed module configuration.
126 :param module_name: The name of the Ceph Mgr module.
127 :type module_name: str
128 :return: The module information, e.g. module name, can run,
129 error string and available module options.
130 :rtype: dict or None
131 """
132 mgr_map = mgr.get('mgr_map')
133 return find_object_in_list('name', module_name,
134 mgr_map['available_modules'])
135
136 def _get_module_options(self, module_name):
137 """
138 Helper function to get the module options.
139 :param module_name: The name of the Ceph Mgr module.
140 :type module_name: str
141 :return: The module options.
142 :rtype: dict
143 """
144 options = self._get_module_config(module_name)['module_options']
145 return self._convert_module_options(options)
146
147 def _convert_module_options(self, options):
148 # Workaround a possible bug in the Ceph Mgr implementation.
149 # Various fields (e.g. default_value, min, max) are always
150 # returned as a string.
151 for option in options.values():
152 if option['type'] == 'str':
153 if option['default_value'] == 'None': # This is Python None
154 option['default_value'] = ''
155 elif option['type'] == 'bool':
156 if option['default_value'] == '':
157 option['default_value'] = False
158 else:
159 option['default_value'] = str_to_bool(
160 option['default_value'])
161 elif option['type'] == 'float':
162 for name in ['default_value', 'min', 'max']:
163 if option[name]: # Skip empty entries
164 option[name] = float(option[name])
165 elif option['type'] in ['uint', 'int', 'size', 'secs']:
166 for name in ['default_value', 'min', 'max']:
167 if option[name]: # Skip empty entries
168 option[name] = int(option[name])
169 return options