Kubernetes memory parameter
[ta/config-manager.git] / cmdatahandlers / src / cmdatahandlers / storage_profiles / config.py
1 # Copyright 2019 Nokia
2
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
6 #
7 #     http://www.apache.org/licenses/LICENSE-2.0
8 #
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.
14
15 from cmdatahandlers.api import configerror
16 from cmdatahandlers.api import config
17 from cmdatahandlers.api import utils
18
19
20 BARE_LVM_MOUNT_DIR = "/var/lib/docker"
21 BARE_LVM_MOUNT_OPT = "noatime,nodiratime,logbufs=8,pquota"
22
23
24 class Config(config.Config):
25     def __init__(self, confman):
26         super(Config, self).__init__(confman)
27         self.ROOT = 'cloud.storage_profiles'
28         self.DOMAIN = 'storage_profiles'
29
30     def init(self):
31         pass
32
33     def validate(self):
34         self.validate_root()
35         self._validate_storage_profiles()
36
37     def _validate_storage_profiles(self):
38         profiles = self.get_storage_profiles()
39         utils.validate_list_items_unique(profiles)
40         for profile in profiles:
41             self._validate_storage_profile(profile)
42
43     def _validate_storage_profile(self, profile):
44         backend = self.get_profile_backend(profile)
45         storageconf = self.confman.get_storage_config_handler()
46         backends = storageconf.get_storage_backends()
47         if backend not in backends:
48             raise configerror.ConfigError(
49                 'Invalid backend %s provided in profile %s' % (backend, profile))
50         if backend == 'ceph':
51             self.get_profile_nr_of_ceph_osd_disks(profile)
52         elif backend == 'lvm':
53             self.get_profile_lvm_cinder_storage_partitions(profile)
54             self.get_profile_lvm_instance_storage_partitions(profile)
55             self.get_profile_lvm_instance_cow_lv_storage_percentage(profile)
56             self.get_profile_instance_storage_percentage(profile)
57
58     def is_valid_profile(self, profile):
59         self.validate_root()
60         profiles = self.get_storage_profiles()
61         if profile not in profiles:
62             raise configerror.ConfigError('Invalid profile name %s' % profile)
63
64     def get_storage_profiles(self):
65         """ get the storage profiles list
66
67             Return:
68
69             A list of storage profile(s) names
70
71             Raise:
72
73             ConfigError in-case of an error
74         """
75         return self.config[self.ROOT].keys()
76
77     def get_profile_ceph_osd_disks(self, profile):
78         """ get the ceph osd disks
79
80             Argument:
81
82             profile name
83
84             Return:
85
86             The ceph osd disks
87
88             Raise:
89
90             ConfigError in-case of an error
91         """
92         return self._get_attribute(profile, 'ceph_osd_disks')
93
94     def get_profile_ceph_osd_journal_disk(self, profile):
95         """ get the ceph osd journal disk
96
97             Argument:
98
99             profile name
100
101             Return:
102
103             The ceph osd journal disk
104
105             Raise:
106
107             ConfigError in-case of an error
108         """
109         return self._get_attribute(profile, 'ceph_osd_journal_disk')
110
111     def get_profile_ceph_openstack_pg_proportion(self, profile):
112         openstack_pg_ratio, caas_pg_ratio = self._get_ceph_pg_share_ratios(profile)
113         return openstack_pg_ratio / (openstack_pg_ratio + caas_pg_ratio)
114
115     def _get_ceph_pg_share_ratios(self, profile):
116         pg_share_ratios = self.get_profile_ceph_openstack_caas_pg_ratio(profile).split(':')
117         return map(lambda r: float(r), pg_share_ratios)
118
119     def get_profile_ceph_caas_pg_proportion(self, profile):
120         openstack_pg_ratio, caas_pg_ratio = self._get_ceph_pg_share_ratios(profile)
121         return caas_pg_ratio / (openstack_pg_ratio + caas_pg_ratio)
122
123     def get_profile_ceph_openstack_caas_pg_ratio(self, profile):
124         """ get the ceph openstack-caas pg share ratio
125
126             Argument:
127
128             profile name
129
130             Return:
131
132             The ceph osd share ratio
133
134             Raise:
135
136             ConfigError in-case of an error
137         """
138         return self._get_optional_attribute(
139             profile, 'ceph_pg_openstack_caas_share_ratio', "1:0")
140
141     def get_profile_nr_of_ceph_osd_disks(self, profile):
142         """ get the number of ceph osd disks
143
144             Argument:
145
146             profile name
147
148             Return:
149
150             The number of ceph osd disks
151
152             Raise:
153
154             ConfigError in-case of an error
155         """
156         self.validate_root()
157         self.is_valid_profile(profile)
158
159         if 'nr_of_ceph_osd_disks' not in self.config[self.ROOT][profile]:
160             ceph_osd_disks = self._get_attribute(profile, 'ceph_osd_disks')
161             return len(ceph_osd_disks)
162         return self.config[self.ROOT][profile]['nr_of_ceph_osd_disks']
163
164     def get_profile_lvm_cinder_storage_partitions(self, profile):
165         """ get the lvm_cinder_storage_partitions
166
167             Argument:
168
169             profile name
170
171             Return:
172
173             The lvm_cinder_storage_partitions
174
175             Raise:
176
177             ConfigError in-case of an error
178         """
179         return self._get_attribute(profile, 'lvm_cinder_storage_partitions')
180
181     def get_profile_backend(self, profile):
182         """ get the storage profile backend
183
184             Argument:
185
186             profile name
187
188             Return:
189
190             The profile backend
191
192             Raise:
193
194             ConfigError in-case of an error
195         """
196         return self._get_attribute(profile, 'backend')
197
198     def get_profile_lvm_instance_storage_partitions(self, profile):
199         """ get the lvm_instance_storage_partitions
200
201             Argument:
202
203             profile name
204
205             Return:
206
207             The lvm_instance_storage_partitions
208
209             Raise:
210
211             ConfigError in-case of an error
212         """
213         return self._get_attribute(profile, 'lvm_instance_storage_partitions')
214
215     def get_profile_lvm_instance_cow_lv_storage_percentage(self, profile):
216         """ get the lvm_instance_cow_lv_storage_percentage
217
218             Argument:
219
220             profile name
221
222             Return:
223
224             The lvm_instance_cow_lv_storage_percentage
225
226             Raise:
227
228             ConfigError in-case of an error
229         """
230         return self._get_attribute(profile, 'lvm_instance_cow_lv_storage_percentage')
231
232     def get_profile_instance_storage_percentage(self, profile):
233         """ get the instance_storage_percentage
234
235             Argument:
236
237             profile name
238
239             Return:
240
241             The instance_storage_percentage
242
243             Raise:
244
245             ConfigError in-case of an error
246         """
247         return self._get_attribute(profile, 'instance_storage_percentage')
248
249     def get_profile_bare_lvm_mount_options(self, profile):
250         """ get the mount_options
251
252             Argument:
253
254             profile name
255
256             Return:
257
258             The mount_options
259
260             Raise:
261
262             ConfigError in-case of an error
263         """
264         return BARE_LVM_MOUNT_OPT 
265
266     def get_profile_bare_lvm_mount_dir(self, profile):
267         """ get the mount_dir
268
269             Argument:
270
271             profile name
272
273             Return:
274
275             The mount_dir
276
277             Raise:
278
279             ConfigError in-case of an error
280         """
281         return BARE_LVM_MOUNT_DIR
282
283     def get_profile_bare_lvm_lv_name(self, profile):
284         """ get the lv_name
285
286             Argument:
287
288             profile name
289
290             Return:
291
292             The lv_name
293
294             Raise:
295
296             ConfigError in-case of an error
297         """
298         return self._get_attribute(profile, 'lv_name')
299
300     def _get_attribute(self, profile, attribute):
301         """ get arbirary storage profile attribute
302
303             Arguments:
304
305             - profile name
306             - attribute name
307
308             Return:
309
310             The attribute
311
312             Raise:
313
314             ConfigError in-case of an error
315         """
316         self.validate_root()
317         self.is_valid_profile(profile)
318         if attribute not in self.config[self.ROOT][profile]:
319             raise configerror.ConfigError(
320                 'Profile %s does not have %s configured' % (attribute, profile))
321         return self.config[self.ROOT][profile][attribute]
322
323     def _get_optional_attribute(self, profile, attribute, default_value=""):
324         """ get arbirary optional storage profile attribute
325
326             Arguments:
327
328             - profile name
329             - attribute name
330
331             Return:
332
333             The attribute
334
335             Raise:
336
337             ConfigError in-case of an error
338         """
339         self.validate_root()
340         self.is_valid_profile(profile)
341         if attribute not in self.config[self.ROOT][profile]:
342             return default_value
343         return self.config[self.ROOT][profile][attribute]