Minor improvements to libjuju.py
[osm/N2VC.git] / n2vc / libjuju.py
index a79d00d..191821a 100644 (file)
@@ -117,7 +117,7 @@ class Libjuju:
     def _create_health_check_task(self):
         return self.loop.create_task(self.health_check())
 
-    async def get_controller(self, timeout: float = 5.0) -> Controller:
+    async def get_controller(self, timeout: float = 15.0) -> Controller:
         """
         Get controller
 
@@ -455,6 +455,7 @@ class Libjuju:
                     nonce=params.nonce,
                     machine_id=machine_id,
                     proxy=self.api_proxy,
+                    series=params.series,
                 )
             )
 
@@ -853,8 +854,9 @@ class Libjuju:
         """
 
         controller = await self.get_controller()
-        model = await self.get_model(controller, model_name)
+        model = None
         try:
+            model = await self.get_model(controller, model_name)
             self.log.debug("Destroying model {}".format(model_name))
             uuid = model.info.uuid
 
@@ -888,6 +890,10 @@ class Libjuju:
             raise Exception(
                 "Timeout waiting for model {} to be destroyed".format(model_name)
             )
+        except Exception as e:
+            if model:
+                await self.disconnect_model(model)
+            raise e
         finally:
             await self.disconnect_controller(controller)
 
@@ -926,33 +932,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
     ):