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_config_path():
28 path = os.environ.get("O2APP_CONFIG", "/configs/o2app.conf")
32 def get_postgres_uri():
33 host = os.environ.get("DB_HOST", "localhost")
34 port = 54321 if host == "localhost" else 5432
35 password = os.environ.get("DB_PASSWORD", "o2ims123")
36 user, db_name = "o2ims", "o2ims"
37 return f"postgresql://{user}:{password}@{host}:{port}/{db_name}"
41 host_interal = os.environ.get("API_HOST", "localhost")
42 host_external = os.environ.get("API_HOST_EXTERNAL_FLOATING")
43 host = host_interal if host_external is None or host_external == '' \
46 port_internal = 5005 if host == "localhost" else 80
48 port = port_internal if host_external is None or host_external == '' \
50 return f"http://{host}:{port}"
53 def get_root_api_base():
57 def get_o2ims_api_base():
58 return get_root_api_base() + 'o2ims-infrastructureInventory/v1'
61 def get_o2ims_monitoring_api_base():
62 return get_root_api_base() + 'o2ims-infrastructureMonitoring/v1'
65 def get_provision_api_base():
66 return get_root_api_base() + 'provision/v1'
69 def get_o2dms_api_base():
70 return get_root_api_base() + "o2dms/v1"
73 def get_redis_host_and_port():
74 host = os.environ.get("REDIS_HOST", "localhost")
75 port = 63791 if host == "localhost" else 6379
76 return dict(host=host, port=port)
79 def get_smo_o2endpoint():
80 smo_o2endpoint = os.environ.get(
81 "SMO_O2_ENDPOINT", "http://localhost/smo_sim")
85 def get_stx_access_info(region_name="RegionOne", subcloud_hostname: str = "",
86 sub_is_https: bool = False):
87 # authurl = os.environ.get("STX_AUTH_URL", "http://192.168.204.1:5000/v3")
88 # username = os.environ.get("STX_USERNAME", "admin")
89 # pswd = os.environ.get("STX_PASSWORD", "passwd1")
90 # stx_access_info = (authurl, username, pswd)
93 auth_url=os.environ.get('OS_AUTH_URL', _DEFAULT_STX_URL),
94 username=os.environ.get('OS_USERNAME', "admin"),
95 api_key=os.environ.get('OS_PASSWORD', "fakepasswd1"),
96 project_name=os.environ.get('OS_PROJECT_NAME', "admin"),
99 logger.error('Please source your RC file before execution, '
100 'e.g.: `source ~/downloads/admin-rc.sh`')
104 for key, val in client_args.items():
105 os_client_args['os_{key}'.format(key=key)] = val
106 if "" != subcloud_hostname:
107 orig_auth_url = urlparse(_DEFAULT_STX_URL)
108 new_auth_url = orig_auth_url._replace(
109 netloc=orig_auth_url.netloc.replace(
110 orig_auth_url.hostname, subcloud_hostname))
111 # new_auth_url = new_auth_url._replace(
112 # netloc=new_auth_url.netloc.replace(str(new_auth_url.port),
115 new_auth_url = new_auth_url._replace(
116 scheme=new_auth_url.scheme.
117 replace(new_auth_url.scheme, 'https'))
118 os_client_args['insecure'] = True
119 os_client_args['os_auth_url'] = new_auth_url.geturl()
120 os_client_args['os_endpoint_type'] = 'public'
121 # os_client_args['system_url'] = os_client_args['os_auth_url']
122 os_client_args['os_password'] = os_client_args.pop('os_api_key')
123 os_client_args['os_region_name'] = region_name
124 os_client_args['api_version'] = 1
125 # os_client_args['user_domain_name'] = 'Default'
126 # os_client_args['project_domain_name'] = 'Default'
127 return os_client_args
130 def get_dc_access_info():
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"),
139 logger.error('Please source your RC file before execution, '
140 'e.g.: `source ~/downloads/admin-rc.sh`')
144 for key, val in client_args.items():
145 os_client_args['os_{key}'.format(key=key)] = val
146 auth_url = urlparse(os_client_args.pop('os_auth_url'))
147 dcmanager_url = urlparse(_DEFAULT_DCMANAGER_URL)
148 dcmanager_url = dcmanager_url._replace(netloc=dcmanager_url.netloc.replace(
149 dcmanager_url.hostname, auth_url.hostname))
151 os_client_args['dcmanager_url'] = dcmanager_url.geturl()
152 os_client_args['auth_url'] = auth_url.geturl()
153 os_client_args['username'] = os_client_args.pop('os_username')
154 os_client_args['api_key'] = os_client_args.pop('os_api_key')
155 os_client_args['project_name'] = os_client_args.pop('os_project_name')
156 os_client_args['user_domain_name'] = 'Default'
157 os_client_args['project_domain_name'] = 'Default'
159 return os_client_args
162 def get_fm_access_info():
165 auth_url=os.environ.get('OS_AUTH_URL', _DEFAULT_STX_URL),
166 username=os.environ.get('OS_USERNAME', "admin"),
167 api_key=os.environ.get('OS_PASSWORD', "fakepasswd1"),
168 project_name=os.environ.get('OS_PROJECT_NAME', "admin"),
171 logger.error('Please source your RC file before execution, '
172 'e.g.: `source ~/downloads/admin-rc.sh`')
176 for key, val in client_args.items():
177 os_client_args['os_{key}'.format(key=key)] = val
178 auth_url = urlparse(os_client_args.pop('os_auth_url'))
180 os_client_args['insecure'] = True
182 os_client_args['auth_url'] = auth_url.geturl()
183 os_client_args['username'] = os_client_args.pop('os_username')
184 os_client_args['password'] = os_client_args.pop('os_api_key')
185 os_client_args['project_name'] = os_client_args.pop('os_project_name')
186 os_client_args['user_domain_name'] = 'Default'
187 os_client_args['project_domain_name'] = 'Default'
189 return os_client_args
192 def get_k8s_api_endpoint():
193 K8S_KUBECONFIG = os.environ.get("K8S_KUBECONFIG", None)
194 K8S_APISERVER = os.environ.get("K8S_APISERVER", None)
195 K8S_TOKEN = os.environ.get("K8S_TOKEN", None)
196 return K8S_KUBECONFIG, K8S_APISERVER, K8S_TOKEN
200 return '/usr/local/bin/helm'
203 def get_system_controller_as_respool():
207 def gen_k8s_config_dict(cluster_api_endpoint, cluster_ca_cert, admin_user,
208 admin_client_cert, admin_client_key):
209 # KUBECONFIG environment variable
211 # https://kubernetes.io/docs/tasks/access-application-cluster/configure-access-multiple-clusters/
218 cluster_api_endpoint,
219 'certificate-authority-data':
222 'name': 'inf-cluster'
227 'cluster': 'inf-cluster',
228 'user': 'kubernetes-admin'
230 'name': 'kubernetes-admin@inf-cluster'
233 'current-context': 'kubernetes-admin@inf-cluster',
240 'client-certificate-data':
251 def get_helmcli_access():
252 host_external = os.environ.get("API_HOST_EXTERNAL_FLOATING")
253 host = "127.0.0.1" if host_external is None or host_external == '' \
255 port = "10022" if host_external is None or host_external == '' \
258 helm_host_with_port = host+':'+port
260 helm_pass = os.environ.get("HELM_USER_PASSWD")
262 return helm_host_with_port, helm_user, helm_pass
265 def get_alarm_yaml_filename():
266 alarm_yaml_name = os.environ.get("ALARM_YAML")
267 if alarm_yaml_name is not None and os.path.isfile(alarm_yaml_name):
268 return alarm_yaml_name
269 return "/configs/alarm.yaml"
272 def get_events_yaml_filename():
273 events_yaml_name = os.environ.get("EVENTS_YAML")
274 if events_yaml_name is not None and os.path.isfile(events_yaml_name):
275 return events_yaml_name
276 return "/configs/events.yaml"