fix(CI3): bruk regional parent i ListBuildsRequest for us-central1

This commit is contained in:
chrischristiansen-glitch 2026-06-10 04:14:48 +02:00
parent e4c79c999d
commit 3c69490a68

View File

@ -6,18 +6,17 @@ import logging
from contextlib import asynccontextmanager from contextlib import asynccontextmanager
from fastapi import FastAPI, HTTPException, Depends, Header from fastapi import FastAPI, HTTPException, Depends, Header
from fastapi.responses import JSONResponse
from pydantic import BaseModel from pydantic import BaseModel
logging.basicConfig(level=logging.INFO) logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
PROJECT_ID = os.environ.get("GOOGLE_CLOUD_PROJECT", "propane-will-491900-m5") PROJECT_ID = os.environ.get("GOOGLE_CLOUD_PROJECT", "propane-will-491900-m5")
REGION = os.environ.get("REGION", "us-central1") REGION = os.environ.get("REGION", "us-central1")
STATIC_BUCKET = os.environ.get("STATIC_BUCKET", "opax-vauco-static") STATIC_BUCKET = os.environ.get("STATIC_BUCKET", "opax-vauco-static")
CLOUD_RUN_SERVICE = os.environ.get("CLOUD_RUN_SERVICE", "osvauco-agent") CLOUD_RUN_SERVICE = os.environ.get("CLOUD_RUN_SERVICE", "osvauco-agent")
MCP_SECRET = os.environ.get("MCP_SECRET", "") MCP_SECRET = os.environ.get("MCP_SECRET", "")
BUILD_TRIGGER_ID = os.environ.get("CLOUD_BUILD_TRIGGER_ID", "38423976-91ff-4ff4-859e-1f262344c609") BUILD_TRIGGER_ID = os.environ.get("CLOUD_BUILD_TRIGGER_ID", "38423976-91ff-4ff4-859e-1f262344c609")
def verify_token(x_mcp_key: str = Header(default="")): def verify_token(x_mcp_key: str = Header(default="")):
@ -44,7 +43,7 @@ class ToolCallRequest(BaseModel):
@asynccontextmanager @asynccontextmanager
async def lifespan(app: FastAPI): async def lifespan(app: FastAPI):
logger.info(f"OPAX-MCP starting | project={PROJECT_ID} | trigger={BUILD_TRIGGER_ID}") logger.info(f"OPAX-MCP starting | project={PROJECT_ID} | region={REGION} | trigger={BUILD_TRIGGER_ID}")
yield yield
app = FastAPI(title="OPAX-MCP", version="1.0.0", lifespan=lifespan) app = FastAPI(title="OPAX-MCP", version="1.0.0", lifespan=lifespan)
@ -52,19 +51,17 @@ app = FastAPI(title="OPAX-MCP", version="1.0.0", lifespan=lifespan)
@app.get("/health") @app.get("/health")
async def health(): async def health():
return {"status": "ok", "service": "opax-mcp", "project": PROJECT_ID} return {"status": "ok", "service": "opax-mcp", "project": PROJECT_ID, "region": REGION}
@app.get("/mcp/tools") @app.get("/mcp/tools")
async def list_tools(_: bool = Depends(verify_token)): async def list_tools(_: bool = Depends(verify_token)):
return { return {"tools": [
"tools": [ {"name": "push_static", "description": "Last opp statisk fil til GCS (opax.vauco.no)"},
{"name": "push_static", "description": "Last opp statisk fil til GCS (opax.vauco.no)", "required": ["file_path", "content"]}, {"name": "get_build_status", "description": "Hent status på siste Cloud Build for osvauco-agent-main-trigger"},
{"name": "get_build_status", "description": "Hent status på siste Cloud Build for osvauco-agent-main-trigger", "required": []}, {"name": "deploy_service", "description": "Trigger ny Cloud Build deploy (HITL)"},
{"name": "deploy_service", "description": "Trigger ny Cloud Build deploy (HITL)", "required": []}, {"name": "get_logs", "description": "Hent Cloud Run-logger for osvauco-agent"},
{"name": "get_logs", "description": "Hent Cloud Run-logger for osvauco-agent", "required": []}, ]}
]
}
@app.post("/tools/push_static") @app.post("/tools/push_static")
@ -72,8 +69,7 @@ async def push_static(req: PushStaticRequest, _: bool = Depends(verify_token)):
try: try:
from google.cloud import storage from google.cloud import storage
client = storage.Client(project=PROJECT_ID) client = storage.Client(project=PROJECT_ID)
bucket = client.bucket(STATIC_BUCKET) blob = client.bucket(STATIC_BUCKET).blob(req.file_path)
blob = bucket.blob(req.file_path)
blob.cache_control = req.cache_control blob.cache_control = req.cache_control
blob.upload_from_string(req.content.encode("utf-8"), content_type=req.content_type) blob.upload_from_string(req.content.encode("utf-8"), content_type=req.content_type)
logger.info(f"[push_static] {req.file_path} ({len(req.content)} bytes)") logger.info(f"[push_static] {req.file_path} ({len(req.content)} bytes)")
@ -89,19 +85,21 @@ async def push_static(req: PushStaticRequest, _: bool = Depends(verify_token)):
async def get_build_status(_: bool = Depends(verify_token)): async def get_build_status(_: bool = Depends(verify_token)):
""" """
Henter siste build fra osvauco-agent-main-trigger (us-central1). Henter siste build fra osvauco-agent-main-trigger (us-central1).
Filtrerer trigger_id for å unngå global/andre builds. Bruker regional parent-format: projects/{project}/locations/{region}
""" """
try: try:
from google.cloud.devtools import cloudbuild_v1 from google.cloud.devtools import cloudbuild_v1
client = cloudbuild_v1.CloudBuildClient() client = cloudbuild_v1.CloudBuildClient()
# Regional builds krever parent i stedet for project_id
parent = f"projects/{PROJECT_ID}/locations/{REGION}"
request = cloudbuild_v1.ListBuildsRequest( request = cloudbuild_v1.ListBuildsRequest(
project_id=PROJECT_ID, parent=parent,
filter=f'trigger_id="{BUILD_TRIGGER_ID}"', filter=f'trigger_id="{BUILD_TRIGGER_ID}"',
page_size=1, page_size=1,
) )
builds = list(client.list_builds(request=request)) builds = list(client.list_builds(request=request))
if not builds: if not builds:
return {"status": "unknown", "message": f"Ingen builds for trigger {BUILD_TRIGGER_ID}"} return {"status": "unknown", "message": f"Ingen builds for trigger {BUILD_TRIGGER_ID} i {REGION}"}
b = builds[0] b = builds[0]
status_map = {1:"queued",2:"working",3:"success",4:"failure", status_map = {1:"queued",2:"working",3:"success",4:"failure",
5:"internal_error",6:"timeout",7:"cancelled"} 5:"internal_error",6:"timeout",7:"cancelled"}
@ -119,6 +117,7 @@ async def get_build_status(_: bool = Depends(verify_token)):
"commit": (b.substitutions or {}).get("SHORT_SHA", ""), "commit": (b.substitutions or {}).get("SHORT_SHA", ""),
"duration_s": duration_s, "duration_s": duration_s,
"log_url": b.log_url or "", "log_url": b.log_url or "",
"region": REGION,
"trigger_id": BUILD_TRIGGER_ID, "trigger_id": BUILD_TRIGGER_ID,
} }
except Exception as e: except Exception as e: