Skip to content

Commit

Permalink
feat: Run Terraform
Browse files Browse the repository at this point in the history
Give Cally the ability to run terraform directly, so that there is no need to mess
around with outputs or stacks in the same directory conflicting with each other.
  • Loading branch information
techman83 committed Nov 24, 2024
1 parent e48a84d commit 1a7592b
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 6 deletions.
4 changes: 4 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -141,3 +141,7 @@ select = [
"S404", # `subprocess` module is possibly insecure - used for 'cdktf get'
"S602" # `subprocess` call with `shell=True` identified - required for 'cdktf get'
]
"src/cally/cli/tools/terraform.py" = [
"S404", # `subprocess` module is possibly insecure - used for 'terraform x'
"S603", # `subprocess` call: check for execution of untrusted input - CLI wrapper is expected to take these inputs
]
29 changes: 29 additions & 0 deletions src/cally/cli/commands/tf.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import sys
from pathlib import Path
from typing import Tuple

import click

Expand Down Expand Up @@ -32,5 +34,32 @@ def write_template(config: CallyStackServiceConfig, output: Path):
click.secho(f'Template written to {output}')


@click.command(
name='run',
cls=CallyStackServiceCommand(),
context_settings={'ignore_unknown_options': True},
)
@click.option(
'--terraform-path',
envvar='CALLY_TERRAFORM_PATH',
type=click.Path(file_okay=True, dir_okay=False, executable=True),
default='/usr/bin/terraform',
help='Path to the terraform binary',
)
@click.argument('args', nargs=-1, type=click.UNPROCESSED)
@click.pass_obj
def run_terraform(
config: CallyStackServiceConfig, terraform_path: str, args: Tuple[str, ...]
):
tf_cmd = terraform.Command(terraform_path=terraform_path, arguments=args)
with terraform.Action(service=config.config) as action:
action.synth_stack()
if not tf_cmd.success:
click.secho(message=tf_cmd.stderr, fg='red')
sys.exit(tf_cmd.returncode)
click.secho(tf_cmd.stdout)


tf.add_command(print_template)
tf.add_command(run_terraform)
tf.add_command(write_template)
11 changes: 5 additions & 6 deletions src/cally/cli/tools/terraform.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from pathlib import Path
from tempfile import TemporaryDirectory
from types import TracebackType
from typing import List, Type
from typing import Tuple, Type

from cally.cdk import stacks

Expand Down Expand Up @@ -57,20 +57,19 @@ def print(self) -> str:


class Command:
arguments: List[str]
arguments: Tuple[str, ...]
terraform_path: str
_result = subprocess.CompletedProcess
_result: subprocess.CompletedProcess

def __init__(self, terraform_path: str, arguments: List[str]) -> None:
def __init__(self, terraform_path: str, arguments: Tuple[str, ...]) -> None:
self.terraform_path = terraform_path
self.arguments = arguments

@property
def result(self) -> subprocess.CompletedProcess:
if getattr(self, '_result', None) is None:
arguments = ' '.join(self.arguments)
self._result = subprocess.run(
command=f'{self.terraform_path} {arguments}',
args=[self.terraform_path, *self.arguments],
capture_output=True,
check=False,
)
Expand Down

0 comments on commit 1a7592b

Please sign in to comment.