]>
Commit | Line | Data |
---|---|---|
20effc67 | 1 | import enum |
f6b5b4d7 TL |
2 | import yaml |
3 | ||
9f95a23c | 4 | from ceph.deployment.inventory import Device |
f67539c2 TL |
5 | from ceph.deployment.service_spec import ServiceSpec, PlacementSpec |
6 | from ceph.deployment.hostspec import SpecValidationError | |
9f95a23c TL |
7 | |
8 | try: | |
f91f0fd5 | 9 | from typing import Optional, List, Dict, Any, Union |
9f95a23c TL |
10 | except ImportError: |
11 | pass | |
9f95a23c TL |
12 | |
13 | ||
20effc67 TL |
14 | class OSDMethod(str, enum.Enum): |
15 | raw = 'raw' | |
16 | lvm = 'lvm' | |
17 | ||
18 | ||
9f95a23c TL |
19 | class 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 | 129 | class 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 | ||
141 | class 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", | |
20effc67 TL |
152 | "journal_size", "unmanaged", "filter_logic", "preview_only", |
153 | "data_allocate_fraction", "method" | |
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 | |
20effc67 TL |
177 | data_allocate_fraction=None, # type: Optional[float] |
178 | method=None, # type: Optional[OSDMethod] | |
9f95a23c TL |
179 | ): |
180 | assert service_type is None or service_type == 'osd' | |
1911f103 TL |
181 | super(DriveGroupSpec, self).__init__('osd', service_id=service_id, |
182 | placement=placement, | |
f6b5b4d7 TL |
183 | unmanaged=unmanaged, |
184 | preview_only=preview_only) | |
9f95a23c TL |
185 | |
186 | #: A :class:`ceph.deployment.drive_group.DeviceSelection` | |
187 | self.data_devices = data_devices | |
188 | ||
189 | #: A :class:`ceph.deployment.drive_group.DeviceSelection` | |
190 | self.db_devices = db_devices | |
191 | ||
192 | #: A :class:`ceph.deployment.drive_group.DeviceSelection` | |
193 | self.wal_devices = wal_devices | |
194 | ||
195 | #: A :class:`ceph.deployment.drive_group.DeviceSelection` | |
196 | self.journal_devices = journal_devices | |
197 | ||
198 | #: Set (or override) the "bluestore_block_wal_size" value, in bytes | |
f91f0fd5 | 199 | self.block_wal_size: Union[int, str, None] = block_wal_size |
9f95a23c TL |
200 | |
201 | #: Set (or override) the "bluestore_block_db_size" value, in bytes | |
f91f0fd5 | 202 | self.block_db_size: Union[int, str, None] = block_db_size |
9f95a23c | 203 | |
f6b5b4d7 | 204 | #: set journal_size in bytes |
f91f0fd5 | 205 | self.journal_size: Union[int, str, None] = journal_size |
9f95a23c TL |
206 | |
207 | #: Number of osd daemons per "DATA" device. | |
208 | #: To fully utilize nvme devices multiple osds are required. | |
209 | self.osds_per_device = osds_per_device | |
210 | ||
211 | #: A list of strings, containing paths which should back OSDs | |
212 | self.data_directories = data_directories | |
213 | ||
214 | #: ``filestore`` or ``bluestore`` | |
215 | self.objectstore = objectstore | |
216 | ||
217 | #: ``true`` or ``false`` | |
218 | self.encrypted = encrypted | |
219 | ||
220 | #: How many OSDs per DB device | |
221 | self.db_slots = db_slots | |
222 | ||
223 | #: How many OSDs per WAL device | |
224 | self.wal_slots = wal_slots | |
225 | ||
1911f103 TL |
226 | #: Optional: mapping of host -> List of osd_ids that should be replaced |
227 | #: See :ref:`orchestrator-osd-replace` | |
228 | self.osd_id_claims = osd_id_claims or dict() | |
9f95a23c | 229 | |
f6b5b4d7 TL |
230 | #: The logic gate we use to match disks with filters. |
231 | #: defaults to 'AND' | |
232 | self.filter_logic = filter_logic.upper() | |
233 | ||
234 | #: If this should be treated as a 'preview' spec | |
235 | self.preview_only = preview_only | |
236 | ||
20effc67 TL |
237 | #: Allocate a fraction of the data device (0,1.0] |
238 | self.data_allocate_fraction = data_allocate_fraction | |
239 | ||
240 | self.method = method | |
241 | ||
9f95a23c TL |
242 | @classmethod |
243 | def _from_json_impl(cls, json_drive_group): | |
244 | # type: (dict) -> DriveGroupSpec | |
245 | """ | |
246 | Initialize 'Drive group' structure | |
247 | ||
248 | :param json_drive_group: A valid json string with a Drive Group | |
249 | specification | |
250 | """ | |
20effc67 | 251 | args: Dict[str, Any] = json_drive_group.copy() |
9f95a23c | 252 | # legacy json (pre Octopus) |
20effc67 TL |
253 | if 'host_pattern' in args and 'placement' not in args: |
254 | args['placement'] = {'host_pattern': args['host_pattern']} | |
255 | del args['host_pattern'] | |
f6b5b4d7 | 256 | |
20effc67 | 257 | s_id = args.get('service_id', '<unnamed>') |
f6b5b4d7 TL |
258 | |
259 | # spec: was not mandatory in octopus | |
20effc67 TL |
260 | if 'spec' in args: |
261 | args['spec'].update(cls._drive_group_spec_from_json(s_id, args['spec'])) | |
f6b5b4d7 | 262 | else: |
20effc67 | 263 | args.update(cls._drive_group_spec_from_json(s_id, args)) |
f6b5b4d7 | 264 | |
20effc67 | 265 | return super(DriveGroupSpec, cls)._from_json_impl(args) |
f6b5b4d7 TL |
266 | |
267 | @classmethod | |
20effc67 | 268 | def _drive_group_spec_from_json(cls, name: str, json_drive_group: dict) -> dict: |
9f95a23c TL |
269 | for applied_filter in list(json_drive_group.keys()): |
270 | if applied_filter not in cls._supported_features: | |
271 | raise DriveGroupValidationError( | |
20effc67 TL |
272 | name, |
273 | "Feature `{}` is not supported".format(applied_filter)) | |
9f95a23c | 274 | |
9f95a23c | 275 | try: |
20effc67 TL |
276 | def to_selection(key: str, vals: dict) -> Optional[DeviceSelection]: |
277 | try: | |
278 | return DeviceSelection.from_json(vals) | |
279 | except KeyError as e: | |
280 | raise DriveGroupValidationError( | |
281 | f'{name}.{key}', | |
282 | f"Filtering for `{e.args[0]}` is not supported") | |
283 | ||
284 | args = {k: (to_selection(k, v) if k.endswith('_devices') else v) for k, v in | |
9f95a23c TL |
285 | json_drive_group.items()} |
286 | if not args: | |
20effc67 | 287 | raise DriveGroupValidationError(name, "Didn't find drive selections") |
f6b5b4d7 | 288 | return args |
9f95a23c | 289 | except (KeyError, TypeError) as e: |
20effc67 | 290 | raise DriveGroupValidationError(name, str(e)) |
9f95a23c TL |
291 | |
292 | def validate(self): | |
293 | # type: () -> None | |
294 | super(DriveGroupSpec, self).validate() | |
295 | ||
20effc67 TL |
296 | if self.placement.is_empty(): |
297 | raise DriveGroupValidationError(self.service_id, '`placement` required') | |
9f95a23c | 298 | |
f67539c2 | 299 | if self.data_devices is None: |
20effc67 | 300 | raise DriveGroupValidationError(self.service_id, "`data_devices` element is required.") |
f67539c2 | 301 | |
20effc67 TL |
302 | specs_names = "data_devices db_devices wal_devices journal_devices".split() |
303 | specs = dict(zip(specs_names, [getattr(self, k) for k in specs_names])) | |
304 | for k, s in [ks for ks in specs.items() if ks[1] is not None]: | |
305 | assert s is not None | |
306 | s.validate(f'{self.service_id}.{k}') | |
9f95a23c TL |
307 | for s in filter(None, [self.db_devices, self.wal_devices, self.journal_devices]): |
308 | if s.all: | |
20effc67 TL |
309 | raise DriveGroupValidationError( |
310 | self.service_id, | |
311 | "`all` is only allowed for data_devices") | |
9f95a23c | 312 | |
f6b5b4d7 | 313 | if self.objectstore not in ('bluestore'): |
20effc67 TL |
314 | raise DriveGroupValidationError(self.service_id, |
315 | f"{self.objectstore} is not supported. Must be " | |
f6b5b4d7 | 316 | f"one of ('bluestore')") |
9f95a23c | 317 | |
f91f0fd5 | 318 | if self.block_wal_size is not None and type(self.block_wal_size) not in [int, str]: |
20effc67 TL |
319 | raise DriveGroupValidationError( |
320 | self.service_id, | |
321 | 'block_wal_size must be of type int or string') | |
f91f0fd5 | 322 | if self.block_db_size is not None and type(self.block_db_size) not in [int, str]: |
20effc67 TL |
323 | raise DriveGroupValidationError( |
324 | self.service_id, | |
325 | 'block_db_size must be of type int or string') | |
f91f0fd5 | 326 | if self.journal_size is not None and type(self.journal_size) not in [int, str]: |
20effc67 TL |
327 | raise DriveGroupValidationError( |
328 | self.service_id, | |
329 | 'journal_size must be of type int or string') | |
9f95a23c | 330 | |
f6b5b4d7 | 331 | if self.filter_logic not in ['AND', 'OR']: |
20effc67 TL |
332 | raise DriveGroupValidationError( |
333 | self.service_id, | |
334 | 'filter_logic must be either <AND> or <OR>') | |
9f95a23c | 335 | |
20effc67 TL |
336 | if self.method not in [None, 'lvm', 'raw']: |
337 | raise DriveGroupValidationError( | |
338 | self.service_id, | |
339 | 'method must be one of None, lvm, raw') | |
340 | if self.method == 'raw' and self.objectstore == 'filestore': | |
341 | raise DriveGroupValidationError( | |
342 | self.service_id, | |
343 | 'method raw only supports bluestore') | |
f6b5b4d7 TL |
344 | |
345 | ||
346 | yaml.add_representer(DriveGroupSpec, DriveGroupSpec.yaml_representer) |