diff --git a/bentoctl/cli/__init__.py b/bentoctl/cli/__init__.py index 459a95f..9bc7d11 100644 --- a/bentoctl/cli/__init__.py +++ b/bentoctl/cli/__init__.py @@ -3,7 +3,7 @@ import click from bentoctl import __version__ -from bentoctl.cli.helper_scripts import terraform_apply, terraform_destroy +from bentoctl.terraform import terraform_apply, terraform_destroy, is_terraform_applied from bentoctl.cli.interactive import deployment_config_builder from bentoctl.cli.operator_management import get_operator_management_subcommands from bentoctl.cli.utils import BentoctlCommandGroup, handle_bentoctl_exceptions @@ -182,10 +182,13 @@ def destroy(deployment_config_file): Destroy all the resources created and remove the registry. """ deployment_config = DeploymentConfig.from_file(deployment_config_file) - if deployment_config.template_type.startswith("terraform"): + if ( + deployment_config.template_type.startswith("terraform") + and is_terraform_applied() + ): terraform_destroy() - deployment_config.delete_repository() - console.print(f"Deleted the repository {deployment_config.repository_name}") + deployment_config.delete_repository() + console.print(f"Deleted the repository {deployment_config.repository_name}") @bentoctl.command() diff --git a/bentoctl/cli/helper_scripts.py b/bentoctl/cli/helper_scripts.py deleted file mode 100644 index d6dfdd2..0000000 --- a/bentoctl/cli/helper_scripts.py +++ /dev/null @@ -1,36 +0,0 @@ -import os -import subprocess - -from bentoctl.exceptions import BentoctlException - -TERRAFORM_VALUES_FILE = "bentoctl.tfvars" -TERRAFORM_INIT_FOLDER = ".terraform" - - -def terraform_run(cmd: list): - try: - subprocess.run(["terraform", *cmd], check=False) - except FileNotFoundError: - raise BentoctlException( - "terraform not available. Please make " - "sure terraform is installed and available your path." - ) - - -def terraform_destroy(): - if not os.path.exists(os.path.join(os.curdir, TERRAFORM_VALUES_FILE)): - raise BentoctlException( - f"{TERRAFORM_VALUES_FILE} not found in current directory." - ) - terraform_run(["destroy", "-var-file", TERRAFORM_VALUES_FILE]) - - -def is_terraform_initialised(): - return os.path.exists(os.path.join(os.curdir, TERRAFORM_INIT_FOLDER)) - - -def terraform_apply(): - if not is_terraform_initialised(): - terraform_run(["init"]) - - terraform_run(["apply", "-var-file", TERRAFORM_VALUES_FILE]) diff --git a/bentoctl/terraform.py b/bentoctl/terraform.py new file mode 100644 index 0000000..8fa739e --- /dev/null +++ b/bentoctl/terraform.py @@ -0,0 +1,63 @@ +import os +import json +import subprocess + +from bentoctl.exceptions import BentoctlException + +TERRAFORM_VALUES_FILE = "bentoctl.tfvars" +TERRAFORM_INIT_FOLDER = ".terraform" + + +def terraform_run(cmd: list, return_output: bool = False): + try: + if not return_output: + subprocess.run(["terraform", *cmd], check=False) + proc = subprocess.Popen( + ["terraform", *cmd], + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + ) + stdout, stderr = proc.communicate() + return proc.returncode, stdout.decode("utf-8"), stderr.decode("utf-8") + except FileNotFoundError: + raise BentoctlException( + "terraform not available. Please make " + "sure terraform is installed and available your path." + ) + + +def terraform_destroy(): + if not is_terraform_initialised(): + raise BentoctlException('terraform is not initialised') + if not os.path.exists(os.path.join(os.curdir, TERRAFORM_VALUES_FILE)): + raise BentoctlException( + f"{TERRAFORM_VALUES_FILE} not found in current directory." + ) + terraform_run(["apply", "-destroy", "-var-file", TERRAFORM_VALUES_FILE, '-auto-approve']) + + +def is_terraform_initialised(): + return os.path.exists(os.path.join(os.curdir, TERRAFORM_INIT_FOLDER)) + + +def terraform_apply(): + if not is_terraform_initialised(): + terraform_run(["init"]) + + terraform_run(["apply", "-var-file", TERRAFORM_VALUES_FILE, '-auto-approve']) + + +def terraform_output(): + return_code, result, error = terraform_run(["output", "-json"], return_output=True) + if return_code != 0: + raise BentoctlException(error) + return json.loads(result) + + +def is_terraform_applied() -> bool: + """ + Check if terraform is applied. + """ + # If the terraform is not currently applied, it will return an empty dict. + result = terraform_output() + return True if result else False