Plugins for configuration manager
[ta/cm-plugins.git] / userconfighandlers / localstorage / localstorage.py
1 #! /usr/bin/python
2 # Copyright 2019 Nokia
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7 #
8 #    http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15
16 import os
17 import yaml
18
19 from cmframework.apis import cmuserconfig
20 from cmframework.apis import cmerror
21 from serviceprofiles import profiles
22
23 extra_localstoragedict = {'cephcontroller':{}}
24
25
26 class localstorage(cmuserconfig.CMUserConfigPlugin):
27     localstorageconfdir = '/etc/opt/localstorage'
28
29     def __init__(self):
30         super(localstorage, self).__init__()
31         profs = profiles.Profiles()
32         allprofs = profs.get_profiles()
33         self.host_group_localstoragedict = {}
34         for name, prof in allprofs.iteritems():
35             self.host_group_localstoragedict[name] = {}
36         self.host_group_localstoragedict.update(extra_localstoragedict)
37
38     def handle(self, confman):
39         try:
40             localstorageconf = confman.get_localstorage_config_handler()
41             deploy_type_dir = os.path.join(self.localstorageconfdir,
42                                            self._get_deployment_type(confman))
43             for localstoragefile in os.listdir(deploy_type_dir):
44                 localstoragefilepath = os.path.join(deploy_type_dir, localstoragefile)
45                 localstorageconfdict = yaml.load(open(localstoragefilepath))
46                 logical_volumes = localstorageconfdict.get("logical_volumes", [])
47                 for host_group in localstorageconfdict.get("service_profiles", []):
48                     if host_group not in self.host_group_localstoragedict.keys():
49                         raise cmerror.CMError(
50                             "%s: Not a valid host group. Check configuration in %s"
51                             % (host_group, localstoragefilepath))
52                     self._add_logical_volumes_to_host_group(logical_volumes, host_group)
53
54             localstorageconf.add_localstorage(self.host_group_localstoragedict)
55
56         except Exception as exp:
57             raise cmerror.CMError(str(exp))
58
59     def _get_deployment_type(self, confman):
60         caasconf = confman.get_caas_config_handler()
61         hostsconf = confman.get_hosts_config_handler()
62         if caasconf.get_caas_only():
63             return "caas"
64         if (hostsconf.get_service_profile_hosts('controller') 
65            and hostsconf.get_service_profile_hosts('caas_master')):
66             return "multinode_hybrid"
67         return "openstack"
68
69     def _add_logical_volumes_to_host_group(self, lvs, host_group):
70         lv_data = {lv["lvm_name"]: lv for lv in lvs}
71         self.host_group_localstoragedict[host_group].update(lv_data)