import { Router } from "express"; import { z } from "zod"; import { requireAuth, requireRole } from "../auth/middleware.js"; import { asyncHandler } from "../utils/asyncHandler.js"; import { ACTIVE_STATUSES, cancelUpdateRun, listUpdateRuns, startUpdateRun, UpdateRunError } from "../services/updateRuns.js"; export const updateRunsRouter = Router(); // Anyone signed in can see how updates are going, like the Updates page itself. Starting or stopping one is for operators. updateRunsRouter.use(requireAuth); // Which hosts the playbook may touch: names, addresses and ansible patterns — and nothing that could be taken for an option. The same rule the // Semaphore page uses, except that it may not be left out: an update run is for one server. const startSchema = z.object({ serverId: z.number().int().positive(), integrationId: z.number().int().positive(), projectId: z.number().int().positive(), templateId: z.number().int().positive(), limit: z.string().trim().regex(/^[A-Za-z0-9][A-Za-z0-9._,:*\-]{0,199}$/, "Use the host name as it is in the Semaphore inventory, like web01"), windowMinutes: z.number().int().min(15).max(480).default(90), }); const shape = (r: Awaited>[number]) => ({ id: r.id, serverId: r.serverId, serverName: r.serverName, integrationId: r.integrationId, template: r.templateName, limit: r.hostLimit, taskId: r.taskId, taskStatus: r.taskStatus, status: r.status, active: ACTIVE_STATUSES.includes(r.status as (typeof ACTIVE_STATUSES)[number]), message: r.message, startedBy: r.startedBy, startedAt: r.startedAt, taskEndedAt: r.taskEndedAt, finishedAt: r.finishedAt, updatesBefore: r.updatesBefore, updatesAfter: r.updatesAfter, rebootRequired: r.rebootRequired, }); updateRunsRouter.get("/", asyncHandler(async (req, res) => { const serverId = Number(req.query.serverId); const runs = await listUpdateRuns({ serverId: Number.isInteger(serverId) && serverId > 0 ? serverId : undefined, limit: 30 }); res.json({ runs: runs.map(shape) }); })); updateRunsRouter.post("/", requireRole("operator"), asyncHandler(async (req, res) => { const parsed = startSchema.safeParse(req.body); if (!parsed.success) { return res.status(400).json({ error: "invalid_body", message: parsed.error.issues[0]?.message ?? "Choose a server, a template and the host name it runs on.", details: parsed.error.flatten() }); } const actor = req.currentUser!; try { const run = await startUpdateRun({ ...parsed.data, actor: actor.name ?? actor.email ?? actor.oidcSub }); const [shaped] = (await listUpdateRuns({ serverId: run.serverId, limit: 1 })).map(shape); res.status(201).json({ run: shaped }); } catch (err) { if (err instanceof UpdateRunError) return res.status(err.status).json({ error: err.code, message: err.message }); throw err; } })); updateRunsRouter.post("/:id/cancel", requireRole("operator"), asyncHandler(async (req, res) => { const id = Number(req.params.id); if (!Number.isInteger(id)) return res.status(400).json({ error: "invalid_id" }); const actor = req.currentUser!; try { const run = await cancelUpdateRun(id, actor.name ?? actor.email ?? actor.oidcSub); if (!run) return res.status(404).json({ error: "not_found" }); res.json({ status: run.status }); } catch (err) { if (err instanceof UpdateRunError) return res.status(err.status).json({ error: err.code, message: err.message }); throw err; } }));