diff --git a/tools/cluster.py b/tools/cluster.py index 986a5c6..9a6270d 100644 --- a/tools/cluster.py +++ b/tools/cluster.py @@ -20,6 +20,7 @@ def __init__( terraform_dir: Path, chart_dir: Path, root_dir: Path | None = None, + terraform_state_file: Path | None = None, ): """ Initialize cluster manager. @@ -28,12 +29,13 @@ def __init__( terraform_dir: Directory containing Terraform configuration chart_dir: Directory containing Helm chart root_dir: Root directory for resolving relative paths + terraform_state_file: Optional path to terraform state file """ self.terraform_dir = terraform_dir self.chart_dir = chart_dir self.root_dir = root_dir or terraform_dir.parent - self.terraform = TerraformManager(terraform_dir) + self.terraform = TerraformManager(terraform_dir, terraform_state_file) def deploy( self, diff --git a/tools/terraform.py b/tools/terraform.py index 6bfd2b3..405f7ae 100644 --- a/tools/terraform.py +++ b/tools/terraform.py @@ -12,19 +12,23 @@ class TerraformManager: """Manages Terraform operations for infrastructure provisioning.""" - def __init__(self, working_dir: Path): + def __init__(self, working_dir: Path, state_file: Path | None = None): """ Initialize Terraform manager. Args: working_dir: Directory containing Terraform configuration + state_file: Optional path to terraform state file (if not in working_dir) """ self.working_dir = working_dir + self.state_file = state_file if not self.working_dir.exists(): fail(f"Terraform directory not found: {working_dir}") def _get_state_args(self) -> list[str]: """Return extra Terraform CLI args for state handling.""" + if self.state_file: + return ["-state", str(self.state_file)] return [] def init(self, upgrade: bool = True) -> None: @@ -85,6 +89,7 @@ def apply( else: if auto_approve: cmd.append("-auto-approve") + cmd.extend(self._get_state_args()) if var_args: cmd.extend(var_args) @@ -121,8 +126,10 @@ def get_outputs(self) -> dict[str, Any]: Returns: Dictionary of output values """ + cmd = ["terraform", "output", "-json"] + cmd.extend(self._get_state_args()) proc = run_command( - ["terraform", "output", "-json"], + cmd, cwd=self.working_dir, capture=True, check=False, @@ -161,6 +168,7 @@ def get_output(self, key: str, raw: bool = False) -> str | None: cmd = ["terraform", "output"] if raw: cmd.append("-raw") + cmd.extend(self._get_state_args()) cmd.append(key) proc = run_command(cmd, cwd=self.working_dir, capture=True, check=False, verbose=False)