From 7ce29c965ee56d12383f347d03c42bf494506c41 Mon Sep 17 00:00:00 2001 From: apollorion Date: Thu, 23 Jan 2025 15:28:23 -0500 Subject: [PATCH] feat: verify container is started before proceeding --- spacemk/exporters/terraform.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/spacemk/exporters/terraform.py b/spacemk/exporters/terraform.py index 32a54ba..2f34e27 100644 --- a/spacemk/exporters/terraform.py +++ b/spacemk/exporters/terraform.py @@ -25,6 +25,10 @@ def __init__(self, organization_id: str, workspace_id: str): message = f"Could not trigger a plan for the '{organization_id}/{workspace_id}' workspace" super().__init__(message) +class AgentStartError(Exception): + def __init__(self): + super().__init__("Failed to verify container has started") + class TerraformExporter(BaseExporter): def __init__(self, config: dict): @@ -1743,6 +1747,20 @@ def _start_agent_container(self, agent_pool_id: str, container_name: str) -> Con remove=True, ) + found = False + attempts = 0 + while not found: + ps = docker.ps() + for container in ps: + if container.name == container_name and container.state.running: + logging.info(f"Container Verified Started: {container}") + found = True + break + attempts += 1 + max_attempts = 10 + if attempts > max_attempts: + raise AgentStartError + logging.debug(f"Using TFC/TFE agent Docker container '{container.id}' from image '{container.config.image}'") return container