]>
git.proxmox.com Git - ceph.git/blob - ceph/src/pybind/mgr/rest/app/manager/osd_request_factory.py
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
5 from rest
.module
import global_instance
as rest_plugin
7 class OsdRequestFactory(RequestFactory
):
9 This class converts CRUD operations to UserRequest objects, and
10 exposes non-crud functions to return the appropriate UserRequest.
12 def update(self
, osd_id
, attributes
):
15 osd_map
= rest_plugin().get_sync_object(OsdMap
)
17 # in/out/down take a vector of strings called 'ids', while 'reweight' takes a single integer
19 if 'in' in attributes
and bool(attributes
['in']) != bool(osd_map
.osds_by_id
[osd_id
]['in']):
21 commands
.append(('osd in', {'ids': [attributes
['id'].__str
__()]}))
23 commands
.append(('osd out', {'ids': [attributes
['id'].__str
__()]}))
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
__()]}))
29 raise RuntimeError("It is not valid to set a down OSD to be up")
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']}))
36 # Returning None indicates no-op
39 msg_attrs
= attributes
.copy()
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(
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))
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()))
56 return OsdMapModifyingRequest(message
, commands
)
58 def scrub(self
, osd_id
):
60 "Initiating scrub on osd.{id}".format(id=osd_id
),
61 [('osd scrub', {'who': str(osd_id
)})])
63 def deep_scrub(self
, osd_id
):
65 "Initiating deep-scrub on osd.{id}".format(id=osd_id
),
66 [('osd deep-scrub', {'who': str(osd_id
)})])
68 def repair(self
, osd_id
):
70 "Initiating repair on osd.{id}".format(id=osd_id
),
71 [('osd repair', {'who': str(osd_id
)})])
73 def get_valid_commands(self
, osds
):
75 For each OSD in osds list valid commands
78 osd_map
= rest_plugin().get_sync_object(OsdMap
)
80 if osd_map
.osds_by_id
[osd_id
]['up']:
81 ret_val
[osd_id
] = {'valid_commands': OSD_IMPLEMENTED_COMMANDS
}
83 ret_val
[osd_id
] = {'valid_commands': []}
87 def _commands_to_set_flags(self
, osd_map
, attributes
):
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
))
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
)
98 for x
in flags_to_set
- flags_that_are_set
:
99 commands
.append(('osd set', {'key': x
}))
101 for x
in flags_that_are_set
& flags_to_unset
:
102 commands
.append(('osd unset', {'key': x
}))
106 def update_config(self
, _
, attributes
):
108 osd_map
= rest_plugin().get_sync_object(OsdMap
)
110 commands
= self
._commands
_to
_set
_flags
(osd_map
, attributes
)
113 return OsdMapModifyingRequest(
114 "Modifying OSD config ({attrs})".format(
115 attrs
=", ".join("%s=%s" % (k
, v
) for k
, v
in attributes
.items())