import { H as createLogger, p as checkDocker, n as buildComposeOptions, x as composePull, o as buildManagedServices, G as composeUp } from './src-CidJbygh.js';
import { g as getState } from './state-fxN45gBp.js';
import { d as getRequestId, r as requireAdmin, e as errorResponse, k as jsonResponse } 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/containers/pull/+server.ts
var logger = createLogger("containers-pull");
var POST = async (event) => {
	const requestId = getRequestId(event);
	logger.info("pull request received", { requestId });
	const authError = requireAdmin(event, requestId);
	if (authError) return authError;
	return withSerialQueue("admin:containers:pull", async () => {
		const state = getState();
		const dockerCheck = await checkDocker();
		if (!dockerCheck.ok) return errorResponse(503, "docker_unavailable", "Docker is not available", { stderr: dockerCheck.stderr }, requestId);
		const composeOpts = buildComposeOptions(state);
		logger.info("pulling images", { requestId });
		const pullResult = await composePull(composeOpts);
		if (!pullResult.ok) {
			logger.error("image pull failed", {
				requestId,
				stderr: pullResult.stderr
			});
			return errorResponse(502, "pull_failed", "Failed to pull images", { stderr: pullResult.stderr }, requestId);
		}
		logger.info("recreating containers", { requestId });
		const managedServices = await buildManagedServices(state);
		const upResult = await composeUp({
			...composeOpts,
			services: managedServices,
			forceRecreate: true
		});
		if (!upResult.ok) {
			logger.error("compose up failed after pull", {
				requestId,
				stderr: upResult.stderr
			});
			return errorResponse(502, "up_failed", "Images pulled but failed to recreate containers", { stderr: upResult.stderr }, requestId);
		}
		logger.info("pull completed", {
			requestId,
			started: managedServices
		});
		return jsonResponse(200, {
			ok: true,
			pulled: pullResult.stdout,
			started: managedServices
		}, requestId);
	});
};

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