Clean up commented or unused code
[osm/N2VC.git] / n2vc / libjuju.py
index 161370b..b766db1 100644 (file)
@@ -32,6 +32,7 @@ from juju.controller import Controller
 from juju.client import client
 from juju import tag
 
+from n2vc.config import ModelConfig
 from n2vc.juju_watcher import JujuModelWatcher
 from n2vc.provisioner import AsyncSSHProvisioner
 from n2vc.n2vc_conn import N2VCConnector
@@ -63,8 +64,7 @@ class Libjuju:
         log: logging.Logger = None,
         db: dict = None,
         n2vc: N2VCConnector = None,
-        apt_mirror: str = None,
-        enable_os_upgrade: bool = True,
+        model_config: ModelConfig = {},
     ):
         """
         Constructor
@@ -99,11 +99,7 @@ class Libjuju:
         self.n2vc = n2vc
 
         # Generate config for models
-        self.model_config = {}
-        if apt_mirror:
-            self.model_config["apt-mirror"] = apt_mirror
-        self.model_config["enable-os-refresh-update"] = enable_os_upgrade
-        self.model_config["enable-os-upgrade"] = enable_os_upgrade
+        self.model_config = model_config
 
         self.loop.set_exception_handler(self.handle_exception)
         self.creating_model = asyncio.Lock(loop=self.loop)
@@ -444,6 +440,7 @@ class Libjuju:
                     nonce=params.nonce,
                     machine_id=machine_id,
                     proxy=self.api_proxy,
+                    series=params.series,
                 )
             )
 
@@ -884,23 +881,59 @@ class Libjuju:
         finally:
             await self.disconnect_controller(controller)
 
-    async def destroy_application(self, model: Model, application_name: str):
+    async def destroy_application(
+        self, model_name: str, application_name: str, total_timeout: float
+    ):
         """
         Destroy application
 
-        :param: model:              Model object
+        :param: model_name:         Model name
         :param: application_name:   Application name
+        :param: total_timeout:      Timeout
         """
-        self.log.debug(
-            "Destroying application {} in model {}".format(
-                application_name, model.info.name
+
+        controller = await self.get_controller()
+        model = None
+
+        try:
+            model = await self.get_model(controller, model_name)
+            self.log.debug(
+                "Destroying application {} in model {}".format(
+                    application_name, model_name
+                )
             )
-        )
-        application = model.applications.get(application_name)
-        if application:
-            await application.destroy()
-        else:
-            self.log.warning("Application not found: {}".format(application_name))
+            application = self._get_application(model, application_name)
+            if application:
+                await application.destroy()
+            else:
+                self.log.warning("Application not found: {}".format(application_name))
+
+            self.log.debug(
+                "Waiting for application {} to be destroyed in model {}...".format(
+                    application_name, model_name
+                )
+            )
+            if total_timeout is None:
+                total_timeout = 3600
+            end = time.time() + total_timeout
+            while time.time() < end:
+                if not self._get_application(model, application_name):
+                    self.log.debug(
+                        "The application {} was destroyed in model {} ".format(
+                            application_name, model_name
+                        )
+                    )
+                    return
+                await asyncio.sleep(5)
+            raise Exception(
+                "Timeout waiting for application {} to be destroyed in model {}".format(
+                    application_name, model_name
+                )
+            )
+        finally:
+            if model is not None:
+                await self.disconnect_model(model)
+            await self.disconnect_controller(controller)
 
     async def _destroy_pending_machines(self, model: Model, only_manual: bool = False):
         """
@@ -919,33 +952,6 @@ class Libjuju:
                 machine = model.machines[machine_id]
                 await machine.destroy(force=True)
 
-    # async def destroy_machine(
-    #     self, model: Model, machine_id: str, total_timeout: float = 3600
-    # ):
-    #     """
-    #     Destroy machine
-
-    #     :param: model:          Model object
-    #     :param: machine_id:     Machine id
-    #     :param: total_timeout:  Timeout in seconds
-    #     """
-    #     machines = await model.get_machines()
-    #     if machine_id in machines:
-    #         machine = machines[machine_id]
-    #         await machine.destroy(force=True)
-    #         # max timeout
-    #         end = time.time() + total_timeout
-
-    #         # wait for machine removal
-    #         machines = await model.get_machines()
-    #         while machine_id in machines and time.time() < end:
-    #             self.log.debug("Waiting for machine {} is destroyed".format(machine_id))
-    #             await asyncio.sleep(0.5)
-    #             machines = await model.get_machines()
-    #         self.log.debug("Machine destroyed: {}".format(machine_id))
-    #     else:
-    #         self.log.debug("Machine not found: {}".format(machine_id))
-
     async def configure_application(
         self, model_name: str, application_name: str, config: dict = None
     ):