|
| 1 | +import type * as CommandExecutor from "@effect/platform/CommandExecutor" |
| 2 | +import type { PlatformError } from "@effect/platform/Error" |
| 3 | +import type * as FileSystem from "@effect/platform/FileSystem" |
| 4 | +import type * as Path from "@effect/platform/Path" |
| 5 | +import { Effect } from "effect" |
| 6 | + |
| 7 | +import { type DockerProbeOutcome, renderDockerAccessDeniedMessage } from "./controller-docker-diagnostics.js" |
| 8 | +import { resolveConfiguredApiBaseUrl } from "./controller-reachability.js" |
| 9 | +import { |
| 10 | + runCommandCaptureWithFailureOutput, |
| 11 | + runCommandExitCode, |
| 12 | + runCommandWithCapturedOutput |
| 13 | +} from "./frontend-lib/shell/command-runner.js" |
| 14 | +import type { ControllerBootstrapError } from "./host-errors.js" |
| 15 | + |
| 16 | +export type ControllerDockerCommandRuntime = CommandExecutor.CommandExecutor | FileSystem.FileSystem | Path.Path |
| 17 | + |
| 18 | +const controllerBootstrapError = (message: string): ControllerBootstrapError => ({ |
| 19 | + _tag: "ControllerBootstrapError", |
| 20 | + message |
| 21 | +}) |
| 22 | + |
| 23 | +const runExitCode = ( |
| 24 | + command: string, |
| 25 | + args: ReadonlyArray<string> |
| 26 | +): Effect.Effect<number, never, CommandExecutor.CommandExecutor> => |
| 27 | + runCommandExitCode({ |
| 28 | + cwd: process.cwd(), |
| 29 | + command, |
| 30 | + args |
| 31 | + }).pipe( |
| 32 | + Effect.match({ |
| 33 | + onFailure: () => 1, |
| 34 | + onSuccess: (exitCode) => exitCode |
| 35 | + }) |
| 36 | + ) |
| 37 | + |
| 38 | +type ProbeFailure = { |
| 39 | + readonly _tag: "ProbeFailure" |
| 40 | + readonly outcome: DockerProbeOutcome |
| 41 | +} |
| 42 | + |
| 43 | +const captureProbeOutcome = ( |
| 44 | + command: string, |
| 45 | + args: ReadonlyArray<string> |
| 46 | +): Effect.Effect<DockerProbeOutcome, never, CommandExecutor.CommandExecutor> => |
| 47 | + runCommandWithCapturedOutput( |
| 48 | + { cwd: process.cwd(), command, args }, |
| 49 | + [0], |
| 50 | + (exitCode, output): ProbeFailure => ({ |
| 51 | + _tag: "ProbeFailure", |
| 52 | + outcome: { exitCode, stderr: output } |
| 53 | + }) |
| 54 | + ).pipe( |
| 55 | + Effect.match({ |
| 56 | + onFailure: (error) => |
| 57 | + "outcome" in error |
| 58 | + ? error.outcome |
| 59 | + : { exitCode: 127, stderr: String(error) }, |
| 60 | + onSuccess: () => ({ exitCode: 0, stderr: "" }) |
| 61 | + }) |
| 62 | + ) |
| 63 | + |
| 64 | +export const resolveDockerCommand = (): Effect.Effect< |
| 65 | + ReadonlyArray<string>, |
| 66 | + ControllerBootstrapError, |
| 67 | + CommandExecutor.CommandExecutor |
| 68 | +> => |
| 69 | + Effect.gen(function*(_) { |
| 70 | + const directProbe = yield* _(captureProbeOutcome("docker", ["info"])) |
| 71 | + if (directProbe.exitCode === 0) { |
| 72 | + return ["docker"] |
| 73 | + } |
| 74 | + |
| 75 | + const sudoProbe = yield* _(captureProbeOutcome("sudo", ["-n", "docker", "info"])) |
| 76 | + if (sudoProbe.exitCode === 0) { |
| 77 | + return ["sudo", "-n", "docker"] |
| 78 | + } |
| 79 | + |
| 80 | + const dockerHostRaw = process.env["DOCKER_HOST"]?.trim() ?? "" |
| 81 | + const accessDeniedMessage = renderDockerAccessDeniedMessage({ |
| 82 | + directProbe, |
| 83 | + sudoProbe, |
| 84 | + apiBaseUrl: resolveConfiguredApiBaseUrl(), |
| 85 | + dockerHost: dockerHostRaw.length > 0 ? dockerHostRaw : null |
| 86 | + }) |
| 87 | + return yield* _(Effect.fail(controllerBootstrapError(accessDeniedMessage))) |
| 88 | + }) |
| 89 | + |
| 90 | +type DockerInvocation = { |
| 91 | + readonly command: string |
| 92 | + readonly args: ReadonlyArray<string> |
| 93 | +} |
| 94 | + |
| 95 | +export const buildDockerInvocation = ( |
| 96 | + dockerCommand: ReadonlyArray<string>, |
| 97 | + args: ReadonlyArray<string> |
| 98 | +): DockerInvocation => ({ |
| 99 | + command: dockerCommand[0] ?? "docker", |
| 100 | + args: [...dockerCommand.slice(1), ...args] |
| 101 | +}) |
| 102 | + |
| 103 | +export const formatDockerInvocationFailure = ( |
| 104 | + headline: string, |
| 105 | + invocation: DockerInvocation, |
| 106 | + exitCode: number |
| 107 | +): string => |
| 108 | + [ |
| 109 | + headline, |
| 110 | + `Command: ${[invocation.command, ...invocation.args].join(" ")}`, |
| 111 | + `Exit code: ${exitCode}` |
| 112 | + ].join("\n") |
| 113 | + |
| 114 | +const formatDockerInvocationFailureWithOutput = ( |
| 115 | + headline: string, |
| 116 | + invocation: DockerInvocation, |
| 117 | + exitCode: number, |
| 118 | + output: string |
| 119 | +): string => |
| 120 | + [ |
| 121 | + formatDockerInvocationFailure(headline, invocation, exitCode), |
| 122 | + output.trim().length > 0 ? `Output:\n${output.trim()}` : "" |
| 123 | + ].filter((part) => part.length > 0).join("\n") |
| 124 | + |
| 125 | +const resolveDockerInvocation = ( |
| 126 | + args: ReadonlyArray<string> |
| 127 | +): Effect.Effect<DockerInvocation, ControllerBootstrapError, ControllerDockerCommandRuntime> => |
| 128 | + resolveDockerCommand().pipe( |
| 129 | + Effect.map((dockerCommand) => buildDockerInvocation(dockerCommand, args)) |
| 130 | + ) |
| 131 | + |
| 132 | +export const runDockerExitCodeCommand = ( |
| 133 | + args: ReadonlyArray<string> |
| 134 | +): Effect.Effect<number, ControllerBootstrapError, ControllerDockerCommandRuntime> => |
| 135 | + resolveDockerInvocation(args).pipe( |
| 136 | + Effect.flatMap((invocation) => runExitCode(invocation.command, invocation.args)) |
| 137 | + ) |
| 138 | + |
| 139 | +const mapDockerCaptureError = |
| 140 | + (label: string) => (error: ControllerBootstrapError | PlatformError): ControllerBootstrapError => |
| 141 | + error._tag === "ControllerBootstrapError" |
| 142 | + ? error |
| 143 | + : controllerBootstrapError(`${label} failed.\nDetails: ${String(error)}`) |
| 144 | + |
| 145 | +const formatDockerCaptureFailure = ( |
| 146 | + label: string, |
| 147 | + invocation: DockerInvocation, |
| 148 | + exitCode: number, |
| 149 | + output: string, |
| 150 | + shouldIncludeOutput: boolean |
| 151 | +): string => |
| 152 | + shouldIncludeOutput |
| 153 | + ? formatDockerInvocationFailureWithOutput(`${label} failed.`, invocation, exitCode, output) |
| 154 | + : formatDockerInvocationFailure(`${label} failed.`, invocation, exitCode) |
| 155 | + |
| 156 | +const runDockerCaptureWithOutputMode = ( |
| 157 | + args: ReadonlyArray<string>, |
| 158 | + label: string, |
| 159 | + shouldIncludeOutput: boolean |
| 160 | +): Effect.Effect<string, ControllerBootstrapError, ControllerDockerCommandRuntime> => |
| 161 | + resolveDockerInvocation(args).pipe( |
| 162 | + Effect.flatMap((invocation) => |
| 163 | + runCommandCaptureWithFailureOutput( |
| 164 | + { |
| 165 | + cwd: process.cwd(), |
| 166 | + command: invocation.command, |
| 167 | + args: invocation.args |
| 168 | + }, |
| 169 | + [0], |
| 170 | + (exitCode, output) => |
| 171 | + controllerBootstrapError(formatDockerCaptureFailure(label, invocation, exitCode, output, shouldIncludeOutput)) |
| 172 | + ) |
| 173 | + ), |
| 174 | + Effect.mapError(mapDockerCaptureError(label)) |
| 175 | + ) |
| 176 | + |
| 177 | +export const runDockerCapture = ( |
| 178 | + args: ReadonlyArray<string>, |
| 179 | + label: string |
| 180 | +): Effect.Effect<string, ControllerBootstrapError, ControllerDockerCommandRuntime> => |
| 181 | + runDockerCaptureWithOutputMode(args, label, false) |
| 182 | + |
| 183 | +export const runDockerCaptureWithFailureOutput = ( |
| 184 | + args: ReadonlyArray<string>, |
| 185 | + label: string |
| 186 | +): Effect.Effect<string, ControllerBootstrapError, ControllerDockerCommandRuntime> => |
| 187 | + runDockerCaptureWithOutputMode(args, label, true) |
0 commit comments