Enhance: Enable O2 DMS for distributed cloud
[pti/o2.git] / o2common / config / config.py
1 # Copyright (C) 2021 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.helper import o2logging
20 logger = o2logging.get_logger(__name__)
21
22
23 _DEFAULT_DCMANAGER_URL = "http://192.168.204.1:8119/v1.0"
24 _DEFAULT_STX_URL = "http://192.168.204.1:5000/v3"
25
26
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}"
33
34
35 def get_api_url():
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 == '' \
39         else host_external
40
41     port_internal = 5005 if host == "localhost" else 80
42     port_external = 30205
43     port = port_internal if host_external is None or host_external == '' \
44         else port_external
45     return f"http://{host}:{port}"
46
47
48 def get_root_api_base():
49     return "/"
50
51
52 def get_o2ims_api_base():
53     return get_root_api_base() + 'o2ims_infrastructureInventory/v1'
54
55
56 def get_provision_api_base():
57     return get_root_api_base() + 'provision/v1'
58
59
60 def get_o2dms_api_base():
61     return get_root_api_base() + "o2dms/v1"
62
63
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)
68
69
70 def get_smo_o2endpoint():
71     smo_o2endpoint = os.environ.get(
72         "SMO_O2_ENDPOINT", "http://localhost/smo_sim")
73     return smo_o2endpoint
74
75
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)
82     try:
83         client_args = dict(
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"),
88         )
89     except KeyError:
90         logger.error('Please source your RC file before execution, '
91                      'e.g.: `source ~/downloads/admin-rc.sh`')
92         sys.exit(1)
93
94     os_client_args = {}
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),
104         # "18002"))
105         if sub_is_https:
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
119
120
121 def get_dc_access_info():
122     try:
123         client_args = dict(
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"),
128         )
129     except KeyError:
130         logger.error('Please source your RC file before execution, '
131                      'e.g.: `source ~/downloads/admin-rc.sh`')
132         sys.exit(1)
133
134     os_client_args = {}
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))
141
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'
149
150     return os_client_args
151
152
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
158
159
160 def get_helm_cli():
161     return '/usr/local/bin/helm'
162
163
164 def get_system_controller_as_respool():
165     return True
166
167
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
171     # reference:
172     # https://kubernetes.io/docs/tasks/access-application-cluster/configure-access-multiple-clusters/
173     data = {
174         'apiVersion': 'v1',
175         'clusters': [
176             {
177                 'cluster': {
178                     'server':
179                     cluster_api_endpoint,
180                     'certificate-authority-data':
181                     cluster_ca_cert,
182                 },
183                 'name': 'inf-cluster'
184             }],
185         'contexts': [
186             {
187                 'context': {
188                     'cluster': 'inf-cluster',
189                     'user': 'kubernetes-admin'
190                 },
191                 'name': 'kubernetes-admin@inf-cluster'
192             }
193         ],
194         'current-context': 'kubernetes-admin@inf-cluster',
195         'kind': 'Config',
196         'preferences': {},
197         'users': [
198             {
199                 'name': admin_user,
200                 'user': {
201                     'client-certificate-data':
202                     admin_client_cert,
203                     'client-key-data':
204                     admin_client_key,
205                 }
206             }]
207     }
208
209     return data