Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 | 6x 6x 57x 57x 53x 4x 54x 54x 52x 38x 14x 2x 38x 38x 20x 16x 22x 22x 12x 12x 10x 10x 7x 3x 1x 2x 10x 10x 10x 18x 1x 6x 11x 11x 1x 10x 10x 4x 6x 24x 24x 34x 34x 22x 22x 22x 22x 2x 6x 5x 5x 6x 24x | /**
* Container runtime detection module.
* Detects Docker or Podman and their compose variants, caching the result per process.
*/
import { spawnSync } from 'child_process';
import { ContainerRuntime, ContainerRuntimeInfo } from './types';
/** Module-level cached runtime info */
let cachedRuntime: ContainerRuntimeInfo | null = null;
/**
* Try running a command and return true if it exits 0.
*/
function commandSucceeds(cmd: string, args: string[]): boolean {
try {
const result = spawnSync(cmd, args, {
stdio: 'pipe',
encoding: 'utf8',
});
return result.status === 0;
} catch {
return false;
}
}
/**
* Try running a command and return its stdout if it exits 0, otherwise undefined.
*/
function commandOutput(cmd: string, args: string[]): string | undefined {
try {
const result = spawnSync(cmd, args, {
stdio: 'pipe',
encoding: 'utf8',
});
if (result.status === 0 && result.stdout) {
return result.stdout.trim();
}
return undefined;
} catch {
return undefined;
}
}
/**
* Extract version string from runtime --version output.
* e.g. "Docker version 24.0.7, build afdd53b" → "24.0.7"
* "podman version 4.9.3" → "4.9.3"
*/
function parseVersion(output: string): string | undefined {
const match = output.match(/(\d+\.\d+\.\d+)/);
return match?.[1];
}
interface ComposeDetection {
cmd: string[];
provider?: 'docker-compose' | 'podman-compose';
version?: string;
}
/** Implementation and version from a compose version banner. */
function describeCompose(cmd: string[], output: string | undefined): ComposeDetection {
if (!output) return { cmd };
return {
cmd,
// `podman compose` delegates to docker-compose or podman-compose; the banner names it.
provider: /podman-compose/i.test(output) ? 'podman-compose' : 'docker-compose',
version: parseVersion(output),
};
}
/**
* Detect the compose command for a given runtime.
* Priority for docker: docker compose → docker-compose
* Priority for podman: podman compose → podman-compose → docker-compose (fallback)
*/
function detectComposeCmd(runtime: ContainerRuntime): ComposeDetection | null {
const runtimeCmd = runtime;
// Try "<runtime> compose version" (compose v2 plugin)
if (commandSucceeds(runtimeCmd, ['compose', 'version'])) {
const cmd = [runtimeCmd, 'compose'];
return describeCompose(cmd, commandOutput(runtimeCmd, ['compose', 'version']));
}
// Try "<runtime>-compose --version" (standalone compose)
const standaloneCompose = `${runtimeCmd}-compose`;
if (commandSucceeds(standaloneCompose, ['--version'])) {
return describeCompose([standaloneCompose], commandOutput(standaloneCompose, ['--version']));
}
// Cross-runtime fallback: try docker-compose as last resort
if (commandSucceeds('docker-compose', ['--version'])) {
return describeCompose(['docker-compose'], commandOutput('docker-compose', ['--version']));
}
return null;
}
/** Whether version `actual` is at least `minimum`; both are `major.minor.patch`. */
function versionAtLeast(actual: string, minimum: string): boolean {
const a = actual.split('.').map(Number);
const m = minimum.split('.').map(Number);
for (let i = 0; i < m.length; i++) {
if (a[i] !== m[i]) return a[i] > m[i];
}
return true;
}
/**
* Why the detected compose cannot run deployments that start the server after a
* one-shot migration (`depends_on` with `condition: service_completed_successfully`),
* or undefined when it can. podman-compose implements the condition from 1.6.0. Docker
* Compose has it from 1.29.0, but the v1 line reached end of life in July 2023 and gets no
* fixes, so Docker deployments require Compose v2.
*/
export function completionDependencyError(info: ContainerRuntimeInfo): string | undefined {
const required = 'Docker Compose v2 or podman-compose 1.6.0 or later';
if (!info.composeCmd || !info.composeVersion) {
return `Could not determine the compose version; PostgreSQL deployments need ${required}.`;
}
const minimum = info.composeProvider === 'podman-compose' ? '1.6.0' : '2.0.0';
if (versionAtLeast(info.composeVersion, minimum)) return undefined;
return `${info.composeProvider} ${info.composeVersion} cannot order the database migration before the server; install ${required}.`;
}
/**
* Perform full container runtime detection.
* Priority: docker > podman.
* Checks runtime availability, daemon status, and compose command.
*/
export function detectContainerRuntime(): ContainerRuntimeInfo {
const runtimes: ContainerRuntime[] = ['docker', 'podman'];
for (const runtime of runtimes) {
const versionOutput = commandOutput(runtime, ['--version']);
if (versionOutput) {
// Runtime binary exists, check if daemon is accessible
const runtimeAvailable = commandSucceeds(runtime, ['info']);
const compose = detectComposeCmd(runtime);
const runtimeVersion = parseVersion(versionOutput);
return {
runtime,
runtimeCmd: runtime,
runtimeAvailable,
composeCmd: compose?.cmd ?? null,
...(compose?.provider && { composeProvider: compose.provider }),
...(compose?.version && { composeVersion: compose.version }),
runtimeVersion,
};
}
}
// No runtime found at all
return {
runtime: 'docker',
runtimeCmd: 'docker',
runtimeAvailable: false,
composeCmd: null,
runtimeVersion: undefined,
};
}
/**
* Get cached container runtime info.
* Detects once per process and caches the result.
*/
export function getContainerRuntime(): ContainerRuntimeInfo {
cachedRuntime ??= detectContainerRuntime();
return cachedRuntime;
}
/**
* Reset the runtime cache. Used in tests to allow re-detection.
*/
export function resetRuntimeCache(): void {
cachedRuntime = null;
}
|