This commit is contained in:
jarek committed 2026-02-08 09:59:06 +01:00
1 parent 9d2266dffe
commit 188ba1967d
87 files changed
+2103 -1774

No files matched your search

+8 -2
View File
@@ -186,13 +186,19 @@ export async function createUserSession(
// Get session timeout from settings
const settings = await getAuthSettings();
const expiresAt = new Date(Date.now() + settings.sessionTimeout * 1000).toISOString();
// Safety: ensure sessionTimeout is valid (1 second to 30 days), default to 24h if invalid
const MAX_SESSION_TIMEOUT = 2592000; // 30 days in seconds
const DEFAULT_SESSION_TIMEOUT = 86400; // 24 hours in seconds
const sessionTimeout = (settings?.sessionTimeout > 0 && settings?.sessionTimeout <= MAX_SESSION_TIMEOUT)
? settings.sessionTimeout
: DEFAULT_SESSION_TIMEOUT;
const expiresAt = new Date(Date.now() + sessionTimeout * 1000).toISOString();
// Create session in database
const session = await dbCreateSession(sessionId, userId, provider, expiresAt);
// Set secure cookie
setSessionCookie(cookies, sessionId, settings.sessionTimeout);
setSessionCookie(cookies, sessionId, sessionTimeout);
// Update user's last login time
await updateUser(userId, { lastLogin: new Date().toISOString() });
+33 -1
View File
@@ -1148,7 +1148,11 @@ export async function updateAuthSettings(data: Partial<AuthSettingsData>): Promi
if (data.authEnabled !== undefined) updateData.authEnabled = data.authEnabled;
if (data.defaultProvider !== undefined) updateData.defaultProvider = data.defaultProvider;
if (data.sessionTimeout !== undefined) updateData.sessionTimeout = data.sessionTimeout;
if (data.sessionTimeout !== undefined) {
// Cap session timeout to safe maximum (30 days)
const MAX_SESSION_TIMEOUT = 2592000; // 30 days in seconds
updateData.sessionTimeout = Math.min(Math.max(1, data.sessionTimeout), MAX_SESSION_TIMEOUT);
}
// Get existing row's id (may not be 1 after db reset/migration)
const existing = await db.select({ id: authSettings.id }).from(authSettings).limit(1);
@@ -2614,6 +2618,34 @@ export async function getStackSource(stackName: string, environmentId?: number |
} as StackSourceWithRepo;
}
export async function getStackSourceByComposePath(composePath: string, environmentId?: number | null): Promise<StackSourceWithRepo | null> {
const envCondition = environmentId !== undefined && environmentId !== null
? eq(stackSources.environmentId, environmentId)
: isNull(stackSources.environmentId);
const results = await db.select().from(stackSources)
.where(and(eq(stackSources.composePath, composePath), envCondition));
if (!results[0]) return null;
const row = results[0];
let repository = null;
let gitStackData = null;
if (row.gitRepositoryId) {
repository = await getGitRepository(row.gitRepositoryId);
}
if (row.gitStackId) {
gitStackData = await getGitStack(row.gitStackId);
}
return {
...row,
repository,
gitStack: gitStackData
} as StackSourceWithRepo;
}
export async function getStackSources(environmentId?: number | null): Promise<StackSourceWithRepo[]> {
let results;
if (environmentId !== undefined && environmentId !== null) {
+208 -93
View File
@@ -233,27 +233,39 @@ function processStreamFrames(
onStdout?: (data: string) => void,
onStderr?: (data: string) => void
): { stdout: string; remaining: Buffer<ArrayBufferLike> } {
let stdout = '';
// Collect stdout frame payloads as raw buffers first, then decode to UTF-8 once
// at the end. Decoding each frame individually corrupts multi-byte UTF-8 characters
// that may be split across frame boundaries (observed with Grype on Synology NAS).
const stdoutChunks: Buffer[] = [];
let stdoutLen = 0;
let offset = 0;
while (buffer.length >= offset + 8) {
const streamType = buffer.readUInt8(offset);
const frameSize = buffer.readUInt32BE(offset + 4);
// Validate stream type (0=stdin, 1=stdout, 2=stderr)
if (streamType > 2) break;
// Sanity check - no single frame should be > 10MB
if (frameSize > 10 * 1024 * 1024) break;
if (buffer.length < offset + 8 + frameSize) break;
const payload = buffer.slice(offset + 8, offset + 8 + frameSize).toString('utf-8');
const payloadBuf = buffer.slice(offset + 8, offset + 8 + frameSize);
if (streamType === 1) {
stdout += payload;
onStdout?.(payload);
stdoutChunks.push(payloadBuf);
stdoutLen += payloadBuf.length;
onStdout?.(payloadBuf.toString('utf-8'));
} else if (streamType === 2) {
onStderr?.(payload);
onStderr?.(payloadBuf.toString('utf-8'));
}
offset += 8 + frameSize;
}
const stdout = Buffer.concat(stdoutChunks, stdoutLen).toString('utf-8');
return { stdout, remaining: buffer.slice(offset) };
}
@@ -402,6 +414,16 @@ function edgeResponseToResponse(edgeResponse: EdgeResponse): Response {
});
}
/**
* Drain a response body to release the underlying socket/TLS connection.
* Must be called on any Response whose body won't otherwise be consumed.
*/
export async function drainResponse(response: Response): Promise<void> {
if (!response.bodyUsed) {
try { await response.arrayBuffer(); } catch {}
}
}
/**
* Make a request to the Docker API
* Exported for use by stacks.ts module
@@ -565,23 +587,18 @@ export async function dockerFetch(
finalOptions.keepalive = false;
}
// Explicitly close connection to prevent TLS session reuse issues
// But only for non-streaming requests (logs, events, exec need keep-alive)
if (!streaming) {
finalOptions.headers = {
...finalOptions.headers,
'Connection': 'close'
};
}
// Optional verbose TLS debugging
// Optional verbose TLS debugging
if (process.env.DEBUG_TLS) {
// @ts-ignore - Bun-specific verbose option
finalOptions.verbose = true;
}
}
// @ts-ignore - Bun supports timeout option
// Add default timeout for non-streaming requests to prevent socket accumulation
if (!streaming && !finalOptions.signal) {
finalOptions.signal = AbortSignal.timeout(30000);
}
try {
const response = await fetch(url, { ...finalOptions, ...bunOptions });
const elapsed = Date.now() - startTime;
@@ -748,23 +765,28 @@ export async function getContainerStats(id: string, envId?: number | null) {
}
export async function startContainer(id: string, envId?: number | null) {
await dockerFetch(`/containers/${id}/start`, { method: 'POST' }, envId);
const response = await dockerFetch(`/containers/${id}/start`, { method: 'POST' }, envId);
await drainResponse(response);
}
export async function stopContainer(id: string, envId?: number | null) {
await dockerFetch(`/containers/${id}/stop`, { method: 'POST' }, envId);
const response = await dockerFetch(`/containers/${id}/stop`, { method: 'POST' }, envId);
await drainResponse(response);
}
export async function restartContainer(id: string, envId?: number | null) {
await dockerFetch(`/containers/${id}/restart`, { method: 'POST' }, envId);
const response = await dockerFetch(`/containers/${id}/restart`, { method: 'POST' }, envId);
await drainResponse(response);
}
export async function pauseContainer(id: string, envId?: number | null) {
await dockerFetch(`/containers/${id}/pause`, { method: 'POST' }, envId);
const response = await dockerFetch(`/containers/${id}/pause`, { method: 'POST' }, envId);
await drainResponse(response);
}
export async function unpauseContainer(id: string, envId?: number | null) {
await dockerFetch(`/containers/${id}/unpause`, { method: 'POST' }, envId);
const response = await dockerFetch(`/containers/${id}/unpause`, { method: 'POST' }, envId);
await drainResponse(response);
}
export async function removeContainer(id: string, force = false, envId?: number | null) {
@@ -787,7 +809,8 @@ export async function removeContainer(id: string, force = false, envId?: number
}
export async function renameContainer(id: string, newName: string, envId?: number | null) {
await dockerFetch(`/containers/${id}/rename?name=${encodeURIComponent(newName)}`, { method: 'POST' }, envId);
const response = await dockerFetch(`/containers/${id}/rename?name=${encodeURIComponent(newName)}`, { method: 'POST' }, envId);
await drainResponse(response);
}
export async function getContainerLogs(id: string, tail = 100, envId?: number | null): Promise<string> {
@@ -1643,6 +1666,7 @@ export async function listImages(envId?: number | null): Promise<ImageInfo[]> {
id: image.Id,
repoTags: image.RepoTags || [],
tags: image.RepoTags || [],
repoDigests: image.RepoDigests || [],
size: image.Size,
virtualSize: image.VirtualSize || image.Size,
created: image.Created,
@@ -1969,6 +1993,7 @@ async function getRegistryBearerToken(registry: string, repo: string): Promise<s
});
if (!tokenResponse.ok) {
await tokenResponse.text(); // Consume body to release socket
console.error(`Token request failed: ${tokenResponse.status}`);
return null;
}
@@ -2247,11 +2272,12 @@ export async function checkImageUpdateAvailable(
}
export async function tagImage(id: string, repo: string, tag: string, envId?: number | null) {
await dockerFetch(
const response = await dockerFetch(
`/images/${encodeURIComponent(id)}/tag?repo=${encodeURIComponent(repo)}&tag=${encodeURIComponent(tag)}`,
{ method: 'POST' },
envId
);
await drainResponse(response);
}
/**
@@ -2774,7 +2800,8 @@ export async function createExec(options: ExecOptions): Promise<{ Id: string }>
export async function resizeExec(execId: string, cols: number, rows: number, envId?: number | null) {
try {
await dockerFetch(`/exec/${execId}/resize?h=${rows}&w=${cols}`, { method: 'POST' }, envId);
const response = await dockerFetch(`/exec/${execId}/resize?h=${rows}&w=${cols}`, { method: 'POST' }, envId);
await drainResponse(response);
} catch {
// Resize may fail if exec is not running, ignore
}
@@ -2941,6 +2968,7 @@ export async function getDockerEvents(
);
if (!response.ok) {
await drainResponse(response);
throw new Error(`Docker events API returned ${response.status}`);
}
@@ -3007,7 +3035,7 @@ export async function runContainer(options: {
try {
// Start container
console.log(`[runContainer] Starting container ${containerId}...`);
await dockerFetch(`/containers/${containerId}/start`, { method: 'POST' }, options.envId);
await drainResponse(await dockerFetch(`/containers/${containerId}/start`, { method: 'POST' }, options.envId));
// Wait for container to finish
console.log(`[runContainer] Waiting for container ${containerId} to finish...`);
@@ -3035,7 +3063,7 @@ export async function runContainer(options: {
} finally {
// Always cleanup container manually
try {
await dockerFetch(`/containers/${containerId}?force=true`, { method: 'DELETE' }, options.envId);
await drainResponse(await dockerFetch(`/containers/${containerId}?force=true`, { method: 'DELETE' }, options.envId));
} catch {
// Ignore cleanup errors
}
@@ -3053,6 +3081,7 @@ export async function runContainerWithStreaming(options: {
envId?: number | null;
onStdout?: (data: string) => void;
onStderr?: (data: string) => void;
timeout?: number; // Overall timeout in ms (0 or undefined = no timeout)
}): Promise<string> {
const baseName = options.name || `dockhand-stream-${Date.now()}`;
const containerName = `${baseName}-${randomSuffix()}`;
@@ -3084,60 +3113,79 @@ export async function runContainerWithStreaming(options: {
const config = await getDockerConfig(options.envId ?? undefined);
try {
// Start container
await dockerFetch(`/containers/${containerId}/start`, { method: 'POST' }, options.envId);
const doWork = async () => {
// Start container
await drainResponse(await dockerFetch(`/containers/${containerId}/start`, { method: 'POST' }, options.envId));
// Stream stderr for real-time progress while container runs
if (config.connectionType === 'hawser-edge' && config.environmentId) {
await streamEdgeStderr(config.environmentId, containerId, options.onStderr);
} else {
await streamLocalStderr(containerId, options.envId, options.onStderr);
}
// Create abort controller to cancel stderr stream when container exits
// On some Docker hosts (e.g. Synology NAS with older kernels), follow=true
// streams don't close when the container exits, causing indefinite hangs.
const abortController = new AbortController();
// Wait for container to fully exit before fetching stdout
// The stderr stream may close before the container finishes writing to stdout
// Use a timeout to prevent hanging if something goes wrong (container should already be exited)
let exitCode: number | undefined;
try {
const waitPromise = dockerFetch(`/containers/${containerId}/wait`, { method: 'POST' }, options.envId);
const timeoutPromise = new Promise<never>((_, reject) =>
setTimeout(() => reject(new Error('Container wait timeout after 10s')), 10000)
);
const waitResult = await Promise.race([waitPromise, timeoutPromise]);
const waitData = await waitResult.json() as { StatusCode?: number };
exitCode = waitData.StatusCode;
console.log(`[runContainerWithStreaming] Container exited with code: ${exitCode}`);
} catch (err) {
// Log but don't fail - container might already be gone or stderr stream was reliable
console.warn(`[runContainerWithStreaming] Wait warning: ${(err as Error).message}`);
}
// Start stderr streaming (non-blocking - may hang on some hosts)
const stderrPromise = (config.connectionType === 'hawser-edge' && config.environmentId)
? streamEdgeStderr(config.environmentId, containerId, options.onStderr, abortController.signal)
: streamLocalStderr(containerId, options.envId, options.onStderr, abortController.signal);
stderrPromise.catch(() => {}); // Suppress unhandled rejection
// Container has exited. Now fetch stdout reliably (no race condition).
const stdout = await fetchContainerStdout(containerId, config, options.envId);
// If stdout is empty and exit code is non-zero, fetch stderr for debugging
if (stdout.length === 0 && exitCode !== 0) {
// Wait for container to exit - this is the reliable signal
let exitCode: number | undefined;
try {
const stderrResponse = await dockerFetch(
`/containers/${containerId}/logs?stdout=false&stderr=true&follow=false`,
{},
options.envId
);
const stderrBuffer = Buffer.from(await stderrResponse.arrayBuffer());
const stderrResult = processStreamFrames(stderrBuffer, undefined, undefined);
if (stderrResult.stderr) {
console.error(`[runContainerWithStreaming] Container stderr: ${stderrResult.stderr.substring(0, 1000)}`);
}
} catch {
// Ignore stderr fetch errors
const waitResult = await dockerFetch(`/containers/${containerId}/wait`, { method: 'POST' }, options.envId);
const waitData = await waitResult.json() as { StatusCode?: number };
exitCode = waitData.StatusCode;
console.log(`[runContainerWithStreaming] Container exited with code: ${exitCode}`);
} catch (err) {
console.warn(`[runContainerWithStreaming] Wait warning: ${(err as Error).message}`);
}
}
return stdout;
// Container exited - abort stderr stream (it may be hanging on some Docker hosts)
abortController.abort();
// Give brief moment for any final stderr data to flush
await Promise.race([stderrPromise, new Promise(r => setTimeout(r, 1000))]);
// Container has exited. Now fetch stdout reliably (no race condition).
const stdout = await fetchContainerStdout(containerId, config, options.envId);
// If stdout is empty and exit code is non-zero, fetch stderr for debugging
if (stdout.length === 0 && exitCode !== 0) {
try {
const stderrResponse = await dockerFetch(
`/containers/${containerId}/logs?stdout=false&stderr=true&follow=false`,
{},
options.envId
);
const stderrBuffer = Buffer.from(await stderrResponse.arrayBuffer());
const stderrOutput = demuxDockerStream(stderrBuffer, { separateStreams: true });
const stderrText = typeof stderrOutput === 'string' ? stderrOutput : stderrOutput.stderr;
if (stderrText) {
console.error(`[runContainerWithStreaming] Container stderr: ${stderrText.substring(0, 1000)}`);
}
} catch {
// Ignore stderr fetch errors
}
}
return stdout;
};
const effectiveTimeout = options.timeout ?? 0;
if (effectiveTimeout > 0) {
return await Promise.race([
doWork(),
new Promise<never>((_, reject) =>
setTimeout(() => reject(new Error(
`Container execution timed out after ${Math.round(effectiveTimeout / 1000)}s`
)), effectiveTimeout)
)
]);
} else {
return await doWork();
}
} finally {
// Always cleanup container
try {
await dockerFetch(`/containers/${containerId}?force=true`, { method: 'DELETE' }, options.envId);
await drainResponse(await dockerFetch(`/containers/${containerId}?force=true`, { method: 'DELETE' }, options.envId));
} catch {
// Ignore cleanup errors
}
@@ -3148,7 +3196,8 @@ export async function runContainerWithStreaming(options: {
async function streamLocalStderr(
containerId: string,
envId: number | null | undefined,
onStderr?: (data: string) => void
onStderr?: (data: string) => void,
signal?: AbortSignal
): Promise<void> {
const response = await dockerFetch(
`/containers/${containerId}/logs?stdout=false&stderr=true&follow=true`,
@@ -3159,13 +3208,20 @@ async function streamLocalStderr(
const reader = response.body?.getReader();
if (!reader) return;
let buffer: Buffer<ArrayBufferLike> = Buffer.alloc(0);
while (true) {
const { done, value } = await reader.read();
if (done) break;
buffer = Buffer.concat([buffer, Buffer.from(value)]);
const result = processStreamFrames(buffer, undefined, onStderr);
buffer = result.remaining;
// Cancel reader when abort signal fires (container exited)
signal?.addEventListener('abort', () => reader.cancel(), { once: true });
try {
let buffer: Buffer<ArrayBufferLike> = Buffer.alloc(0);
while (true) {
const { done, value } = await reader.read();
if (done) break;
buffer = Buffer.concat([buffer, Buffer.from(value)]);
const result = processStreamFrames(buffer, undefined, onStderr);
buffer = result.remaining;
}
} catch {
// Reader was cancelled via abort signal - expected
}
}
@@ -3173,10 +3229,16 @@ async function streamLocalStderr(
async function streamEdgeStderr(
environmentId: number,
containerId: string,
onStderr?: (data: string) => void
onStderr?: (data: string) => void,
signal?: AbortSignal
): Promise<void> {
return new Promise((resolve, reject) => {
let buffer: Buffer<ArrayBufferLike> = Buffer.alloc(0);
let resolved = false;
const finish = () => { if (!resolved) { resolved = true; resolve(); } };
// Resolve when abort signal fires (container exited)
signal?.addEventListener('abort', finish, { once: true });
sendEdgeStreamRequest(
environmentId,
@@ -3184,6 +3246,7 @@ async function streamEdgeStderr(
`/containers/${containerId}/logs?stdout=false&stderr=true&follow=true`,
{
onData: (data: string) => {
if (resolved) return;
try {
const decoded = Buffer.from(data, 'base64');
buffer = Buffer.concat([buffer, decoded]);
@@ -3193,12 +3256,13 @@ async function streamEdgeStderr(
// Ignore decode errors
}
},
onEnd: () => resolve(),
onEnd: () => finish(),
onError: (error: string) => {
// Container exited = success
if (error.includes('container') && (error.includes('exited') || error.includes('not running'))) {
resolve();
} else {
finish();
} else if (!resolved) {
resolved = true;
reject(new Error(error));
}
}
@@ -3207,6 +3271,38 @@ async function streamEdgeStderr(
});
}
// Extract stdout from a buffer, with raw fallback if frame parsing returns nothing.
// Mirrors demuxDockerStream's fallback (line ~202-205) for non-multiplexed Docker output.
function extractStdoutFromBuffer(buffer: Buffer): string {
const result = processStreamFrames(buffer, undefined, undefined);
if (buffer.length > 100000) {
console.log(
`[extractStdoutFromBuffer] Raw buffer: ${buffer.length} bytes, stdout: ${result.stdout.length} chars, ` +
`remaining: ${result.remaining.length} bytes`
);
}
if (result.stdout.length === 0 && buffer.length > 0) {
console.warn(
`[extractStdoutFromBuffer] Frame parsing empty but buffer has ${buffer.length} bytes. ` +
`First 16 bytes: [${Array.from(buffer.slice(0, 16)).join(', ')}]. Falling back to raw.`
);
return buffer.toString('utf-8').replace(/[\x00-\x08\x0B\x0C\x0E-\x1F]/g, '');
}
// If there's remaining data after frame parsing, append it as raw text
if (result.remaining.length > 0) {
console.warn(
`[extractStdoutFromBuffer] ${result.remaining.length} bytes remaining after frame parsing, appending as raw`
);
const rawTail = result.remaining.toString('utf-8').replace(/[\x00-\x08\x0B\x0C\x0E-\x1F]/g, '');
return result.stdout + rawTail;
}
return result.stdout;
}
// Fetch stdout after container has exited (reliable, no race)
async function fetchContainerStdout(
containerId: string,
@@ -3223,19 +3319,34 @@ async function fetchContainerStdout(
const bodyData = typeof response.body === 'string'
? Buffer.from(response.body, 'base64')
: Buffer.from(response.body);
const result = processStreamFrames(bodyData, undefined, undefined);
return result.stdout;
return extractStdoutFromBuffer(bodyData);
}
// Local/standard mode
// Local/standard mode - read via streaming to handle large Docker log responses
const response = await dockerFetch(
`/containers/${containerId}/logs?stdout=true&stderr=false&follow=false`,
{},
envId
);
const buffer = Buffer.from(await response.arrayBuffer());
const result = processStreamFrames(buffer, undefined, undefined);
return result.stdout;
const reader = response.body?.getReader();
if (!reader) {
const buffer = Buffer.from(await response.arrayBuffer());
return extractStdoutFromBuffer(buffer);
}
const chunks: Uint8Array[] = [];
let totalSize = 0;
while (true) {
const { done, value } = await reader.read();
if (done) break;
if (value) {
chunks.push(value);
totalSize += value.length;
}
}
const buffer = Buffer.concat(chunks, totalSize);
return extractStdoutFromBuffer(buffer);
}
// Push image to registry
@@ -3859,7 +3970,10 @@ async function ensureVolumeHelperImage(envId?: number | null): Promise<void> {
async function isContainerRunning(containerId: string, envId?: number | null): Promise<boolean> {
try {
const response = await dockerFetch(`/containers/${containerId}/json`, {}, envId);
if (!response.ok) return false;
if (!response.ok) {
await response.text(); // Consume body to release socket
return false;
}
const info = await response.json();
return info.State?.Running === true;
} catch {
@@ -3934,7 +4048,7 @@ export async function getOrCreateVolumeHelperContainer(
const containerId = response.Id;
// Start the container
await dockerFetch(`/containers/${containerId}/start`, { method: 'POST' }, envId);
await drainResponse(await dockerFetch(`/containers/${containerId}/start`, { method: 'POST' }, envId));
// Cache the container
volumeHelperCache.set(cacheKey, {
@@ -4021,13 +4135,13 @@ export async function removeVolumeHelperContainer(
): Promise<void> {
try {
// Stop the container first (force)
await dockerFetch(`/containers/${containerId}/stop?t=1`, { method: 'POST' }, envId);
await drainResponse(await dockerFetch(`/containers/${containerId}/stop?t=1`, { method: 'POST' }, envId));
} catch {
// Ignore stop errors
}
// Remove the container
await dockerFetch(`/containers/${containerId}?force=true`, { method: 'DELETE' }, envId);
await drainResponse(await dockerFetch(`/containers/${containerId}?force=true`, { method: 'DELETE' }, envId));
}
/**
@@ -4046,6 +4160,7 @@ async function cleanupStaleVolumeHelpersForEnv(envId?: number | null): Promise<n
);
if (!response.ok) {
await response.text(); // Consume body to release socket
return 0;
}
+13 -1
View File
@@ -1089,11 +1089,23 @@ export async function deployGitStackWithProgress(
// Step 5: Deploying stack
// Uses `docker compose up -d --remove-orphans` which only recreates changed services
onProgress({ status: 'deploying', message: `Deploying ${gitStack.stackName}...`, step: 5, totalSteps });
// Determine env filename relative to compose dir (same logic as syncGitStack)
let envFileName: string | undefined;
if (gitStack.envFilePath) {
const envFilePath = join(repoPath, gitStack.envFilePath);
if (existsSync(envFilePath)) {
envFileName = relative(composeDir, envFilePath);
}
}
const result = await deployStack({
name: gitStack.stackName,
compose: composeContent,
envId: gitStack.environmentId,
sourceDir: composeDir // Copy entire directory from git repo
sourceDir: composeDir, // Copy entire directory from git repo
composeFileName: basename(gitStack.composePath), // Use original compose filename from repo
envFileName // Env file relative to compose dir (for --env-file flag, optional)
});
if (result.success) {
+148 -68
View File
@@ -10,7 +10,8 @@ import {
removeVolume,
runContainer,
runContainerWithStreaming,
inspectImage
inspectImage,
checkImageUpdateAvailable
} from './docker';
import { getEnvironment, getEnvSetting, getSetting } from './db';
import { sendEventNotification } from './notifications';
@@ -73,8 +74,31 @@ const TRIVY_VOLUME_NAME = 'dockhand-trivy-db';
const DATA_DIR = process.env.DATA_DIR || '/app/data';
const SCANNER_CACHE_DIR = 'scanner-cache';
// Track running scanner instances to detect concurrent scans
const runningScanners = new Map<string, number>(); // key: "grype" or "trivy", value: count
// Per-type serial lock to prevent concurrent scans of the same scanner type.
// This avoids DB lock conflicts AND ensures the second scan uses warm cache
// instead of re-downloading the entire vulnerability database (~100MB).
const scannerLocks = new Map<string, Promise<void>>(); // key: "grype" or "trivy"
async function withScannerLock<T>(scannerType: string, fn: () => Promise<T>): Promise<T> {
const existing = scannerLocks.get(scannerType);
if (existing) {
console.log(`[Scanner] Waiting for previous ${scannerType} scan to complete...`);
await existing.catch(() => {}); // Don't fail if previous scan errored
}
let resolve: () => void;
const lockPromise = new Promise<void>(r => { resolve = r; });
scannerLocks.set(scannerType, lockPromise);
try {
return await fn();
} finally {
resolve!();
if (scannerLocks.get(scannerType) === lockPromise) {
scannerLocks.delete(scannerType);
}
}
}
// Track in-progress scans per image to prevent duplicate scans
// Key: "{scannerType}:{imageName}", Value: Promise that resolves to the scan result
@@ -249,6 +273,71 @@ async function ensureScannerImage(
}
}
// Extract JSON object from raw scanner output that may contain non-JSON content
// (binary Docker stream headers, warning lines, control characters)
function extractJson(output: string): string {
const firstBrace = output.indexOf('{');
const lastBrace = output.lastIndexOf('}');
if (firstBrace === -1 || lastBrace === -1 || lastBrace <= firstBrace) {
throw new Error('No JSON object found in scanner output');
}
return output.slice(firstBrace, lastBrace + 1);
}
/**
* Sanitize control characters inside JSON string values that would cause parse failures.
* Some scanners (Grype) may include raw control chars (newlines, tabs, null bytes)
* in vulnerability descriptions that aren't properly JSON-escaped.
*/
function sanitizeJsonString(json: string): string {
// Replace unescaped control characters (0x00-0x1F) inside JSON string values
// by walking through the string and tracking whether we're inside a quoted string
let result = '';
let inString = false;
let escaped = false;
let sanitized = 0;
for (let i = 0; i < json.length; i++) {
const ch = json.charCodeAt(i);
if (escaped) {
result += json[i];
escaped = false;
continue;
}
if (inString) {
if (ch === 0x5C) { // backslash
result += json[i];
escaped = true;
} else if (ch === 0x22) { // closing quote
result += json[i];
inString = false;
} else if (ch < 0x20) {
// Control character inside a string - escape it
if (ch === 0x0A) result += '\\n';
else if (ch === 0x0D) result += '\\r';
else if (ch === 0x09) result += '\\t';
else result += `\\u${ch.toString(16).padStart(4, '0')}`;
sanitized++;
} else {
result += json[i];
}
} else {
if (ch === 0x22) { // opening quote
inString = true;
}
result += json[i];
}
}
if (sanitized > 0) {
console.warn(`[Scanner] Sanitized ${sanitized} control characters in JSON output`);
}
return result;
}
// Parse Grype JSON output
function parseGrypeOutput(output: string): { vulnerabilities: Vulnerability[]; summary: VulnerabilitySeverity } {
const vulnerabilities: Vulnerability[] = [];
@@ -263,10 +352,10 @@ function parseGrypeOutput(output: string): { vulnerabilities: Vulnerability[]; s
console.log('[Grype] Raw output length:', output.length);
console.log('[Grype] Output starts with:', output.slice(0, 200));
console.log('[Grype] Output ends with:', JSON.stringify(output.slice(-50)));
try {
const data = JSON.parse(output);
console.log('[Grype] Parsed JSON, matches count:', data.matches?.length || 0);
const data = JSON.parse(sanitizeJsonString(extractJson(output)));
if (data.matches) {
for (const match of data.matches) {
@@ -295,9 +384,9 @@ function parseGrypeOutput(output: string): { vulnerabilities: Vulnerability[]; s
} catch (error) {
const errorMsg = error instanceof Error ? error.message : String(error);
console.error('[Grype] Failed to parse output:', errorMsg);
if (output.length > 0) {
console.error('[Grype] Output preview:', output.slice(0, 200));
}
console.error('[Grype] Output length:', output.length);
console.error('[Grype] First 200 chars:', output.slice(0, 200));
console.error('[Grype] Last 200 chars:', output.slice(-200));
// Check if output looks like an error message from grype
const firstLine = output.split('\n')[0].trim();
if (firstLine && !firstLine.startsWith('{')) {
@@ -306,7 +395,6 @@ function parseGrypeOutput(output: string): { vulnerabilities: Vulnerability[]; s
throw new Error('Failed to parse scanner output - ensure CLI args include "-o json"');
}
console.log('[Grype] Parsed vulnerabilities:', vulnerabilities.length);
return { vulnerabilities, summary };
}
@@ -323,7 +411,7 @@ function parseTrivyOutput(output: string): { vulnerabilities: Vulnerability[]; s
};
try {
const data = JSON.parse(output);
const data = JSON.parse(sanitizeJsonString(extractJson(output)));
const results = data.Results || [];
for (const result of results) {
@@ -354,9 +442,9 @@ function parseTrivyOutput(output: string): { vulnerabilities: Vulnerability[]; s
} catch (error) {
const errorMsg = error instanceof Error ? error.message : String(error);
console.error('[Trivy] Failed to parse output:', errorMsg);
if (output.length > 0) {
console.error('[Trivy] Output preview:', output.slice(0, 200));
}
console.error('[Trivy] Output length:', output.length);
console.error('[Trivy] First 32 bytes (hex):', Buffer.from(output.slice(0, 32)).toString('hex'));
console.error('[Trivy] Full output:', output);
// Check if output looks like an error message from trivy
const firstLine = output.split('\n')[0].trim();
if (firstLine && !firstLine.startsWith('{')) {
@@ -469,21 +557,26 @@ async function runScannerContainerImpl(
cmd: string[],
envId?: number,
onOutput?: (line: string) => void
): Promise<string> {
// Serialize scans of the same type to avoid DB lock conflicts and re-downloads
return withScannerLock(scannerType, () =>
runScannerContainerCore(scannerImage, scannerType, imageName, cmd, envId, onOutput)
);
}
async function runScannerContainerCore(
scannerImage: string,
scannerType: 'grype' | 'trivy',
imageName: string,
cmd: string[],
envId?: number,
onOutput?: (line: string) => void
): Promise<string> {
console.log(`[Scanner] Starting ${scannerType} scan for image: ${imageName}, envId: ${envId ?? 'local'}`);
// Check if another scanner of the same type is already running
// If so, use a unique cache subdirectory to avoid lock conflicts
const currentCount = runningScanners.get(scannerType) || 0;
const scanId = currentCount > 0 ? `-${Date.now()}-${Math.random().toString(36).slice(2, 8)}` : '';
// Increment running counter
runningScanners.set(scannerType, currentCount + 1);
// Configure volume mount based on scanner type
// Use a unique subdirectory if another scan is in progress
// Always use the base cache path — serial lock prevents concurrent conflicts
const basePath = scannerType === 'grype' ? '/cache/grype' : '/cache/trivy';
const dbPath = scanId ? `${basePath}${scanId}` : basePath;
const dbPath = basePath;
// Detect the host Docker socket path based on connection type
// For local socket environments, detect the actual host socket path (handles rootless Docker)
@@ -547,60 +640,47 @@ async function runScannerContainerImpl(
console.log(`[Scanner] Container bind mounts: ${JSON.stringify(binds)}`);
// Environment variables to ensure scanners use the correct cache path
// For concurrent scans, use a unique subdirectory
const envVars = scannerType === 'grype'
? [`GRYPE_DB_CACHE_DIR=${dbPath}`]
: [`TRIVY_CACHE_DIR=${dbPath}`];
if (scanId) {
console.log(`[Scanner] Concurrent scan detected - using unique cache dir: ${dbPath}`);
}
console.log(`[Scanner] Running ${scannerType} with cache mounted at ${basePath}`);
console.log(`[Scanner] Container command: ${cmd.join(' ')}`);
if (containerUser) {
console.log(`[Scanner] Running scanner container as UID ${containerUser} to match socket owner`);
}
try {
// Run the scanner container
const output = await runContainerWithStreaming({
image: scannerImage,
cmd,
binds,
env: envVars,
name: `dockhand-${scannerType}-${Date.now()}`,
user: containerUser,
envId,
onStderr: (data) => {
// Stream stderr lines for real-time progress output
const lines = data.split('\n');
for (const line of lines) {
if (line.trim()) {
onOutput?.(line);
}
// Run the scanner container with a 10-minute timeout to prevent indefinite hangs
const output = await runContainerWithStreaming({
image: scannerImage,
cmd,
binds,
env: envVars,
name: `dockhand-${scannerType}-${Date.now()}`,
user: containerUser,
envId,
timeout: 600_000, // 10 minutes
onStderr: (data) => {
// Stream stderr lines for real-time progress output
const lines = data.split('\n');
for (const line of lines) {
if (line.trim()) {
onOutput?.(line);
}
}
});
console.log(`[Scanner] ${scannerType} container completed, output length: ${output.length}`);
if (output.length === 0) {
console.error(`[Scanner] WARNING: Empty output from ${scannerType} container`);
console.error(`[Scanner] This may indicate the scanner couldn't access Docker socket`);
console.error(`[Scanner] Host socket path used: ${hostSocketPath}`);
} else if (output.length < 100) {
console.log(`[Scanner] ${scannerType} output preview: ${output}`);
}
});
return output;
} finally {
// Decrement running counter
const newCount = (runningScanners.get(scannerType) || 1) - 1;
if (newCount <= 0) {
runningScanners.delete(scannerType);
} else {
runningScanners.set(scannerType, newCount);
}
console.log(`[Scanner] ${scannerType} container completed, output length: ${output.length}`);
if (output.length === 0) {
console.error(`[Scanner] WARNING: Empty output from ${scannerType} container`);
console.error(`[Scanner] This may indicate the scanner couldn't access Docker socket`);
console.error(`[Scanner] Host socket path used: ${hostSocketPath}`);
} else if (output.length < 100) {
console.log(`[Scanner] ${scannerType} output preview: ${output}`);
}
return output;
}
// Scan image with Grype
@@ -967,11 +1047,11 @@ export async function checkScannerUpdates(envId?: number): Promise<{
img.tags?.includes(imageName)
);
if (localImage) {
result[scanner].localDigest = localImage.id?.substring(7, 19); // Short digest
// Note: Remote digest checking would require pulling or using registry API
// For simplicity, we just note that checking for updates requires a pull
result[scanner].hasUpdate = false;
if (localImage && localImage.id) {
const updateResult = await checkImageUpdateAvailable(imageName, localImage.id, envId);
result[scanner].hasUpdate = updateResult.hasUpdate;
result[scanner].localDigest = updateResult.currentDigest;
result[scanner].remoteDigest = updateResult.registryDigest;
}
} catch (error) {
const errorMsg = error instanceof Error ? error.message : String(error);
File diff suppressed because it is too large. Load diff
@@ -22,9 +22,6 @@ import {
inspectContainer,
checkImageUpdateAvailable,
pullImage,
stopContainer,
removeContainer,
createContainer,
getTempImageTag,
isDigestBasedImage,
getImageIdByTag,
@@ -34,6 +31,8 @@ import {
import { sendEventNotification } from '../../notifications';
import { getScannerSettings, scanImage, type VulnerabilitySeverity } from '../../scanner';
import { parseImageNameAndTag, shouldBlockUpdate, combineScanSummaries, isSystemContainer } from './update-utils';
import { recreateContainer, updateStackContainer } from './container-update';
import { pullStackService } from '../../stacks';
interface UpdateInfo {
containerId: string;
@@ -239,9 +238,14 @@ export async function runEnvUpdateCheckJob(
// Get full container config
const inspectData = await inspectContainer(update.containerId, environmentId) as any;
const wasRunning = inspectData.State.Running;
const containerConfig = inspectData.Config;
const hostConfig = inspectData.HostConfig;
// Detect stack membership early (needed for both pull and recreate)
const containerLabels = containerConfig.Labels || {};
const composeProject = containerLabels['com.docker.compose.project'];
const composeService = containerLabels['com.docker.compose.service'];
const composeConfigFiles = containerLabels['com.docker.compose.project.config_files'];
const isStackContainer = !!(composeProject && composeService);
// SAFE-PULL FLOW
if (shouldScan && !isDigestBasedImage(update.imageName)) {
@@ -249,8 +253,17 @@ export async function runEnvUpdateCheckJob(
await log(` Safe-pull with temp tag: ${tempTag}`);
// Step 1: Pull new image
await log(` Pulling ${update.imageName}...`);
await pullImage(update.imageName, () => {}, environmentId);
if (isStackContainer) {
await log(` Pulling via compose (stack: ${composeProject}, service: ${composeService})...`);
const pullResult = await pullStackService(composeProject, composeService, environmentId, composeConfigFiles);
if (!pullResult.success) {
await log(` Compose pull failed, falling back to direct pull...`);
await pullImage(update.imageName, () => {}, environmentId);
}
} else {
await log(` Pulling ${update.imageName}...`);
await pullImage(update.imageName, () => {}, environmentId);
}
// Step 2: Get new image ID
const newImageId = await getImageIdByTag(update.imageName, environmentId);
@@ -359,48 +372,38 @@ export async function runEnvUpdateCheckJob(
} catch { /* ignore cleanup errors */ }
} else {
// Simple pull (no scanning or digest-based image)
await log(` Pulling ${update.imageName}...`);
await pullImage(update.imageName, () => {}, environmentId);
}
// Stop container if running
if (wasRunning) {
await log(` Stopping...`);
await stopContainer(update.containerId, environmentId);
}
// Remove old container
await log(` Removing old container...`);
await removeContainer(update.containerId, true, environmentId);
// Prepare port bindings
const ports: { [key: string]: { HostPort: string } } = {};
if (hostConfig.PortBindings) {
for (const [containerPort, bindings] of Object.entries(hostConfig.PortBindings)) {
if (bindings && (bindings as any[]).length > 0) {
ports[containerPort] = { HostPort: (bindings as any[])[0].HostPort || '' };
if (isStackContainer) {
await log(` Pulling via compose (stack: ${composeProject}, service: ${composeService})...`);
const pullResult = await pullStackService(composeProject, composeService, environmentId, composeConfigFiles);
if (!pullResult.success) {
await log(` Compose pull failed, falling back to direct pull...`);
await pullImage(update.imageName, () => {}, environmentId);
}
} else {
await log(` Pulling ${update.imageName}...`);
await pullImage(update.imageName, () => {}, environmentId);
}
}
// Create new container
await log(` Creating new container...`);
const newContainer = await createContainer({
name: update.containerName,
image: update.imageName,
ports,
volumeBinds: hostConfig.Binds || [],
env: containerConfig.Env || [],
labels: containerConfig.Labels || {},
cmd: containerConfig.Cmd || undefined,
restartPolicy: hostConfig.RestartPolicy?.Name || 'no',
networkMode: hostConfig.NetworkMode || undefined
}, environmentId);
// Start if was running
if (wasRunning) {
await log(` Starting...`);
await newContainer.start();
// Recreate container using compose-native or full recreation
if (isStackContainer) {
await log(` Updating via compose (stack: ${composeProject}, service: ${composeService})`);
const stackSuccess = await updateStackContainer(
composeProject, composeService, environmentId,
(msg) => { log(` ${msg}`); },
composeConfigFiles
);
if (!stackSuccess) {
await log(` Compose file not found, falling back to container recreation...`);
const ok = await recreateContainer(update.containerName, environmentId,
(msg) => { log(` ${msg}`); });
if (!ok) throw new Error('Container recreation failed');
}
} else {
await log(` Recreating standalone container...`);
const ok = await recreateContainer(update.containerName, environmentId,
(msg) => { log(` ${msg}`); });
if (!ok) throw new Error('Container recreation failed');
}
await log(` Updated successfully`);
+2 -2
View File
@@ -213,9 +213,9 @@ export async function adoptStack(
// Get all existing stack sources to check for duplicates
const existingSources = await getStackSources();
// Check if already adopted (by composePath)
// Check if already adopted (by composePath within the same environment)
const alreadyAdopted = existingSources.some(
(s) => s.composePath === stack.composePath
(s) => s.composePath === stack.composePath && s.environmentId === environmentId
);
if (alreadyAdopted) {
+177 -31
View File
@@ -22,7 +22,8 @@ import {
deleteStackEnvVars,
removePendingContainerUpdate,
deleteAutoUpdateSchedule,
getAutoUpdateSetting
getAutoUpdateSetting,
getStackSourceByComposePath
} from './db';
import { unregisterSchedule } from './scheduler';
import { deleteGitStackFiles, parseEnvFileContent } from './git';
@@ -273,21 +274,31 @@ export async function getStackDir(stackName: string, envId?: number | null): Pro
/**
* Find stack directory, checking paths in order:
* 1. New path (envName): $DATA_DIR/stacks/<envName>/<stackName>/
* 2. ID-based path (envId): $DATA_DIR/stacks/<envId>/<stackName>/
* 3. Legacy path: $DATA_DIR/stacks/<stackName>/
* 1. Database: Custom composePath in stackSources table (adopted/imported stacks)
* 2. New path (envName): $DATA_DIR/stacks/<envName>/<stackName>/
* 3. ID-based path (envId): $DATA_DIR/stacks/<envId>/<stackName>/
* 4. Legacy path: $DATA_DIR/stacks/<stackName>/
*
* Automatically looks up environment name from database.
* Always checks legacy path for backwards compatibility with pre-env stacks.
*/
export async function findStackDir(stackName: string, envId?: number | null): Promise<string | null> {
// 1. Check database for custom compose path first (adopted/imported stacks)
const source = await getStackSource(stackName, envId);
if (source?.composePath) {
const customDir = dirname(source.composePath);
if (existsSync(customDir)) {
return customDir;
}
}
const stacksDir = getStacksDir();
// Look up environment name if we have an ID
if (envId) {
const env = await getEnvironment(envId);
// 1. Check new path (with envName)
// 2. Check new path (with envName)
if (env) {
const namePath = join(stacksDir, env.name, stackName);
if (existsSync(namePath)) {
@@ -295,14 +306,14 @@ export async function findStackDir(stackName: string, envId?: number | null): Pr
}
}
// 2. Check ID-based path
// 3. Check ID-based path
const idPath = join(stacksDir, String(envId), stackName);
if (existsSync(idPath)) {
return idPath;
}
}
// 3. Always check legacy path (stacks created before env-scoping was added)
// 4. Always check legacy path (stacks created before env-scoping was added)
const legacyPath = join(stacksDir, stackName);
if (existsSync(legacyPath)) {
return legacyPath;
@@ -339,9 +350,15 @@ export interface GetComposeFileResult {
*/
export async function getStackComposeFile(
stackName: string,
envId?: number | null
envId?: number | null,
composeConfigPath?: string
): Promise<GetComposeFileResult> {
const source = await getStackSource(stackName, envId);
let source = await getStackSource(stackName, envId);
// Fallback: try lookup by compose file path from Docker labels
if (!source && composeConfigPath) {
source = await getStackSourceByComposePath(composeConfigPath, envId);
}
// Case 1: Stack not in database = untracked (discovered from Docker but not imported)
// User must select the compose file location - don't guess from default location
@@ -742,6 +759,10 @@ interface ComposeCommandOptions {
envPath?: string;
/** When true, write non-secret envVars to .env.dockhand override file (git stacks only) */
useOverrideFile?: boolean;
/** Target specific service only (with --no-deps) for single-service updates */
serviceName?: string;
/** Compose filename for Hawser (e.g., "docker-compose.prod.yml") - extracted from composePath */
composeFileName?: string;
}
/**
@@ -767,7 +788,8 @@ async function executeLocalCompose(
workingDir?: string,
customComposePath?: string,
customEnvPath?: string,
useOverrideFile?: boolean
useOverrideFile?: boolean,
serviceName?: string
): Promise<StackOperationResult> {
const logPrefix = `[Stack:${stackName}]`;
@@ -817,20 +839,34 @@ async function executeLocalCompose(
}
}
// Build spawn environment:
// 1. Start with process.env
// 2. Add DOCKER_HOST if specified
// 3. Add non-secret envVars (for backward compat when .env file doesn't exist)
// 4. Add secret envVars (CRITICAL: these are NEVER written to disk, only passed via shell env)
const spawnEnv: Record<string, string> = { ...(process.env as Record<string, string>) };
// Build spawn environment with ONLY essential system variables.
// CRITICAL: Do NOT spread process.env! Docker Compose shell env has higher
// priority than --env-file, so Dockhand's vars would override user's .env values.
const spawnEnv: Record<string, string> = {
PATH: process.env.PATH || '/usr/local/bin:/usr/bin:/bin',
HOME: process.env.HOME || '/root',
};
// Docker connection config
if (dockerHost) {
spawnEnv.DOCKER_HOST = dockerHost;
} else if (process.env.DOCKER_HOST) {
spawnEnv.DOCKER_HOST = process.env.DOCKER_HOST;
}
// Non-secret vars (backup for when .env file doesn't exist yet)
if (envVars) {
// Check if .env file exists on disk (for legacy support decision)
const defaultEnvPath = join(stackDir, '.env');
const hasEnvFile = existsSync(defaultEnvPath) || (customEnvPath && existsSync(customEnvPath));
// LEGACY SUPPORT: Only inject envVars via shell if NO .env file exists
// This is for stacks created with older Dockhand versions that stored env vars
// in DB but didn't write .env files to disk.
// For modern stacks with .env files, Docker Compose reads them via --env-file.
if (!hasEnvFile && envVars) {
Object.assign(spawnEnv, envVars);
}
// SECRET vars: injected via shell environment at runtime (NEVER written to .env file)
// SECRET vars: always injected via shell env (NEVER written to .env files)
if (secretVars) {
Object.assign(spawnEnv, secretVars);
}
@@ -876,8 +912,7 @@ async function executeLocalCompose(
const useStdin = finalComposeContent !== composeContent;
const args = ['docker', 'compose', '-p', stackName, '-f', useStdin ? '-' : composeFile];
// Always auto-detect .env in compose directory
const defaultEnvPath = join(stackDir, '.env');
// Always auto-detect .env in compose directory (defaultEnvPath already defined above)
if (existsSync(defaultEnvPath)) {
args.push('--env-file', defaultEnvPath);
}
@@ -909,6 +944,10 @@ async function executeLocalCompose(
case 'up':
args.push('up', '-d', '--remove-orphans');
if (forceRecreate) args.push('--force-recreate');
// If targeting a specific service, only update that service
if (serviceName) {
args.push(serviceName);
}
break;
case 'down':
args.push('down');
@@ -925,6 +964,10 @@ async function executeLocalCompose(
break;
case 'pull':
args.push('pull');
// If targeting a specific service, pull only that service
if (serviceName) {
args.push(serviceName);
}
break;
}
@@ -938,6 +981,7 @@ async function executeLocalCompose(
console.log(`${logPrefix} DOCKER_HOST:`, dockerHost || '(local socket)');
console.log(`${logPrefix} Force recreate:`, forceRecreate ?? false);
console.log(`${logPrefix} Remove volumes:`, removeVolumes ?? false);
console.log(`${logPrefix} Service name:`, serviceName ?? '(all services)');
console.log(`${logPrefix} Env vars count:`, envVars ? Object.keys(envVars).length : 0);
if (envVars && Object.keys(envVars).length > 0) {
console.log(`${logPrefix} Env vars being injected (masked):`, JSON.stringify(maskSecrets(envVars), null, 2));
@@ -1062,7 +1106,9 @@ async function executeComposeViaHawser(
secretVars?: Record<string, string>,
forceRecreate?: boolean,
removeVolumes?: boolean,
stackFiles?: Record<string, string>
stackFiles?: Record<string, string>,
serviceName?: string,
composeFileName?: string
): Promise<StackOperationResult> {
const logPrefix = `[Stack:${stackName}]`;
// Import dockerFetch dynamically to avoid circular dependency
@@ -1080,6 +1126,8 @@ async function executeComposeViaHawser(
console.log(`${logPrefix} Environment ID:`, envId);
console.log(`${logPrefix} Force recreate:`, forceRecreate ?? false);
console.log(`${logPrefix} Remove volumes:`, removeVolumes ?? false);
console.log(`${logPrefix} Service name:`, serviceName ?? '(all services)');
console.log(`${logPrefix} Compose filename:`, composeFileName ?? '(auto-detect)');
console.log(`${logPrefix} Non-secret env vars count:`, envVars ? Object.keys(envVars).length : 0);
console.log(`${logPrefix} Secret env vars count:`, secretCount);
if (allEnvVars && Object.keys(allEnvVars).length > 0) {
@@ -1128,11 +1176,13 @@ async function executeComposeViaHawser(
operation,
projectName: stackName,
composeFile: composeContent,
composeFileName, // Explicit compose filename to use (e.g., "docker-compose.prod.yml")
envVars: allEnvVars, // All vars (including secrets) - Hawser injects via shell env
files, // Files including .env (secrets NOT in .env file)
forceRecreate: forceRecreate || false,
removeVolumes: removeVolumes || false,
registries // Registry credentials for docker login
registries, // Registry credentials for docker login
serviceName // Target specific service only (with --no-deps)
});
console.log(`${logPrefix} Sending request to Hawser agent...`);
@@ -1198,7 +1248,7 @@ async function executeComposeCommand(
envVars?: Record<string, string>,
secretVars?: Record<string, string>
): Promise<StackOperationResult> {
const { stackName, envId, forceRecreate, removeVolumes, stackFiles, workingDir, composePath, envPath, useOverrideFile } = options;
const { stackName, envId, forceRecreate, removeVolumes, stackFiles, workingDir, composePath, envPath, useOverrideFile, serviceName, composeFileName } = options;
// Get environment configuration
const env = envId ? await getEnvironment(envId) : null;
@@ -1219,7 +1269,8 @@ async function executeComposeCommand(
workingDir,
composePath,
envPath,
useOverrideFile
useOverrideFile,
serviceName
);
}
@@ -1251,7 +1302,9 @@ async function executeComposeCommand(
secretVars,
forceRecreate,
removeVolumes,
stackFiles
stackFiles,
serviceName,
composeFileName
);
}
@@ -1281,7 +1334,8 @@ async function executeComposeCommand(
workingDir,
composePath,
envPath,
useOverrideFile
useOverrideFile,
serviceName
);
}
@@ -1301,7 +1355,8 @@ async function executeComposeCommand(
workingDir,
composePath,
envPath,
useOverrideFile
useOverrideFile,
serviceName
);
}
}
@@ -1530,9 +1585,10 @@ export interface RequireComposeResult {
*/
async function requireComposeFile(
stackName: string,
envId?: number | null
envId?: number | null,
composeConfigPath?: string
): Promise<RequireComposeResult> {
const composeResult = await getStackComposeFile(stackName, envId);
const composeResult = await getStackComposeFile(stackName, envId, composeConfigPath);
// If compose file not found, return info about what's needed
if (!composeResult.success) {
@@ -2011,7 +2067,9 @@ export async function deployStack(options: DeployStackOptions): Promise<StackOpe
workingDir,
composePath: actualComposePath,
envPath: actualEnvPath,
useOverrideFile: isGitStack
useOverrideFile: isGitStack,
// Pass compose filename for Hawser (extracted from path or provided explicitly)
composeFileName: composeFileName || (actualComposePath ? basename(actualComposePath) : undefined)
},
compose,
isGitStack ? dbNonSecretVars : undefined,
@@ -2056,6 +2114,93 @@ export async function pullStackImages(
);
}
/**
* Pull image for a specific service within a stack using docker compose pull <service>.
* This is the Compose-native approach to pulling images for auto-updates.
*
* @param stackName - The compose project name
* @param serviceName - The service name to pull
* @param envId - Optional environment ID
* @returns Operation result
*/
export async function pullStackService(
stackName: string,
serviceName: string,
envId?: number | null,
composeConfigPath?: string
): Promise<StackOperationResult> {
const result = await requireComposeFile(stackName, envId, composeConfigPath);
if (!result.success) {
return {
success: false,
error: result.error || `Compose file not found for stack "${stackName}"`
};
}
return executeComposeCommand(
'pull',
{
stackName,
envId,
workingDir: result.stackDir,
composePath: result.composePath,
envPath: result.envPath,
serviceName
},
result.content!,
result.nonSecretVars,
result.secretVars
);
}
/**
* Update a specific service within a stack using docker compose up -d --no-deps.
* Docker Compose detects image changes naturally (the image is pulled beforehand),
* so --force-recreate is not needed and can cause permission issues on bind mounts.
* This preserves all compose configuration (static IPs, network aliases, etc.) while only
* recreating the specified service when its image has changed.
*
* @param stackName - The compose project name
* @param serviceName - The service name to update
* @param envId - Optional environment ID
* @returns Operation result
*/
export async function updateStackService(
stackName: string,
serviceName: string,
envId?: number | null,
composeConfigPath?: string
): Promise<StackOperationResult> {
const result = await requireComposeFile(stackName, envId, composeConfigPath);
if (!result.success) {
return {
success: false,
error: result.error || `Compose file not found for stack "${stackName}"`
};
}
// Don't use forceRecreate - Docker Compose will detect the image change
// naturally since the image was already pulled before this function is called.
// Using forceRecreate can cause permission issues on bind mounts.
// This matches the behavior of: docker compose pull && docker compose up -d
return executeComposeCommand(
'up',
{
stackName,
envId,
workingDir: result.stackDir,
composePath: result.composePath,
envPath: result.envPath,
serviceName
},
result.content!,
result.nonSecretVars,
result.secretVars
);
}
// =============================================================================
// ENVIRONMENT VARIABLE HELPERS
// =============================================================================
@@ -2187,3 +2332,4 @@ export async function saveStackEnvVars(
// They can be removed once all imports are updated
export type { StackOperationResult as CreateStackResult };