]>
Commit | Line | Data |
---|---|---|
11fdf7f2 TL |
1 | # -*- coding: utf-8 -*- |
2 | from __future__ import absolute_import | |
3 | ||
4 | import cherrypy | |
5 | ||
6 | from . import ApiController, RESTController | |
7 | from .. import mgr | |
8 | from ..security import Scope | |
9 | from ..services.ceph_service import CephService | |
10 | from ..exceptions import DashboardException | |
11 | ||
12 | ||
13 | @ApiController('/cluster_conf', Scope.CONFIG_OPT) | |
14 | class ClusterConfiguration(RESTController): | |
15 | ||
16 | def _append_config_option_values(self, options): | |
17 | """ | |
18 | Appends values from the config database (if available) to the given options | |
19 | :param options: list of config options | |
20 | :return: list of config options extended by their current values | |
21 | """ | |
22 | config_dump = CephService.send_command('mon', 'config dump') | |
23 | for config_dump_entry in config_dump: | |
24 | for i, elem in enumerate(options): | |
25 | if config_dump_entry['name'] == elem['name']: | |
26 | if 'value' not in elem: | |
27 | options[i]['value'] = [] | |
28 | options[i]['source'] = 'mon' | |
29 | ||
30 | options[i]['value'].append({'section': config_dump_entry['section'], | |
31 | 'value': config_dump_entry['value']}) | |
32 | return options | |
33 | ||
34 | def list(self): | |
35 | options = mgr.get('config_options')['options'] | |
36 | return self._append_config_option_values(options) | |
37 | ||
38 | def get(self, name): | |
39 | return self._get_config_option(name) | |
40 | ||
81eedcae TL |
41 | @RESTController.Collection('GET', query_params=['name']) |
42 | def filter(self, names=None): | |
43 | config_options = [] | |
44 | ||
45 | if names: | |
46 | for name in names.split(','): | |
47 | try: | |
48 | config_options.append(self._get_config_option(name)) | |
49 | except cherrypy.HTTPError: | |
50 | pass | |
51 | ||
52 | if not config_options: | |
53 | raise cherrypy.HTTPError(404, 'Config options `{}` not found'.format(names)) | |
54 | ||
55 | return config_options | |
56 | ||
11fdf7f2 TL |
57 | def create(self, name, value): |
58 | # Check if config option is updateable at runtime | |
59 | self._updateable_at_runtime([name]) | |
60 | ||
61 | # Update config option | |
9f95a23c | 62 | avail_sections = ['global', 'mon', 'mgr', 'osd', 'mds', 'client'] |
11fdf7f2 | 63 | |
9f95a23c | 64 | for section in avail_sections: |
11fdf7f2 | 65 | for entry in value: |
92f5a8d4 | 66 | if entry['value'] is None: |
11fdf7f2 TL |
67 | break |
68 | ||
69 | if entry['section'] == section: | |
70 | CephService.send_command('mon', 'config set', who=section, name=name, | |
71 | value=str(entry['value'])) | |
72 | break | |
73 | else: | |
74 | CephService.send_command('mon', 'config rm', who=section, name=name) | |
75 | ||
81eedcae TL |
76 | def delete(self, name, section): |
77 | return CephService.send_command('mon', 'config rm', who=section, name=name) | |
78 | ||
11fdf7f2 TL |
79 | def bulk_set(self, options): |
80 | self._updateable_at_runtime(options.keys()) | |
81 | ||
82 | for name, value in options.items(): | |
83 | CephService.send_command('mon', 'config set', who=value['section'], | |
84 | name=name, value=str(value['value'])) | |
85 | ||
86 | def _get_config_option(self, name): | |
87 | for option in mgr.get('config_options')['options']: | |
88 | if option['name'] == name: | |
89 | return self._append_config_option_values([option])[0] | |
90 | ||
91 | raise cherrypy.HTTPError(404) | |
92 | ||
93 | def _updateable_at_runtime(self, config_option_names): | |
94 | not_updateable = [] | |
95 | ||
96 | for name in config_option_names: | |
97 | config_option = self._get_config_option(name) | |
98 | if not config_option['can_update_at_runtime']: | |
99 | not_updateable.append(name) | |
100 | ||
101 | if not_updateable: | |
102 | raise DashboardException( | |
103 | msg='Config option {} is/are not updatable at runtime'.format( | |
104 | ', '.join(not_updateable)), | |
105 | code='config_option_not_updatable_at_runtime', | |
106 | component='cluster_configuration') |