Skip to content

Commit

Permalink
Merge pull request #211 from D10S0VSkY-OSS/refactor/outputs
Browse files Browse the repository at this point in the history
Refactor/outputs
  • Loading branch information
D10S0VSkY-OSS authored Dec 4, 2023
2 parents e0e5394 + 082b0fc commit 4318e9d
Show file tree
Hide file tree
Showing 9 changed files with 110 additions and 178 deletions.
103 changes: 66 additions & 37 deletions sld-api-backend/src/deploy/api/container/deploy/get.py
Original file line number Diff line number Diff line change
@@ -1,35 +1,16 @@
import requests
from fastapi import Depends, HTTPException
from sqlalchemy.orm import Session

from src.deploy.infrastructure import repositories as crud_deploys
from src.shared.helpers.get_data import check_squad_user, deploy, deploy_squad
from src.shared.helpers.push_task import async_output, async_show, async_unlock
from src.shared.security import deps
from src.users.domain.entities import users as schemas_users
from src.users.infrastructure import repositories as crud_users
from config.api import settings



async def unlock_deploy(
deploy_id: int,
db: Session = Depends(deps.get_db),
current_user: schemas_users.User = Depends(deps.get_current_active_user),
):
# Get info from deploy data
deploy_data = deploy(db, deploy_id=deploy_id)
squad = deploy_data.squad
if not crud_users.is_master(db, current_user):
if not check_squad_user(current_user.squad, [squad]):
raise HTTPException(
status_code=403, detail=f"Not enough permissions in {squad}"
)
try:
stack_name = deploy_data.stack_name
environment = deploy_data.environment
name = deploy_data.name
# Get credentials by providers supported
return {"task": async_unlock(stack_name, squad, environment, name)}
except Exception as err:
raise HTTPException(status_code=400, detail=f"{err}")


