]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | from rest.app.manager.request_factory import RequestFactory |
2 | from rest.app.types import OsdMap, OSD_IMPLEMENTED_COMMANDS, OSD_FLAGS | |
3 | from rest.app.manager.user_request import OsdMapModifyingRequest, RadosRequest | |
4 | ||
5 | from rest.module import global_instance as rest_plugin | |
6 | ||
7 | class OsdRequestFactory(RequestFactory): | |
8 | """ | |
9 | This class converts CRUD operations to UserRequest objects, and | |
10 | exposes non-crud functions to return the appropriate UserRequest. | |
11 | """ | |
12 | def update(self, osd_id, attributes): | |
13 | commands = [] | |
14 | ||
15 | osd_map = rest_plugin().get_sync_object(OsdMap) | |
16 | ||
17 | # in/out/down take a vector of strings called 'ids', while 'reweight' takes a single integer | |
18 | ||
19 | if 'in' in attributes and bool(attributes['in']) != bool(osd_map.osds_by_id[osd_id]['in']): | |
20 | if attributes['in']: | |
21 | commands.append(('osd in', {'ids': [attributes['id'].__str__()]})) | |
22 | else: | |
23 | commands.append(('osd out', {'ids': [attributes['id'].__str__()]})) | |
24 | ||
25 | if 'up' in attributes and bool(attributes['up']) != bool(osd_map.osds_by_id[osd_id]['up']): | |
26 | if not attributes['up']: | |
27 | commands.append(('osd down', {'ids': [attributes['id'].__str__()]})) | |
28 | else: | |
29 | raise RuntimeError("It is not valid to set a down OSD to be up") | |
30 | ||
31 | if 'reweight' in attributes: | |
32 | if attributes['reweight'] != float(osd_map.osd_tree_node_by_id[osd_id]['reweight']): | |
33 | commands.append(('osd reweight', {'id': osd_id, 'weight': attributes['reweight']})) | |
34 | ||
35 | if not commands: | |
36 | # Returning None indicates no-op | |
37 | return None | |
38 | ||
39 | msg_attrs = attributes.copy() | |
40 | del msg_attrs['id'] | |
41 | ||
42 | if msg_attrs.keys() == ['in']: | |
43 | message = "Marking osd.{id} {state}".format( | |
44 | id=osd_id, state=("in" if msg_attrs['in'] else "out")) | |
45 | elif msg_attrs.keys() == ['up']: | |
46 | message = "Marking osd.{id} down".format( | |
47 | id=osd_id) | |
48 | elif msg_attrs.keys() == ['reweight']: | |
49 | message = "Re-weighting osd.{id} to {pct}%".format( | |
50 | id=osd_id, pct="{0:.1f}".format(msg_attrs['reweight'] * 100.0)) | |
51 | else: | |
52 | message = "Modifying osd.{id} ({attrs})".format( | |
53 | id=osd_id, attrs=", ".join( | |
54 | "%s=%s" % (k, v) for k, v in msg_attrs.items())) | |
55 | ||
56 | return OsdMapModifyingRequest(message, commands) | |
57 | ||
58 | def scrub(self, osd_id): | |
59 | return RadosRequest( | |
60 | "Initiating scrub on osd.{id}".format(id=osd_id), | |
61 | [('osd scrub', {'who': str(osd_id)})]) | |
62 | ||
63 | def deep_scrub(self, osd_id): | |
64 | return RadosRequest( | |
65 | "Initiating deep-scrub on osd.{id}".format(id=osd_id), | |
66 | [('osd deep-scrub', {'who': str(osd_id)})]) | |
67 | ||
68 | def repair(self, osd_id): | |
69 | return RadosRequest( | |
70 | "Initiating repair on osd.{id}".format(id=osd_id), | |
71 | [('osd repair', {'who': str(osd_id)})]) | |
72 | ||
73 | def get_valid_commands(self, osds): | |
74 | """ | |
75 | For each OSD in osds list valid commands | |
76 | """ | |
77 | ret_val = {} | |
78 | osd_map = rest_plugin().get_sync_object(OsdMap) | |
79 | for osd_id in osds: | |
80 | if osd_map.osds_by_id[osd_id]['up']: | |
81 | ret_val[osd_id] = {'valid_commands': OSD_IMPLEMENTED_COMMANDS} | |
82 | else: | |
83 | ret_val[osd_id] = {'valid_commands': []} | |
84 | ||
85 | return ret_val | |
86 | ||
87 | def _commands_to_set_flags(self, osd_map, attributes): | |
88 | commands = [] | |
89 | ||
90 | flags_not_implemented = set(attributes.keys()) - set(OSD_FLAGS) | |
91 | if flags_not_implemented: | |
92 | raise RuntimeError("%s not valid to set/unset" % list(flags_not_implemented)) | |
93 | ||
94 | flags_to_set = set(k for k, v in attributes.iteritems() if v) | |
95 | flags_to_unset = set(k for k, v in attributes.iteritems() if not v) | |
96 | flags_that_are_set = set(k for k, v in osd_map.flags.iteritems() if v) | |
97 | ||
98 | for x in flags_to_set - flags_that_are_set: | |
99 | commands.append(('osd set', {'key': x})) | |
100 | ||
101 | for x in flags_that_are_set & flags_to_unset: | |
102 | commands.append(('osd unset', {'key': x})) | |
103 | ||
104 | return commands | |
105 | ||
106 | def update_config(self, _, attributes): | |
107 | ||
108 | osd_map = rest_plugin().get_sync_object(OsdMap) | |
109 | ||
110 | commands = self._commands_to_set_flags(osd_map, attributes) | |
111 | ||
112 | if commands: | |
113 | return OsdMapModifyingRequest( | |
114 | "Modifying OSD config ({attrs})".format( | |
115 | attrs=", ".join("%s=%s" % (k, v) for k, v in attributes.items()) | |
116 | ), commands) | |
117 | ||
118 | else: | |
119 | return None |