Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/open tofu #208

Merged
merged 4 commits into from
Dec 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.28.0
run: docker build . --file Dockerfile --tag ${{ secrets.DOCKER_USERNAME }}/sld-api:3.0.0

- name: Docker Push with tag
#if: github.event.pull_request.merged == true
run: docker push ${{ secrets.DOCKER_USERNAME }}/sld-api:2.28.0
run: docker push ${{ secrets.DOCKER_USERNAME }}/sld-api:3.0.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 @@ -24,11 +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.29.0
run: docker build . --file Dockerfile --tag ${{ secrets.DOCKER_USERNAME }}/sld-dashboard:3.0.0

- name: Docker Push with tags
#if: github.event.pull_request.merged == true
run: docker push ${{ secrets.DOCKER_USERNAME }}/sld-dashboard:2.29.0
run: docker push ${{ secrets.DOCKER_USERNAME }}/sld-dashboard:3.0.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.28.0
image: d10s0vsky/sld-api:3.0.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.29.0
image: d10s0vsky/sld-dashboard:3.0.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.28.0
image: d10s0vsky/sld-api:3.0.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.28.0
image: d10s0vsky/sld-api:3.0.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.28.0
image: d10s0vsky/sld-api:3.0.0
imagePullPolicy: Always
env:
- name: TF_WARN_OUTPUT_ERRORS
Expand Down
2 changes: 1 addition & 1 deletion sld-api-backend/src/deploy/api/container/deploy/create.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
from src.worker.domain.entities.worker import DeployParams, DownloadGitRepoParams



