]>
Commit | Line | Data |
---|---|---|
11fdf7f2 TL |
1 | # -*- coding: utf-8 -*- |
2 | from __future__ import absolute_import | |
3 | ||
4 | import errno | |
5 | import inspect | |
6 | from six import add_metaclass | |
7 | ||
8 | from . import mgr | |
9 | ||
10 | ||
11 | class Options(object): | |
12 | """ | |
13 | If you need to store some configuration value please add the config option | |
14 | name as a class attribute to this class. | |
15 | ||
16 | Example:: | |
17 | ||
18 | GRAFANA_API_HOST = ('localhost', str) | |
19 | GRAFANA_API_PORT = (3000, int) | |
20 | """ | |
21 | ENABLE_BROWSABLE_API = (True, bool) | |
22 | REST_REQUESTS_TIMEOUT = (45, int) | |
23 | ||
24 | # API auditing | |
25 | AUDIT_API_ENABLED = (False, bool) | |
26 | AUDIT_API_LOG_PAYLOAD = (True, bool) | |
27 | ||
28 | # RGW settings | |
29 | RGW_API_HOST = ('', str) | |
30 | RGW_API_PORT = (80, int) | |
31 | RGW_API_ACCESS_KEY = ('', str) | |
32 | RGW_API_SECRET_KEY = ('', str) | |
33 | RGW_API_ADMIN_RESOURCE = ('admin', str) | |
34 | RGW_API_SCHEME = ('http', str) | |
35 | RGW_API_USER_ID = ('', str) | |
36 | RGW_API_SSL_VERIFY = (True, bool) | |
37 | ||
38 | # Grafana settings | |
39 | GRAFANA_API_URL = ('', str) | |
40 | GRAFANA_API_USERNAME = ('admin', str) | |
41 | GRAFANA_API_PASSWORD = ('admin', str) | |
81eedcae | 42 | GRAFANA_UPDATE_DASHBOARDS = (False, bool) |
11fdf7f2 TL |
43 | |
44 | # NFS Ganesha settings | |
45 | GANESHA_CLUSTERS_RADOS_POOL_NAMESPACE = ('', str) | |
46 | ||
47 | # Prometheus settings | |
494da23a | 48 | PROMETHEUS_API_HOST = ('', str) |
11fdf7f2 TL |
49 | ALERTMANAGER_API_HOST = ('', str) |
50 | ||
51 | # iSCSI management settings | |
52 | ISCSI_API_SSL_VERIFICATION = (True, bool) | |
53 | ||
54 | @staticmethod | |
55 | def has_default_value(name): | |
56 | return getattr(Settings, name, None) is None or \ | |
57 | getattr(Settings, name) == getattr(Options, name)[0] | |
58 | ||
59 | ||
60 | class SettingsMeta(type): | |
61 | def __getattr__(cls, attr): | |
62 | default, stype = getattr(Options, attr) | |
63 | if stype == bool and str(mgr.get_module_option( | |
64 | attr, | |
65 | default)).lower() == 'false': | |
66 | value = False | |
67 | else: | |
68 | value = stype(mgr.get_module_option(attr, default)) | |
69 | return value | |
70 | ||
71 | def __setattr__(cls, attr, value): | |
72 | if not attr.startswith('_') and hasattr(Options, attr): | |
73 | mgr.set_module_option(attr, str(value)) | |
74 | else: | |
75 | setattr(SettingsMeta, attr, value) | |
76 | ||
77 | def __delattr__(cls, attr): | |
78 | if not attr.startswith('_') and hasattr(Options, attr): | |
79 | mgr.set_module_option(attr, None) | |
80 | ||
81 | ||
82 | # pylint: disable=no-init | |
83 | @add_metaclass(SettingsMeta) | |
84 | class Settings(object): | |
85 | pass | |
86 | ||
87 | ||
88 | def _options_command_map(): | |
89 | def filter_attr(member): | |
90 | return not inspect.isroutine(member) | |
91 | ||
92 | cmd_map = {} | |
93 | for option, value in inspect.getmembers(Options, filter_attr): | |
94 | if option.startswith('_'): | |
95 | continue | |
96 | key_get = 'dashboard get-{}'.format(option.lower().replace('_', '-')) | |
97 | key_set = 'dashboard set-{}'.format(option.lower().replace('_', '-')) | |
98 | key_reset = 'dashboard reset-{}'.format(option.lower().replace('_', '-')) | |
99 | cmd_map[key_get] = {'name': option, 'type': None} | |
100 | cmd_map[key_set] = {'name': option, 'type': value[1]} | |
101 | cmd_map[key_reset] = {'name': option, 'type': None} | |
102 | return cmd_map | |
103 | ||
104 | ||
105 | _OPTIONS_COMMAND_MAP = _options_command_map() | |
106 | ||
107 | ||
108 | def options_command_list(): | |
109 | """ | |
110 | This function generates a list of ``get`` and ``set`` commands | |
111 | for each declared configuration option in class ``Options``. | |
112 | """ | |
113 | def py2ceph(pytype): | |
114 | if pytype == str: | |
115 | return 'CephString' | |
116 | elif pytype == int: | |
117 | return 'CephInt' | |
118 | return 'CephString' | |
119 | ||
120 | cmd_list = [] | |
121 | for cmd, opt in _OPTIONS_COMMAND_MAP.items(): | |
122 | if cmd.startswith('dashboard get'): | |
123 | cmd_list.append({ | |
124 | 'cmd': '{}'.format(cmd), | |
125 | 'desc': 'Get the {} option value'.format(opt['name']), | |
126 | 'perm': 'r' | |
127 | }) | |
128 | elif cmd.startswith('dashboard set'): | |
129 | cmd_list.append({ | |
130 | 'cmd': '{} name=value,type={}' | |
131 | .format(cmd, py2ceph(opt['type'])), | |
132 | 'desc': 'Set the {} option value'.format(opt['name']), | |
133 | 'perm': 'w' | |
134 | }) | |
135 | elif cmd.startswith('dashboard reset'): | |
136 | desc = 'Reset the {} option to its default value'.format( | |
137 | opt['name']) | |
138 | cmd_list.append({ | |
139 | 'cmd': '{}'.format(cmd), | |
140 | 'desc': desc, | |
141 | 'perm': 'w' | |
142 | }) | |
143 | ||
144 | return cmd_list | |
145 | ||
146 | ||
147 | def options_schema_list(): | |
148 | def filter_attr(member): | |
149 | return not inspect.isroutine(member) | |
150 | ||
151 | result = [] | |
152 | for option, value in inspect.getmembers(Options, filter_attr): | |
153 | if option.startswith('_'): | |
154 | continue | |
155 | result.append({'name': option, 'default': value[0]}) | |
156 | ||
157 | return result | |
158 | ||
159 | ||
160 | def handle_option_command(cmd): | |
161 | if cmd['prefix'] not in _OPTIONS_COMMAND_MAP: | |
162 | return -errno.ENOSYS, '', "Command not found '{}'".format(cmd['prefix']) | |
163 | ||
164 | opt = _OPTIONS_COMMAND_MAP[cmd['prefix']] | |
165 | ||
166 | if cmd['prefix'].startswith('dashboard reset'): | |
167 | delattr(Settings, opt['name']) | |
168 | return 0, 'Option {} reset to default value "{}"'.format( | |
169 | opt['name'], getattr(Settings, opt['name'])), '' | |
170 | elif cmd['prefix'].startswith('dashboard get'): | |
171 | return 0, str(getattr(Settings, opt['name'])), '' | |
172 | elif cmd['prefix'].startswith('dashboard set'): | |
173 | value = opt['type'](cmd['value']) | |
174 | if opt['type'] == bool and cmd['value'].lower() == 'false': | |
175 | value = False | |
176 | setattr(Settings, opt['name'], value) | |
177 | return 0, 'Option {} updated'.format(opt['name']), '' |