mirror of
https://github.com/Studio-Saelix/sencho.git
synced 2026-08-13 12:17:34 +00:00
d26ab58189
* feat(fleet): cross-node bulk label assign with authoritative label discovery Make Fleet Actions > Bulk label assign work across the fleet. Pick a stack label that exists anywhere in the fleet, select stacks on one or more nodes, and the control orchestrates: each target node resolves the label by name, creating it with the same name and color if missing, then adds it to the selected stacks while preserving their existing labels. The local node runs in process; each remote runs its own admin-only local-assign receiver over the node proxy. Per-node failures (unknown node, no proxy target, unreachable, mixed-version remote) degrade that node only and are reported per node in the result. Assignment writes use a transactional INSERT OR IGNORE so the add-preserve path is idempotent and race-free. Also make the shared fleet label discovery authoritative: suggestions, match-preview, and the fleet-stop remote leg now read each node's labels live over the proxy instead of the control database, which does not mirror remote labels. A propagated label therefore appears in, and is stoppable by, Stop-by-label across the fleet, and unreachable nodes are surfaced rather than silently dropped. Fleet Actions runs against the unfiltered node list, so overview filters no longer narrow its scope. The previous node-scoped, replace-by-id bulk-assign endpoint is removed. * fix(fleet): treat malformed remote label responses as per-node failures A 200 response from a remote node whose body is not the expected shape was treated as a benign empty result, so a malformed remote could read as a clean zero-stack assign or a "matched, nothing to stop" no-op and even surface a success toast. Validate the wire shape in the bulk-assign and fleet-stop remote legs and in the authoritative label discovery fan-out; on a malformed body, report the node as a per-node failure with the error attributed to its stacks instead of silently dropping it. * chore: drop accidentally committed temp file
88 lines
4.6 KiB
TypeScript
88 lines
4.6 KiB
TypeScript
import { Router, type Request, type Response } from 'express';
|
|
import { authMiddleware } from '../middleware/auth';
|
|
import { requireAdmin, requireBody } from '../middleware/tierGates';
|
|
import { getErrorMessage } from '../utils/errors';
|
|
import { isDebugEnabled } from '../utils/debug';
|
|
import { MAX_ASSIGNMENTS } from '../helpers/constants';
|
|
import { runLocalLabelStop, type LabelLocalStopResponse } from '../helpers/fleetLabelStop';
|
|
import { runLocalLabelAssign, validateLabelTemplate, type LabelLocalAssignResponse } from '../helpers/fleetLabelAssign';
|
|
|
|
// Per-node fleet-action endpoints. Mounted under `/api/fleet-actions/`, which
|
|
// is NOT in `PROXY_EXEMPT_PREFIXES`, so when `x-node-id` targets a remote node
|
|
// the gateway proxies the call and the remote Sencho instance runs its own
|
|
// local handler. Multi-node orchestration endpoints live in `routes/fleet.ts`
|
|
// because their path must sit behind the `/api/fleet/` proxy-exempt prefix.
|
|
export const fleetActionsRouter = Router();
|
|
|
|
// Per-node label-matched stop. A control instance calls this on each remote
|
|
// node during a fleet-wide stop-by-label so the destructive work runs under the
|
|
// remote's own admin auth and per-node bulk lock. Admin-only and available on
|
|
// every license, matching the rest of the Fleet Actions surface. The paid
|
|
// label-driven action lives at `POST /api/labels/:id/action`; this receiver is
|
|
// the fleet-plumbing equivalent the control fans out to, so a fleet-stop on a
|
|
// Community fleet stops remote stacks instead of 403'ing on the remote leg.
|
|
fleetActionsRouter.post(
|
|
'/labels/local-stop',
|
|
authMiddleware,
|
|
async (req: Request, res: Response): Promise<void> => {
|
|
if (!requireAdmin(req, res)) return;
|
|
if (!requireBody(req, res)) return;
|
|
const { labelName, dryRun } = req.body as { labelName?: unknown; dryRun?: unknown };
|
|
if (typeof labelName !== 'string' || labelName.trim().length === 0) {
|
|
res.status(400).json({ error: 'labelName is required' });
|
|
return;
|
|
}
|
|
const nodeId = req.nodeId ?? 0;
|
|
const trimmedLabel = labelName.trim();
|
|
try {
|
|
const outcome = await runLocalLabelStop(nodeId, trimmedLabel, dryRun === true);
|
|
if (isDebugEnabled()) console.debug('[FleetActions:debug] local-stop:', { nodeId, dryRun: dryRun === true, matched: outcome.matched, stacks: outcome.stackResults.length });
|
|
const body: LabelLocalStopResponse = { matched: outcome.matched, results: outcome.stackResults };
|
|
res.json(body);
|
|
} catch (err) {
|
|
console.error('[FleetActions] local-stop error:', { nodeId, labelName: trimmedLabel }, err);
|
|
res.status(500).json({ error: getErrorMessage(err, 'Failed to run local label stop') });
|
|
}
|
|
},
|
|
);
|
|
|
|
// Per-node label assign. A control instance calls this on each target node
|
|
// during a fleet-wide bulk label assign so the label is resolved or created
|
|
// under the node's own database, by name, and assigned to the given stacks while
|
|
// preserving their existing labels (add semantics). Admin-only and available on
|
|
// every license, matching the rest of the Fleet Actions surface. Labels are
|
|
// node-local, so the control never reuses a local label id on a remote: the
|
|
// receiver owns label resolution for its own node.
|
|
fleetActionsRouter.post(
|
|
'/labels/local-assign',
|
|
authMiddleware,
|
|
async (req: Request, res: Response): Promise<void> => {
|
|
if (!requireAdmin(req, res)) return;
|
|
if (!requireBody(req, res)) return;
|
|
const { label, stackNames } = req.body as { label?: unknown; stackNames?: unknown };
|
|
const validated = validateLabelTemplate(label);
|
|
if (!validated.ok) {
|
|
res.status(400).json({ error: validated.error });
|
|
return;
|
|
}
|
|
if (!Array.isArray(stackNames) || !stackNames.every(s => typeof s === 'string')) {
|
|
res.status(400).json({ error: 'stackNames must be an array of strings' });
|
|
return;
|
|
}
|
|
if (stackNames.length > MAX_ASSIGNMENTS) {
|
|
res.status(400).json({ error: `stackNames may not exceed ${MAX_ASSIGNMENTS} entries` });
|
|
return;
|
|
}
|
|
const nodeId = req.nodeId ?? 0;
|
|
try {
|
|
const outcome = await runLocalLabelAssign(nodeId, validated.template, stackNames as string[]);
|
|
if (isDebugEnabled()) console.debug('[FleetActions:debug] local-assign:', { nodeId, label: validated.template.name, created: outcome.created, stacks: outcome.stackResults.length });
|
|
const body: LabelLocalAssignResponse = { created: outcome.created, results: outcome.stackResults };
|
|
res.json(body);
|
|
} catch (err) {
|
|
console.error('[FleetActions] local-assign error:', { nodeId, label: validated.template.name }, err);
|
|
res.status(500).json({ error: getErrorMessage(err, 'Failed to run local label assign') });
|
|
}
|
|
},
|
|
);
|