Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion tools/cluster.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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,
Expand Down
12 changes: 10 additions & 2 deletions tools/terraform.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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)
Expand Down
Loading