1 # Copyright (C) 2021-2024 Wind River Systems, Inc.
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.
18 from urllib.parse import urlparse
20 from o2common import config
21 from o2common.helper import o2logging
22 logger = o2logging.get_logger(__name__)
25 CGTS_INSECURE_SSL = os.environ.get("CGTS_INSECURE_SSL", "0") == "1"
27 _DEFAULT_STX_URL = "http://192.168.204.1:5000/v3"
28 _DCMANAGER_URL_PORT = os.environ.get("DCMANAGER_API_PORT", "8119")
29 _DCMANAGER_URL_PATH = os.environ.get("DCMANAGER_API_PATH", "/v1.0")
32 def get_config_path():
33 path = os.environ.get("O2APP_CONFIG", "/configs/o2app.conf")
37 def get_smo_ca_config_path():
38 path = os.environ.get("SMO_CA_CONFIG", "/configs/smoca.crt")
42 def get_postgres_uri():
43 host = os.environ.get("DB_HOST", "localhost")
44 port = int(os.environ.get("DB_PORT", 5432))
45 password = os.environ.get("DB_PASSWORD", "o2ims123")
46 user, db_name = "o2ims", "o2ims"
47 return f"postgresql://{user}:{password}@{host}:{port}/{db_name}"
51 host_interal = os.environ.get("API_HOST", "localhost")
52 host_external = os.environ.get("API_HOST_EXTERNAL_FLOATING")
53 if config.conf.OCLOUD.API_HOST_EXTERNAL_FLOATING is not None and \
54 config.conf.OCLOUD.API_HOST_EXTERNAL_FLOATING != '':
55 host_external = config.conf.OCLOUD.API_HOST_EXTERNAL_FLOATING
56 host = host_interal if host_external is None or host_external == '' \
59 port_internal = 5005 if host == "localhost" else 80
61 port = port_internal if host_external is None or host_external == '' \
63 return f"https://{host}:{port}"
68 return get_stx_client_args()["auth_url"]
70 logger.error('Please source your RC file before execution, '
71 'e.g.: `source ~/downloads/admin-rc.sh`')
75 def get_dc_manager_url():
76 auth_url = os.environ.get("DCMANAGER_OS_AUTH_URL", None)
78 temp_url = get_stx_url()
79 u = urlparse(temp_url)
80 u = u._replace(netloc=f"{u.hostname}:{_DCMANAGER_URL_PORT}")
81 u = u._replace(path=_DCMANAGER_URL_PATH)
86 def get_root_api_base():
90 def get_o2ims_api_base():
91 return get_root_api_base() + 'o2ims-infrastructureInventory'
94 def get_o2ims_monitoring_api_v1():
98 def get_o2ims_inventory_api_v1():
102 def get_o2ims_monitoring_api_base():
103 return get_root_api_base() + 'o2ims-infrastructureMonitoring'
106 def get_o2dms_api_base():
107 return get_root_api_base() + "o2dms/v1"
110 def get_redis_host_and_port():
111 host = os.environ.get("REDIS_HOST", "localhost")
112 port = int(os.environ.get("REDIS_PORT", 6379))
113 return dict(host=host, port=port)
116 def get_smo_o2endpoint():
117 smo_o2endpoint = os.environ.get(
118 "SMO_O2_ENDPOINT", "http://localhost/smo_sim")
119 return smo_o2endpoint
122 def get_stx_client_args():
124 auth_url=os.environ.get('OS_AUTH_URL', _DEFAULT_STX_URL),
125 username=os.environ.get('OS_USERNAME', "admin"),
126 api_key=os.environ.get('OS_PASSWORD', "fakepasswd1"),
127 project_name=os.environ.get('OS_PROJECT_NAME', "admin"),
129 if config.conf.OCLOUD.OS_AUTH_URL is not None and \
130 config.conf.OCLOUD.OS_AUTH_URL != '':
131 client_args['auth_url'] = config.conf.OCLOUD.OS_AUTH_URL
132 if config.conf.OCLOUD.OS_USERNAME is not None and \
133 config.conf.OCLOUD.OS_USERNAME != '':
134 client_args['username'] = config.conf.OCLOUD.OS_USERNAME
135 if config.conf.OCLOUD.OS_PASSWORD is not None and \
136 config.conf.OCLOUD.OS_PASSWORD != '':
137 client_args['api_key'] = config.conf.OCLOUD.OS_PASSWORD
138 if config.conf.OCLOUD.OS_PROJECT_NAME is not None and \
139 config.conf.OCLOUD.OS_PROJECT_NAME != '':
140 client_args['project_name'] = config.conf.OCLOUD.OS_PROJECT_NAME
144 def is_ipv6(address):
146 # Try to convert the address and check the IP version
147 ip = ipaddress.ip_address(address)
148 return ip.version == 6
153 def get_stx_access_info(region_name="RegionOne", subcloud_hostname: str = "",
154 sub_is_https: bool = False):
156 client_args = get_stx_client_args()
158 logger.error('Please source your RC file before execution, '
159 'e.g.: `source ~/downloads/admin-rc.sh`')
163 for key, val in client_args.items():
164 os_client_args['os_{key}'.format(key=key)] = val
165 if "" != subcloud_hostname:
166 if is_ipv6(subcloud_hostname):
167 subcloud_hostname = "[" + subcloud_hostname + "]"
168 orig_auth_url = urlparse(get_stx_url())
169 new_auth_url = orig_auth_url._replace(
170 netloc=orig_auth_url.netloc.replace(
171 orig_auth_url.hostname, subcloud_hostname))
172 # new_auth_url = new_auth_url._replace(
173 # netloc=new_auth_url.netloc.replace(str(new_auth_url.port),
176 new_auth_url = new_auth_url._replace(
177 scheme=new_auth_url.scheme.
178 replace(new_auth_url.scheme, 'https'))
179 os_client_args['insecure'] = CGTS_INSECURE_SSL
180 os_client_args['os_auth_url'] = new_auth_url.geturl()
181 os_client_args['os_endpoint_type'] = 'public'
182 # os_client_args['system_url'] = os_client_args['os_auth_url']
183 os_client_args['os_password'] = os_client_args.pop('os_api_key')
184 os_client_args['os_region_name'] = region_name
185 os_client_args['api_version'] = 1
186 # os_client_args['user_domain_name'] = 'Default'
187 # os_client_args['project_domain_name'] = 'Default'
188 return os_client_args
191 def get_dc_access_info():
193 client_args = get_stx_client_args()
195 logger.error('Please source your RC file before execution, '
196 'e.g.: `source ~/downloads/admin-rc.sh`')
200 for key, val in client_args.items():
201 os_client_args['os_{key}'.format(key=key)] = val
202 auth_url = urlparse(os_client_args.pop('os_auth_url'))
203 hostname = f"[{auth_url.hostname}]" if is_ipv6(auth_url.hostname) \
204 else auth_url.hostname
205 dcmanager_url = urlparse(get_dc_manager_url())
206 dcmanager_url = dcmanager_url._replace(netloc=dcmanager_url.netloc.replace(
207 dcmanager_url.hostname, hostname))
209 os_client_args['dcmanager_url'] = dcmanager_url.geturl()
210 os_client_args['auth_url'] = auth_url.geturl()
211 os_client_args['username'] = os_client_args.pop('os_username')
212 os_client_args['api_key'] = os_client_args.pop('os_api_key')
213 os_client_args['project_name'] = os_client_args.pop('os_project_name')
214 os_client_args['user_domain_name'] = 'Default'
215 os_client_args['project_domain_name'] = 'Default'
217 return os_client_args
220 def get_fm_access_info(subcloud_hostname: str = "",
221 sub_is_https: bool = False):
223 client_args = get_stx_client_args()
225 logger.error('Please source your RC file before execution, '
226 'e.g.: `source ~/downloads/admin-rc.sh`')
230 for key, val in client_args.items():
231 os_client_args['os_{key}'.format(key=key)] = val
233 auth_url = urlparse(os_client_args.pop('os_auth_url'))
234 os_client_args['auth_url'] = auth_url.geturl()
236 if "" != subcloud_hostname:
237 subcloud_hostname = f"[{subcloud_hostname}]" if \
238 is_ipv6(subcloud_hostname) else subcloud_hostname
239 orig_auth_url = urlparse(get_stx_url())
240 new_auth_url = orig_auth_url._replace(
241 netloc=orig_auth_url.netloc.replace(
242 orig_auth_url.hostname, subcloud_hostname))
244 new_auth_url = new_auth_url._replace(
245 scheme=new_auth_url.scheme.
246 replace(new_auth_url.scheme, 'https'))
247 os_client_args['auth_url'] = new_auth_url.geturl()
248 os_client_args['endpoint_type'] = 'publicURL'
250 os_client_args['insecure'] = CGTS_INSECURE_SSL
252 os_client_args['username'] = os_client_args.pop('os_username')
253 os_client_args['password'] = os_client_args.pop('os_api_key')
254 os_client_args['project_name'] = os_client_args.pop('os_project_name')
255 os_client_args['user_domain_name'] = 'Default'
256 os_client_args['project_domain_name'] = 'Default'
258 return os_client_args
261 def get_k8s_api_endpoint():
262 K8S_KUBECONFIG = os.environ.get("K8S_KUBECONFIG", None)
263 K8S_APISERVER = os.environ.get("K8S_APISERVER", None)
264 K8S_TOKEN = os.environ.get("K8S_TOKEN", None)
265 return K8S_KUBECONFIG, K8S_APISERVER, K8S_TOKEN
269 return '/usr/local/bin/helm'
272 def get_containers_shared_folder():
276 def get_system_controller_as_respool():
280 def gen_k8s_config_dict(cluster_api_endpoint, cluster_ca_cert, admin_user,
281 admin_client_cert, admin_client_key):
282 # KUBECONFIG environment variable
284 # https://kubernetes.io/docs/tasks/access-application-cluster/configure-access-multiple-clusters/
291 cluster_api_endpoint,
292 'certificate-authority-data':
295 'name': 'inf-cluster'
300 'cluster': 'inf-cluster',
301 'user': 'kubernetes-admin'
303 'name': 'kubernetes-admin@inf-cluster'
306 'current-context': 'kubernetes-admin@inf-cluster',
313 'client-certificate-data':
324 def get_helmcli_access():
325 host_external = os.environ.get("API_HOST_EXTERNAL_FLOATING")
326 if config.conf.OCLOUD.API_HOST_EXTERNAL_FLOATING is not None and \
327 config.conf.OCLOUD.API_HOST_EXTERNAL_FLOATING != '':
328 host_external = config.conf.OCLOUD.API_HOST_EXTERNAL_FLOATING
329 host = "127.0.0.1" if host_external is None or host_external == '' \
331 port = "10022" if host_external is None or host_external == '' \
334 helm_host_with_port = host+':'+port
336 helm_pass = os.environ.get("HELM_USER_PASSWD")
338 return helm_host_with_port, helm_user, helm_pass
341 def get_alarm_yaml_filename():
342 alarm_yaml_name = os.environ.get("ALARM_YAML")
343 if alarm_yaml_name is not None and os.path.isfile(alarm_yaml_name):
344 return alarm_yaml_name
345 return "/configs/alarm.yaml"
348 def get_events_yaml_filename():
349 events_yaml_name = os.environ.get("EVENTS_YAML")
350 if events_yaml_name is not None and os.path.isfile(events_yaml_name):
351 return events_yaml_name
352 return "/configs/events.yaml"
355 # get k8s host from env:
357 k8s_host = os.environ.get("KUBERNETES_SERVICE_HOST")
359 raise Exception('Get k8s host failed.')
363 # get k8s host port from env:
365 k8s_port = os.environ.get("KUBERNETES_SERVICE_PORT_HTTPS", '443')
370 def get_review_url():
372 api = '/apis/authentication.k8s.io/v1/tokenreviews'
373 return "{0}{1}:{2}{3}".format(
374 'https://', get_k8s_host(), get_k8s_port(), api)
376 raise Exception('Get k8s review url failed')
380 def get_reviewer_token():
381 # token path default is below.
382 token_path = '/var/run/secrets/kubernetes.io/serviceaccount/token'
383 with open(token_path, 'r') as f:
388 def get_auth_provider():
389 return config.conf.auth_provider
392 def get_dms_support_profiles():
394 profiles_str = config.conf.API.DMS_SUPPORT_PROFILES
396 profiles_strip = profiles_str.strip(' []')
397 profiles_str = profiles_strip.replace("'", "").replace(
399 profiles_list = profiles_str.split(',')
400 if 'native_k8sapi' not in profiles_list:
401 profiles_list.append('native_k8sapi')