Skip to content

Commit

Permalink
Merge pull request #197 from D10S0VSkY-OSS/refactor/terraform-commands
Browse files Browse the repository at this point in the history
Refactor/terraform commands
  • Loading branch information
D10S0VSkY-OSS authored Nov 28, 2023
2 parents 5130676 + d7a1c39 commit decad67
Show file tree
Hide file tree
Showing 22 changed files with 372 additions and 379 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/sld-api-docker-image.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ jobs:
- name: Build the Docker image with tag
working-directory: ./sld-api-backend
run: docker build . --file Dockerfile --tag ${{ secrets.DOCKER_USERNAME }}/sld-api:2.26.1
run: docker build . --file Dockerfile --tag ${{ secrets.DOCKER_USERNAME }}/sld-api:2.27.0

- name: Docker Push with tag
#if: github.event.pull_request.merged == true
run: docker push ${{ secrets.DOCKER_USERNAME }}/sld-api:2.26.1
run: docker push ${{ secrets.DOCKER_USERNAME }}/sld-api:2.27.0

- name: Build the Docker image
working-directory: ./sld-api-backend
Expand Down
5 changes: 2 additions & 3 deletions .github/workflows/sld-dashboard-docker-image.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,11 @@ jobs:
- name: Build the Docker image with tags
working-directory: ./sld-dashboard
run: docker build . --file Dockerfile --tag ${{ secrets.DOCKER_USERNAME }}/sld-dashboard:2.26.2
run: docker build . --file Dockerfile --tag ${{ secrets.DOCKER_USERNAME }}/sld-dashboard:2.27.0

- name: Docker Push with tags
#if: github.event.pull_request.merged == true
run: docker push ${{ secrets.DOCKER_USERNAME }}/sld-dashboard:2.26.2

run: docker push ${{ secrets.DOCKER_USERNAME }}/sld-dashboard:2.27.0
- name: Build the Docker image
working-directory: ./sld-dashboard
run: docker build . --file Dockerfile --tag ${{ secrets.DOCKER_USERNAME }}/sld-dashboard:latest
Expand Down
2 changes: 1 addition & 1 deletion play-with-sld/kubernetes/k8s/sld-api-backend.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ spec:
subdomain: primary
containers:
- name: api-backend
image: d10s0vsky/sld-api:2.26.1
image: d10s0vsky/sld-api:2.27.0
imagePullPolicy: Always
command: ["python", "-m", "uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000", "--workers", "1"]
ports:
Expand Down
2 changes: 1 addition & 1 deletion play-with-sld/kubernetes/k8s/sld-dashboard.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ spec:
subdomain: primary
containers:
- name: sld-dashboard
image: d10s0vsky/sld-dashboard:2.26.2
image: d10s0vsky/sld-dashboard:2.27.0
env:
- name: PATH
value: "/home/sld/.asdf/shims:/home/sld/.asdf/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
Expand Down
2 changes: 1 addition & 1 deletion play-with-sld/kubernetes/k8s/sld-worker-default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ spec:
subdomain: primary
containers:
- name: stack-deploy-worker-default
image: d10s0vsky/sld-api:2.26.1
image: d10s0vsky/sld-api:2.27.0
imagePullPolicy: Always
env:
- name: TF_WARN_OUTPUT_ERRORS
Expand Down
2 changes: 1 addition & 1 deletion play-with-sld/kubernetes/k8s/sld-worker-squad1.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ spec:
subdomain: primary
containers:
- name: stack-deploy-worker-squad1
image: d10s0vsky/sld-api:2.26.1
image: d10s0vsky/sld-api:2.27.0
imagePullPolicy: Always
env:
- name: TF_WARN_OUTPUT_ERRORS
Expand Down
2 changes: 1 addition & 1 deletion play-with-sld/kubernetes/k8s/sld-worker-squad2.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ spec:
subdomain: primary
containers:
- name: stack-deploy-worker-squad2
image: d10s0vsky/sld-api:2.26.1
image: d10s0vsky/sld-api:2.27.0
imagePullPolicy: Always
env:
- name: TF_WARN_OUTPUT_ERRORS
Expand Down
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
36 changes: 31 additions & 5 deletions sld-api-backend/src/worker/domain/entities/worker.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
from pydantic import BaseModel
from typing import Optional, Any
from typing import Optional, Any, Dict


import logging
from typing import List, Tuple
from subprocess import Popen, PIPE



class DownloadBinaryParams(BaseModel):
version: str
#url: Optional[str]


# url: Optional[str]
class Config:
frozenset = True


class DeployParamsBase(BaseModel):
name: str
stack_name: str
Expand All @@ -30,6 +38,7 @@ class TfvarsParams(DeployParamsBase):
class config:
frozenset = True


class DeployParams(BaseModel):
git_repo: str
name: str
Expand All @@ -38,11 +47,12 @@ class DeployParams(BaseModel):
squad: str
branch: str
version: str
variables: Any
variables: Optional[Dict[str, Any]] = {}
secreto: Any
variables_file: Optional[str] = ""
project_path: Optional[str] = ""
user: Optional[str] = ""
task_id: Optional[str] = ""

class Config:
frozenset = True
Expand All @@ -60,6 +70,7 @@ class DownloadGitRepoParams(BaseModel):
class Config:
frozenset = True


class ApplyParams(DeployParamsBase, DownloadBinaryParams):
branch: str
secreto: Any
Expand All @@ -69,8 +80,23 @@ class ApplyParams(DeployParamsBase, DownloadBinaryParams):
class Config:
frozenset = True


class PlanParams(ApplyParams):
pass


class DestroyParams(ApplyParams):
pass
pass


class ActionBase(BaseModel):
name: str
stack_name: str
branch: str
environment: str
squad: str
version: str
secreto: dict
project_path: Optional[str] = ""
variables_file: Optional[str] = ""
task_id: Optional[str] = ""
51 changes: 51 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,51 @@
import logging
import subprocess
import redis

redis_client = redis.Redis(host='localhost', port=6379, db=15)


def command(command: str, channel: str):
try:
output_lines = []
command_description = f"Executing command: {command}"
output_lines.append(command_description)
redis_client.publish(channel, "-" * 80)
redis_client.publish(channel, command_description)
redis_client.publish(channel, "-" * 80)

process = subprocess.Popen(
command,
shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
text=True,
)

logging.info("#" * 80)
for line in process.stdout:
if "[DEBUG]" not in line:
cleaned_line = line.strip()
output_lines.append(cleaned_line)
logging.info(cleaned_line)
try:
redis_client.publish(f'{channel}', cleaned_line)
except Exception as err:
logging.error(f"Error publish redis: {err}")

logging.info("#" * 80)

process.wait()

if process.returncode == 0:
logging.info(f"Command {command} executed successfully.")
else:
logging.error(
f"Error executing the command {command}. Exit code: {process.returncode}"
)
return process.returncode, output_lines
except subprocess.CalledProcessError as err:
logging.error(
f"Error execute command code: {err.returncode}. Error:\n{err.stderr}"
)
return None
Loading

0 comments on commit decad67

Please sign in to comment.