7a2afb2e41e2264eaa95b5908aff82be3da6c3e6
[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 import string
22 from random import choice
23
24 CAAS_CONFIG_FILE_PATH = "/etc/cmframework/config/"
25 CAAS_CONFIG_FILE = "caas.yaml"
26 DEFAULT_CAAS_DNS_DOMAIN = "rec.io"
27 VNF_EMBEDDED_SOFT_EVICTION_THRESHOLD = "300Mi"
28 BM_SOFT_EVICTION_THRESHOLD = "4Gi"
29 VNF_EMBEDDED_HARD_EVICTION_THRESHOLD = "200Mi"
30 BM_HARD_EVICTION_THRESHOLD = "2Gi"
31 ADMIN_PWD_LENGTH = 20
32 DEFAULT_CAAS_INFRA_LOG_TYPE = 'elasticsearch'
33 AUDIT_DISK_LIMIT = 0.87
34 CAAS_AUDIT_DISK_RATIO = 0.25
35 DEFAULT_VALUES_MAP = { "docker0_cidr": "127.17.0.1/16",
36                        "oam_cidr": "10.244.0.0/16" }
37
38
39 class Config(config.Config):
40     valid_redundancy_models = ['non-redundant', 'active-cold-standby']
41
42     def __init__(self, confman):
43         super(Config, self).__init__(confman)
44         self.ROOT = 'cloud.caas'
45         self.DOMAIN = 'caas'
46
47     def init(self):
48         pass
49
50     @staticmethod
51     def validate():
52         print("validate")
53
54     def flavour_set(self):
55         hostsconf = self.confman.get_hosts_config_handler()
56         caas_masters = []
57         for host in hostsconf.get_hosts():
58             if 'caas_master' in hostsconf.get_service_profiles(host):
59                 caas_masters.append(host)
60         return "multi" if len(caas_masters) > 1 else "single"
61  
62     def get_caas_max_audit_size(self):
63         if self.is_caas_deployment():
64             return self.get_audit_disk_limit()*self.get_audit_disk_ratio()
65         else:
66             return 0
67
68     def set_dynamic_config(self):
69         if utils.is_virtualized():
70             self.config[self.ROOT]['vnf_embedded_deployment'] = self.get_vnf_flag()
71         user_conf = self.confman.get_users_config_handler()
72         self.set_caas_parameter('helm_home', "/home/{}/.helm".format(user_conf.get_admin_user()))
73         self.set_caas_parameter('flavour', self.flavour_set())
74         self.config[self.ROOT]['caas_max_audit_size'] = self.get_caas_max_audit_size()
75         admin_pwd = self.get_caas_parameter('admin_password')
76         self.config[self.ROOT]['admin_password'] = \
77                 admin_pwd if admin_pwd != '' else self.generate_pwd(ADMIN_PWD_LENGTH)
78         if not self.get_caas_parameter('dns_domain'):
79             self.set_caas_parameter('dns_domain', DEFAULT_CAAS_DNS_DOMAIN)
80         if not self.get_caas_parameter('infra_log_store'):
81             self.set_caas_parameter('infra_log_store', DEFAULT_CAAS_INFRA_LOG_TYPE)
82         if not self.get_caas_parameter('log_forwarding'):
83             self.set_caas_parameter('log_forwarding', [])
84         hostsconf = self.confman.get_hosts_config_handler()
85         hostsconf.set_nodeindex()
86
87     def set_static_config(self):
88         try:
89             template = jinja2.Environment(
90                 loader=jinja2.FileSystemLoader(
91                     CAAS_CONFIG_FILE_PATH)).get_template(CAAS_CONFIG_FILE)
92             with open(CAAS_CONFIG_FILE_PATH + CAAS_CONFIG_FILE) as config_file:
93                 data = yaml.load(config_file)
94             self.config[self.ROOT].update(
95                 self._template_config(template, self.config[self.ROOT], data))
96         except jinja2.exceptions.TemplateNotFound:
97             return
98         except Exception:
99             raise configerror.ConfigError("Unexpected issue has occured!")
100
101     def set_post_config(self):
102         self.config[self.ROOT]['swift_credential'] = \
103             dict(
104                 user=self.get_caas_parameter('swift_credential').get('user'),
105                 tenant=self.get_caas_parameter('swift_credential').get('tenant'),
106                 password=self.generate_pwd(ADMIN_PWD_LENGTH)
107             )
108
109     def set_default_values_to_optional_params(self):
110         for parameter_name, default_value in DEFAULT_VALUES_MAP.iteritems():
111             if self.config[self.ROOT].get(parameter_name, '') == '':
112                 self.config[self.ROOT][parameter_name] = default_value
113
114     @staticmethod
115     def _template_config(template, base_config, initial_data):
116         config_data = initial_data.copy()
117         config_data.update(base_config)
118         output_text = template.render(config_data)
119         previous_output_text = ""
120         while output_text != previous_output_text:
121             config_data = yaml.load(output_text)
122             config_data.update(base_config)
123             output_text = template.render(config_data)
124             previous_output_text = output_text
125         return yaml.load(output_text)
126
127     def add_defaults(self):
128         if not self.config.get('cloud.caas', ''):
129             return
130         self.set_dynamic_config()
131         self.set_static_config()
132         self.set_default_values_to_optional_params()
133         self.set_post_config()
134
135     def is_vnf_embedded_deployment(self):
136         return self.get_caas_only() and self.get_vnf_flag()
137
138     def get_vnf_flag(self):
139         return bool(self.config.get(self.ROOT, {}).get('vnf_embedded_deployment',
140                                                   False))
141
142     def get_caas_only(self):
143         return self.is_caas_deployment() and not self.is_openstack_deployment()
144
145     def is_openstack_deployment(self):
146         return bool(self.get_controller_hosts())
147
148     def is_caas_deployment(self):
149         return bool(self.get_caas_master_hosts())
150
151     def is_hybrid_deployment(self):
152         return self.is_caas_deployment() and self.is_openstack_deployment()
153
154     def get_caas_master_hosts(self):
155         service_profiles_lib = profiles.Profiles()
156         return self._get_hosts_for_service_profile(service_profiles_lib.get_caasmaster_service_profile())
157
158     def _get_hosts_for_service_profile(self, profile):
159         hostsconf = self.confman.get_hosts_config_handler()
160         return hostsconf.get_service_profile_hosts(profile)
161
162     def get_controller_hosts(self):
163         service_profiles_lib = profiles.Profiles()
164         return self._get_hosts_for_service_profile(service_profiles_lib.get_controller_service_profile())
165
166     def get_apiserver_in_hosts(self):
167         return self.config.get(self.ROOT, {}).get('apiserver_in_hosts', '')
168
169     def get_registry_url(self):
170         return self.config.get(self.ROOT, {}).get('registry_url', '')
171
172     def get_update_registry_url(self):
173         return self.config.get(self.ROOT, {}).get('update_registry_url', '')
174
175     def get_swift_url(self):
176         return self.config.get(self.ROOT, {}).get('swift_url', '')
177
178     def get_swift_update_url(self):
179         return self.config.get(self.ROOT, {}).get('swift_update_url', '')
180
181     def get_ldap_master_url(self):
182         return self.config.get(self.ROOT, {}).get('ldap_master_url', '')
183
184     def get_ldap_slave_url(self):
185         return self.config.get(self.ROOT, {}).get('ldap_slave_url', '')
186
187     def get_chart_repo_url(self):
188         return self.config.get(self.ROOT, {}).get('chart_repo_url', '')
189
190     def get_tiller_url(self):
191         return self.config.get(self.ROOT, {}).get('tiller_url', '')
192
193     def get_apiserver_svc_ip(self):
194         return self.config.get(self.ROOT, {}).get('apiserver_svc_ip', '')
195
196     def get_caas_parameter(self, parameter):
197         return self.config.get(self.ROOT, {}).get(parameter, '')
198
199     def set_caas_parameter(self, parameter, value):
200         self.config[self.ROOT][parameter] = value
201
202     def get_admin_password(self):
203         return self.config.get(self.ROOT, {}).get('admin_password')
204
205     @staticmethod
206     def generate_pwd(pwd_length):
207         character_pool = string.ascii_letters + string.digits
208         return ''.join(choice(character_pool) for i in range(pwd_length))
209
210     def get_kubernetes_domain(self):
211         return 'kubernetes.default.svc.{}'.format(
212             self.config.get(self.ROOT, {}).get('dns_domain', ''))
213
214     def get_caas_soft_eviction_threshold(self):
215         if self.is_vnf_embedded_deployment():
216             return VNF_EMBEDDED_SOFT_EVICTION_THRESHOLD
217         else:
218             return BM_SOFT_EVICTION_THRESHOLD
219
220     def get_caas_hard_eviction_threshold(self):
221         if self.is_vnf_embedded_deployment():
222             return VNF_EMBEDDED_HARD_EVICTION_THRESHOLD
223         else:
224             return BM_HARD_EVICTION_THRESHOLD
225
226     def get_audit_disk_ratio(self):
227         return CAAS_AUDIT_DISK_RATIO
228
229     def get_audit_disk_limit(self):
230         return AUDIT_DISK_LIMIT