1 from ceph
.deployment
.inventory
import Device
2 from ceph
.deployment
.service_spec
import ServiceSpecValidationError
, ServiceSpec
, PlacementSpec
5 from typing
import Optional
, List
, Dict
, Any
11 class DeviceSelection(object):
13 Used within :class:`ceph.deployment.drive_group.DriveGroupSpec` to specify the devices
14 used by the Drive Group.
16 Any attributes (even none) can be included in the device
17 specification structure.
20 _supported_filters
= [
21 "paths", "size", "vendor", "model", "rotational", "limit", "all"
25 paths
=None, # type: Optional[List[str]]
26 model
=None, # type: Optional[str]
27 size
=None, # type: Optional[str]
28 rotational
=None, # type: Optional[bool]
29 limit
=None, # type: Optional[int]
30 vendor
=None, # type: Optional[str]
31 all
=False, # type: bool
34 ephemeral drive group device specification
36 #: List of Device objects for devices paths.
37 self
.paths
= [] if paths
is None else [Device(path
) for path
in paths
] # type: List[Device]
39 #: A wildcard string. e.g: "SDD*" or "SanDisk SD8SN8U5"
42 #: Match on the VENDOR property of the drive
45 #: Size specification of format LOW:HIGH.
46 #: Can also take the the form :HIGH, LOW:
47 #: or an exact value (as ceph-volume inventory reports)
50 #: is the drive rotating or not
51 self
.rotational
= rotational
53 #: Limit the number of devices added to this Drive Group. Devices
54 #: are used from top to bottom in the output of ``ceph-volume inventory``
57 #: Matches all devices. Can only be used for data devices
64 props
= [self
.model
, self
.vendor
, self
.size
, self
.rotational
] # type: List[Any]
65 if self
.paths
and any(p
is not None for p
in props
):
66 raise DriveGroupValidationError(
67 'DeviceSelection: `paths` and other parameters are mutually exclusive')
68 is_empty
= not any(p
is not None and p
!= [] for p
in [self
.paths
] + props
)
69 if not self
.all
and is_empty
:
70 raise DriveGroupValidationError('DeviceSelection cannot be empty')
72 if self
.all
and not is_empty
:
73 raise DriveGroupValidationError(
74 'DeviceSelection `all` and other parameters are mutually exclusive. {}'.format(
78 def from_json(cls
, device_spec
):
79 # type: (dict) -> DeviceSelection
82 for applied_filter
in list(device_spec
.keys()):
83 if applied_filter
not in cls
._supported
_filters
:
84 raise DriveGroupValidationError(
85 "Filtering for <{}> is not supported".format(applied_filter
))
87 return cls(**device_spec
)
90 # type: () -> Dict[str, Any]
91 c
= self
.__dict
__.copy()
93 c
['paths'] = [p
.path
for p
in self
.paths
]
98 key
for key
in self
._supported
_filters
+ ['limit'] if getattr(self
, key
) is not None
100 if 'paths' in keys
and self
.paths
== []:
102 return "DeviceSelection({})".format(
103 ', '.join('{}={}'.format(key
, repr(getattr(self
, key
))) for key
in keys
)
106 def __eq__(self
, other
):
107 return repr(self
) == repr(other
)
110 class DriveGroupValidationError(ServiceSpecValidationError
):
112 Defining an exception here is a bit problematic, cause you cannot properly catch it,
113 if it was raised in a different mgr module.
116 def __init__(self
, msg
):
117 super(DriveGroupValidationError
, self
).__init
__('Failed to validate Drive Group: ' + msg
)
120 class DriveGroupSpec(ServiceSpec
):
122 Describe a drive group in the same form that ceph-volume
126 _supported_features
= [
127 "encrypted", "block_wal_size", "osds_per_device",
128 "db_slots", "wal_slots", "block_db_size", "placement", "service_id", "service_type",
129 "data_devices", "db_devices", "wal_devices", "journal_devices",
130 "data_directories", "osds_per_device", "objectstore", "osd_id_claims",
131 "journal_size", "unmanaged"
135 placement
=None, # type: Optional[PlacementSpec]
136 service_id
=None, # type: str
137 data_devices
=None, # type: Optional[DeviceSelection]
138 db_devices
=None, # type: Optional[DeviceSelection]
139 wal_devices
=None, # type: Optional[DeviceSelection]
140 journal_devices
=None, # type: Optional[DeviceSelection]
141 data_directories
=None, # type: Optional[List[str]]
142 osds_per_device
=None, # type: Optional[int]
143 objectstore
='bluestore', # type: str
144 encrypted
=False, # type: bool
145 db_slots
=None, # type: Optional[int]
146 wal_slots
=None, # type: Optional[int]
147 osd_id_claims
=None, # type: Optional[Dict[str, List[str]]]
148 block_db_size
=None, # type: Optional[int]
149 block_wal_size
=None, # type: Optional[int]
150 journal_size
=None, # type: Optional[int]
151 service_type
=None, # type: Optional[str]
152 unmanaged
=False, # type: bool
154 assert service_type
is None or service_type
== 'osd'
155 super(DriveGroupSpec
, self
).__init
__('osd', service_id
=service_id
,
159 #: A :class:`ceph.deployment.drive_group.DeviceSelection`
160 self
.data_devices
= data_devices
162 #: A :class:`ceph.deployment.drive_group.DeviceSelection`
163 self
.db_devices
= db_devices
165 #: A :class:`ceph.deployment.drive_group.DeviceSelection`
166 self
.wal_devices
= wal_devices
168 #: A :class:`ceph.deployment.drive_group.DeviceSelection`
169 self
.journal_devices
= journal_devices
171 #: Set (or override) the "bluestore_block_wal_size" value, in bytes
172 self
.block_wal_size
= block_wal_size
174 #: Set (or override) the "bluestore_block_db_size" value, in bytes
175 self
.block_db_size
= block_db_size
177 #: set journal_size is bytes
178 self
.journal_size
= journal_size
180 #: Number of osd daemons per "DATA" device.
181 #: To fully utilize nvme devices multiple osds are required.
182 self
.osds_per_device
= osds_per_device
184 #: A list of strings, containing paths which should back OSDs
185 self
.data_directories
= data_directories
187 #: ``filestore`` or ``bluestore``
188 self
.objectstore
= objectstore
190 #: ``true`` or ``false``
191 self
.encrypted
= encrypted
193 #: How many OSDs per DB device
194 self
.db_slots
= db_slots
196 #: How many OSDs per WAL device
197 self
.wal_slots
= wal_slots
199 #: Optional: mapping of host -> List of osd_ids that should be replaced
200 #: See :ref:`orchestrator-osd-replace`
201 self
.osd_id_claims
= osd_id_claims
or dict()
204 def _from_json_impl(cls
, json_drive_group
):
205 # type: (dict) -> DriveGroupSpec
207 Initialize 'Drive group' structure
209 :param json_drive_group: A valid json string with a Drive Group
212 # legacy json (pre Octopus)
213 if 'host_pattern' in json_drive_group
and 'placement' not in json_drive_group
:
214 json_drive_group
['placement'] = {'host_pattern': json_drive_group
['host_pattern']}
215 del json_drive_group
['host_pattern']
217 for applied_filter
in list(json_drive_group
.keys()):
218 if applied_filter
not in cls
._supported
_features
:
219 raise DriveGroupValidationError(
220 "Feature <{}> is not supported".format(applied_filter
))
222 for key
in ('block_wal_size', 'block_db_size', 'journal_size'):
223 if key
in json_drive_group
:
224 if isinstance(json_drive_group
[key
], six
.string_types
):
225 from ceph
.deployment
.drive_selection
import SizeMatcher
226 json_drive_group
[key
] = SizeMatcher
.str_to_byte(json_drive_group
[key
])
228 if 'placement' in json_drive_group
:
229 json_drive_group
['placement'] = PlacementSpec
.from_json(json_drive_group
['placement'])
232 args
= {k
: (DeviceSelection
.from_json(v
) if k
.endswith('_devices') else v
) for k
, v
in
233 json_drive_group
.items()}
235 raise DriveGroupValidationError("Didn't find Drivegroup specs")
236 return DriveGroupSpec(**args
)
237 except (KeyError, TypeError) as e
:
238 raise DriveGroupValidationError(str(e
))
242 super(DriveGroupSpec
, self
).validate()
244 if not isinstance(self
.placement
.host_pattern
, six
.string_types
):
245 raise DriveGroupValidationError('host_pattern must be of type string')
247 specs
= [self
.data_devices
, self
.db_devices
, self
.wal_devices
, self
.journal_devices
]
248 for s
in filter(None, specs
):
250 for s
in filter(None, [self
.db_devices
, self
.wal_devices
, self
.journal_devices
]):
252 raise DriveGroupValidationError("`all` is only allowed for data_devices")
254 if self
.objectstore
not in ('filestore', 'bluestore'):
255 raise DriveGroupValidationError("objectstore not in ('filestore', 'bluestore')")
257 if self
.block_wal_size
is not None and type(self
.block_wal_size
) != int:
258 raise DriveGroupValidationError('block_wal_size must be of type int')
259 if self
.block_db_size
is not None and type(self
.block_db_size
) != int:
260 raise DriveGroupValidationError('block_db_size must be of type int')
264 key
for key
in self
._supported
_features
if getattr(self
, key
) is not None
266 if 'encrypted' in keys
and not self
.encrypted
:
267 keys
.remove('encrypted')
268 if 'objectstore' in keys
and self
.objectstore
== 'bluestore':
269 keys
.remove('objectstore')
270 return "DriveGroupSpec(name={}->{})".format(
272 ', '.join('{}={}'.format(key
, repr(getattr(self
, key
))) for key
in keys
)
276 # type: () -> Dict[str, Any]
277 c
= self
.__dict
__.copy()
279 c
['placement'] = self
.placement
.to_json()
280 if self
.data_devices
:
281 c
['data_devices'] = self
.data_devices
.to_json()
283 c
['db_devices'] = self
.db_devices
.to_json()
285 c
['wal_devices'] = self
.wal_devices
.to_json()
286 c
['service_name'] = self
.service_name()
289 def __eq__(self
, other
):
290 return repr(self
) == repr(other
)