Fix bug 1267: Destroy machines that are pending
[osm/N2VC.git] / n2vc / libjuju.py
index a457309..8d1fdad 100644 (file)
@@ -817,6 +817,10 @@ class Libjuju:
             self.log.debug("Destroying model {}".format(model_name))
             uuid = model.info.uuid
 
+            # Destroy machines that are manually provisioned
+            # and still are in pending state
+            await self._destroy_pending_machines(model, only_manual=True)
+
             # Disconnect model
             await self.disconnect_model(model)
 
@@ -864,6 +868,23 @@ class Libjuju:
         else:
             self.log.warning("Application not found: {}".format(application_name))
 
+    async def _destroy_pending_machines(self, model: Model, only_manual: bool = False):
+        """
+        Destroy pending machines in a given model
+
+        :param: only_manual:    Bool that indicates only manually provisioned
+                                machines should be destroyed (if True), or that
+                                all pending machines should be destroyed
+        """
+        status = await model.get_status()
+        for machine_id in status.machines:
+            machine_status = status.machines[machine_id]
+            if machine_status.agent_status.status == "pending":
+                if only_manual and not machine_status.instance_id.startswith("manual:"):
+                    break
+                machine = model.machines[machine_id]
+                await machine.destroy(force=True)
+
     # async def destroy_machine(
     #     self, model: Model, machine_id: str, total_timeout: float = 3600
     # ):