3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
15 from cmdatahandlers.api import configerror
16 from cmdatahandlers.api import config
17 from cmdatahandlers.api import utils
20 class Config(config.Config):
21 def __init__(self, confman):
22 super(Config, self).__init__(confman)
23 self.ROOT = 'cloud.storage_profiles'
24 self.DOMAIN = 'storage_profiles'
31 self._validate_storage_profiles()
33 def _validate_storage_profiles(self):
34 profiles = self.get_storage_profiles()
35 utils.validate_list_items_unique(profiles)
36 for profile in profiles:
37 self._validate_storage_profile(profile)
39 def _validate_storage_profile(self, profile):
40 backend = self.get_profile_backend(profile)
41 storageconf = self.confman.get_storage_config_handler()
42 backends = storageconf.get_storage_backends()
43 if backend not in backends:
44 raise configerror.ConfigError(
45 'Invalid backend %s provided in profile %s' % (backend, profile))
47 self.get_profile_nr_of_ceph_osd_disks(profile)
48 elif backend == 'lvm':
49 self.get_profile_lvm_cinder_storage_partitions(profile)
50 self.get_profile_lvm_instance_storage_partitions(profile)
51 self.get_profile_lvm_instance_cow_lv_storage_percentage(profile)
52 self.get_profile_instance_storage_percentage(profile)
54 def is_valid_profile(self, profile):
56 profiles = self.get_storage_profiles()
57 if profile not in profiles:
58 raise configerror.ConfigError('Invalid profile name %s' % profile)
60 def get_storage_profiles(self):
61 """ get the storage profiles list
65 A list of storage profile(s) names
69 ConfigError in-case of an error
71 return self.config[self.ROOT].keys()
73 def get_profile_ceph_osd_disks(self, profile):
74 """ get the ceph osd disks
86 ConfigError in-case of an error
88 return self._get_attribute(profile, 'ceph_osd_disks')
90 def get_profile_ceph_osd_journal_disk(self, profile):
91 """ get the ceph osd journal disk
99 The ceph osd journal disk
103 ConfigError in-case of an error
105 return self._get_attribute(profile, 'ceph_osd_journal_disk')
107 def get_profile_ceph_openstack_pg_proportion(self, profile):
108 openstack_pg_ratio, caas_pg_ratio = self._get_ceph_pg_share_ratios(profile)
109 return openstack_pg_ratio / (openstack_pg_ratio + caas_pg_ratio)
111 def _get_ceph_pg_share_ratios(self, profile):
112 pg_share_ratios = self.get_profile_ceph_openstack_caas_pg_ratio(profile).split(':')
113 return map(lambda r: float(r), pg_share_ratios)
115 def get_profile_ceph_caas_pg_proportion(self, profile):
116 openstack_pg_ratio, caas_pg_ratio = self._get_ceph_pg_share_ratios(profile)
117 return caas_pg_ratio / (openstack_pg_ratio + caas_pg_ratio)
119 def get_profile_ceph_openstack_caas_pg_ratio(self, profile):
120 """ get the ceph openstack-caas pg share ratio
128 The ceph osd share ratio
132 ConfigError in-case of an error
134 return self._get_optional_attribute(
135 profile, 'ceph_pg_openstack_caas_share_ratio', "1:0")
137 def get_profile_nr_of_ceph_osd_disks(self, profile):
138 """ get the number of ceph osd disks
146 The number of ceph osd disks
150 ConfigError in-case of an error
153 self.is_valid_profile(profile)
155 if 'nr_of_ceph_osd_disks' not in self.config[self.ROOT][profile]:
156 ceph_osd_disks = self._get_attribute(profile, 'ceph_osd_disks')
157 return len(ceph_osd_disks)
158 return self.config[self.ROOT][profile]['nr_of_ceph_osd_disks']
160 def get_profile_lvm_cinder_storage_partitions(self, profile):
161 """ get the lvm_cinder_storage_partitions
169 The lvm_cinder_storage_partitions
173 ConfigError in-case of an error
175 return self._get_attribute(profile, 'lvm_cinder_storage_partitions')
177 def get_profile_backend(self, profile):
178 """ get the storage profile backend
190 ConfigError in-case of an error
192 return self._get_attribute(profile, 'backend')
194 def get_profile_lvm_instance_storage_partitions(self, profile):
195 """ get the lvm_instance_storage_partitions
203 The lvm_instance_storage_partitions
207 ConfigError in-case of an error
209 return self._get_attribute(profile, 'lvm_instance_storage_partitions')
211 def get_profile_lvm_instance_cow_lv_storage_percentage(self, profile):
212 """ get the lvm_instance_cow_lv_storage_percentage
220 The lvm_instance_cow_lv_storage_percentage
224 ConfigError in-case of an error
226 return self._get_attribute(profile, 'lvm_instance_cow_lv_storage_percentage')
228 def get_profile_instance_storage_percentage(self, profile):
229 """ get the instance_storage_percentage
237 The instance_storage_percentage
241 ConfigError in-case of an error
243 return self._get_attribute(profile, 'instance_storage_percentage')
245 def get_profile_bare_lvm_mount_options(self, profile):
246 """ get the mount_options
258 ConfigError in-case of an error
260 return self._get_optional_attribute(profile, 'mount_options')
262 def get_profile_bare_lvm_mount_dir(self, profile):
263 """ get the mount_dir
275 ConfigError in-case of an error
277 return self._get_attribute(profile, 'mount_dir')
279 def get_profile_bare_lvm_lv_name(self, profile):
292 ConfigError in-case of an error
294 return self._get_attribute(profile, 'lv_name')
296 def _get_attribute(self, profile, attribute):
297 """ get arbirary storage profile attribute
310 ConfigError in-case of an error
313 self.is_valid_profile(profile)
314 if attribute not in self.config[self.ROOT][profile]:
315 raise configerror.ConfigError(
316 'Profile %s does not have %s configured' % (attribute, profile))
317 return self.config[self.ROOT][profile][attribute]
319 def _get_optional_attribute(self, profile, attribute, default_value=""):
320 """ get arbirary optional storage profile attribute
333 ConfigError in-case of an error
336 self.is_valid_profile(profile)
337 if attribute not in self.config[self.ROOT][profile]:
339 return self.config[self.ROOT][profile][attribute]