async def get_all_deploys(
Expand Down Expand Up @@ -66,7 +47,6 @@ async def get_deploy_by_id(
raise HTTPException(status_code=404, detail="Deploy id Not Found")
return result
except Exception as err:
print(err)
raise HTTPException(status_code=404, detail=f"{err}")


Expand All @@ -78,24 +58,50 @@ async def get_show(
# Get info from deploy data
if crud_users.is_master(db, current_user):
deploy_data = deploy(db, deploy_id=deploy_id)
squad = deploy_data.squad
else:
# Get squad from current user
squad = current_user.squad
deploy_data = deploy_squad(db, deploy_id=deploy_id, squad=squad)
stack_name = deploy_data.stack_name
environment = deploy_data.environment
name = deploy_data.name
try:
return {"task": async_show(stack_name, squad, environment, name)}
except Exception as err:
raise HTTPException(status_code=400, detail=f"{err}")
get_path = f"{deploy_data.stack_name}-{deploy_data.squad}-{deploy_data.environment}-{deploy_data.name}"
response = requests.get(
f"{settings.REMOTE_STATE}/terraform_state/{get_path}"
)
result = response.json()
if not result:
raise HTTPException(
status_code=404, detail=f"Not enough output in {deploy_data.name}"
)
return result


async def get_output(
deploy_id: int,
db: Session = Depends(deps.get_db),
current_user: schemas_users.User = Depends(deps.get_current_active_user),
):
if crud_users.is_master(db, current_user):
deploy_data = deploy(db, deploy_id=deploy_id)
else:
# Get squad from current user
squad = current_user.squad
deploy_data = deploy_squad(db, deploy_id=deploy_id, squad=squad)
get_path = f"{deploy_data.stack_name}-{deploy_data.squad}-{deploy_data.environment}-{deploy_data.name}"
response = requests.get(
f"{settings.REMOTE_STATE}/terraform_state/{get_path}"
)
json_data = response.json()
result = json_data.get("outputs")
if not result:
raise HTTPException(
status_code=404, detail=f"Not enough output in {deploy_data.name}"
)
return result


async def unlock_deploy(
deploy_id: int,
db: Session = Depends(deps.get_db),
current_user: schemas_users.User = Depends(deps.get_current_active_user),
):
# Get info from deploy data
deploy_data = deploy(db, deploy_id=deploy_id)
Expand All @@ -106,10 +112,33 @@ async def get_output(
status_code=403, detail=f"Not enough permissions in {squad}"
)
try:
stack_name = deploy_data.stack_name
environment = deploy_data.environment
name = deploy_data.name
# Get credentials by providers supported
return {"task": async_output(stack_name, squad, environment, name)}
get_path = f"{deploy_data.stack_name}-{deploy_data.squad}-{deploy_data.environment}-{deploy_data.name}"
response = requests.delete(
f"{settings.REMOTE_STATE}/terraform_lock/{get_path}", json={}
)
return response.json()
except Exception as err:
raise HTTPException(status_code=400, detail=f"{err}")
raise err


async def lock_deploy(
deploy_id: int,
db: Session = Depends(deps.get_db),
current_user: schemas_users.User = Depends(deps.get_current_active_user),
):
# Get info from deploy data
deploy_data = deploy(db, deploy_id=deploy_id)
squad = deploy_data.squad
if not crud_users.is_master(db, current_user):
if not check_squad_user(current_user.squad, [squad]):
raise HTTPException(
status_code=403, detail=f"Not enough permissions in {squad}"
)
try:
get_path = f"{deploy_data.stack_name}-{deploy_data.squad}-{deploy_data.environment}-{deploy_data.name}"
response = requests.put(
f"{settings.REMOTE_STATE}/terraform_lock/{get_path}", json={}
)
return response.json()
except Exception as err:
raise err
8 changes: 7 additions & 1 deletion sld-api-backend/src/deploy/api/v1/deploy.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,18 @@ async def get_output(
return get_output


@router.put("/unlock/{deploy_id}", status_code=200)
@router.delete("/unlock/{deploy_id}", status_code=200)
async def unlock_deploy(
unlock_deploy: schemas_deploy.DeployBase = Depends(get.unlock_deploy),
):
return unlock_deploy

@router.put("/lock/{deploy_id}", status_code=200)
async def unlock_deploy(
lock_deploy: schemas_deploy.DeployBase = Depends(get.lock_deploy),
):
return lock_deploy


@router.get("/show/{deploy_id}", status_code=202)
async def get_show(
Expand Down
24 changes: 0 additions & 24 deletions sld-api-backend/src/shared/helpers/push_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

from src.worker.domain.entities.worker import DeployParams, DownloadGitRepoParams
from src.worker.tasks.terraform_worker import (
output,
pipeline_deploy,
pipeline_destroy,
pipeline_git_pull,
Expand All @@ -15,8 +14,6 @@
schedule_get,
schedule_update,
schedules_list,
show,
unlock,
)


Expand Down Expand Up @@ -59,20 +56,6 @@ def async_plan(plan_params: DeployParams):
return pipeline_deploy_result.task_id


def async_output(stack_name: str, environment: str, squad: str, name: str):
output_result = output.s(stack_name, environment, squad, name).apply_async(
queue="squad"
)
return output_result.task_id


def async_unlock(stack_name: str, squad: str, environment: str, name: str):
unlock_result = unlock.s(stack_name, squad, environment, name).apply_async(
queue="squad"
)
return unlock_result.task_id


def async_schedule_delete(deploy_name: str, squad: str):
deploy_schedule_delete_result = schedule_delete.s(deploy_name).apply_async(
queue="squad"
Expand Down Expand Up @@ -100,13 +83,6 @@ def async_schedule_update(deploy_name: str):
return schedule_update_result.task_id


def async_show(stack_name: str, environment: str, squad: str, name: str):
show_result = show.s(stack_name, environment, squad, name).apply_async(
queue="squad"
)
return show_result.task_id


def sync_git(
stack_name: str,
git_repo: str,
Expand Down
20 changes: 1 addition & 19 deletions sld-api-backend/src/worker/domain/services/provider.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# DI terraform provider
from src.worker.providers.hashicorp.actions import Actions, SimpleActions
from src.worker.providers.hashicorp.actions import Actions
from src.worker.providers.hashicorp.artifact import Artifact
from src.worker.providers.hashicorp.download import BinaryDownload
from src.worker.providers.hashicorp.templates import Backend, GetVars, Tfvars
Expand Down Expand Up @@ -123,21 +123,3 @@ def destroy(params: DeployParams, action: Actions = Actions) -> dict:
params.task_id,
)
return config_action.execute_terraform_command("destroy")

def output(
stack_name: str, squad: str, environment: str, name: str, action=SimpleActions
) -> dict:
config_action = action(stack_name, squad, environment, name)
return config_action.output_execute()

def unlock(
stack_name: str, squad: str, environment: str, name: str, action=SimpleActions
) -> dict:
config_action = action(stack_name, squad, environment, name)
return config_action.unlock_execute()

def show(
stack_name: str, squad: str, environment: str, name: str, action=SimpleActions
) -> dict:
config_action = action(stack_name, squad, environment, name)
return config_action.show_execute()
47 changes: 1 addition & 46 deletions sld-api-backend/src/worker/providers/hashicorp/actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,49 +91,4 @@ def execute_terraform_command(self, action: str) -> dict:
"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": output,
}


@dataclass
class SimpleActions:
stack_name: str
squad: str
environment: str
name: str

def output_execute(self):
try:
get_path = f"{self.stack_name}-{self.squad}-{self.environment}-{self.name}"
print(get_path)
response = requests.get(
f"{settings.REMOTE_STATE}/terraform_state/{get_path}"
)
json_data = response.json()
result = json_data.get("outputs")
if not result:
result = jmespath.search("modules[*].outputs", json_data)
return result
except Exception as err:
return {"command": "output", "rc": 1, "stdout": err}

def unlock_execute(self):
try:
get_path = f"{self.stack_name}-{self.squad}-{self.environment}-{self.name}"
response = requests.delete(
f"{settings.REMOTE_STATE}/terraform_lock/{get_path}", json={}
)
return response.json()
except Exception as err:
return {"command": "unlock", "rc": 1, "stdout": err}

def show_execute(self):
try:
get_path = f"{self.stack_name}-{self.squad}-{self.environment}-{self.name}"
print(get_path)
response = requests.get(
f"{settings.REMOTE_STATE}/terraform_state/{get_path}"
)
json_data = response.json()
return json_data
except Exception as err:
return {"command": "show", "rc": 1, "stdout": err}
}
43 changes: 1 addition & 42 deletions sld-api-backend/src/worker/tasks/terraform_worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ def pipeline_plan(
if not settings.DEBUG:
Utils.delete_local_folder(dir_path)


@celery_app.task(
bind=True, acks_late=True, time_limit=settings.GIT_TMOUT, name="pipeline git pull"
)
Expand Down Expand Up @@ -166,48 +167,6 @@ def pipeline_git_pull(
Utils.delete_local_folder(dir_path)



@celery_app.task(
bind=True,
acks_late=True,
time_limit=settings.WORKER_TMOUT,
max_retries=1,
name="terraform output",
)
def output(self, stack_name: str, squad: str, environment: str, name: str):
try:
output_result = ProviderActions.output(stack_name, squad, environment, name)
return output_result
except Exception as err:
return {"stdout": err}
finally:
dir_path = f"/tmp/{ stack_name }/{environment}/{squad}/{name}"
Utils.delete_local_folder(dir_path)


@celery_app.task(
bind=True,
acks_late=True,
time_limit=settings.WORKER_TMOUT,
max_retries=1,
name="terraform unlock",
)
def unlock(self, stack_name: str, squad: str, environment: str, name: str):
try:
unlock_result = ProviderActions.unlock(stack_name, squad, environment, name)
return unlock_result
except Exception as err:
return {"stdout": err}


@celery_app.task(
bind=True, acks_late=True, time_limit=settings.WORKER_TMOUT, name="terraform show"
)
def show(self, stack_name: str, squad: str, environment: str, name: str):
show_result = ProviderActions.show(stack_name, squad, environment, name)
return show_result


@celery_app.task(
bind=True, acks_late=True, time_limit=settings.WORKER_TMOUT, name="schedules list"
)
Expand Down
2 changes: 1 addition & 1 deletion sld-dashboard/app/home/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class StackForm(FlaskForm):
)
iac_type = SelectField(
"IaC Type",
choices=[('terraform', 'Terraform'), ('openTofu', 'openTofu')],
choices=[('', 'Select an IaC Type'), ('terraform', 'Terraform'), ('openTofu', 'openTofu')],
validators=[validators.DataRequired()],
coerce=lambda x: 'tofu' if x == 'openTofu' else x
)
Expand Down
Loading

0 comments on commit 4318e9d

Please sign in to comment.