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_provision_api_base():
57 return get_root_api_base() + 'provision/v1'
60 def get_o2dms_api_base():
61 return get_root_api_base() + "o2dms/v1"
64 def get_redis_host_and_port():
65 host = os.environ.get("REDIS_HOST", "localhost")
66 port = 63791 if host == "localhost" else 6379
67 return dict(host=host, port=port)
70 def get_smo_o2endpoint():
71 smo_o2endpoint = os.environ.get(
72 "SMO_O2_ENDPOINT", "http://localhost/smo_sim")
76 def get_stx_access_info(region_name="RegionOne", subcloud_hostname: str = "",
77 sub_is_https: bool = False):
78 # authurl = os.environ.get("STX_AUTH_URL", "http://192.168.204.1:5000/v3")
79 # username = os.environ.get("STX_USERNAME", "admin")
80 # pswd = os.environ.get("STX_PASSWORD", "passwd1")
81 # stx_access_info = (authurl, username, pswd)
84 auth_url=os.environ.get('OS_AUTH_URL', _DEFAULT_STX_URL),
85 username=os.environ.get('OS_USERNAME', "admin"),
86 api_key=os.environ.get('OS_PASSWORD', "fakepasswd1"),
87 project_name=os.environ.get('OS_PROJECT_NAME', "admin"),
90 logger.error('Please source your RC file before execution, '
91 'e.g.: `source ~/downloads/admin-rc.sh`')
95 for key, val in client_args.items():
96 os_client_args['os_{key}'.format(key=key)] = val
97 if "" != subcloud_hostname:
98 orig_auth_url = urlparse(_DEFAULT_STX_URL)
99 new_auth_url = orig_auth_url._replace(
100 netloc=orig_auth_url.netloc.replace(
101 orig_auth_url.hostname, subcloud_hostname))
102 # new_auth_url = new_auth_url._replace(
103 # netloc=new_auth_url.netloc.replace(str(new_auth_url.port),
106 new_auth_url = new_auth_url._replace(
107 scheme=new_auth_url.scheme.
108 replace(new_auth_url.scheme, 'https'))
109 os_client_args['insecure'] = True
110 os_client_args['os_auth_url'] = new_auth_url.geturl()
111 os_client_args['os_endpoint_type'] = 'public'
112 # os_client_args['system_url'] = os_client_args['os_auth_url']
113 os_client_args['os_password'] = os_client_args.pop('os_api_key')
114 os_client_args['os_region_name'] = region_name
115 os_client_args['api_version'] = 1
116 # os_client_args['user_domain_name'] = 'Default'
117 # os_client_args['project_domain_name'] = 'Default'
118 return os_client_args
121 def get_dc_access_info():
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"),
130 logger.error('Please source your RC file before execution, '
131 'e.g.: `source ~/downloads/admin-rc.sh`')
135 for key, val in client_args.items():
136 os_client_args['os_{key}'.format(key=key)] = val
137 auth_url = urlparse(os_client_args.pop('os_auth_url'))
138 dcmanager_url = urlparse(_DEFAULT_DCMANAGER_URL)
139 dcmanager_url = dcmanager_url._replace(netloc=dcmanager_url.netloc.replace(
140 dcmanager_url.hostname, auth_url.hostname))
142 os_client_args['dcmanager_url'] = dcmanager_url.geturl()
143 os_client_args['auth_url'] = auth_url.geturl()
144 os_client_args['username'] = os_client_args.pop('os_username')
145 os_client_args['api_key'] = os_client_args.pop('os_api_key')
146 os_client_args['project_name'] = os_client_args.pop('os_project_name')
147 os_client_args['user_domain_name'] = 'Default'
148 os_client_args['project_domain_name'] = 'Default'
150 return os_client_args
153 def get_k8s_api_endpoint():
154 K8S_KUBECONFIG = os.environ.get("K8S_KUBECONFIG", None)
155 K8S_APISERVER = os.environ.get("K8S_APISERVER", None)
156 K8S_TOKEN = os.environ.get("K8S_TOKEN", None)
157 return K8S_KUBECONFIG, K8S_APISERVER, K8S_TOKEN
161 return '/usr/local/bin/helm'
164 def get_system_controller_as_respool():
168 def gen_k8s_config_dict(cluster_api_endpoint, cluster_ca_cert, admin_user,
169 admin_client_cert, admin_client_key):
170 # KUBECONFIG environment variable
172 # https://kubernetes.io/docs/tasks/access-application-cluster/configure-access-multiple-clusters/
179 cluster_api_endpoint,
180 'certificate-authority-data':
183 'name': 'inf-cluster'
188 'cluster': 'inf-cluster',
189 'user': 'kubernetes-admin'
191 'name': 'kubernetes-admin@inf-cluster'
194 'current-context': 'kubernetes-admin@inf-cluster',
201 'client-certificate-data':
212 def get_helmcli_access():
213 host_external = os.environ.get("API_HOST_EXTERNAL_FLOATING")
214 host = "127.0.0.1" if host_external is None or host_external == '' \
216 port = "10022" if host_external is None or host_external == '' \
219 helm_host_with_port = host+':'+port
221 helm_pass = os.environ.get("HELM_USER_PASSWD")
223 return helm_host_with_port, helm_user, helm_pass