import { H as createLogger, Y as ensureHomeDirs, _ as ensureOpenCodeConfig, $ as ensureOpenCodeSystemConfig, g as applyUpdate, p as checkDocker, o as buildManagedServices, n as buildComposeOptions, x as composePull, G as composeUp, ax as parseComposeStderr, b4 as summarizeComposeStderr } from './src-CidJbygh.js';
import { g as getState } from './state-fxN45gBp.js';
import { d as getRequestId, r as requireAdmin, k as jsonResponse, e as errorResponse } from './helpers-kzwksNq1.js';
import { w as withSerialQueue } from './serial-queue-D9FEpYVv.js';
import './chunk-CLZ62Ad-.js';
import 'node:module';
import './provider-constants-DPrj3yBX.js';
import './dist-DOFcG0Zp.js';
import 'node:fs';
import 'node:path';
import 'buffer';
import 'fs';
import 'path';
import 'node:child_process';
import 'node:url';
import 'node:crypto';
import 'events';
import 'node:events';
import 'node:stream';
import 'node:string_decoder';
import 'assert';
import 'zlib';
import 'node:assert';
import 'node:fs/promises';
import 'node:os';
import './endpoints-UdQGgKsR.js';

//#region src/routes/admin/update/+server.ts
var logger = createLogger("update");
var POST = async (event) => {
	const requestId = getRequestId(event);
	logger.info("update request received", { requestId });
	const authError = requireAdmin(event, requestId);
	if (authError) return authError;
	return withSerialQueue("admin:update", async () => {
		try {
			const state = getState();
			ensureHomeDirs();
			ensureOpenCodeConfig();
			ensureOpenCodeSystemConfig();
			const result = await applyUpdate(state);
			logger.info("update applied, re-running compose", {
				requestId,
				intended: result.restarted
			});
			const dockerCheck = await checkDocker();
			const intendedServices = await buildManagedServices(state);
			let restarted = [];
			let failed = [];
			let dockerError;
			if (dockerCheck.ok) {
				const composeOpts = buildComposeOptions(state);
				const pullResult = await composePull(composeOpts);
				if (!pullResult.ok) logger.warn("update: image pull failed — recreating from local images", {
					requestId,
					stderr: pullResult.stderr?.slice(0, 300)
				});
				const composeResult = await composeUp({
					...composeOpts,
					services: intendedServices,
					forceRecreate: true
				});
				if (composeResult.ok) restarted = intendedServices;
				else {
					failed = parseComposeStderr(composeResult.stderr);
					const failedNames = new Set(failed.map((f) => f.service));
					restarted = intendedServices.filter((s) => !failedNames.has(s));
					if (failed.length === 0) {
						failed = [{
							service: "stack",
							reason: summarizeComposeStderr(composeResult.stderr) || `docker compose exited with code ${composeResult.code}`
						}];
						restarted = [];
					}
					dockerError = summarizeComposeStderr(composeResult.stderr);
					logger.warn("compose up reported failures", {
						requestId,
						code: composeResult.code,
						failed,
						restarted
					});
				}
			}
			const overallSuccess = dockerCheck.ok && failed.length === 0;
			const status = failed.length > 0 ? 502 : 200;
			logger.info("update completed", {
				requestId,
				dockerAvailable: dockerCheck.ok,
				overallSuccess,
				restartedCount: restarted.length,
				failedCount: failed.length
			});
			return jsonResponse(status, {
				ok: overallSuccess,
				restarted,
				failed,
				dockerAvailable: dockerCheck.ok,
				overallSuccess,
				...dockerError ? { error: dockerError } : {}
			}, requestId);
		} catch (err) {
			const msg = err instanceof Error ? err.message : String(err);
			logger.error("update failed", {
				requestId,
				error: msg
			});
			return errorResponse(500, "update_failed", msg, {}, requestId);
		}
	});
};

export { POST };
//# sourceMappingURL=_server.ts-C5SDazDN.js.map
