1 # Copyright (C) 2021 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.
17 from urllib.parse import urlparse
19 from o2common.helper import o2logging
20 logger = o2logging.get_logger(__name__)
23 _DEFAULT_DCMANAGER_URL = "http://192.168.204.1:8119/v1.0"
24 _DEFAULT_STX_URL = "http://192.168.204.1:5000/v3"
27 def get_postgres_uri():
28 host = os.environ.get("DB_HOST", "localhost")
29 port = 54321 if host == "localhost" else 5432
30 password = os.environ.get("DB_PASSWORD", "o2ims123")
31 user, db_name = "o2ims", "o2ims"
32 return f"postgresql://{user}:{password}@{host}:{port}/{db_name}"
36 host_interal = os.environ.get("API_HOST", "localhost")
37 host_external = os.environ.get("API_HOST_EXTERNAL_FLOATING")
38 host = host_interal if host_external is None or host_external == '' \
41 port_internal = 5005 if host == "localhost" else 80
43 port = port_internal if host_external is None or host_external == '' \
45 return f"http://{host}:{port}"
48 def get_root_api_base():
52 def get_o2ims_api_base():
53 return get_root_api_base() + 'o2ims-infrastructureInventory/v1'
56 def get_o2ims_monitoring_api_base():
57 return get_root_api_base() + 'o2ims-infrastructureMonitoring/v1'
60 def get_provision_api_base():
61 return get_root_api_base() + 'provision/v1'
64 def get_o2dms_api_base():
65 return get_root_api_base() + "o2dms/v1"
68 def get_redis_host_and_port():
69 host = os.environ.get("REDIS_HOST", "localhost")
70 port = 63791 if host == "localhost" else 6379
71 return dict(host=host, port=port)
74 def get_smo_o2endpoint():
75 smo_o2endpoint = os.environ.get(
76 "SMO_O2_ENDPOINT", "http://localhost/smo_sim")
80 def get_stx_access_info(region_name="RegionOne", subcloud_hostname: str = "",
81 sub_is_https: bool = False):
82 # authurl = os.environ.get("STX_AUTH_URL", "http://192.168.204.1:5000/v3")
83 # username = os.environ.get("STX_USERNAME", "admin")
84 # pswd = os.environ.get("STX_PASSWORD", "passwd1")
85 # stx_access_info = (authurl, username, pswd)
88 auth_url=os.environ.get('OS_AUTH_URL', _DEFAULT_STX_URL),
89 username=os.environ.get('OS_USERNAME', "admin"),
90 api_key=os.environ.get('OS_PASSWORD', "fakepasswd1"),
91 project_name=os.environ.get('OS_PROJECT_NAME', "admin"),
94 logger.error('Please source your RC file before execution, '
95 'e.g.: `source ~/downloads/admin-rc.sh`')
99 for key, val in client_args.items():
100 os_client_args['os_{key}'.format(key=key)] = val
101 if "" != subcloud_hostname:
102 orig_auth_url = urlparse(_DEFAULT_STX_URL)
103 new_auth_url = orig_auth_url._replace(
104 netloc=orig_auth_url.netloc.replace(
105 orig_auth_url.hostname, subcloud_hostname))
106 # new_auth_url = new_auth_url._replace(
107 # netloc=new_auth_url.netloc.replace(str(new_auth_url.port),
110 new_auth_url = new_auth_url._replace(
111 scheme=new_auth_url.scheme.
112 replace(new_auth_url.scheme, 'https'))
113 os_client_args['insecure'] = True
114 os_client_args['os_auth_url'] = new_auth_url.geturl()
115 os_client_args['os_endpoint_type'] = 'public'
116 # os_client_args['system_url'] = os_client_args['os_auth_url']
117 os_client_args['os_password'] = os_client_args.pop('os_api_key')
118 os_client_args['os_region_name'] = region_name
119 os_client_args['api_version'] = 1
120 # os_client_args['user_domain_name'] = 'Default'
121 # os_client_args['project_domain_name'] = 'Default'
122 return os_client_args
125 def get_dc_access_info():
128 auth_url=os.environ.get('OS_AUTH_URL', _DEFAULT_STX_URL),
129 username=os.environ.get('OS_USERNAME', "admin"),
130 api_key=os.environ.get('OS_PASSWORD', "fakepasswd1"),
131 project_name=os.environ.get('OS_PROJECT_NAME', "admin"),
134 logger.error('Please source your RC file before execution, '
135 'e.g.: `source ~/downloads/admin-rc.sh`')
139 for key, val in client_args.items():
140 os_client_args['os_{key}'.format(key=key)] = val
141 auth_url = urlparse(os_client_args.pop('os_auth_url'))
142 dcmanager_url = urlparse(_DEFAULT_DCMANAGER_URL)
143 dcmanager_url = dcmanager_url._replace(netloc=dcmanager_url.netloc.replace(
144 dcmanager_url.hostname, auth_url.hostname))
146 os_client_args['dcmanager_url'] = dcmanager_url.geturl()
147 os_client_args['auth_url'] = auth_url.geturl()
148 os_client_args['username'] = os_client_args.pop('os_username')
149 os_client_args['api_key'] = os_client_args.pop('os_api_key')
150 os_client_args['project_name'] = os_client_args.pop('os_project_name')
151 os_client_args['user_domain_name'] = 'Default'
152 os_client_args['project_domain_name'] = 'Default'
154 return os_client_args
157 def get_fm_access_info():
160 auth_url=os.environ.get('OS_AUTH_URL', _DEFAULT_STX_URL),
161 username=os.environ.get('OS_USERNAME', "admin"),
162 api_key=os.environ.get('OS_PASSWORD', "fakepasswd1"),
163 project_name=os.environ.get('OS_PROJECT_NAME', "admin"),
166 logger.error('Please source your RC file before execution, '
167 'e.g.: `source ~/downloads/admin-rc.sh`')
171 for key, val in client_args.items():
172 os_client_args['os_{key}'.format(key=key)] = val
173 auth_url = urlparse(os_client_args.pop('os_auth_url'))
175 os_client_args['insecure'] = True
177 os_client_args['auth_url'] = auth_url.geturl()
178 os_client_args['username'] = os_client_args.pop('os_username')
179 os_client_args['password'] = os_client_args.pop('os_api_key')
180 os_client_args['project_name'] = os_client_args.pop('os_project_name')
181 os_client_args['user_domain_name'] = 'Default'
182 os_client_args['project_domain_name'] = 'Default'
184 return os_client_args
187 def get_k8s_api_endpoint():
188 K8S_KUBECONFIG = os.environ.get("K8S_KUBECONFIG", None)
189 K8S_APISERVER = os.environ.get("K8S_APISERVER", None)
190 K8S_TOKEN = os.environ.get("K8S_TOKEN", None)
191 return K8S_KUBECONFIG, K8S_APISERVER, K8S_TOKEN
195 return '/usr/local/bin/helm'
198 def get_system_controller_as_respool():
202 def gen_k8s_config_dict(cluster_api_endpoint, cluster_ca_cert, admin_user,
203 admin_client_cert, admin_client_key):
204 # KUBECONFIG environment variable
206 # https://kubernetes.io/docs/tasks/access-application-cluster/configure-access-multiple-clusters/
213 cluster_api_endpoint,
214 'certificate-authority-data':
217 'name': 'inf-cluster'
222 'cluster': 'inf-cluster',
223 'user': 'kubernetes-admin'
225 'name': 'kubernetes-admin@inf-cluster'
228 'current-context': 'kubernetes-admin@inf-cluster',
235 'client-certificate-data':
246 def get_helmcli_access():
247 host_external = os.environ.get("API_HOST_EXTERNAL_FLOATING")
248 host = "127.0.0.1" if host_external is None or host_external == '' \
250 port = "10022" if host_external is None or host_external == '' \
253 helm_host_with_port = host+':'+port
255 helm_pass = os.environ.get("HELM_USER_PASSWD")
257 return helm_host_with_port, helm_user, helm_pass
260 def get_alarm_yaml_filename():
261 alarm_yaml_name = os.environ.get("ALARM_YAML")
262 if alarm_yaml_name is not None and os.path.isfile(alarm_yaml_name):
263 return alarm_yaml_name
264 return "/configs/alarm.yaml"
267 def get_events_yaml_filename():
268 events_yaml_name = os.environ.get("EVENTS_YAML")
269 if events_yaml_name is not None and os.path.isfile(events_yaml_name):
270 return events_yaml_name
271 return "/configs/events.yaml"