]>
Commit | Line | Data |
---|---|---|
9f95a23c TL |
1 | from ceph.deployment.inventory import Device |
2 | from ceph.deployment.service_spec import ServiceSpecValidationError, ServiceSpec, PlacementSpec | |
3 | ||
4 | try: | |
5 | from typing import Optional, List, Dict, Any | |
6 | except ImportError: | |
7 | pass | |
8 | import six | |
9 | ||
10 | ||
11 | class DeviceSelection(object): | |
12 | """ | |
13 | Used within :class:`ceph.deployment.drive_group.DriveGroupSpec` to specify the devices | |
14 | used by the Drive Group. | |
15 | ||
16 | Any attributes (even none) can be included in the device | |
17 | specification structure. | |
18 | """ | |
19 | ||
20 | _supported_filters = [ | |
21 | "paths", "size", "vendor", "model", "rotational", "limit", "all" | |
22 | ] | |
23 | ||
24 | def __init__(self, | |
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 | |
32 | ): | |
33 | """ | |
34 | ephemeral drive group device specification | |
35 | """ | |
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] | |
38 | ||
39 | #: A wildcard string. e.g: "SDD*" or "SanDisk SD8SN8U5" | |
40 | self.model = model | |
41 | ||
42 | #: Match on the VENDOR property of the drive | |
43 | self.vendor = vendor | |
44 | ||
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) | |
48 | self.size = size | |
49 | ||
50 | #: is the drive rotating or not | |
51 | self.rotational = rotational | |
52 | ||
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`` | |
55 | self.limit = limit | |
56 | ||
57 | #: Matches all devices. Can only be used for data devices | |
58 | self.all = all | |
59 | ||
60 | self.validate() | |
61 | ||
62 | def validate(self): | |
63 | # type: () -> None | |
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') | |
71 | ||
72 | if self.all and not is_empty: | |
73 | raise DriveGroupValidationError( | |
74 | 'DeviceSelection `all` and other parameters are mutually exclusive. {}'.format( | |
75 | repr(self))) | |
76 | ||
77 | @classmethod | |
78 | def from_json(cls, device_spec): | |
79 | # type: (dict) -> DeviceSelection | |
80 | if not device_spec: | |
81 | return # type: ignore | |
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)) | |
86 | ||
87 | return cls(**device_spec) | |
88 | ||
89 | def to_json(self): | |
90 | # type: () -> Dict[str, Any] | |
91 | c = self.__dict__.copy() | |
92 | if self.paths: | |
93 | c['paths'] = [p.path for p in self.paths] | |
94 | return c | |
95 | ||
96 | def __repr__(self): | |
97 | keys = [ | |
98 | key for key in self._supported_filters + ['limit'] if getattr(self, key) is not None | |
99 | ] | |
100 | if 'paths' in keys and self.paths == []: | |
101 | keys.remove('paths') | |
102 | return "DeviceSelection({})".format( | |
103 | ', '.join('{}={}'.format(key, repr(getattr(self, key))) for key in keys) | |
104 | ) | |
105 | ||
106 | def __eq__(self, other): | |
107 | return repr(self) == repr(other) | |
108 | ||
109 | ||
110 | class DriveGroupValidationError(ServiceSpecValidationError): | |
111 | """ | |
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. | |
114 | """ | |
115 | ||
116 | def __init__(self, msg): | |
117 | super(DriveGroupValidationError, self).__init__('Failed to validate Drive Group: ' + msg) | |
118 | ||
119 | ||
120 | class DriveGroupSpec(ServiceSpec): | |
121 | """ | |
122 | Describe a drive group in the same form that ceph-volume | |
123 | understands. | |
124 | """ | |
125 | ||
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", | |
801d1391 | 131 | "journal_size", "unmanaged" |
9f95a23c TL |
132 | ] |
133 | ||
134 | def __init__(self, | |
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, DeviceSelection]] | |
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] | |
801d1391 | 152 | unmanaged=None, # type: Optional[bool] |
9f95a23c TL |
153 | ): |
154 | assert service_type is None or service_type == 'osd' | |
155 | super(DriveGroupSpec, self).__init__('osd', service_id=service_id, placement=placement) | |
156 | ||
157 | #: A :class:`ceph.deployment.drive_group.DeviceSelection` | |
158 | self.data_devices = data_devices | |
159 | ||
160 | #: A :class:`ceph.deployment.drive_group.DeviceSelection` | |
161 | self.db_devices = db_devices | |
162 | ||
163 | #: A :class:`ceph.deployment.drive_group.DeviceSelection` | |
164 | self.wal_devices = wal_devices | |
165 | ||
166 | #: A :class:`ceph.deployment.drive_group.DeviceSelection` | |
167 | self.journal_devices = journal_devices | |
168 | ||
169 | #: Set (or override) the "bluestore_block_wal_size" value, in bytes | |
170 | self.block_wal_size = block_wal_size | |
171 | ||
172 | #: Set (or override) the "bluestore_block_db_size" value, in bytes | |
173 | self.block_db_size = block_db_size | |
174 | ||
175 | #: set journal_size is bytes | |
176 | self.journal_size = journal_size | |
177 | ||
178 | #: Number of osd daemons per "DATA" device. | |
179 | #: To fully utilize nvme devices multiple osds are required. | |
180 | self.osds_per_device = osds_per_device | |
181 | ||
182 | #: A list of strings, containing paths which should back OSDs | |
183 | self.data_directories = data_directories | |
184 | ||
185 | #: ``filestore`` or ``bluestore`` | |
186 | self.objectstore = objectstore | |
187 | ||
188 | #: ``true`` or ``false`` | |
189 | self.encrypted = encrypted | |
190 | ||
191 | #: How many OSDs per DB device | |
192 | self.db_slots = db_slots | |
193 | ||
194 | #: How many OSDs per WAL device | |
195 | self.wal_slots = wal_slots | |
196 | ||
197 | #: Optional: mapping of OSD id to DeviceSelection, used when the | |
198 | #: created OSDs are meant to replace previous OSDs on | |
199 | #: the same node. See :ref:`orchestrator-osd-replace` | |
200 | self.osd_id_claims = osd_id_claims | |
201 | ||
202 | @classmethod | |
203 | def _from_json_impl(cls, json_drive_group): | |
204 | # type: (dict) -> DriveGroupSpec | |
205 | """ | |
206 | Initialize 'Drive group' structure | |
207 | ||
208 | :param json_drive_group: A valid json string with a Drive Group | |
209 | specification | |
210 | """ | |
211 | # legacy json (pre Octopus) | |
212 | if 'host_pattern' in json_drive_group and 'placement' not in json_drive_group: | |
213 | json_drive_group['placement'] = {'host_pattern': json_drive_group['host_pattern']} | |
214 | del json_drive_group['host_pattern'] | |
215 | ||
216 | for applied_filter in list(json_drive_group.keys()): | |
217 | if applied_filter not in cls._supported_features: | |
218 | raise DriveGroupValidationError( | |
219 | "Feature <{}> is not supported".format(applied_filter)) | |
220 | ||
221 | for key in ('block_wal_size', 'block_db_size', 'journal_size'): | |
222 | if key in json_drive_group: | |
223 | if isinstance(json_drive_group[key], six.string_types): | |
224 | from ceph.deployment.drive_selection import SizeMatcher | |
225 | json_drive_group[key] = SizeMatcher.str_to_byte(json_drive_group[key]) | |
226 | ||
227 | if 'placement' in json_drive_group: | |
228 | json_drive_group['placement'] = PlacementSpec.from_json(json_drive_group['placement']) | |
229 | ||
230 | try: | |
231 | args = {k: (DeviceSelection.from_json(v) if k.endswith('_devices') else v) for k, v in | |
232 | json_drive_group.items()} | |
233 | if not args: | |
234 | raise DriveGroupValidationError("Didn't find Drivegroup specs") | |
235 | return DriveGroupSpec(**args) | |
236 | except (KeyError, TypeError) as e: | |
237 | raise DriveGroupValidationError(str(e)) | |
238 | ||
239 | def validate(self): | |
240 | # type: () -> None | |
241 | super(DriveGroupSpec, self).validate() | |
242 | ||
243 | if not isinstance(self.placement.host_pattern, six.string_types): | |
244 | raise DriveGroupValidationError('host_pattern must be of type string') | |
245 | ||
246 | specs = [self.data_devices, self.db_devices, self.wal_devices, self.journal_devices] | |
247 | for s in filter(None, specs): | |
248 | s.validate() | |
249 | for s in filter(None, [self.db_devices, self.wal_devices, self.journal_devices]): | |
250 | if s.all: | |
251 | raise DriveGroupValidationError("`all` is only allowed for data_devices") | |
252 | ||
253 | if self.objectstore not in ('filestore', 'bluestore'): | |
254 | raise DriveGroupValidationError("objectstore not in ('filestore', 'bluestore')") | |
255 | ||
256 | if self.block_wal_size is not None and type(self.block_wal_size) != int: | |
257 | raise DriveGroupValidationError('block_wal_size must be of type int') | |
258 | if self.block_db_size is not None and type(self.block_db_size) != int: | |
259 | raise DriveGroupValidationError('block_db_size must be of type int') | |
260 | ||
261 | def __repr__(self): | |
262 | keys = [ | |
263 | key for key in self._supported_features if getattr(self, key) is not None | |
264 | ] | |
265 | if 'encrypted' in keys and not self.encrypted: | |
266 | keys.remove('encrypted') | |
267 | if 'objectstore' in keys and self.objectstore == 'bluestore': | |
268 | keys.remove('objectstore') | |
269 | return "DriveGroupSpec(name={}->{})".format( | |
270 | self.service_id, | |
271 | ', '.join('{}={}'.format(key, repr(getattr(self, key))) for key in keys) | |
272 | ) | |
273 | ||
274 | def to_json(self): | |
275 | # type: () -> Dict[str, Any] | |
276 | c = self.__dict__.copy() | |
277 | if self.placement: | |
278 | c['placement'] = self.placement.to_json() | |
279 | if self.data_devices: | |
280 | c['data_devices'] = self.data_devices.to_json() | |
281 | if self.db_devices: | |
282 | c['db_devices'] = self.db_devices.to_json() | |
283 | if self.wal_devices: | |
284 | c['wal_devices'] = self.wal_devices.to_json() | |
285 | return c | |
286 | ||
287 | def __eq__(self, other): | |
288 | return repr(self) == repr(other) |