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.
18 from datetime import datetime
20 # from sqlalchemy import select
21 from o2common.service import unit_of_work
22 # from o2ims.adapter.orm import deploymentmanager
23 from o2common.helper import o2logging
24 from o2common.config import config
25 logger = o2logging.get_logger(__name__)
28 def deployment_managers(uow: unit_of_work.AbstractUnitOfWork):
30 # res = uow.session.execute(select(deploymentmanager))
31 # return [dict(r) for r in res]
33 li = uow.deployment_managers.list()
34 return [r.serialize() for r in li]
37 def deployment_manager_one(deploymentManagerId: str,
38 uow: unit_of_work.AbstractUnitOfWork,
39 profile: str = 'params'):
41 # res = uow.session.execute(select(deploymentmanager).where(
42 # deploymentmanager.c.deploymentManagerId == deploymentManagerId))
44 # return None if first is None else dict(first)
46 # first = uow.deployment_managers.get(deploymentManagerId)
47 # return first.serialize() if first is not None else None
49 first = uow.deployment_managers.get(deploymentManagerId)
52 result = first.serialize()
54 if "params" == profile:
56 elif "file" == profile and result.hasattr("profile"):
57 p = result.pop("profile", None)
58 result["profile"] = _gen_kube_config(deploymentManagerId, p)
60 result.pop("profile", None)
65 def _gen_kube_config(dmId: str, kubeconfig: dict) -> dict:
67 # KUBECONFIG environment variable
69 # https://kubernetes.io/docs/tasks/access-application-cluster/configure-access-multiple-clusters/
76 kubeconfig.pop('cluster_api_endpoint', None),
77 'certificate-authority-data':
78 kubeconfig.pop('cluster_ca_cert', None),
85 'cluster': 'inf-cluster',
86 'user': 'kubernetes-admin'
88 'name': 'kubernetes-admin@inf-cluster'
91 'current-context': 'kubernetes-admin@inf-cluster',
96 'name': kubeconfig.pop('admin_user', None),
98 'client-certificate-data':
99 kubeconfig.pop('admin_client_cert', None),
101 kubeconfig.pop('admin_client_key', None),
106 # Generate a random key for tmp kube config file
107 letters = string.ascii_uppercase
108 random_key = ''.join(random.choice(letters) for i in range(10))
110 # Get datetime of now as tag of the tmp file
111 current_time = datetime.now().strftime("%Y%m%d%H%M%S")
112 tmp_file_name = random_key + "_" + current_time
114 # write down the yaml file of kubectl into tmp folder
115 with open('/tmp/kubeconfig_' + tmp_file_name, 'w') as file:
116 yaml.dump(data, file)
118 kubeconfig["kube_config_file"] = config.get_api_url() + \
119 config.get_o2dms_api_base() + "/" + dmId + "/download/" + tmp_file_name