Skip to content

Commit

Permalink
Merge pull request #67 from D10S0VSkY-OSS/feature/custom-tfvars
Browse files Browse the repository at this point in the history
feat: Add tfvars files parameter for deploy stack
  • Loading branch information
D10S0VSkY-OSS authored Jun 29, 2022
2 parents 5b52e2e + 0a37a85 commit 64e1a8a
Show file tree
Hide file tree
Showing 16 changed files with 233 additions and 82 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 @@ -26,10 +26,10 @@ 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.2.0
run: docker build . --file Dockerfile --tag ${{ secrets.DOCKER_USERNAME }}/sld-api:2.3.0

- name: Docker Push with tag
run: docker push ${{ secrets.DOCKER_USERNAME }}/sld-api:2.2.0
run: docker push ${{ secrets.DOCKER_USERNAME }}/sld-api:2.3.0

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

- name: Docker Push with tags
run: docker push ${{ secrets.DOCKER_USERNAME }}/sld-dashboard:2.2.0
run: docker push ${{ secrets.DOCKER_USERNAME }}/sld-dashboard:2.3.0

- name: Build the Docker image
working-directory: ./sld-dashboard
Expand Down
16 changes: 12 additions & 4 deletions sld-api-backend/api_v1/endpoints/deploy.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ async def deploy_infra_by_stack_name(
branch,
tf_ver,
deploy.variables,
secreto)
secreto,
deploy.tfvar_file)
# Push deploy task data
db_deploy = crud_deploys.create_new_deploy(
db=db,
Expand Down Expand Up @@ -149,14 +150,16 @@ async def update_deploy_by_id(
branch,
tf_ver,
deploy_update.variables,
secreto)
secreto,
deploy_update.tfvar_file)
# Push deploy task data
crud_deploys.update_deploy(
db=db,
deploy_id=deploy_id,
task_id=pipeline_deploy,
action="Update",
user_id=current_user.id,
tfvar_file=deploy_update.tfvar_file,
variables=deploy_update.variables,
start_time=deploy_update.start_time,
destroy_time=deploy_update.destroy_time,
Expand Down Expand Up @@ -202,6 +205,7 @@ async def destroy_infra(
start_time = deploy_data.start_time
destroy_time = deploy_data.destroy_time
variables = deploy_data.variables
tfvar_file = deploy_data.tfvar_file
name = deploy_data.name
# Get credentials by providers supported
secreto = tokens.check_prefix(
Expand All @@ -227,7 +231,8 @@ async def destroy_infra(
branch,
tf_ver,
variables,
secreto
secreto,
tfvar_file
)
# Push deploy task data
crud_deploys.update_deploy(
Expand All @@ -238,6 +243,7 @@ async def destroy_infra(
user_id=current_user.id,
start_time=start_time,
destroy_time=destroy_time,
tfvar_file=tfvar_file,
variables=variables,
username=current_user.username)
# Push task data
Expand Down Expand Up @@ -312,6 +318,7 @@ async def delete_infra_by_id(
stack_name = deploy_data.stack_name
environment = deploy_data.environment
name = deploy_data.name
tfvar_file = deploy_data.tfvar_file
variables = deploy_data.variables
# Get credentials by providers supported
secreto = tokens.check_prefix(
Expand Down Expand Up @@ -341,7 +348,8 @@ async def delete_infra_by_id(
branch,
tf_ver,
variables,
secreto
secreto,
tfvar_file
)
# Push task data
db_task = crud_tasks.create_task(
Expand Down
11 changes: 8 additions & 3 deletions sld-api-backend/api_v1/endpoints/plan.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ async def plan_infra_by_stack_name(
branch,
tf_ver,
deploy.variables,
secreto)
secreto,
deploy.tfvar_file)
# Push deploy task data
db_deploy = crud_deploys.create_new_deploy(
db=db,
Expand Down Expand Up @@ -132,7 +133,9 @@ async def update_plan_by_id(
branch,
tf_ver,
deploy_update.variables,
secreto)
secreto,
deploy_update.tfvar_file
)
# Push deploy task data
if "Plan" in deploy_data.action:
crud_deploys.update_deploy(
Expand All @@ -141,6 +144,7 @@ async def update_plan_by_id(
task_id=pipeline_plan,
action="Plan",
user_id=current_user.id,
tfvar_file=deploy_update.tfvar_file,
variables=deploy_update.variables,
start_time=deploy_update.start_time,
destroy_time=deploy_update.destroy_time,
Expand Down Expand Up @@ -202,7 +206,8 @@ async def get_plan_by_id_deploy(
branch,
tf_ver,
deploy_data.variables,
secreto)
secreto,
deploy_data.tfvar_file)
return {"task": pipeline_plan}
except Exception as err:
raise HTTPException(
Expand Down
43 changes: 37 additions & 6 deletions sld-api-backend/core/providers/terraform.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import subprocess
import jmespath
from os import path
from os.path import isfile, join
from os import listdir

import hcl
import shutil
Expand Down Expand Up @@ -72,16 +74,18 @@ def git_clone(
module='git',
module_args=f'repo={git_repo} dest={stack_name}/{environment}/{squad}/{name} version={branch} force=yes',
)
path_tfvars = f'/tmp/{stack_name}/{environment}/{squad}/{name}'
tfvars_files = [f for f in listdir(path_tfvars) if f.endswith('.tfvars') and isfile(join(path_tfvars, f))]
logs = [i for i in runner_response.events]
git_stdout = jmespath.search(
'[*].event_data.res.stdout_lines', logs)
git_stderr = jmespath.search('[*].event_data.res.msg', logs)
rc = runner_response.rc
if rc != 0:
return {"command": "git", "rc": rc, "stdout": git_stderr}
return {"command": "git", "rc": rc, "stdout": git_stdout}
return {"command": "git", "rc": rc, "tfvars": tfvars_files, "stdout": git_stderr}
return {"command": "git", "rc": rc, "tfvars": tfvars_files, "stdout": git_stdout}
except Exception as err:
return {"command": "git", "rc": 1, "stdout": err}
return {"command": "git", "rc": 1, "tfvars": tfvars_files, "stdout": err}

@staticmethod
def tfstate_render(
Expand Down Expand Up @@ -155,18 +159,21 @@ def plan_execute(
squad: str,
name: str,
version: str,
variables_file: str = "",
**secreto: dict) -> dict:
try:
secret(stack_name, environment, squad, name, secreto)
deploy_state=f'{environment}_{stack_name}_{squad}_{name}'
# Execute task
variables_files = f'{stack_name}.tfvars.json' if variables_file == "" or variables_file == None else variables_file
runner_response = ansible_runner.run(
private_data_dir=f'/tmp/{stack_name}/{environment}/{squad}/{name}',
host_pattern='localhost',
module='terraform',
module_args=f'binary_path=/tmp/{version}/terraform '
f'force_init=True project_path=/tmp/{stack_name}/{environment}/{squad}/{name} '
f'plan_file=/tmp/{stack_name}/{environment}/{squad}/{name}/{stack_name}.tfplan '
f'variables_files={stack_name}.tfvars.json state=planned',
f'variables_files={variables_files} state=planned',
)
unsecret(stack_name, environment, squad, name, secreto)

Expand All @@ -184,6 +191,8 @@ def plan_execute(
"squad": squad,
"environment": environment,
"rc": rc,
"tfvars_files": variables_file,
"remote_state": f'http://remote-state:8080/terraform_state/{deploy_state}',
"stdout": plan_stderr
}
return {
Expand All @@ -192,6 +201,8 @@ def plan_execute(
"squad": squad,
"environment": environment,
"rc": rc,
"tfvars_files": variables_file,
"remote_state": f'http://remote-state:8080/terraform_state/{deploy_state}',
"stdout": plan_stdout
}
except Exception as err:
Expand All @@ -201,6 +212,8 @@ def plan_execute(
"squad": squad,
"environment": environment,
"rc": 1,
"tfvars_files": variables_file,
"remote_state": f'http://remote-state:8080/terraform_state/{deploy_state}',
"stdout": f'{err}'
}

Expand All @@ -211,17 +224,20 @@ def apply_execute(
squad: str,
name: str,
version: str,
variables_file: str = "",
**secreto: dict) -> dict:
try:
secret(stack_name, environment, squad, name, secreto)
deploy_state=f'{environment}_{stack_name}_{squad}_{name}'
# Execute task
variables_files = f'{stack_name}.tfvars.json' if variables_file == "" or variables_file == None else variables_file
runner_response = ansible_runner.run(
private_data_dir=f'/tmp/{stack_name}/{environment}/{squad}/{name}',
host_pattern='localhost',
module='terraform',
module_args=f'binary_path=/tmp/{version}/terraform lock=True force_init=True project_path=/tmp/{stack_name}/{environment}/{squad}/{name} '
f'plan_file=/tmp/{stack_name}/{environment}/{squad}/{name}/{stack_name}.tfplan state=present '
f'variables_files={stack_name}.tfvars.json',
f'variables_files={variables_files}',
)
unsecret(stack_name, environment, squad, name, secreto)
# Capture events
Expand All @@ -239,6 +255,8 @@ def apply_execute(
"squad": squad,
"environment": environment,
"rc": rc,
"tfvars_files": variables_file,
"remote_state": f'http://remote-state:8080/terraform_state/{deploy_state}',
"stdout": apply_stderr
}
return {
Expand All @@ -247,6 +265,8 @@ def apply_execute(
"squad": squad,
"environment": environment,
"rc": rc,
"tfvars_files": variables_file,
"remote_state": f'http://remote-state:8080/terraform_state/{deploy_state}',
"stdout": apply_stdout
}
except Exception as err:
Expand All @@ -256,6 +276,8 @@ def apply_execute(
"squad": squad,
"environment": environment,
"rc": 1,
"tfvars_files": variables_file,
"remote_state": f'http://remote-state:8080/terraform_state/{deploy_state}',
"stdout": f'{err}'
}

Expand All @@ -266,16 +288,19 @@ def destroy_execute(
squad: str,
name: str,
version: str,
variables_file: str = "",
**secreto: dict) -> dict:
try:
secret(stack_name, environment, squad, name, secreto)
deploy_state=f'{environment}_{stack_name}_{squad}_{name}'
# Execute task
variables_files = f'{stack_name}.tfvars.json' if variables_file == "" or variables_file == None else variables_file
runner_response = ansible_runner.run(
private_data_dir=f'/tmp/{stack_name}/{environment}/{squad}/{name}',
host_pattern='localhost',
module='terraform',
module_args=f'binary_path=/tmp/{version}/terraform force_init=True project_path=/tmp/{stack_name}/{environment}/{squad}/{name} '
f'variables_files={stack_name}.tfvars.json state=absent',
f'variables_files={variables_files} state=absent',
)
unsecret(stack_name, environment, squad, name, secreto)
# Capture events
Expand All @@ -292,6 +317,8 @@ def destroy_execute(
"squad": squad,
"environment": environment,
"rc": rc,
"tfvars_files": variables_file,
"remote_state": f'http://remote-state:8080/terraform_state/{deploy_state}',
"stdout": destroy_stderr
}
return {
Expand All @@ -300,6 +327,8 @@ def destroy_execute(
"squad": squad,
"environment": environment,
"rc": rc,
"tfvars_files": variables_file,
"remote_state": f'http://remote-state:8080/terraform_state/{deploy_state}',
"stdout": destroy_stdout
}
except Exception as err:
Expand All @@ -309,6 +338,8 @@ def destroy_execute(
"squad": squad,
"environment": environment,
"rc": 1,
"tfvars_files": variables_file,
"remote_state": f'http://remote-state:8080/terraform_state/{deploy_state}',
"stdout": f'{err}'
}

Expand Down
3 changes: 3 additions & 0 deletions sld-api-backend/crud/deploys.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ def create_new_deploy(
name=deploy.name,
stack_name=deploy.stack_name,
environment=deploy.environment,
tfvar_file=deploy.tfvar_file,
variables=deploy.variables,
action=action,
username=username,
Expand Down Expand Up @@ -48,6 +49,7 @@ def update_deploy(
task_id: str,
start_time: str,
destroy_time: str,
tfvar_file: str,
variables: dict):
db_deploy = db.query(models.Deploy).filter(
models.Deploy.id == deploy_id).first()
Expand All @@ -56,6 +58,7 @@ def update_deploy(
db_deploy.task_id = task_id
db_deploy.username = username
db_deploy.user_id = user_id
db_deploy.tfvar_file = tfvar_file
db_deploy.variables = variables
db_deploy.updated_at = datetime.datetime.now()
check_None = ["string"]
Expand Down
1 change: 1 addition & 0 deletions sld-api-backend/db/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ class Deploy(Base):
squad = Column(String(50), nullable=False)
variables = Column(JSON)
environment = Column(String(50))
tfvar_file = Column(String(50))
__table_args__ = (UniqueConstraint(
'squad', 'environment', 'name', 'stack_name'),)

Expand Down
Loading

0 comments on commit 64e1a8a

Please sign in to comment.