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):
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 # profile = profile.lower()
51 # first = uow.deployment_managers.get(deploymentManagerId)
54 # result = first.serialize()
56 # profile_data = result.pop("profile", None)
57 # result['profileName'] = 'default'
59 # if "sol018" == profile:
60 # result['profileName'] = profile
61 # result['profileData'] = profile_data
62 # elif "file" == profile and result.hasattr("profile"):
63 # p = result.pop("profile", None)
64 # result["profile"] = _gen_kube_config(deploymentManagerId, p)
69 def _gen_kube_config(dmId: str, kubeconfig: dict) -> dict:
71 data = config.gen_k8s_config_dict(
72 kubeconfig.pop('cluster_api_endpoint', None),
73 kubeconfig.pop('cluster_ca_cert', None),
74 kubeconfig.pop('admin_user', None),
75 kubeconfig.pop('admin_client_cert', None),
76 kubeconfig.pop('admin_client_key', None),
79 # Generate a random key for tmp kube config file
80 letters = string.ascii_uppercase
81 random_key = ''.join(random.choice(letters) for i in range(10))
83 # Get datetime of now as tag of the tmp file
84 current_time = datetime.now().strftime("%Y%m%d%H%M%S")
85 tmp_file_name = random_key + "_" + current_time
87 # write down the yaml file of kubectl into tmp folder
88 with open('/tmp/kubeconfig_' + tmp_file_name, 'w') as file:
91 kubeconfig["kube_config_file"] = config.get_api_url() + \
92 config.get_o2dms_api_base() + "/" + dmId + "/download/" + tmp_file_name