mirror of
https://github.com/Dasemu/remote-mcp-manager.git
synced 2026-11-26 23:36:31 +00:00
feat: add start/stop/restart controls and explain build vs deploy in the panel
Start/stop/restart act on the existing container instead of recreating it. Docker's 304 (already started/stopped) is treated as success. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
0d38b3950c
commit
a999b8c694
8 files changed
+222
No files matched your search
@@ -13,6 +13,9 @@ import {
|
||||
buildInstallation,
|
||||
deployInstallation,
|
||||
redeployInstallation,
|
||||
startInstallation,
|
||||
stopInstallation,
|
||||
restartInstallation,
|
||||
removeInstallationContainer,
|
||||
} from "../service/orchestrator.js";
|
||||
import { buildLogPath } from "../build/nixpacks.js";
|
||||
@@ -117,6 +120,24 @@ installationsRouter.post("/:id/redeploy", async (req, res) => {
|
||||
}
|
||||
});
|
||||
|
||||
for (const [action, handler] of [
|
||||
["start", startInstallation],
|
||||
["stop", stopInstallation],
|
||||
["restart", restartInstallation],
|
||||
] as const) {
|
||||
installationsRouter.post(`/:id/${action}`, async (req, res) => {
|
||||
if (!getInstallationById(req.params.id)) {
|
||||
res.status(404).json({ error: "not found" });
|
||||
return;
|
||||
}
|
||||
try {
|
||||
res.json(toPublicJson(await handler(req.params.id)));
|
||||
} catch (err) {
|
||||
res.status(500).json({ error: (err as Error).message });
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
installationsRouter.get("/:id/logs", (req, res) => {
|
||||
const installation = getInstallationById(req.params.id);
|
||||
if (!installation) {
|
||||
|
||||
@@ -3,6 +3,7 @@ import Docker from "dockerode";
|
||||
import { config } from "../config/env.js";
|
||||
import type { Installation } from "../db/installations.js";
|
||||
import { withTimeout } from "../util/withTimeout.js";
|
||||
import { isNotFoundError, isNotModifiedError } from "./errors.js";
|
||||
|
||||
const docker = new Docker({ socketPath: config.dockerSocketPath });
|
||||
|
||||
@@ -18,6 +19,8 @@ export async function ensureNetwork(name: string): Promise<void> {
|
||||
// cache plus a call timeout keeps one wedged Docker daemon from snowballing into the proxy.
|
||||
const IP_CACHE_TTL_MS = 5_000;
|
||||
const DOCKER_CALL_TIMEOUT_MS = 10_000;
|
||||
// stop/restart send SIGTERM and wait the daemon's grace period (10s) before SIGKILL.
|
||||
const DOCKER_STOP_TIMEOUT_MS = 30_000;
|
||||
const ipCache = new Map<string, { ip: Promise<string>; expires: number }>();
|
||||
|
||||
export async function removeContainerIfExists(containerName: string): Promise<void> {
|
||||
@@ -99,6 +102,39 @@ export async function stopInstallationContainer(containerName: string): Promise<
|
||||
await removeContainerIfExists(containerName);
|
||||
}
|
||||
|
||||
// Lifecycle actions on the EXISTING container (no remove/recreate, so the image and the
|
||||
// config it was created with stay untouched). Resolves false when the container no longer
|
||||
// exists, so the caller can fall back to a full deploy. The container's IP can change across a
|
||||
// stop/start, hence the cache is dropped before and after.
|
||||
async function actOnExistingContainer(
|
||||
containerName: string,
|
||||
what: string,
|
||||
timeoutMs: number,
|
||||
action: (container: Docker.Container) => Promise<unknown>,
|
||||
): Promise<boolean> {
|
||||
ipCache.delete(containerName);
|
||||
try {
|
||||
await withTimeout(action(docker.getContainer(containerName)), timeoutMs, `docker ${what} ${containerName}`);
|
||||
return true;
|
||||
} catch (err) {
|
||||
if (isNotFoundError(err)) return false;
|
||||
// start on a running container / stop on a stopped one: already where we want it.
|
||||
if (isNotModifiedError(err)) return true;
|
||||
throw err;
|
||||
} finally {
|
||||
ipCache.delete(containerName);
|
||||
}
|
||||
}
|
||||
|
||||
export const stopContainer = (containerName: string) =>
|
||||
actOnExistingContainer(containerName, "stop", DOCKER_STOP_TIMEOUT_MS, (c) => c.stop());
|
||||
|
||||
export const startContainer = (containerName: string) =>
|
||||
actOnExistingContainer(containerName, "start", DOCKER_CALL_TIMEOUT_MS, (c) => c.start());
|
||||
|
||||
export const restartContainer = (containerName: string) =>
|
||||
actOnExistingContainer(containerName, "restart", DOCKER_STOP_TIMEOUT_MS, (c) => c.restart());
|
||||
|
||||
async function lookupContainerIp(containerName: string): Promise<string> {
|
||||
const info = await withTimeout(
|
||||
docker.getContainer(containerName).inspect(),
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { isNotFoundError, isNotModifiedError } from "./errors.js";
|
||||
|
||||
describe("isNotFoundError", () => {
|
||||
it("matches a Docker API 404", () => {
|
||||
expect(isNotFoundError(Object.assign(new Error("no such container"), { statusCode: 404 }))).toBe(true);
|
||||
});
|
||||
|
||||
it("does not match other Docker errors or non-errors", () => {
|
||||
expect(isNotFoundError(Object.assign(new Error("server error"), { statusCode: 500 }))).toBe(false);
|
||||
expect(isNotFoundError(new Error("docker inspect x timed out after 10000ms"))).toBe(false);
|
||||
expect(isNotFoundError(undefined)).toBe(false);
|
||||
expect(isNotFoundError("404")).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe("isNotModifiedError", () => {
|
||||
it("matches the 304 docker-modem raises for already started/stopped containers", () => {
|
||||
expect(isNotModifiedError(Object.assign(new Error("already started"), { statusCode: 304 }))).toBe(true);
|
||||
expect(isNotModifiedError(Object.assign(new Error("boom"), { statusCode: 500 }))).toBe(false);
|
||||
expect(isNotModifiedError(new Error("plain"))).toBe(false);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,9 @@
|
||||
/** True when a dockerode call failed because the target container (or image) doesn't exist. */
|
||||
export function isNotFoundError(err: unknown): boolean {
|
||||
return typeof err === "object" && err !== null && (err as { statusCode?: unknown }).statusCode === 404;
|
||||
}
|
||||
|
||||
/** True for HTTP 304: docker-modem surfaces "already started/stopped" as an error, but it means the container is already in the wanted state. */
|
||||
export function isNotModifiedError(err: unknown): boolean {
|
||||
return typeof err === "object" && err !== null && (err as { statusCode?: unknown }).statusCode === 304;
|
||||
}
|
||||
@@ -12,6 +12,9 @@ import { runNixpacksBuild } from "../build/nixpacks.js";
|
||||
import {
|
||||
runInstallationContainer,
|
||||
stopInstallationContainer,
|
||||
stopContainer,
|
||||
startContainer,
|
||||
restartContainer,
|
||||
waitForContainerHealth,
|
||||
getImageId,
|
||||
removeImageIfUnused,
|
||||
@@ -144,6 +147,52 @@ export async function redeployInstallation(id: string, opts: { rebuild?: boolean
|
||||
return deployInstallation(id);
|
||||
}
|
||||
|
||||
// Start/restart act on the existing container, so they never apply changed env/config (that
|
||||
// needs a deploy, which recreates it). If the container is gone (e.g. pruned after a host
|
||||
// reboot) they fall back to a deploy from the built image.
|
||||
async function resumeInstallation(
|
||||
id: string,
|
||||
act: (containerName: string) => Promise<boolean>,
|
||||
): Promise<Installation> {
|
||||
const installation = getInstallationById(id);
|
||||
if (!installation) throw new Error("installation not found");
|
||||
|
||||
updateInstallation(id, { status: "starting", statusDetail: null });
|
||||
|
||||
try {
|
||||
if (!(await act(installation.containerName))) {
|
||||
if (!installation.imageTag) {
|
||||
throw new Error("container does not exist and the installation has no built image — build it first");
|
||||
}
|
||||
return await deployInstallation(id);
|
||||
}
|
||||
await waitForContainerHealth(installation);
|
||||
|
||||
const bearerToken = installation.bearerToken ?? generateBearerToken();
|
||||
return updateInstallation(id, { status: "running", statusDetail: null, bearerToken });
|
||||
} catch (err) {
|
||||
return updateInstallation(id, { status: "error", statusDetail: (err as Error).message });
|
||||
}
|
||||
}
|
||||
|
||||
export const startInstallation = (id: string) => resumeInstallation(id, startContainer);
|
||||
|
||||
export const restartInstallation = (id: string) => resumeInstallation(id, restartContainer);
|
||||
|
||||
/** Stops the container without removing it; it stays stopped (restart policy `unless-stopped`) until started again. */
|
||||
export async function stopInstallation(id: string): Promise<Installation> {
|
||||
const installation = getInstallationById(id);
|
||||
if (!installation) throw new Error("installation not found");
|
||||
|
||||
try {
|
||||
// A missing container is already stopped as far as the operator is concerned.
|
||||
await stopContainer(installation.containerName);
|
||||
return updateInstallation(id, { status: "stopped", statusDetail: null });
|
||||
} catch (err) {
|
||||
return updateInstallation(id, { status: "error", statusDetail: (err as Error).message });
|
||||
}
|
||||
}
|
||||
|
||||
export async function removeInstallationContainer(id: string): Promise<void> {
|
||||
const installation = getInstallationById(id);
|
||||
if (!installation) return;
|
||||
|
||||
Reference in new issue
Block a user