]> git.proxmox.com Git - ceph.git/blob - ceph/src/python-common/ceph/deployment/drive_group.py
0b5b489de98d0820da8c90d25ca9645c349a25dc
[ceph.git] / ceph / src / python-common / ceph / deployment / drive_group.py
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",
131 "journal_size", "unmanaged"
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, 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
153 ):
154 assert service_type is None or service_type == 'osd'
155 super(DriveGroupSpec, self).__init__('osd', service_id=service_id,
156 placement=placement,
157 unmanaged=unmanaged)
158
159 #: A :class:`ceph.deployment.drive_group.DeviceSelection`
160 self.data_devices = data_devices
161
162 #: A :class:`ceph.deployment.drive_group.DeviceSelection`
163 self.db_devices = db_devices
164
165 #: A :class:`ceph.deployment.drive_group.DeviceSelection`
166 self.wal_devices = wal_devices
167
168 #: A :class:`ceph.deployment.drive_group.DeviceSelection`
169 self.journal_devices = journal_devices
170
171 #: Set (or override) the "bluestore_block_wal_size" value, in bytes
172 self.block_wal_size = block_wal_size
173
174 #: Set (or override) the "bluestore_block_db_size" value, in bytes
175 self.block_db_size = block_db_size
176
177 #: set journal_size is bytes
178 self.journal_size = journal_size
179
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
183
184 #: A list of strings, containing paths which should back OSDs
185 self.data_directories = data_directories
186
187 #: ``filestore`` or ``bluestore``
188 self.objectstore = objectstore
189
190 #: ``true`` or ``false``
191 self.encrypted = encrypted
192
193 #: How many OSDs per DB device
194 self.db_slots = db_slots
195
196 #: How many OSDs per WAL device
197 self.wal_slots = wal_slots
198
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()
202
203 @classmethod
204 def _from_json_impl(cls, json_drive_group):
205 # type: (dict) -> DriveGroupSpec
206 """
207 Initialize 'Drive group' structure
208
209 :param json_drive_group: A valid json string with a Drive Group
210 specification
211 """
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']
216
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))
221
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])
227
228 if 'placement' in json_drive_group:
229 json_drive_group['placement'] = PlacementSpec.from_json(json_drive_group['placement'])
230
231 try:
232 args = {k: (DeviceSelection.from_json(v) if k.endswith('_devices') else v) for k, v in
233 json_drive_group.items()}
234 if not args:
235 raise DriveGroupValidationError("Didn't find Drivegroup specs")
236 return DriveGroupSpec(**args)
237 except (KeyError, TypeError) as e:
238 raise DriveGroupValidationError(str(e))
239
240 def validate(self):
241 # type: () -> None
242 super(DriveGroupSpec, self).validate()
243
244 if not isinstance(self.placement.host_pattern, six.string_types):
245 raise DriveGroupValidationError('host_pattern must be of type string')
246
247 specs = [self.data_devices, self.db_devices, self.wal_devices, self.journal_devices]
248 for s in filter(None, specs):
249 s.validate()
250 for s in filter(None, [self.db_devices, self.wal_devices, self.journal_devices]):
251 if s.all:
252 raise DriveGroupValidationError("`all` is only allowed for data_devices")
253
254 if self.objectstore not in ('filestore', 'bluestore'):
255 raise DriveGroupValidationError("objectstore not in ('filestore', 'bluestore')")
256
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')
261
262 def __repr__(self):
263 keys = [
264 key for key in self._supported_features if getattr(self, key) is not None
265 ]
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(
271 self.service_id,
272 ', '.join('{}={}'.format(key, repr(getattr(self, key))) for key in keys)
273 )
274
275 def to_json(self):
276 # type: () -> Dict[str, Any]
277 c = self.__dict__.copy()
278 if self.placement:
279 c['placement'] = self.placement.to_json()
280 if self.data_devices:
281 c['data_devices'] = self.data_devices.to_json()
282 if self.db_devices:
283 c['db_devices'] = self.db_devices.to_json()
284 if self.wal_devices:
285 c['wal_devices'] = self.wal_devices.to_json()
286 c['service_name'] = self.service_name()
287 return c
288
289 def __eq__(self, other):
290 return repr(self) == repr(other)