Fix the objref issue.
[pti/o2.git] / o2common / config / config.py
1 # Copyright (C) 2021-2022 Wind River Systems, Inc.
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 import os
16 import sys
17 from urllib.parse import urlparse
18
19 from o2common import config
20 from o2common.helper import o2logging
21 logger = o2logging.get_logger(__name__)
22
23
24 _DEFAULT_DCMANAGER_URL = "http://192.168.204.1:8119/v1.0"
25 _DEFAULT_STX_URL = "http://192.168.204.1:5000/v3"
26
27
28 def get_config_path():
29     path = os.environ.get("O2APP_CONFIG", "/configs/o2app.conf")
30     return path
31
32
33 def get_smo_ca_config_path():
34     path = os.environ.get("SMO_CA_CONFIG", "/configs/smoca.crt")
35     return path
36
37
38 def get_postgres_uri():
39     # host = os.environ.get("DB_HOST", "localhost")
40     # port = 54321 if host == "localhost" else 5432
41     host = "localhost"
42     port = 5432
43     password = os.environ.get("DB_PASSWORD", "o2ims123")
44     user, db_name = "o2ims", "o2ims"
45     return f"postgresql://{user}:{password}@{host}:{port}/{db_name}"
46
47
48 def get_api_url():
49     # host_interal = os.environ.get("API_HOST", "localhost")
50     host_interal = "localhost"
51     host_external = os.environ.get("API_HOST_EXTERNAL_FLOATING")
52     if config.conf.OCLOUD.API_HOST_EXTERNAL_FLOATING is not None and \
53             config.conf.OCLOUD.API_HOST_EXTERNAL_FLOATING != '':
54         host_external = config.conf.OCLOUD.API_HOST_EXTERNAL_FLOATING
55     host = host_interal if host_external is None or host_external == '' \
56         else host_external
57
58     port_internal = 5005 if host == "localhost" else 80
59     port_external = 30205
60     port = port_internal if host_external is None or host_external == '' \
61         else port_external
62     return f"https://{host}:{port}"
63
64
65 def get_root_api_base():
66     return "/"
67
68
69 def get_o2ims_api_base():
70     return get_root_api_base() + 'o2ims-infrastructureInventory'
71
72
73 def get_o2ims_monitoring_api_v1():
74     return '/v1'
75
76
77 def get_o2ims_inventory_api_v1():
78     return '/v1'
79
80
81 def get_o2ims_monitoring_api_base():
82     return get_root_api_base() + 'o2ims-infrastructureMonitoring'
83
84
85 def get_o2dms_api_base():
86     return get_root_api_base() + "o2dms/v1"
87
88
89 def get_redis_host_and_port():
90     # host = os.environ.get("REDIS_HOST", "localhost")
91     # port = 63791 if host == "localhost" else 6379
92     host = "localhost"
93     port = 6379
94     return dict(host=host, port=port)
95
96
97 def get_smo_o2endpoint():
98     smo_o2endpoint = os.environ.get(
99         "SMO_O2_ENDPOINT", "http://localhost/smo_sim")
100     return smo_o2endpoint
101
102
103 def get_stx_client_args():
104     client_args = dict(
105         auth_url=os.environ.get('OS_AUTH_URL', _DEFAULT_STX_URL),
106         username=os.environ.get('OS_USERNAME', "admin"),
107         api_key=os.environ.get('OS_PASSWORD', "fakepasswd1"),
108         project_name=os.environ.get('OS_PROJECT_NAME', "admin"),
109     )
110     if config.conf.OCLOUD.OS_AUTH_URL is not None and \
111             config.conf.OCLOUD.OS_AUTH_URL != '':
112         client_args['auth_url'] = config.conf.OCLOUD.OS_AUTH_URL
113     if config.conf.OCLOUD.OS_USERNAME is not None and \
114             config.conf.OCLOUD.OS_USERNAME != '':
115         client_args['username'] = config.conf.OCLOUD.OS_USERNAME
116     if config.conf.OCLOUD.OS_PASSWORD is not None and \
117             config.conf.OCLOUD.OS_PASSWORD != '':
118         client_args['api_key'] = config.conf.OCLOUD.OS_PASSWORD
119     if config.conf.OCLOUD.OS_PROJECT_NAME is not None and \
120             config.conf.OCLOUD.OS_PROJECT_NAME != '':
121         client_args['project_name'] = config.conf.OCLOUD.OS_PROJECT_NAME
122     return client_args
123
124
125 def get_stx_access_info(region_name="RegionOne", subcloud_hostname: str = "",
126                         sub_is_https: bool = False):
127     # authurl = os.environ.get("STX_AUTH_URL", "http://192.168.204.1:5000/v3")
128     # username = os.environ.get("STX_USERNAME", "admin")
129     # pswd = os.environ.get("STX_PASSWORD", "passwd1")
130     # stx_access_info = (authurl, username, pswd)
131     try:
132         # client_args = dict(
133         #     auth_url=os.environ.get('OS_AUTH_URL', _DEFAULT_STX_URL),
134         #     username=os.environ.get('OS_USERNAME', "admin"),
135         #     api_key=os.environ.get('OS_PASSWORD', "fakepasswd1"),
136         #     project_name=os.environ.get('OS_PROJECT_NAME', "admin"),
137         # )
138         client_args = get_stx_client_args()
139     except KeyError:
140         logger.error('Please source your RC file before execution, '
141                      'e.g.: `source ~/downloads/admin-rc.sh`')
142         sys.exit(1)
143
144     os_client_args = {}
145     for key, val in client_args.items():
146         os_client_args['os_{key}'.format(key=key)] = val
147     if "" != subcloud_hostname:
148         orig_auth_url = urlparse(_DEFAULT_STX_URL)
149         new_auth_url = orig_auth_url._replace(
150             netloc=orig_auth_url.netloc.replace(
151                 orig_auth_url.hostname, subcloud_hostname))
152         # new_auth_url = new_auth_url._replace(
153         #     netloc=new_auth_url.netloc.replace(str(new_auth_url.port),
154         # "18002"))
155         if sub_is_https:
156             new_auth_url = new_auth_url._replace(
157                 scheme=new_auth_url.scheme.
158                 replace(new_auth_url.scheme, 'https'))
159             os_client_args['insecure'] = True
160         os_client_args['os_auth_url'] = new_auth_url.geturl()
161         os_client_args['os_endpoint_type'] = 'public'
162     # os_client_args['system_url'] = os_client_args['os_auth_url']
163     os_client_args['os_password'] = os_client_args.pop('os_api_key')
164     os_client_args['os_region_name'] = region_name
165     os_client_args['api_version'] = 1
166     # os_client_args['user_domain_name'] = 'Default'
167     # os_client_args['project_domain_name'] = 'Default'
168     return os_client_args
169
170
171 def get_dc_access_info():
172     try:
173         # client_args = dict(
174         #     auth_url=os.environ.get('OS_AUTH_URL', _DEFAULT_STX_URL),
175         #     username=os.environ.get('OS_USERNAME', "admin"),
176         #     api_key=os.environ.get('OS_PASSWORD', "fakepasswd1"),
177         #     project_name=os.environ.get('OS_PROJECT_NAME', "admin"),
178         # )
179         client_args = get_stx_client_args()
180     except KeyError:
181         logger.error('Please source your RC file before execution, '
182                      'e.g.: `source ~/downloads/admin-rc.sh`')
183         sys.exit(1)
184
185     os_client_args = {}
186     for key, val in client_args.items():
187         os_client_args['os_{key}'.format(key=key)] = val
188     auth_url = urlparse(os_client_args.pop('os_auth_url'))
189     dcmanager_url = urlparse(_DEFAULT_DCMANAGER_URL)
190     dcmanager_url = dcmanager_url._replace(netloc=dcmanager_url.netloc.replace(
191         dcmanager_url.hostname, auth_url.hostname))
192
193     os_client_args['dcmanager_url'] = dcmanager_url.geturl()
194     os_client_args['auth_url'] = auth_url.geturl()
195     os_client_args['username'] = os_client_args.pop('os_username')
196     os_client_args['api_key'] = os_client_args.pop('os_api_key')
197     os_client_args['project_name'] = os_client_args.pop('os_project_name')
198     os_client_args['user_domain_name'] = 'Default'
199     os_client_args['project_domain_name'] = 'Default'
200
201     return os_client_args
202
203
204 def get_fm_access_info(subcloud_hostname: str = "",
205                        sub_is_https: bool = False):
206     try:
207         # client_args = dict(
208         #     auth_url=os.environ.get('OS_AUTH_URL', _DEFAULT_STX_URL),
209         #     username=os.environ.get('OS_USERNAME', "admin"),
210         #     api_key=os.environ.get('OS_PASSWORD', "fakepasswd1"),
211         #     project_name=os.environ.get('OS_PROJECT_NAME', "admin"),
212         # )
213         client_args = get_stx_client_args()
214     except KeyError:
215         logger.error('Please source your RC file before execution, '
216                      'e.g.: `source ~/downloads/admin-rc.sh`')
217         sys.exit(1)
218
219     os_client_args = {}
220     for key, val in client_args.items():
221         os_client_args['os_{key}'.format(key=key)] = val
222
223     auth_url = urlparse(os_client_args.pop('os_auth_url'))
224     os_client_args['auth_url'] = auth_url.geturl()
225
226     if "" != subcloud_hostname:
227         orig_auth_url = urlparse(_DEFAULT_STX_URL)
228         new_auth_url = orig_auth_url._replace(
229             netloc=orig_auth_url.netloc.replace(
230                 orig_auth_url.hostname, subcloud_hostname))
231         if sub_is_https:
232             new_auth_url = new_auth_url._replace(
233                 scheme=new_auth_url.scheme.
234                 replace(new_auth_url.scheme, 'https'))
235         os_client_args['auth_url'] = new_auth_url.geturl()
236         os_client_args['endpoint_type'] = 'publicURL'
237
238     os_client_args['insecure'] = True
239
240     os_client_args['username'] = os_client_args.pop('os_username')
241     os_client_args['password'] = os_client_args.pop('os_api_key')
242     os_client_args['project_name'] = os_client_args.pop('os_project_name')
243     os_client_args['user_domain_name'] = 'Default'
244     os_client_args['project_domain_name'] = 'Default'
245
246     return os_client_args
247
248
249 def get_k8s_api_endpoint():
250     K8S_KUBECONFIG = os.environ.get("K8S_KUBECONFIG", None)
251     K8S_APISERVER = os.environ.get("K8S_APISERVER", None)
252     K8S_TOKEN = os.environ.get("K8S_TOKEN", None)
253     return K8S_KUBECONFIG, K8S_APISERVER, K8S_TOKEN
254
255
256 def get_helm_cli():
257     return '/usr/local/bin/helm'
258
259
260 def get_system_controller_as_respool():
261     return True
262
263
264 def gen_k8s_config_dict(cluster_api_endpoint, cluster_ca_cert, admin_user,
265                         admin_client_cert, admin_client_key):
266     # KUBECONFIG environment variable
267     # reference:
268     # https://kubernetes.io/docs/tasks/access-application-cluster/configure-access-multiple-clusters/
269     data = {
270         'apiVersion': 'v1',
271         'clusters': [
272             {
273                 'cluster': {
274                     'server':
275                     cluster_api_endpoint,
276                     'certificate-authority-data':
277                     cluster_ca_cert,
278                 },
279                 'name': 'inf-cluster'
280             }],
281         'contexts': [
282             {
283                 'context': {
284                     'cluster': 'inf-cluster',
285                     'user': 'kubernetes-admin'
286                 },
287                 'name': 'kubernetes-admin@inf-cluster'
288             }
289         ],
290         'current-context': 'kubernetes-admin@inf-cluster',
291         'kind': 'Config',
292         'preferences': {},
293         'users': [
294             {
295                 'name': admin_user,
296                 'user': {
297                     'client-certificate-data':
298                     admin_client_cert,
299                     'client-key-data':
300                     admin_client_key,
301                 }
302             }]
303     }
304
305     return data
306
307
308 def get_helmcli_access():
309     host_external = os.environ.get("API_HOST_EXTERNAL_FLOATING")
310     if config.conf.OCLOUD.API_HOST_EXTERNAL_FLOATING is not None and \
311             config.conf.OCLOUD.API_HOST_EXTERNAL_FLOATING != '':
312         host_external = config.conf.OCLOUD.API_HOST_EXTERNAL_FLOATING
313     host = "127.0.0.1" if host_external is None or host_external == '' \
314         else host_external
315     port = "10022" if host_external is None or host_external == '' \
316         else "30022"
317
318     helm_host_with_port = host+':'+port
319     helm_user = 'helm'
320     helm_pass = os.environ.get("HELM_USER_PASSWD")
321
322     return helm_host_with_port, helm_user, helm_pass
323
324
325 def get_alarm_yaml_filename():
326     alarm_yaml_name = os.environ.get("ALARM_YAML")
327     if alarm_yaml_name is not None and os.path.isfile(alarm_yaml_name):
328         return alarm_yaml_name
329     return "/configs/alarm.yaml"
330
331
332 def get_events_yaml_filename():
333     events_yaml_name = os.environ.get("EVENTS_YAML")
334     if events_yaml_name is not None and os.path.isfile(events_yaml_name):
335         return events_yaml_name
336     return "/configs/events.yaml"
337
338
339 # get k8s host from env:
340 def get_k8s_host():
341     k8s_host = os.environ.get("KUBERNETES_SERVICE_HOST")
342     if k8s_host is None:
343         raise Exception('Get k8s host failed.')
344     return k8s_host
345
346
347 # get k8s host port from env:
348 def get_k8s_port():
349     k8s_port = os.environ.get("KUBERNETES_SERVICE_PORT_HTTPS", '443')
350     return k8s_port
351
352
353 # token review url
354 def get_review_url():
355     try:
356         api = '/apis/authentication.k8s.io/v1/tokenreviews'
357         return "{0}{1}:{2}{3}".format(
358             'https://', get_k8s_host(), get_k8s_port(), api)
359     except Exception:
360         raise Exception('Get k8s review url failed')
361
362
363 # get reviewer token
364 def get_reviewer_token():
365     # token path default is below.
366     token_path = '/var/run/secrets/kubernetes.io/serviceaccount/token'
367     with open(token_path, 'r') as f:
368         ctt = f.read()
369     return ctt
370
371
372 def get_auth_provider():
373     return 'k8s'
374
375
376 def get_dms_support_profiles():
377     profiles = config.conf.API.DMS_SUPPORT_PROFILES
378     if profiles is None or profiles == '':
379         profiles = []
380     elif "[" in profiles and "]" in profiles:
381         profiles = profiles.replace("'", "").replace(
382             '"', "").replace('[', "").replace(']', "")
383         profiles = profiles.split(',')
384     if 'native_k8sapi' not in profiles:
385         profiles.append('native_k8sapi')
386     return profiles