Skip to content

Commit

Permalink
🐛fix: when terraform error not output
Browse files Browse the repository at this point in the history
  • Loading branch information
D10S0VSkY-OSS committed Nov 27, 2023
1 parent 782c00a commit 6c96c55
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 42 deletions.
2 changes: 1 addition & 1 deletion sld-api-backend/config/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class Settings(BaseSettings):
AWS_CONGIG_DEFAULT_FOLDER: str = f"{os.environ['HOME']}/.aws"
AWS_SHARED_CREDENTIALS_FILE: str = f"{AWS_CONGIG_DEFAULT_FOLDER}/credentials"
AWS_SHARED_CONFIG_FILE: str = f"{AWS_CONGIG_DEFAULT_FOLDER}/config"
TASK_MAX_RETRY: int = os.getenv("SLD_TASK_MAX_RETRY", 1)
TASK_MAX_RETRY: int = os.getenv("SLD_TASK_MAX_RETRY", 0)
TASK_RETRY_INTERVAL: int = os.getenv("SLD_TASK_RETRY_INTERVAL", 20)
TASK_LOCKED_EXPIRED: int = os.getenv("SLD_TASK_LOCKED_EXPIRED", 300)
TASK_ROUTE: bool = os.getenv("SLD_TASK_ROUTE", False)
Expand Down
3 changes: 2 additions & 1 deletion sld-api-backend/config/celery_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,5 @@
celery_app.conf.update(task_track_started=True)
celery_app.conf.update(result_extended=True)
celery_app.conf.broker_transport_options = {"visibility_timeout": 28800} # 8 hours.
celery_app.conf.result_expires = os.getenv("SLD_RESULT_EXPIRE", "259200")
celery_app.conf.result_expires = os.getenv("SLD_RESULT_EXPIRE", "259200")
celery_app.conf.broker_connection_retry_on_startup = True
35 changes: 35 additions & 0 deletions sld-api-backend/src/worker/domain/services/command.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import logging
import subprocess
from subprocess import Popen, PIPE
from typing import Tuple, List


class SubprocessHandler:
def run_command(self, command: str) -> Tuple[int, List[str], List[str]]:
try:
process = Popen(
command,
shell=True,
stdout=PIPE,
stderr=PIPE,
universal_newlines=True
)

stdout_lines, stderr_lines = process.communicate()

output_lines = stdout_lines.strip().split('\n') if stdout_lines else []
error_lines = stderr_lines.strip().split('\n') if stderr_lines else []

if process.returncode == 0:
return process.returncode, output_lines
return process.returncode, error_lines

except subprocess.CalledProcessError as err:
logging.error(
f"Error executing command, code: {err.returncode}. Error:\n{err.output}"
)
return err.returncode, [], [err.output]

except Exception as e:
logging.error(f"An error occurred: {e}")
return 1, [], [str(e)]
46 changes: 9 additions & 37 deletions sld-api-backend/src/worker/providers/hashicorp/actions.py
Original file line number Diff line number Diff line change
@@ -1,42 +1,13 @@
import os
import logging
from typing import List, Tuple
import subprocess
from dataclasses import dataclass
from subprocess import Popen, PIPE, STDOUT
import jmespath
import requests
from config.api import settings

from src.worker.security.providers_credentials import secret, unsecret
from src.worker.domain.services.command import SubprocessHandler


class SubprocessHandler:
def run_command(self, command: str) -> Tuple[int, List[str]]:
try:
process = Popen(
command,
shell=True,
stdout=PIPE,
stderr=PIPE,
universal_newlines=True
)

# Read stdout and stderr in real-time
output_lines = []
while True:
line = process.stdout.readline()
logging.info(line.rstrip('\n'))
if not line:
break
output_lines.append(line.strip())

# Wait for the process to finish
returncode = process.wait()
return returncode, output_lines

except Exception as e:
return 1, [str(e)]


@dataclass
Expand All @@ -54,15 +25,15 @@ class Actions(StructBase):
secreto: dict
variables_file: str
project_path: str
subprocess_handler = SubprocessHandler()
subprocess_handler: SubprocessHandler = SubprocessHandler()

