X-Git-Url: https://osm.etsi.org/gitweb/?p=osm%2FN2VC.git;a=blobdiff_plain;f=n2vc%2Fk8s_juju_conn.py;h=737cac69d56363e928c901874c1f360ff1ddda82;hp=1704ec0cc93c83f24a5ab9c97bbef5276827b1c1;hb=d4cee8c1edd901a2922bb2593e5b643844f83b3a;hpb=47f653875c18b0bfe00627de5c554ad5d480ad1d diff --git a/n2vc/k8s_juju_conn.py b/n2vc/k8s_juju_conn.py index 1704ec0..737cac6 100644 --- a/n2vc/k8s_juju_conn.py +++ b/n2vc/k8s_juju_conn.py @@ -13,6 +13,7 @@ # limitations under the License. import asyncio +from typing import Union import os import uuid import yaml @@ -77,6 +78,7 @@ class K8sJujuConnector(K8sConnector): db_uri = EnvironConfig(prefixes=["OSMLCM_", "OSMMON_"]).get("database_uri") self._store = MotorStore(db_uri) self.loading_libjuju = asyncio.Lock(loop=self.loop) + self.uninstall_locks = {} self.log.debug("K8S Juju connector initialized") # TODO: Remove these commented lines: @@ -193,6 +195,9 @@ class K8sJujuConnector(K8sConnector): name: str, url: str, _type: str = "charm", + cert: str = None, + user: str = None, + password: str = None, ): raise MethodNotImplemented() @@ -503,18 +508,30 @@ class K8sJujuConnector(K8sConnector): """ self.log.debug("[uninstall] Destroying model") - libjuju = await self._get_libjuju(kwargs.get("vca_id")) - await libjuju.destroy_model(kdu_instance, total_timeout=3600) + will_not_delete = False + if kdu_instance not in self.uninstall_locks: + self.uninstall_locks[kdu_instance] = asyncio.Lock(loop=self.loop) + delete_lock = self.uninstall_locks[kdu_instance] + + while delete_lock.locked(): + will_not_delete = True + await asyncio.sleep(0.1) + + if will_not_delete: + self.log.info("Model {} deleted by another worker.".format(kdu_instance)) + return True - # self.log.debug("[uninstall] Model destroyed and disconnecting") - # await controller.disconnect() + try: + async with delete_lock: + libjuju = await self._get_libjuju(kwargs.get("vca_id")) + + await libjuju.destroy_model(kdu_instance, total_timeout=3600) + finally: + self.uninstall_locks.pop(kdu_instance) + self.log.debug(f"[uninstall] Model {kdu_instance} destroyed") return True - # TODO: Remove these commented lines - # if not self.authenticated: - # self.log.debug("[uninstall] Connecting to controller") - # await self.login(cluster_uuid) async def exec_primitive( self, @@ -653,7 +670,7 @@ class K8sJujuConnector(K8sConnector): complete_status: bool = False, yaml_format: bool = False, **kwargs, - ) -> dict: + ) -> Union[str, dict]: """Get the status of the KDU Get the current status of the KDU instance.