Initial commit
[ta/config-manager.git] / cmdatahandlers / src / cmdatahandlers / caas / 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 config
16 from cmdatahandlers.api import utils
17 from cmdatahandlers.api import configerror
18 from serviceprofiles import profiles
19 import yaml
20 import jinja2
21
22 CAAS_CONFIG_FILE_PATH = "/etc/cmframework/config/"
23 CAAS_CONFIG_FILE = "caas.yaml"
24
25
26 class Config(config.Config):
27     valid_redundancy_models = ['non-redundant', 'active-cold-standby']
28
29     def __init__(self, confman):
30         super(Config, self).__init__(confman)
31         self.ROOT = 'cloud.caas'
32         self.DOMAIN = 'caas'
33
34     def init(self):
35         pass
36
37     def validate(self):
38         print("validate")
39
40     def flavour_set(self):
41         hostsconf = self.confman.get_hosts_config_handler()
42         caas_masters = []
43         for host in hostsconf.get_hosts():
44             if 'caas_master' in hostsconf.get_service_profiles(host):
45                 caas_masters.append(host)
46
47         if len(caas_masters) > 1:
48             return "multi"
49         else:
50             return "single"
51
52     def set_dynamic_config(self):
53         if utils.is_virtualized():
54             self.config[self.ROOT]['vnf_embedded_deployment'] = self.get_vnf_flag()
55         user_conf = self.confman.get_users_config_handler()
56         self.config[self.ROOT]['helm_home'] = "/home/" + user_conf.get_admin_user() + "/.helm"
57         self.config[self.ROOT]['flavour'] = self.flavour_set()
58
59     def set_static_config(self):
60         try:
61             template = jinja2.Environment(
62                 loader=jinja2.FileSystemLoader(
63                     CAAS_CONFIG_FILE_PATH)).get_template(CAAS_CONFIG_FILE)
64             with open(CAAS_CONFIG_FILE_PATH + CAAS_CONFIG_FILE) as config_file:
65                 data = yaml.load(config_file)
66             outputText = template.render(data)
67             config_data = yaml.load(outputText)
68             for key in config_data:
69                 self.config[self.ROOT][key] = config_data[key]
70         except jinja2.exceptions.TemplateNotFound:
71             return
72         except Exception:
73             raise configerror.ConfigError("Unexpected issue occured!")
74
75     def add_defaults(self):
76         if not self.config.get('cloud.caas', ''):
77             return
78         self.set_dynamic_config()
79         self.set_static_config()
80
81     def is_vnf_embedded_deployment(self):
82         return (self.get_caas_only() and self.get_vnf_flag())
83
84     def get_vnf_flag(self):
85         return bool(self.config.get(self.ROOT, {}).get('vnf_embedded_deployment',
86                                                   False))
87
88     def get_caas_only(self):
89         return self.is_caas_deployment() and not self.is_openstack_deployment()
90
91     def is_openstack_deployment(self):
92         return bool(self.get_controller_hosts())
93
94     def is_caas_deployment(self):
95         return bool(self.get_caas_master_hosts())
96
97     def is_hybrid_deployment(self):
98         return self.is_caas_deployment() and self.is_openstack_deployment()
99
100     def get_caas_master_hosts(self):
101         service_profiles_lib = profiles.Profiles()
102         return self._get_hosts_for_service_profile(service_profiles_lib.get_caasmaster_service_profile())
103
104     def _get_hosts_for_service_profile(self, profile):
105         hostsconf = self.confman.get_hosts_config_handler()
106         return hostsconf.get_service_profile_hosts(profile)
107
108     def get_controller_hosts(self):
109         service_profiles_lib = profiles.Profiles()
110         return self._get_hosts_for_service_profile(service_profiles_lib.get_controller_service_profile())
111
112     def get_apiserver_in_hosts(self):
113         return self.config.get(self.ROOT, {}).get('apiserver_in_hosts', '')
114
115     def get_registry_url(self):
116         return self.config.get(self.ROOT, {}).get('registry_url', '')
117
118     def get_update_registry_url(self):
119         return self.config.get(self.ROOT, {}).get('update_registry_url', '')
120
121     def get_swift_url(self):
122         return self.config.get(self.ROOT, {}).get('swift_url', '')
123
124     def get_swift_update_url(self):
125         return self.config.get(self.ROOT, {}).get('swift_update_url', '')
126
127     def get_ldap_master_url(self):
128         return self.config.get(self.ROOT, {}).get('ldap_master_url', '')
129
130     def get_ldap_slave_url(self):
131         return self.config.get(self.ROOT, {}).get('ldap_slave_url', '')
132
133     def get_chart_repo_url(self):
134         return self.config.get(self.ROOT, {}).get('chart_repo_url', '')
135
136     def get_tiller_url(self):
137         return self.config.get(self.ROOT, {}).get('tiller_url', '')
138
139     def get_apiserver_svc_ip(self):
140         return self.config.get(self.ROOT, {}).get('apiserver_svc_ip', '')
141
142     def get_caas_parameter(self, parameter):
143         return self.config.get(self.ROOT, {}).get(parameter, '')