def execute_terraform_command(self, command: str) -> dict:
try:
secret(self.stack_name, self.environment, self.squad, self.name, self.secreto)
deploy_state = f"{self.environment}_{self.stack_name}_{self.squad}_{self.name}"

variables_files = (
f"{self.stack_name}.tfvars.json"
f"{self.name}.tfvars.json"
if not self.variables_file
else self.variables_file
)
Expand All @@ -73,8 +44,8 @@ def execute_terraform_command(self, command: str) -> dict:
os.chdir(f"/tmp/{self.stack_name}/{self.environment}/{self.squad}/{self.name}/{self.project_path}")

init_command = f"/tmp/{self.version}/terraform init -no-color -input=false --upgrade"
plan_command = f"/tmp/{self.version}/terraform {command} -input=false -refresh -no-color -var-file={variables_files} -out={self.stack_name}.tfplan"
apply_command = f"/tmp/{self.version}/terraform {command} -input=false -auto-approve -no-color {self.stack_name}.tfplan"
plan_command = f"/tmp/{self.version}/terraform {command} -input=false -refresh -no-color -var-file={variables_files} -out={self.name}.tfplan"
apply_command = f"/tmp/{self.version}/terraform {command} -input=false -auto-approve -no-color {self.name}.tfplan"
destroy_command = f"/tmp/{self.version}/terraform {command} -input=false -auto-approve -no-color -var-file={variables_files}"

result, output = self.subprocess_handler.run_command(init_command)
Expand All @@ -86,6 +57,7 @@ def execute_terraform_command(self, command: str) -> dict:
result, output = self.subprocess_handler.run_command(destroy_command)

unsecret(self.stack_name, self.environment, self.squad, self.name, self.secreto)

rc = result

output_data = {
Expand All @@ -105,7 +77,7 @@ def execute_terraform_command(self, command: str) -> dict:

return output_data

except Exception as e:
except Exception as err:
return {
"command": command,
"deploy": self.name,
Expand All @@ -116,8 +88,8 @@ def execute_terraform_command(self, command: str) -> dict:
"tfvars_files": self.variables_file,
"project_path": f"/tmp/{self.stack_name}/{self.environment}/{self.squad}/{self.name}/{self.project_path}",
"remote_state": f"http://remote-state:8080/terraform_state/{deploy_state}",
"stdout": "ko",
"error_message": str(e),
"stdout": output,
"error_message": err,
}


Expand Down
4 changes: 2 additions & 2 deletions sld-api-backend/src/worker/providers/hashicorp/templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ class Tfvars(StructBase):

def save(self) -> dict:
try:
file_path = f"/tmp/{self.stack_name}/{self.environment}/{self.squad}/{self.name}/{self.stack_name}.tfvars.json"
file_path = f"/tmp/{self.stack_name}/{self.environment}/{self.squad}/{self.name}/{self.name}.tfvars.json"
if self.project_path:
file_path = f"/tmp/{self.stack_name}/{self.environment}/{self.squad}/{self.name}/{self.project_path}/{self.stack_name}.tfvars.json"
file_path = f"/tmp/{self.stack_name}/{self.environment}/{self.squad}/{self.name}/{self.project_path}/{self.name}.tfvars.json"
with open(file_path, "w") as tfvars_json:
json.dump(self.variables, tfvars_json)
return {"command": "tfvars", "rc": 0, "stdout": self.variables}
Expand Down
2 changes: 1 addition & 1 deletion sld-dashboard/app/home/templates/deploy-edit.html
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ <h6><strong>{{key}}</strong></h6>
<div class="border-top my-4"></div>
<!-- End Add custom variables -->
{% endif %}
<button type="submit" class="btn btn-success" name="button" value=plan
<button type="submit" class="btn btn-success" name="button" value=plan style="width: 20vw;"
action="{{url_for('.list_deploys')}}">
Plan
</button>
Expand Down

0 comments on commit 6c96c55

Please sign in to comment.