async def deploy_infra_by_stack_name(
response: Response,
deploy: schemas_deploy.DeployCreate,
Expand Down Expand Up @@ -69,6 +68,7 @@ async def deploy_infra_by_stack_name(
squad=squad,
branch=branch,
version=tf_ver,
iac_type=stack_data.iac_type if stack_data.iac_type else "terraform",
variables=deploy.variables,
secreto=secreto,
variables_file=deploy.tfvar_file,
Expand Down
1 change: 1 addition & 0 deletions sld-api-backend/src/deploy/api/container/deploy/delete.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ async def delete_infra_by_id(
environment=environment,
squad=squad,
branch=branch,
iac_type=stack_data.iac_type if stack_data.iac_type else "terraform",
version=tf_ver,
variables=variables,
secreto=secreto,
Expand Down
1 change: 1 addition & 0 deletions sld-api-backend/src/deploy/api/container/deploy/destroy.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ async def destroy_infra(
squad=squad,
branch=branch,
version=tf_ver,
iac_type=stack_data.iac_type if stack_data.iac_type else "terraform",
variables=variables,
secreto=secreto,
tfvar_file=tfvar_file,
Expand Down
1 change: 1 addition & 0 deletions sld-api-backend/src/deploy/api/container/deploy/update.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ async def deploy_by_id(
environment=environment,
squad=squad,
branch=branch,
iac_type=stack_data.iac_type if stack_data.iac_type else "terraform",
version=tf_ver,
variables=deploy_update.variables,
secreto=secreto,
Expand Down
1 change: 1 addition & 0 deletions sld-api-backend/src/deploy/api/container/plan/create.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ async def plan_infra_by_stack_name(
environment=deploy.environment,
squad=squad,
branch=branch,
iac_type=stack_data.iac_type if stack_data.iac_type else "terraform",
version=tf_ver,
variables=deploy.variables,
secreto=secreto,
Expand Down
1 change: 1 addition & 0 deletions sld-api-backend/src/deploy/api/container/plan/update.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ async def update_plan_by_id(
environment=environment,
squad=squad,
branch=branch,
iac_type=stack_data.iac_type if stack_data.iac_type else "terraform",
version=tf_ver,
variables=deploy_update.variables,
secreto=secreto,
Expand Down
2 changes: 1 addition & 1 deletion sld-api-backend/src/deploy/domain/entities/deploy.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Optional
from typing import Optional, Literal, Dict

from pydantic import BaseModel, Field, constr

Expand Down
4 changes: 2 additions & 2 deletions sld-api-backend/src/shared/helpers/push_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,8 @@
)



def async_deploy(deploy_params: DeployParams):
queue = "any" if not settings.TASK_ROUTE else deploy_params.squad
queue = "any" if not settings.TASK_ROUTE else deploy_params.squad
pipeline_deploy_result = pipeline_deploy.s(deploy_params.model_dump()).apply_async(
queue=queue,
retry=True,
Expand All @@ -46,6 +45,7 @@ def async_destroy(destroy_params: DeployParams):
)
return pipeline_destroy_result.task_id


def async_plan(plan_params: DeployParams):
queue = "any" if not settings.TASK_ROUTE else plan_params.squad
pipeline_deploy_result = pipeline_plan.s(plan_params.model_dump()).apply_async(
Expand Down
1 change: 1 addition & 0 deletions sld-api-backend/src/stacks/api/container/create.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ def create_new_stack(
db=db,
stack=stack,
user_id=current_user.id,
username=current_user.username,
task_id=task[0],
var_json=task[1],
var_list=variables_list,
Expand Down
5 changes: 3 additions & 2 deletions sld-api-backend/src/stacks/api/container/get.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ async def get_stack_by_id_or_name(
raise HTTPException(status_code=404, detail="stack id not found")
if (
not check_squad_user(current_user.squad, result.squad_access)
and not "*" in result.squad_access
and "*" not in result.squad_access
):
raise HTTPException(
status_code=403,
Expand All @@ -50,10 +50,11 @@ async def get_stack_by_id_or_name(
if not crud_users.is_master(db, current_user):
if (
not check_squad_user(current_user.squad, result.squad_access)
and not "*" in result.squad_access
and "*" not in result.squad_access
):
raise HTTPException(
status_code=403,
detail=f"Not enough permissions in {result.squad_access}",
)

return result
1 change: 1 addition & 0 deletions sld-api-backend/src/stacks/api/container/update.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ def update_stack(
stack_id=stack_id,
stack=stack,
user_id=current_user.id,
username=current_user.username,
task_id=task[0],
var_json=task[1],
var_list=variables_list,
Expand Down
9 changes: 5 additions & 4 deletions sld-api-backend/src/stacks/api/v1/stacks.py
Original file line number Diff line number Diff line change
@@ -1,33 +1,34 @@
from fastapi import APIRouter, Depends
from typing import List

from src.stacks.api.container import create, delete, get, update
from src.stacks.domain.entities import stacks as schemas_stacks

router = APIRouter()


@router.post("/", response_model=schemas_stacks.Stack)
@router.post("/", response_model=schemas_stacks.StackResponse)
def create_new_stack(
create_stack: schemas_stacks.StackCreate = Depends(create.create_new_stack),
):
return create_stack


@router.patch("/{stack_id}", response_model=schemas_stacks.Stack)
@router.patch("/{stack_id}", response_model=schemas_stacks.StackResponse)
def update_stack(
update_stack: schemas_stacks.StackCreate = Depends(update.update_stack),
):
return update_stack


@router.get("/")
@router.get("/", response_model=List[schemas_stacks.StackResponse])
async def get_all_stacks(
get_all_stacks: schemas_stacks.Stack = Depends(get.get_all_stacks),
):
return get_all_stacks


@router.get("/{stack}")
@router.get("/{stack}", response_model=schemas_stacks.StackResponse)
async def get_stack_by_id_or_name(
get_stack: schemas_stacks.Stack = Depends(get.get_stack_by_id_or_name),
):
Expand Down
26 changes: 20 additions & 6 deletions sld-api-backend/src/stacks/domain/entities/stacks.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,42 @@
from typing import List, Optional
from typing import List, Optional, Literal, Dict

from pydantic import BaseModel, Field, constr
from datetime import datetime


class StackBase(BaseModel):
stack_name: constr(strip_whitespace=True)
git_repo: constr(strip_whitespace=True)
branch: constr(strip_whitespace=True) = "master"
branch: constr(strip_whitespace=True) = "main"
squad_access: List[str] = ["*"]
tf_version: constr(strip_whitespace=True) = "1.3.2"
iac_type: Optional[Literal["terraform", "tofu"]] = "terraform"
tf_version: constr(strip_whitespace=True) = "1.6.5"
tags: Optional[List[str]] = []
project_path: Optional[constr(strip_whitespace=True)] = Field("", example="")
description: constr(strip_whitespace=True)

class Config:
"""Extra configuration options"""

freeze = True
str_strip_whitespace = True # remove trailing whitespace


class StackCreate(StackBase):
pass

class Config:
"""Extra configuration options"""
freeze = True
str_strip_whitespace = True # remove trailing whitespace


class StackResponse(StackBase):
id: int
task_id: constr(strip_whitespace=True)
username: Optional[constr(strip_whitespace=True)]
created_at: Optional[datetime]
updated_at: Optional[datetime]

class Config:
freeze = True
str_strip_whitespace = True # remove trailing whitespace


Expand All @@ -33,4 +46,5 @@ class Stack(StackBase):
user_id: int

class Config:
freeze = True
from_attributes = True
4 changes: 4 additions & 0 deletions sld-api-backend/src/stacks/infrastructure/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,14 @@ class Stack(Base):
task_id = Column(String(200))
var_json = Column(JSON)
var_list = Column(JSON)
tags = Column(JSON)
squad_access = Column(JSON)
iac_type = Column(String(30))
tf_version = Column(String(30))
project_path = Column(String(50))
created_at = Column(DateTime, default=datetime.datetime.now())
updated_at = Column(DateTime)
description = Column(Text())
user_id = Column(Integer)
username = Column(String(50))
deploy = relationship("Deploy")
15 changes: 13 additions & 2 deletions sld-api-backend/src/stacks/infrastructure/repositories.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
def create_new_stack(
db: Session,
stack: schemas.StackCreate,
user_id: int,
user_id: str,
username: str,
task_id: str,
var_json: str,
var_list: str,
Expand All @@ -20,14 +21,17 @@ def create_new_stack(
db_stack = models.Stack(
stack_name=stack.stack_name,
git_repo=stack.git_repo,
iac_type=stack.iac_type,
tf_version=stack.tf_version,
project_path=stack.project_path,
description=stack.description,
branch=stack.branch,
user_id=user_id,
username=username,
created_at=datetime.datetime.now(),
var_json=var_json,
var_list=var_list,
tags=stack.tags,
squad_access=squad_access,
task_id=task_id,
)
Expand All @@ -47,6 +51,7 @@ def update_stack(
stack: schemas.StackCreate,
stack_id: int,
user_id: int,
username: str,
task_id: str,
var_json: str,
var_list: str,
Expand All @@ -55,9 +60,11 @@ def update_stack(
db_stack = db.query(models.Stack).filter(models.Stack.id == stack_id).first()

db_stack.user_id = user_id
db_stack.username = username
db_stack.task_id = task_id
db_stack.var_json = var_json
db_stack.var_list = var_list
db_stack.tags = stack.tags
db_stack.updated_at = datetime.datetime.now()
check_None = ["string", ""]
if db_stack.stack_name not in check_None:
Expand All @@ -66,6 +73,8 @@ def update_stack(
db_stack.git_repo = stack.git_repo
if db_stack.branch not in check_None:
db_stack.branch = stack.branch
if db_stack.iac_type not in check_None:
db_stack.iac_type = stack.iac_type
if db_stack.tf_version not in check_None:
db_stack.tf_version = stack.tf_version
if db_stack.project_path not in check_None:
Expand All @@ -90,6 +99,7 @@ def get_all_stacks_by_squad(
filter_all = (
db.query(models.Stack)
.filter(models.Stack.squad_access.contains("*"))
.order_by(models.Stack.created_at.desc())
.offset(skip)
.limit(limit)
.all()
Expand All @@ -102,6 +112,7 @@ def get_all_stacks_by_squad(
result.extend(
db.query(models.Stack)
.filter(func.json_contains(models.Stack.squad_access, a) == 1)
.order_by(models.Stack.created_at.desc())
.all()
)
merge_query = result + filter_all
Expand All @@ -112,7 +123,7 @@ def get_all_stacks_by_squad(

def get_all_stacks(db: Session, squad_access: str, skip: int = 0, limit: int = 100):
try:
return db.query(models.Stack).offset(skip).limit(limit).all()
return db.query(models.Stack).order_by(models.Stack.created_at.desc()).offset(skip).limit(limit).all()
except Exception as err:
raise err

Expand Down
Loading
Loading