Add DevOps reconciliation models and integrate into deployment service

Signed-off-by: zhenyus <zhenyus@mathmast.com>
This commit is contained in:
zhenyus 2025-06-23 16:46:06 +08:00
parent d660002076
commit 979fcd298c
3 changed files with 41 additions and 14 deletions

View File

@ -1,5 +1,6 @@
from app.common.models.code_depot.code_depot import CodeDepotDoc from app.common.models.code_depot.code_depot import CodeDepotDoc
from app.common.models.deployment.deployment import Deployment from app.common.models.deployment.deployment import Deployment
from app.common.models.deployment.deployment import DevOpsReconcileRequest, DevOpsReconcileOperationType
# list of beanie document models, # list of beanie document models,
# must add here so that the mongo db collection can be automatically created # must add here so that the mongo db collection can be automatically created

View File

@ -1,5 +1,7 @@
from datetime import datetime, timedelta from datetime import datetime, timedelta
from typing import Literal, List from typing import Literal, List, Optional
from dataclasses import dataclass
from enum import Enum
from beanie import Document from beanie import Document
from bson import ObjectId from bson import ObjectId
@ -69,8 +71,19 @@ class CheckApplicationLogsResponse(BaseModel):
limit: int limit: int
logs: list[str] logs: list[str]
class DevOpsReconcileOperationType(Enum):
START = "start"
TERMINATE = "terminate"
RESTART = "restart"
@dataclass
class DevOpsReconcileRequest(BaseModel):
operation: DevOpsReconcileOperationType
id: str
devops_proj_id: str
triggered_user_id: str
causes: str
commit_sha256: Optional[str] = None
target_env: Literal["alpha", "prod"]
ttl_controled: bool = False
ttl: int = 10800

View File

@ -8,7 +8,7 @@ import requests
from fastapi import HTTPException, Depends from fastapi import HTTPException, Depends
from app.common.config.site_settings import site_settings from app.common.config.site_settings import site_settings
from app.common.models import Deployment from app.common.models import Deployment, DevOpsReconcileRequest, DevOpsReconcileOperationType
from app.common.models.code_depot.code_depot import CodeDepotDoc, DepotStatus from app.common.models.code_depot.code_depot import CodeDepotDoc, DepotStatus
from app.common.models.deployment.deployment import InitDeploymentRequest, CheckApplicationLogsRequest, \ from app.common.models.deployment.deployment import InitDeploymentRequest, CheckApplicationLogsRequest, \
CheckApplicationLogsResponse CheckApplicationLogsResponse
@ -148,15 +148,28 @@ class DeploymentService:
) -> bool: ) -> bool:
""" """
Start the deployment Start the deployment
Return true atm, modify calling reconsile service later Return true atm, modify calling reconcile service later
""" """
# async with httpx.AsyncClient() as client: # construct request body
# response = await client.post( request = DevOpsReconcileRequest(
# f"{reconsile_base_url}/api/devops/reconcile", operation=DevOpsReconcileOperationType.START,
# json=deployment.model_dump() id=deployment.deployment_id,
# ) devops_proj_id=deployment.deployment_project_id,
# if response.status_code != 200: triggered_user_id=deployment.deployed_by,
# raise HTTPException(status_code=response.status_code, detail=response.text) causes=deployment.deployment_reason,
target_env=deployment.deployment_target_env,
ttl_controled=True,
ttl=deployment.deployment_ttl_hours,
commit_sha256=deployment.deployment_git_sha256,
)
# send request to reoncile service
async with httpx.AsyncClient() as client:
response = await client.post(
f"{reconsile_base_url}/api/devops/reconcile",
json=request.model_dump()
)
if response.status_code != 200:
raise HTTPException(status_code=response.status_code, detail=response.text)
return True return True
async def check_application_logs( async def check_application_logs(