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 = 'remote_syslog'
35 class Config(config.Config):
36 valid_redundancy_models = ['non-redundant', 'active-cold-standby']
38 def __init__(self, confman):
39 super(Config, self).__init__(confman)
40 self.ROOT = 'cloud.caas'
50 def flavour_set(self):
51 hostsconf = self.confman.get_hosts_config_handler()
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"
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', [])
71 def set_static_config(self):
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:
83 raise configerror.ConfigError("Unexpected issue has occured!")
85 def set_post_config(self):
86 self.config[self.ROOT]['swift_credential'] = \
88 user=self.get_caas_parameter('swift_credential').get('user'),
89 tenant=self.get_caas_parameter('swift_credential').get('tenant'),
90 password=self.generate_pwd(ADMIN_PWD_LENGTH)
94 def _template_config(template, base_config, initial_data):
95 config_data = initial_data.copy()
96 config_data.update(base_config)
97 output_text = template.render(config_data)
98 previous_output_text = ""
99 while output_text != previous_output_text:
100 config_data = yaml.load(output_text)
101 config_data.update(base_config)
102 output_text = template.render(config_data)
103 previous_output_text = output_text
104 return yaml.load(output_text)
106 def add_defaults(self):
107 if not self.config.get('cloud.caas', ''):
109 self.set_dynamic_config()
110 self.set_static_config()
111 self.set_post_config()
113 def is_vnf_embedded_deployment(self):
114 return self.get_caas_only() and self.get_vnf_flag()
116 def get_vnf_flag(self):
117 return bool(self.config.get(self.ROOT, {}).get('vnf_embedded_deployment',
120 def get_caas_only(self):
121 return self.is_caas_deployment() and not self.is_openstack_deployment()
123 def is_openstack_deployment(self):
124 return bool(self.get_controller_hosts())
126 def is_caas_deployment(self):
127 return bool(self.get_caas_master_hosts())
129 def is_hybrid_deployment(self):
130 return self.is_caas_deployment() and self.is_openstack_deployment()
132 def get_caas_master_hosts(self):
133 service_profiles_lib = profiles.Profiles()
134 return self._get_hosts_for_service_profile(service_profiles_lib.get_caasmaster_service_profile())
136 def _get_hosts_for_service_profile(self, profile):
137 hostsconf = self.confman.get_hosts_config_handler()
138 return hostsconf.get_service_profile_hosts(profile)
140 def get_controller_hosts(self):
141 service_profiles_lib = profiles.Profiles()
142 return self._get_hosts_for_service_profile(service_profiles_lib.get_controller_service_profile())
144 def get_apiserver_in_hosts(self):
145 return self.config.get(self.ROOT, {}).get('apiserver_in_hosts', '')
147 def get_registry_url(self):
148 return self.config.get(self.ROOT, {}).get('registry_url', '')
150 def get_update_registry_url(self):
151 return self.config.get(self.ROOT, {}).get('update_registry_url', '')
153 def get_swift_url(self):
154 return self.config.get(self.ROOT, {}).get('swift_url', '')
156 def get_swift_update_url(self):
157 return self.config.get(self.ROOT, {}).get('swift_update_url', '')
159 def get_ldap_master_url(self):
160 return self.config.get(self.ROOT, {}).get('ldap_master_url', '')
162 def get_ldap_slave_url(self):
163 return self.config.get(self.ROOT, {}).get('ldap_slave_url', '')
165 def get_chart_repo_url(self):
166 return self.config.get(self.ROOT, {}).get('chart_repo_url', '')
168 def get_tiller_url(self):
169 return self.config.get(self.ROOT, {}).get('tiller_url', '')
171 def get_apiserver_svc_ip(self):
172 return self.config.get(self.ROOT, {}).get('apiserver_svc_ip', '')
174 def get_caas_parameter(self, parameter):
175 return self.config.get(self.ROOT, {}).get(parameter, '')
177 def set_caas_parameter(self, parameter, value):
178 self.config[self.ROOT][parameter] = value
181 def generate_pwd(pwd_length):
182 character_pool = string.ascii_letters + string.digits
183 return ''.join(choice(character_pool) for i in range(pwd_length))
185 def get_kubernetes_domain(self):
186 return 'kubernetes.default.svc.{}'.format(
187 self.config.get(self.ROOT, {}).get('dns_domain', ''))
189 def get_caas_soft_eviction_threshold(self):
190 if self.is_vnf_embedded_deployment():
191 return VNF_EMBEDDED_SOFT_EVICTION_THRESHOLD
193 return BM_SOFT_EVICTION_THRESHOLD
195 def get_caas_hard_eviction_threshold(self):
196 if self.is_vnf_embedded_deployment():
197 return VNF_EMBEDDED_HARD_EVICTION_THRESHOLD
199 return BM_HARD_EVICTION_THRESHOLD