-
Notifications
You must be signed in to change notification settings - Fork 30
refactor terraform scripts and apply/destroy cli commands #126
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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']) | ||
|
||
Comment on lines
+36
to
+37
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. having -auto-approve here would not give the users a change to see exactly what changes are being made to the infra right? They should probably have a plan phase before destroy There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes. It won't let user to see what changed. I am not sure user can interact with subprocess. Also I think auto approve could be an Orion in our cli. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. since we don't pipe the stdin the user can interact with it as if we called it from the terminal, so it will be a low friction way to perform all the terraform commands and yeah, we should have the auto-approve command or a more general command like '--yes' or something. |
||
|
||
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should we pipe out the outputs and stderr from terraform? we want the users to know exactly what terraform is doing write?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Doesn't user already see the changes in their console with subprocess?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ohh the issue was there were two subprocess calls happening if return_output if false.