Error correction, some refactoring
[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 DEFAULT_CAAS_DNS_DOMAIN = "rec.io"
25 VNF_EMBEDDED_SOFT_EVICTION_THRESHOLD = "300Mi"
26 BM_SOFT_EVICTION_THRESHOLD = "4Gi"
27 VNF_EMBEDDED_HARD_EVICTION_THRESHOLD = "200Mi"
28 BM_HARD_EVICTION_THRESHOLD = "2Gi"
29 DEFAULT_CAAS_INFRA_LOG_TYPE = 'remote_syslog'
30
31
32 class Config(config.Config):
33     valid_redundancy_models = ['non-redundant', 'active-cold-standby']
34
35     def __init__(self, confman):
36         super(Config, self).__init__(confman)
37         self.ROOT = 'cloud.caas'
38         self.DOMAIN = 'caas'
39
40     def init(self):
41         pass
42
43     def validate(self):
44         print("validate")
45
46     def flavour_set(self):
47         hostsconf = self.confman.get_hosts_config_handler()
48         caas_masters = []
49         for host in hostsconf.get_hosts():
50             if 'caas_master' in hostsconf.get_service_profiles(host):
51                 caas_masters.append(host)
52
53         if len(caas_masters) > 1:
54             return "multi"
55         else:
56             return "single"
57
58     def set_dynamic_config(self):
59         if utils.is_virtualized():
60             self.config[self.ROOT]['vnf_embedded_deployment'] = self.get_vnf_flag()
61         user_conf = self.confman.get_users_config_handler()
62         self.set_caas_parameter('helm_home', "/home/{}/.helm".format(user_conf.get_admin_user()))
63         self.set_caas_parameter('flavour', self.flavour_set())
64         if not self.get_caas_parameter('dns_domain'):
65             self.set_caas_parameter('dns_domain', DEFAULT_CAAS_DNS_DOMAIN)
66         if not self.get_caas_parameter('infra_log_store'):
67             self.set_caas_parameter('infra_log_store', DEFAULT_CAAS_INFRA_LOG_TYPE)
68         if not self.get_caas_parameter('log_forwarding'):
69             self.set_caas_parameter('log_forwarding', [])
70
71     def set_static_config(self):
72         try:
73             template = jinja2.Environment(
74                 loader=jinja2.FileSystemLoader(
75                     CAAS_CONFIG_FILE_PATH)).get_template(CAAS_CONFIG_FILE)
76             with open(CAAS_CONFIG_FILE_PATH + CAAS_CONFIG_FILE) as config_file:
77                 data = yaml.load(config_file)
78             self.config[self.ROOT].update(
79                 self._template_config(template, self.config[self.ROOT], data))
80         except jinja2.exceptions.TemplateNotFound:
81             return
82         except Exception:
83             raise configerror.ConfigError("Unexpected issue occured!")
84
85     @staticmethod
86     def _template_config(template, base_config, initial_data):
87         config_data = initial_data.copy()
88         config_data.update(base_config)
89         output_text = template.render(config_data)
90         previous_output_text = ""
91         while output_text != previous_output_text:
92             config_data = yaml.load(output_text)
93             config_data.update(base_config)
94             output_text = template.render(config_data)
95             previous_output_text = output_text
96         return yaml.load(output_text)
97
98     def add_defaults(self):
99         if not self.config.get('cloud.caas', ''):
100             return
101         self.set_dynamic_config()
102         self.set_static_config()
103
104     def is_vnf_embedded_deployment(self):
105         return self.get_caas_only() and self.get_vnf_flag()
106
107     def get_vnf_flag(self):
108         return bool(self.config.get(self.ROOT, {}).get('vnf_embedded_deployment',
109                                                   False))
110
111     def get_caas_only(self):
112         return self.is_caas_deployment() and not self.is_openstack_deployment()
113
114     def is_openstack_deployment(self):
115         return bool(self.get_controller_hosts())
116
117     def is_caas_deployment(self):
118         return bool(self.get_caas_master_hosts())
119
120     def is_hybrid_deployment(self):
121         return self.is_caas_deployment() and self.is_openstack_deployment()
122
123     def get_caas_master_hosts(self):
124         service_profiles_lib = profiles.Profiles()
125         return self._get_hosts_for_service_profile(service_profiles_lib.get_caasmaster_service_profile())
126
127     def _get_hosts_for_service_profile(self, profile):
128         hostsconf = self.confman.get_hosts_config_handler()
129         return hostsconf.get_service_profile_hosts(profile)
130
131     def get_controller_hosts(self):
132         service_profiles_lib = profiles.Profiles()
133         return self._get_hosts_for_service_profile(service_profiles_lib.get_controller_service_profile())
134
135     def get_apiserver_in_hosts(self):
136         return self.config.get(self.ROOT, {}).get('apiserver_in_hosts', '')
137
138     def get_registry_url(self):
139         return self.config.get(self.ROOT, {}).get('registry_url', '')
140
141     def get_update_registry_url(self):
142         return self.config.get(self.ROOT, {}).get('update_registry_url', '')
143
144     def get_swift_url(self):
145         return self.config.get(self.ROOT, {}).get('swift_url', '')
146
147     def get_swift_update_url(self):
148         return self.config.get(self.ROOT, {}).get('swift_update_url', '')
149
150     def get_ldap_master_url(self):
151         return self.config.get(self.ROOT, {}).get('ldap_master_url', '')
152
153     def get_ldap_slave_url(self):
154         return self.config.get(self.ROOT, {}).get('ldap_slave_url', '')
155
156     def get_chart_repo_url(self):
157         return self.config.get(self.ROOT, {}).get('chart_repo_url', '')
158
159     def get_tiller_url(self):
160         return self.config.get(self.ROOT, {}).get('tiller_url', '')
161
162     def get_apiserver_svc_ip(self):
163         return self.config.get(self.ROOT, {}).get('apiserver_svc_ip', '')
164
165     def get_caas_parameter(self, parameter):
166         return self.config.get(self.ROOT, {}).get(parameter, '')
167
168     def set_caas_parameter(self, parameter, value):
169         self.config[self.ROOT][parameter] = value
170
171     def get_kubernetes_domain(self):
172         return 'kubernetes.default.svc.{}'.format(
173             self.config.get(self.ROOT, {}).get('dns_domain', ''))
174
175     def get_caas_soft_eviction_threshold(self):
176         if self.is_vnf_embedded_deployment():
177             return VNF_EMBEDDED_SOFT_EVICTION_THRESHOLD
178         else:
179             return BM_SOFT_EVICTION_THRESHOLD
180
181     def get_caas_hard_eviction_threshold(self):
182         if self.is_vnf_embedded_deployment():
183             return VNF_EMBEDDED_HARD_EVICTION_THRESHOLD
184         else:
185             return BM_HARD_EVICTION_THRESHOLD