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
7 # http://www.apache.org/licenses/LICENSE-2.0
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.
15 from cmdatahandlers.api import config
16 from cmdatahandlers.api import utils
17 from cmdatahandlers.api import configerror
18 from serviceprofiles import profiles
22 from random import choice
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"
32 DEFAULT_CAAS_INFRA_LOG_TYPE = 'elasticsearch'
33 AUDIT_DISK_LIMIT = 0.87
34 CAAS_AUDIT_DISK_RATIO = 0.25
37 class Config(config.Config):
38 valid_redundancy_models = ['non-redundant', 'active-cold-standby']
40 def __init__(self, confman):
41 super(Config, self).__init__(confman)
42 self.ROOT = 'cloud.caas'
52 def flavour_set(self):
53 hostsconf = self.confman.get_hosts_config_handler()
55 for host in hostsconf.get_hosts():
56 if 'caas_master' in hostsconf.get_service_profiles(host):
57 caas_masters.append(host)
58 return "multi" if len(caas_masters) > 1 else "single"
60 def get_caas_max_audit_size(self):
61 if self.is_caas_deployment():
62 return self.get_audit_disk_limit()*self.get_audit_disk_ratio()
66 def set_dynamic_config(self):
67 if utils.is_virtualized():
68 self.config[self.ROOT]['vnf_embedded_deployment'] = self.get_vnf_flag()
69 user_conf = self.confman.get_users_config_handler()
70 self.set_caas_parameter('helm_home', "/home/{}/.helm".format(user_conf.get_admin_user()))
71 self.set_caas_parameter('flavour', self.flavour_set())
72 self.config[self.ROOT]['caas_max_audit_size'] = self.get_caas_max_audit_size()
73 admin_pwd = self.get_caas_parameter('admin_password')
74 self.config[self.ROOT]['admin_password'] = \
75 admin_pwd if admin_pwd != '' else self.generate_pwd(ADMIN_PWD_LENGTH)
76 if not self.get_caas_parameter('dns_domain'):
77 self.set_caas_parameter('dns_domain', DEFAULT_CAAS_DNS_DOMAIN)
78 if not self.get_caas_parameter('infra_log_store'):
79 self.set_caas_parameter('infra_log_store', DEFAULT_CAAS_INFRA_LOG_TYPE)
80 if not self.get_caas_parameter('log_forwarding'):
81 self.set_caas_parameter('log_forwarding', [])
82 hostsconf = self.confman.get_hosts_config_handler()
83 hostsconf.set_nodeindex()
85 def set_static_config(self):
87 template = jinja2.Environment(
88 loader=jinja2.FileSystemLoader(
89 CAAS_CONFIG_FILE_PATH)).get_template(CAAS_CONFIG_FILE)
90 with open(CAAS_CONFIG_FILE_PATH + CAAS_CONFIG_FILE) as config_file:
91 data = yaml.load(config_file)
92 self.config[self.ROOT].update(
93 self._template_config(template, self.config[self.ROOT], data))
94 except jinja2.exceptions.TemplateNotFound:
97 raise configerror.ConfigError("Unexpected issue has occured!")
99 def set_post_config(self):
100 self.config[self.ROOT]['swift_credential'] = \
102 user=self.get_caas_parameter('swift_credential').get('user'),
103 tenant=self.get_caas_parameter('swift_credential').get('tenant'),
104 password=self.generate_pwd(ADMIN_PWD_LENGTH)
108 def _template_config(template, base_config, initial_data):
109 config_data = initial_data.copy()
110 config_data.update(base_config)
111 output_text = template.render(config_data)
112 previous_output_text = ""
113 while output_text != previous_output_text:
114 config_data = yaml.load(output_text)
115 config_data.update(base_config)
116 output_text = template.render(config_data)
117 previous_output_text = output_text
118 return yaml.load(output_text)
120 def add_defaults(self):
121 if not self.config.get('cloud.caas', ''):
123 self.set_dynamic_config()
124 self.set_static_config()
125 self.set_post_config()
127 def is_vnf_embedded_deployment(self):
128 return self.get_caas_only() and self.get_vnf_flag()
130 def get_vnf_flag(self):
131 return bool(self.config.get(self.ROOT, {}).get('vnf_embedded_deployment',
134 def get_caas_only(self):
135 return self.is_caas_deployment() and not self.is_openstack_deployment()
137 def is_openstack_deployment(self):
138 return bool(self.get_controller_hosts())
140 def is_caas_deployment(self):
141 return bool(self.get_caas_master_hosts())
143 def is_hybrid_deployment(self):
144 return self.is_caas_deployment() and self.is_openstack_deployment()
146 def get_caas_master_hosts(self):
147 service_profiles_lib = profiles.Profiles()
148 return self._get_hosts_for_service_profile(service_profiles_lib.get_caasmaster_service_profile())
150 def _get_hosts_for_service_profile(self, profile):
151 hostsconf = self.confman.get_hosts_config_handler()
152 return hostsconf.get_service_profile_hosts(profile)
154 def get_controller_hosts(self):
155 service_profiles_lib = profiles.Profiles()
156 return self._get_hosts_for_service_profile(service_profiles_lib.get_controller_service_profile())
158 def get_apiserver_in_hosts(self):
159 return self.config.get(self.ROOT, {}).get('apiserver_in_hosts', '')
161 def get_registry_url(self):
162 return self.config.get(self.ROOT, {}).get('registry_url', '')
164 def get_update_registry_url(self):
165 return self.config.get(self.ROOT, {}).get('update_registry_url', '')
167 def get_swift_url(self):
168 return self.config.get(self.ROOT, {}).get('swift_url', '')
170 def get_swift_update_url(self):
171 return self.config.get(self.ROOT, {}).get('swift_update_url', '')
173 def get_ldap_master_url(self):
174 return self.config.get(self.ROOT, {}).get('ldap_master_url', '')
176 def get_ldap_slave_url(self):
177 return self.config.get(self.ROOT, {}).get('ldap_slave_url', '')
179 def get_chart_repo_url(self):
180 return self.config.get(self.ROOT, {}).get('chart_repo_url', '')
182 def get_tiller_url(self):
183 return self.config.get(self.ROOT, {}).get('tiller_url', '')
185 def get_apiserver_svc_ip(self):
186 return self.config.get(self.ROOT, {}).get('apiserver_svc_ip', '')
188 def get_caas_parameter(self, parameter):
189 return self.config.get(self.ROOT, {}).get(parameter, '')
191 def set_caas_parameter(self, parameter, value):
192 self.config[self.ROOT][parameter] = value
194 def get_admin_password(self):
195 return self.config.get(self.ROOT, {}).get('admin_password')
198 def generate_pwd(pwd_length):
199 character_pool = string.ascii_letters + string.digits
200 return ''.join(choice(character_pool) for i in range(pwd_length))
202 def get_kubernetes_domain(self):
203 return 'kubernetes.default.svc.{}'.format(
204 self.config.get(self.ROOT, {}).get('dns_domain', ''))
206 def get_caas_soft_eviction_threshold(self):
207 if self.is_vnf_embedded_deployment():
208 return VNF_EMBEDDED_SOFT_EVICTION_THRESHOLD
210 return BM_SOFT_EVICTION_THRESHOLD
212 def get_caas_hard_eviction_threshold(self):
213 if self.is_vnf_embedded_deployment():
214 return VNF_EMBEDDED_HARD_EVICTION_THRESHOLD
216 return BM_HARD_EVICTION_THRESHOLD
218 def get_audit_disk_ratio(self):
219 return CAAS_AUDIT_DISK_RATIO
221 def get_audit_disk_limit(self):
222 return AUDIT_DISK_LIMIT