Skip to content

Commit

Permalink
🔧refactor: pydantic provider validate
Browse files Browse the repository at this point in the history
  • Loading branch information
D10S0VSkY-OSS committed Nov 26, 2023
1 parent 52a3578 commit 782c00a
Show file tree
Hide file tree
Showing 6 changed files with 179 additions and 131 deletions.
64 changes: 59 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,48 @@
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 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)]


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 +65,7 @@ class TfvarsParams(DeployParamsBase):
class config:
frozenset = True


class DeployParams(BaseModel):
git_repo: str
name: str
Expand All @@ -38,11 +74,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 +97,7 @@ class DownloadGitRepoParams(BaseModel):
class Config:
frozenset = True


class ApplyParams(DeployParamsBase, DownloadBinaryParams):
branch: str
secreto: Any
Expand All @@ -69,8 +107,24 @@ 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] = ""
subprocess_handler: Optional[Any] = SubprocessHandler()
94 changes: 42 additions & 52 deletions sld-api-backend/src/worker/domain/services/pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
decode_responses=True,
)


class DownloadGitRepo:
def __init__(self, params: DownloadGitRepoParams, provider: ProviderRequirements):
self.params = params
Expand All @@ -29,6 +30,8 @@ def __call__(self):
self.params.branch,
self.params.project_path
)


class DownloadBinary:
def __init__(self, params: DownloadBinaryParams, provider: ProviderRequirements):
self.params = params
Expand All @@ -39,6 +42,7 @@ def __call__(self):
self.params.version,
)


class RemoteState:
def __init__(self, params: RemoteStateParams, provider: ProviderRequirements):
self.params = params
Expand All @@ -53,6 +57,7 @@ def __call__(self):
self.params.project_path
)


class Tfvars:
def __init__(self, params: TfvarsParams, provider: ProviderRequirements):
self.params = params
Expand All @@ -68,78 +73,54 @@ def __call__(self):
self.params.variables
)


class GetVariables:
def __init__(self, params: GetVariablesParams, provider: ProviderGetVars):
self.params = params
self.provider = provider

def __call__(self):
return self.provider.json_vars(
self.params.name,
self.params.stack_name,
self.params.environment,
self.params.name,
self.params.stack_name,
self.params.environment,
self.params.squad,
self.params.project_path
)


class plan:
def __init__(self, params: PlanParams, provider: ProviderActions):
self.params = params
self.provider = provider

def __call__(self):
return self.provider.plan(
self.params.name,
self.params.stack_name,
self.params.branch,
self.params.environment,
self.params.squad,
self.params.version,
self.params.secreto,
self.params.variables_file,
self.params.project_path,
)
return self.provider.plan(self.params)


class apply:
def __init__(self, params: ApplyParams, provider: ProviderActions):
self.params = params
self.provider = provider

def __call__(self):
return self.provider.apply(
self.params.name,
self.params.stack_name,
self.params.branch,
self.params.environment,
self.params.squad,
self.params.version,
self.params.secreto,
self.params.variables_file,
self.params.project_path,
)
return self.provider.apply(self.params)


class destroy:
def __init__(self, params: DestroyParams, provider: ProviderActions):
def __init__(self, params: DeployParams, provider: ProviderActions):
self.params = params
self.provider = provider

def __call__(self):
return self.provider.destroy(
self.params.name,
self.params.stack_name,
self.params.branch,
self.params.environment,
self.params.squad,
self.params.version,
self.params.secreto,
self.params.variables_file,
self.params.project_path,
)

return self.provider.destroy(self.params)


# PIPELINE
class Pipeline:
def __init__(self, params: DeployParams):
self.params = params

def locked_task(self):
logging.info(f"Checking if task {self.params.name}-{self.params.squad}-{self.params.environment} is locked in cache server {settings.CACHE_SERVER}")
logging.info(f"Locking task {self.params.name}-{self.params.squad}-{self.params.environment}")
Expand All @@ -148,8 +129,7 @@ def locked_task(self):

def unlock_task(self):
r.delete(f"{self.params.name}-{self.params.squad}-{self.params.environment}")



# Git clone repo
def download_git_repo(self):
git_params = DownloadGitRepoParams(
Expand All @@ -166,8 +146,7 @@ def download_git_repo(self):
if download_git_repo_result["rc"] != 0:
raise Exception(download_git_repo_result)
return download_git_repo_result



# Download terrafom
def download_binary(self):
binari_params = DownloadBinaryParams(
Expand Down Expand Up @@ -209,7 +188,6 @@ def set_tfvars(self):
if tfvars_result["rc"] != 0:
raise Exception(tfvars_result)
return tfvars_result


def get_variables(self):
get_variables_params = GetVariablesParams(
Expand All @@ -227,16 +205,20 @@ def get_variables(self):

# Plan execute
def execute_plan(self):
plan_params = PlanParams(
plan_params = DeployParams(
git_repo=self.params.git_repo,
name=self.params.name,
stack_name=self.params.stack_name,
branch=self.params.branch,
environment=self.params.environment,
squad=self.params.squad,
branch=self.params.branch,
version=self.params.version,
variables=self.params.variables,
project_path=self.params.project_path,
secreto=self.params.secreto,
variables_file=self.params.variables_file,
project_path=self.params.project_path,
user=self.params.user,
task_id=self.params.task_id
)
plan_execute = plan(params=plan_params, provider=ProviderActions)
plan_result = plan_execute()
Expand All @@ -246,16 +228,20 @@ def execute_plan(self):

# Apply execute
def execute_apply(self):
apply_params = ApplyParams(
apply_params = DeployParams(
git_repo=self.params.git_repo,
name=self.params.name,
stack_name=self.params.stack_name,
branch=self.params.branch,
environment=self.params.environment,
squad=self.params.squad,
branch=self.params.branch,
version=self.params.version,
variables=self.params.variables,
project_path=self.params.project_path,
secreto=self.params.secreto,
variables_file=self.params.variables_file,
project_path=self.params.project_path,
user=self.params.user,
task_id=self.params.task_id
)
apply_execute = apply(params=apply_params, provider=ProviderActions)
apply_result = apply_execute()
Expand All @@ -265,16 +251,20 @@ def execute_apply(self):

# Destroy execute
def execute_destroy(self):
destroy_params = DestroyParams(
destroy_params = DeployParams(
git_repo=self.params.git_repo,
name=self.params.name,
stack_name=self.params.stack_name,
branch=self.params.branch,
environment=self.params.environment,
squad=self.params.squad,
branch=self.params.branch,
version=self.params.version,
variables=self.params.variables,
project_path=self.params.project_path,
secreto=self.params.secreto,
variables_file=self.params.variables_file,
project_path=self.params.project_path,
user=self.params.user,
task_id=self.params.task_id
)
destroy_execute = destroy(params=destroy_params, provider=ProviderActions)
destroy_result = destroy_execute()
Expand Down
Loading

0 comments on commit 782c00a

Please sign in to comment.