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