From 6c96c557f7adbbe462933da9af631ef415db262b Mon Sep 17 00:00:00 2001 From: d10s <79284025+D10S0VSkY-OSS@users.noreply.github.com> Date: Mon, 27 Nov 2023 02:07:16 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9Bfix:=20when=20terraform=20error=20n?= =?UTF-8?q?ot=20output?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sld-api-backend/config/api.py | 2 +- sld-api-backend/config/celery_config.py | 3 +- .../src/worker/domain/services/command.py | 35 ++++++++++++++ .../src/worker/providers/hashicorp/actions.py | 46 ++++--------------- .../worker/providers/hashicorp/templates.py | 4 +- .../app/home/templates/deploy-edit.html | 2 +- 6 files changed, 50 insertions(+), 42 deletions(-) create mode 100644 sld-api-backend/src/worker/domain/services/command.py diff --git a/sld-api-backend/config/api.py b/sld-api-backend/config/api.py index 36164151..482ed62c 100644 --- a/sld-api-backend/config/api.py +++ b/sld-api-backend/config/api.py @@ -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) diff --git a/sld-api-backend/config/celery_config.py b/sld-api-backend/config/celery_config.py index f8f3f0cf..22e01b8c 100644 --- a/sld-api-backend/config/celery_config.py +++ b/sld-api-backend/config/celery_config.py @@ -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") \ No newline at end of file +celery_app.conf.result_expires = os.getenv("SLD_RESULT_EXPIRE", "259200") +celery_app.conf.broker_connection_retry_on_startup = True \ No newline at end of file diff --git a/sld-api-backend/src/worker/domain/services/command.py b/sld-api-backend/src/worker/domain/services/command.py new file mode 100644 index 00000000..f65df2c2 --- /dev/null +++ b/sld-api-backend/src/worker/domain/services/command.py @@ -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)] diff --git a/sld-api-backend/src/worker/providers/hashicorp/actions.py b/sld-api-backend/src/worker/providers/hashicorp/actions.py index ddd1900e..27a14af5 100644 --- a/sld-api-backend/src/worker/providers/hashicorp/actions.py +++ b/sld-api-backend/src/worker/providers/hashicorp/actions.py @@ -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 @@ -54,7 +25,7 @@ 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: @@ -62,7 +33,7 @@ def execute_terraform_command(self, command: str) -> dict: 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 ) @@ -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) @@ -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 = { @@ -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, @@ -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, } diff --git a/sld-api-backend/src/worker/providers/hashicorp/templates.py b/sld-api-backend/src/worker/providers/hashicorp/templates.py index 8af4273b..cbdbd2f9 100644 --- a/sld-api-backend/src/worker/providers/hashicorp/templates.py +++ b/sld-api-backend/src/worker/providers/hashicorp/templates.py @@ -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} diff --git a/sld-dashboard/app/home/templates/deploy-edit.html b/sld-dashboard/app/home/templates/deploy-edit.html index fce3d407..0ae08b27 100644 --- a/sld-dashboard/app/home/templates/deploy-edit.html +++ b/sld-dashboard/app/home/templates/deploy-edit.html @@ -108,7 +108,7 @@
{{key}}
{% endif %} -