e5275c54f2e7b18617728e21c261d9264037ccf1
[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 = 'remote_syslog'
33
34
35 class Config(config.Config):
36     valid_redundancy_models = ['non-redundant', 'active-cold-standby']
37
38     def __init__(self, confman):
39         super(Config, self).__init__(confman)
40         self.ROOT = 'cloud.caas'
41         self.DOMAIN = 'caas'
42
43     def init(self):
44         pass
45
46     @staticmethod
47     def validate():
48         print("validate")
49
50     def flavour_set(self):
51         hostsconf = self.confman.get_hosts_config_handler()
52         caas_masters = []
53         for host in hostsconf.get_hosts():
54             if 'caas_master' in hostsconf.get_service_profiles(host):
55                 caas_masters.append(host)
56         return "multi" if len(caas_masters) > 1 else "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         hostsconf = self.confman.get_hosts_config_handler()
71         hostsconf.set_nodeindex()
72
73     def set_static_config(self):
74         try:
75             template = jinja2.Environment(
76                 loader=jinja2.FileSystemLoader(
77                     CAAS_CONFIG_FILE_PATH)).get_template(CAAS_CONFIG_FILE)
78             with open(CAAS_CONFIG_FILE_PATH + CAAS_CONFIG_FILE) as config_file:
79                 data = yaml.load(config_file)
80             self.config[self.ROOT].update(
81                 self._template_config(template, self.config[self.ROOT], data))
82         except jinja2.exceptions.TemplateNotFound:
83             return
84         except Exception:
85             raise configerror.ConfigError("Unexpected issue has occured!")
86
87     def set_post_config(self):
88         self.config[self.ROOT]['swift_credential'] = \
89             dict(
90                 user=self.get_caas_parameter('swift_credential').get('user'),
91                 tenant=self.get_caas_parameter('swift_credential').get('tenant'),
92                 password=self.generate_pwd(ADMIN_PWD_LENGTH)
93             )
94
95     @staticmethod
96     def _template_config(template, base_config, initial_data):
97         config_data = initial_data.copy()
98         config_data.update(base_config)
99         output_text = template.render(config_data)
100         previous_output_text = ""
101         while output_text != previous_output_text:
102             config_data = yaml.load(output_text)
103             config_data.update(base_config)
104             output_text = template.render(config_data)
105             previous_output_text = output_text
106         return yaml.load(output_text)
107
108     def add_defaults(self):
109         if not self.config.get('cloud.caas', ''):
110             return
111         self.set_dynamic_config()
112         self.set_static_config()
113         self.set_post_config()
114
115     def is_vnf_embedded_deployment(self):
116         return self.get_caas_only() and self.get_vnf_flag()
117
118     def get_vnf_flag(self):
119         return bool(self.config.get(self.ROOT, {}).get('vnf_embedded_deployment',
120                                                   False))
121
122     def get_caas_only(self):
123         return self.is_caas_deployment() and not self.is_openstack_deployment()
124
125     def is_openstack_deployment(self):
126         return bool(self.get_controller_hosts())
127
128     def is_caas_deployment(self):
129         return bool(self.get_caas_master_hosts())
130
131     def is_hybrid_deployment(self):
132         return self.is_caas_deployment() and self.is_openstack_deployment()
133
134     def get_caas_master_hosts(self):
135         service_profiles_lib = profiles.Profiles()
136         return self._get_hosts_for_service_profile(service_profiles_lib.get_caasmaster_service_profile())
137
138     def _get_hosts_for_service_profile(self, profile):
139         hostsconf = self.confman.get_hosts_config_handler()
140         return hostsconf.get_service_profile_hosts(profile)
141
142     def get_controller_hosts(self):
143         service_profiles_lib = profiles.Profiles()
144         return self._get_hosts_for_service_profile(service_profiles_lib.get_controller_service_profile())
145
146     def get_apiserver_in_hosts(self):
147         return self.config.get(self.ROOT, {}).get('apiserver_in_hosts', '')
148
149     def get_registry_url(self):
150         return self.config.get(self.ROOT, {}).get('registry_url', '')
151
152     def get_update_registry_url(self):
153         return self.config.get(self.ROOT, {}).get('update_registry_url', '')
154
155     def get_swift_url(self):
156         return self.config.get(self.ROOT, {}).get('swift_url', '')
157
158     def get_swift_update_url(self):
159         return self.config.get(self.ROOT, {}).get('swift_update_url', '')
160
161     def get_ldap_master_url(self):
162         return self.config.get(self.ROOT, {}).get('ldap_master_url', '')
163
164     def get_ldap_slave_url(self):
165         return self.config.get(self.ROOT, {}).get('ldap_slave_url', '')
166
167     def get_chart_repo_url(self):
168         return self.config.get(self.ROOT, {}).get('chart_repo_url', '')
169
170     def get_tiller_url(self):
171         return self.config.get(self.ROOT, {}).get('tiller_url', '')
172
173     def get_apiserver_svc_ip(self):
174         return self.config.get(self.ROOT, {}).get('apiserver_svc_ip', '')
175
176     def get_caas_parameter(self, parameter):
177         return self.config.get(self.ROOT, {}).get(parameter, '')
178
179     def set_caas_parameter(self, parameter, value):
180         self.config[self.ROOT][parameter] = value
181
182     @staticmethod
183     def generate_pwd(pwd_length):
184         character_pool = string.ascii_letters + string.digits
185         return ''.join(choice(character_pool) for i in range(pwd_length))
186
187     def get_kubernetes_domain(self):
188         return 'kubernetes.default.svc.{}'.format(
189             self.config.get(self.ROOT, {}).get('dns_domain', ''))
190
191     def get_caas_soft_eviction_threshold(self):
192         if self.is_vnf_embedded_deployment():
193             return VNF_EMBEDDED_SOFT_EVICTION_THRESHOLD
194         else:
195             return BM_SOFT_EVICTION_THRESHOLD
196
197     def get_caas_hard_eviction_threshold(self):
198         if self.is_vnf_embedded_deployment():
199             return VNF_EMBEDDED_HARD_EVICTION_THRESHOLD
200         else:
201             return BM_HARD_EVICTION_THRESHOLD