|
| 1 | +from __future__ import annotations |
| 2 | +import os, sys, subprocess, json, pathlib |
| 3 | +import click |
| 4 | + |
| 5 | +ROOT = pathlib.Path(__file__).parent.resolve() |
| 6 | +TF_DIRS = { |
| 7 | + "serverless_public": ROOT / "terraform" / "serverless_public", |
| 8 | + "private_mig": ROOT / "terraform" / "private_mig", |
| 9 | +} |
| 10 | + |
| 11 | +def run(cmd: list[str], cwd: pathlib.Path): |
| 12 | + p = subprocess.Popen(cmd, cwd=str(cwd), stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True) |
| 13 | + for line in p.stdout: |
| 14 | + print(line, end="") |
| 15 | + p.wait() |
| 16 | + if p.returncode != 0: |
| 17 | + raise SystemExit(p.returncode) |
| 18 | + |
| 19 | +@click.group() |
| 20 | +def cli(): |
| 21 | + pass |
| 22 | + |
| 23 | +@cli.command() |
| 24 | +@click.option("--scenario", type=click.Choice(["serverless_public", "private_mig"]), required=True) |
| 25 | +@click.option("--var-file", "-var-file", "var_file", required=False, help="Path to terraform.tfvars file.") |
| 26 | +def init(scenario, var_file): |
| 27 | + tf = TF_DIRS[scenario] |
| 28 | + run(["terraform", "init"], tf) |
| 29 | + print("✅ init complete") |
| 30 | + |
| 31 | +@cli.command() |
| 32 | +@click.option("--scenario", type=click.Choice(["serverless_public", "private_mig"]), required=True) |
| 33 | +@click.option("--var-file", "-var-file", "var_file", required=False, help="Path to terraform.tfvars file.") |
| 34 | +def plan(scenario, var_file): |
| 35 | + tf = TF_DIRS[scenario] |
| 36 | + cmd = ["terraform", "plan"] |
| 37 | + if var_file: |
| 38 | + cmd += ["-var-file", var_file] |
| 39 | + run(cmd, tf) |
| 40 | + |
| 41 | +@cli.command() |
| 42 | +@click.option("--scenario", type=click.Choice(["serverless_public", "private_mig"]), required=True) |
| 43 | +@click.option("--auto-approve", is_flag=True, default=True, help="Auto-approve apply.") |
| 44 | +@click.option("--var-file", "-var-file", "var_file", required=False, help="Path to terraform.tfvars file.") |
| 45 | +def deploy(scenario, auto_approve, var_file): |
| 46 | + tf = TF_DIRS[scenario] |
| 47 | + cmd = ["terraform", "apply"] |
| 48 | + if var_file: |
| 49 | + cmd += ["-var-file", var_file] |
| 50 | + if auto_approve: |
| 51 | + cmd += ["-auto-approve"] |
| 52 | + run(cmd, tf) |
| 53 | + |
| 54 | +@cli.command() |
| 55 | +@click.option("--scenario", type=click.Choice(["serverless_public", "private_mig"]), required=True) |
| 56 | +def outputs(scenario): |
| 57 | + tf = TF_DIRS[scenario] |
| 58 | + p = subprocess.run(["terraform", "output", "-json"], cwd=tf, capture_output=True, text=True) |
| 59 | + if p.returncode != 0: |
| 60 | + print(p.stdout + p.stderr) |
| 61 | + raise SystemExit(p.returncode) |
| 62 | + try: |
| 63 | + data = json.loads(p.stdout or "{}") |
| 64 | + except Exception: |
| 65 | + data = {} |
| 66 | + print(json.dumps(data, indent=2)) |
| 67 | + |
| 68 | +@cli.command() |
| 69 | +@click.option("--scenario", type=click.Choice(["serverless_public", "private_mig"]), required=True) |
| 70 | +@click.option("--auto-approve", is_flag=True, default=True) |
| 71 | +def destroy(scenario, auto_approve): |
| 72 | + tf = TF_DIRS[scenario] |
| 73 | + cmd = ["terraform", "destroy"] |
| 74 | + if auto_approve: |
| 75 | + cmd += ["-auto-approve"] |
| 76 | + run(cmd, tf) |
| 77 | + |
| 78 | +if __name__ == "__main__": |
| 79 | + cli() |
0 commit comments