]> git.proxmox.com Git - ceph.git/blame - ceph/src/python-common/ceph/deployment/drive_group.py
import ceph quincy 17.2.4
[ceph.git] / ceph / src / python-common / ceph / deployment / drive_group.py
CommitLineData
20effc67 1import enum
f6b5b4d7
TL
2import yaml
3
9f95a23c 4from ceph.deployment.inventory import Device
2a845540 5from ceph.deployment.service_spec import ServiceSpec, PlacementSpec, CustomConfig
f67539c2 6from ceph.deployment.hostspec import SpecValidationError
9f95a23c
TL
7
8try:
f91f0fd5 9 from typing import Optional, List, Dict, Any, Union
9f95a23c
TL
10except ImportError:
11 pass
9f95a23c
TL
12
13
20effc67
TL
14class OSDMethod(str, enum.Enum):
15 raw = 'raw'
16 lvm = 'lvm'
17
18
9f95a23c
TL
19class DeviceSelection(object):
20 """
21 Used within :class:`ceph.deployment.drive_group.DriveGroupSpec` to specify the devices
22 used by the Drive Group.
23
24 Any attributes (even none) can be included in the device
25 specification structure.
26 """
27
28 _supported_filters = [
29 "paths", "size", "vendor", "model", "rotational", "limit", "all"
30 ]
31
32 def __init__(self,
33 paths=None, # type: Optional[List[str]]
34 model=None, # type: Optional[str]
35 size=None, # type: Optional[str]
36 rotational=None, # type: Optional[bool]
37 limit=None, # type: Optional[int]
38 vendor=None, # type: Optional[str]
39 all=False, # type: bool
40 ):
41 """
42 ephemeral drive group device specification
43 """
44 #: List of Device objects for devices paths.
45 self.paths = [] if paths is None else [Device(path) for path in paths] # type: List[Device]
46
47 #: A wildcard string. e.g: "SDD*" or "SanDisk SD8SN8U5"
48 self.model = model
49
50 #: Match on the VENDOR property of the drive
51 self.vendor = vendor
52
53 #: Size specification of format LOW:HIGH.
54 #: Can also take the the form :HIGH, LOW:
55 #: or an exact value (as ceph-volume inventory reports)
f6b5b4d7 56 self.size: Optional[str] = size
9f95a23c
TL
57
58 #: is the drive rotating or not
59 self.rotational = rotational
60
61 #: Limit the number of devices added to this Drive Group. Devices
62 #: are used from top to bottom in the output of ``ceph-volume inventory``
63 self.limit = limit
64
65 #: Matches all devices. Can only be used for data devices
66 self.all = all
67
20effc67 68 def validate(self, name: str) -> None:
9f95a23c
TL
69 props = [self.model, self.vendor, self.size, self.rotational] # type: List[Any]
70 if self.paths and any(p is not None for p in props):
71 raise DriveGroupValidationError(
20effc67
TL
72 name,
73 'device selection: `paths` and other parameters are mutually exclusive')
9f95a23c
TL
74 is_empty = not any(p is not None and p != [] for p in [self.paths] + props)
75 if not self.all and is_empty:
20effc67 76 raise DriveGroupValidationError(name, 'device selection cannot be empty')
9f95a23c
TL
77
78 if self.all and not is_empty:
79 raise DriveGroupValidationError(
20effc67
TL
80 name,
81 'device selection: `all` and other parameters are mutually exclusive. {}'.format(
9f95a23c
TL
82 repr(self)))
83
84 @classmethod
85 def from_json(cls, device_spec):
f6b5b4d7 86 # type: (dict) -> Optional[DeviceSelection]
9f95a23c 87 if not device_spec:
20effc67 88 return None
9f95a23c
TL
89 for applied_filter in list(device_spec.keys()):
90 if applied_filter not in cls._supported_filters:
20effc67 91 raise KeyError(applied_filter)
9f95a23c
TL
92
93 return cls(**device_spec)
94
95 def to_json(self):
96 # type: () -> Dict[str, Any]
f6b5b4d7 97 ret: Dict[str, Any] = {}
9f95a23c 98 if self.paths:
f6b5b4d7
TL
99 ret['paths'] = [p.path for p in self.paths]
100 if self.model:
101 ret['model'] = self.model
102 if self.vendor:
103 ret['vendor'] = self.vendor
104 if self.size:
105 ret['size'] = self.size
adb31ebb 106 if self.rotational is not None:
f6b5b4d7
TL
107 ret['rotational'] = self.rotational
108 if self.limit:
109 ret['limit'] = self.limit
110 if self.all:
111 ret['all'] = self.all
112
113 return ret
9f95a23c 114
f67539c2 115 def __repr__(self) -> str:
9f95a23c
TL
116 keys = [
117 key for key in self._supported_filters + ['limit'] if getattr(self, key) is not None
118 ]
119 if 'paths' in keys and self.paths == []:
120 keys.remove('paths')
121 return "DeviceSelection({})".format(
122 ', '.join('{}={}'.format(key, repr(getattr(self, key))) for key in keys)
123 )
124
f67539c2 125 def __eq__(self, other: Any) -> bool:
9f95a23c
TL
126 return repr(self) == repr(other)
127
128
f67539c2 129class DriveGroupValidationError(SpecValidationError):
9f95a23c
TL
130 """
131 Defining an exception here is a bit problematic, cause you cannot properly catch it,
132 if it was raised in a different mgr module.
133 """
134
20effc67
TL
135 def __init__(self, name: Optional[str], msg: str):
136 name = name or "<unnamed>"
137 super(DriveGroupValidationError, self).__init__(
138 f'Failed to validate OSD spec "{name}": {msg}')
9f95a23c
TL
139
140
141class DriveGroupSpec(ServiceSpec):
142 """
143 Describe a drive group in the same form that ceph-volume
144 understands.
145 """
146
147 _supported_features = [
148 "encrypted", "block_wal_size", "osds_per_device",
149 "db_slots", "wal_slots", "block_db_size", "placement", "service_id", "service_type",
150 "data_devices", "db_devices", "wal_devices", "journal_devices",
151 "data_directories", "osds_per_device", "objectstore", "osd_id_claims",
33c7a0ef 152 "journal_size", "unmanaged", "filter_logic", "preview_only", "extra_container_args",
2a845540 153 "data_allocate_fraction", "method", "crush_device_class", "config",
9f95a23c
TL
154 ]
155
156 def __init__(self,
157 placement=None, # type: Optional[PlacementSpec]
f67539c2 158 service_id=None, # type: Optional[str]
9f95a23c
TL
159 data_devices=None, # type: Optional[DeviceSelection]
160 db_devices=None, # type: Optional[DeviceSelection]
161 wal_devices=None, # type: Optional[DeviceSelection]
162 journal_devices=None, # type: Optional[DeviceSelection]
163 data_directories=None, # type: Optional[List[str]]
164 osds_per_device=None, # type: Optional[int]
165 objectstore='bluestore', # type: str
166 encrypted=False, # type: bool
167 db_slots=None, # type: Optional[int]
168 wal_slots=None, # type: Optional[int]
1911f103 169 osd_id_claims=None, # type: Optional[Dict[str, List[str]]]
f91f0fd5
TL
170 block_db_size=None, # type: Union[int, str, None]
171 block_wal_size=None, # type: Union[int, str, None]
172 journal_size=None, # type: Union[int, str, None]
9f95a23c 173 service_type=None, # type: Optional[str]
1911f103 174 unmanaged=False, # type: bool
f6b5b4d7
TL
175 filter_logic='AND', # type: str
176 preview_only=False, # type: bool
33c7a0ef 177 extra_container_args=None, # type: Optional[List[str]]
20effc67
TL
178 data_allocate_fraction=None, # type: Optional[float]
179 method=None, # type: Optional[OSDMethod]
2a845540
TL
180 crush_device_class=None, # type: Optional[str]
181 config=None, # type: Optional[Dict[str, str]]
182 custom_configs=None, # type: Optional[List[CustomConfig]]
9f95a23c
TL
183 ):
184 assert service_type is None or service_type == 'osd'
1911f103
TL
185 super(DriveGroupSpec, self).__init__('osd', service_id=service_id,
186 placement=placement,
2a845540 187 config=config,
f6b5b4d7 188 unmanaged=unmanaged,
33c7a0ef 189 preview_only=preview_only,
2a845540
TL
190 extra_container_args=extra_container_args,
191 custom_configs=custom_configs)
9f95a23c
TL
192
193 #: A :class:`ceph.deployment.drive_group.DeviceSelection`
194 self.data_devices = data_devices
195
196 #: A :class:`ceph.deployment.drive_group.DeviceSelection`
197 self.db_devices = db_devices
198
199 #: A :class:`ceph.deployment.drive_group.DeviceSelection`
200 self.wal_devices = wal_devices
201
202 #: A :class:`ceph.deployment.drive_group.DeviceSelection`
203 self.journal_devices = journal_devices
204
205 #: Set (or override) the "bluestore_block_wal_size" value, in bytes
f91f0fd5 206 self.block_wal_size: Union[int, str, None] = block_wal_size
9f95a23c
TL
207
208 #: Set (or override) the "bluestore_block_db_size" value, in bytes
f91f0fd5 209 self.block_db_size: Union[int, str, None] = block_db_size
9f95a23c 210
f6b5b4d7 211 #: set journal_size in bytes
f91f0fd5 212 self.journal_size: Union[int, str, None] = journal_size
9f95a23c
TL
213
214 #: Number of osd daemons per "DATA" device.
215 #: To fully utilize nvme devices multiple osds are required.
33c7a0ef 216 #: Can be used to split dual-actuator devices across 2 OSDs, by setting the option to 2.
9f95a23c
TL
217 self.osds_per_device = osds_per_device
218
219 #: A list of strings, containing paths which should back OSDs
220 self.data_directories = data_directories
221
222 #: ``filestore`` or ``bluestore``
223 self.objectstore = objectstore
224
225 #: ``true`` or ``false``
226 self.encrypted = encrypted
227
228 #: How many OSDs per DB device
229 self.db_slots = db_slots
230
231 #: How many OSDs per WAL device
232 self.wal_slots = wal_slots
233
1911f103
TL
234 #: Optional: mapping of host -> List of osd_ids that should be replaced
235 #: See :ref:`orchestrator-osd-replace`
236 self.osd_id_claims = osd_id_claims or dict()
9f95a23c 237
f6b5b4d7
TL
238 #: The logic gate we use to match disks with filters.
239 #: defaults to 'AND'
240 self.filter_logic = filter_logic.upper()
241
242 #: If this should be treated as a 'preview' spec
243 self.preview_only = preview_only
244
20effc67
TL
245 #: Allocate a fraction of the data device (0,1.0]
246 self.data_allocate_fraction = data_allocate_fraction
247
248 self.method = method
249
2a845540
TL
250 #: Crush device class to assign to OSDs
251 self.crush_device_class = crush_device_class
252
9f95a23c
TL
253 @classmethod
254 def _from_json_impl(cls, json_drive_group):
255 # type: (dict) -> DriveGroupSpec
256 """
257 Initialize 'Drive group' structure
258
259 :param json_drive_group: A valid json string with a Drive Group
260 specification
261 """
20effc67 262 args: Dict[str, Any] = json_drive_group.copy()
9f95a23c 263 # legacy json (pre Octopus)
20effc67
TL
264 if 'host_pattern' in args and 'placement' not in args:
265 args['placement'] = {'host_pattern': args['host_pattern']}
266 del args['host_pattern']
f6b5b4d7 267
20effc67 268 s_id = args.get('service_id', '<unnamed>')
f6b5b4d7
TL
269
270 # spec: was not mandatory in octopus
20effc67
TL
271 if 'spec' in args:
272 args['spec'].update(cls._drive_group_spec_from_json(s_id, args['spec']))
f6b5b4d7 273 else:
20effc67 274 args.update(cls._drive_group_spec_from_json(s_id, args))
f6b5b4d7 275
20effc67 276 return super(DriveGroupSpec, cls)._from_json_impl(args)
f6b5b4d7
TL
277
278 @classmethod
20effc67 279 def _drive_group_spec_from_json(cls, name: str, json_drive_group: dict) -> dict:
9f95a23c
TL
280 for applied_filter in list(json_drive_group.keys()):
281 if applied_filter not in cls._supported_features:
282 raise DriveGroupValidationError(
20effc67
TL
283 name,
284 "Feature `{}` is not supported".format(applied_filter))
9f95a23c 285
9f95a23c 286 try:
20effc67
TL
287 def to_selection(key: str, vals: dict) -> Optional[DeviceSelection]:
288 try:
289 return DeviceSelection.from_json(vals)
290 except KeyError as e:
291 raise DriveGroupValidationError(
292 f'{name}.{key}',
293 f"Filtering for `{e.args[0]}` is not supported")
294
295 args = {k: (to_selection(k, v) if k.endswith('_devices') else v) for k, v in
9f95a23c
TL
296 json_drive_group.items()}
297 if not args:
20effc67 298 raise DriveGroupValidationError(name, "Didn't find drive selections")
f6b5b4d7 299 return args
9f95a23c 300 except (KeyError, TypeError) as e:
20effc67 301 raise DriveGroupValidationError(name, str(e))
9f95a23c
TL
302
303 def validate(self):
304 # type: () -> None
305 super(DriveGroupSpec, self).validate()
306
20effc67
TL
307 if self.placement.is_empty():
308 raise DriveGroupValidationError(self.service_id, '`placement` required')
9f95a23c 309
f67539c2 310 if self.data_devices is None:
20effc67 311 raise DriveGroupValidationError(self.service_id, "`data_devices` element is required.")
f67539c2 312
20effc67
TL
313 specs_names = "data_devices db_devices wal_devices journal_devices".split()
314 specs = dict(zip(specs_names, [getattr(self, k) for k in specs_names]))
315 for k, s in [ks for ks in specs.items() if ks[1] is not None]:
316 assert s is not None
317 s.validate(f'{self.service_id}.{k}')
9f95a23c
TL
318 for s in filter(None, [self.db_devices, self.wal_devices, self.journal_devices]):
319 if s.all:
20effc67
TL
320 raise DriveGroupValidationError(
321 self.service_id,
322 "`all` is only allowed for data_devices")
9f95a23c 323
f6b5b4d7 324 if self.objectstore not in ('bluestore'):
20effc67
TL
325 raise DriveGroupValidationError(self.service_id,
326 f"{self.objectstore} is not supported. Must be "
f6b5b4d7 327 f"one of ('bluestore')")
9f95a23c 328
f91f0fd5 329 if self.block_wal_size is not None and type(self.block_wal_size) not in [int, str]:
20effc67
TL
330 raise DriveGroupValidationError(
331 self.service_id,
332 'block_wal_size must be of type int or string')
f91f0fd5 333 if self.block_db_size is not None and type(self.block_db_size) not in [int, str]:
20effc67
TL
334 raise DriveGroupValidationError(
335 self.service_id,
336 'block_db_size must be of type int or string')
f91f0fd5 337 if self.journal_size is not None and type(self.journal_size) not in [int, str]:
20effc67
TL
338 raise DriveGroupValidationError(
339 self.service_id,
340 'journal_size must be of type int or string')
9f95a23c 341
f6b5b4d7 342 if self.filter_logic not in ['AND', 'OR']:
20effc67
TL
343 raise DriveGroupValidationError(
344 self.service_id,
345 'filter_logic must be either <AND> or <OR>')
9f95a23c 346
20effc67
TL
347 if self.method not in [None, 'lvm', 'raw']:
348 raise DriveGroupValidationError(
349 self.service_id,
350 'method must be one of None, lvm, raw')
351 if self.method == 'raw' and self.objectstore == 'filestore':
352 raise DriveGroupValidationError(
353 self.service_id,
354 'method raw only supports bluestore')
f6b5b4d7
TL
355
356
357yaml.add_representer(DriveGroupSpec, DriveGroupSpec.yaml_representer)