Repository navigation
Expand file tree
/
Copy pathdocker.ts
More file actions
405 lines (353 loc) · 14.7 KB
/
Copy pathdocker.ts
File metadata and controls
405 lines (353 loc) · 14.7 KB
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
// SPDX-License-Identifier: AGPL-3.0-only
// Copyright (C) 2026 Giancarlo Erra - Altaire Limited
import { execFile } from "node:child_process";
import { promisify } from "node:util";
import {
OLLAMA_CONTAINER_NAME,
OLLAMA_HOST,
OLLAMA_IMAGE,
OLLAMA_PORT,
QDRANT_API_KEY,
QDRANT_CONTAINER_NAME,
QDRANT_GRPC_PORT,
QDRANT_HOST,
QDRANT_IMAGE,
QDRANT_MODE,
QDRANT_PORT,
QDRANT_URL,
} from "../constants.js";
import { logger } from "./logger.js";
const execFileAsync = promisify(execFile);
/** Callback for reporting infrastructure setup progress to the user */
export type InfraProgressCallback = (message: string) => void;
async function run(cmd: string, args: string[], timeoutMs = 120_000): Promise<{ stdout: string; stderr: string }> {
try {
return await execFileAsync(cmd, args, { timeout: timeoutMs });
} catch (err: unknown) {
const e = err as { stdout?: string; stderr?: string; message?: string; killed?: boolean };
if (e.killed) {
throw new Error(`Command timed out after ${timeoutMs / 1000}s: ${cmd} ${args.join(" ")}`);
}
throw new Error(`Command failed: ${cmd} ${args.join(" ")}\n${e.stderr || e.message}`);
}
}
// ── Docker ────────────────────────────────────────────────────────────────
/** Check if Docker CLI is reachable */
export async function isDockerAvailable(): Promise<boolean> {
try {
await run("docker", ["info"]);
return true;
} catch {
return false;
}
}
// ── Qdrant ────────────────────────────────────────────────────────────────
/** Check if the Qdrant Docker image is pulled */
export async function isQdrantImagePresent(): Promise<boolean> {
try {
const { stdout } = await run("docker", ["images", "--format", "{{.Repository}}:{{.Tag}}", QDRANT_IMAGE]);
return stdout.includes("qdrant/qdrant");
} catch {
return false;
}
}
/** Pull the Qdrant Docker image (may take several minutes on first launch) */
export async function pullQdrantImage(onProgress?: InfraProgressCallback): Promise<void> {
onProgress?.("Downloading Qdrant Docker image (first time only, may take a few minutes)...");
logger.info("Pulling Qdrant Docker image", { image: QDRANT_IMAGE });
try {
await run("docker", ["pull", QDRANT_IMAGE], 10 * 60_000); // 10 minute timeout for image pull
} catch (error) {
const msg = error instanceof Error ? error.message : String(error);
if (msg.includes("timed out")) {
throw new Error(
`Qdrant image download timed out after 10 minutes. Check your network connection and try again.`,
);
}
throw new Error(
`Failed to download Qdrant image. Check your network connection and available disk space.\nDetails: ${msg}`,
);
}
}
/** Check if the Qdrant container is running */
export async function isQdrantRunning(): Promise<boolean> {
try {
const { stdout } = await run("docker", [
"ps", "--filter", `name=${QDRANT_CONTAINER_NAME}`, "--format", "{{.Names}}",
]);
return stdout.trim().includes(QDRANT_CONTAINER_NAME);
} catch {
return false;
}
}
/** Check if the Qdrant container exists (even if stopped) */
async function doesQdrantContainerExist(): Promise<boolean> {
try {
const { stdout } = await run("docker", [
"ps", "-a", "--filter", `name=${QDRANT_CONTAINER_NAME}`, "--format", "{{.Names}}",
]);
return stdout.trim().includes(QDRANT_CONTAINER_NAME);
} catch {
return false;
}
}
/** Start the Qdrant container, creating it if necessary */
export async function startQdrant(onProgress?: InfraProgressCallback): Promise<void> {
if (await isQdrantRunning()) return;
if (await doesQdrantContainerExist()) {
onProgress?.("Starting existing Qdrant container...");
logger.info("Starting existing Qdrant container");
await run("docker", ["start", QDRANT_CONTAINER_NAME]);
onProgress?.("Waiting for Qdrant to be ready...");
await waitForService(`http://${QDRANT_HOST}:${QDRANT_PORT}/healthz`, "Qdrant");
return;
}
onProgress?.("Creating and starting Qdrant container...");
logger.info("Creating and starting Qdrant container", { port: QDRANT_PORT });
try {
await run("docker", [
"run", "-d",
"--name", QDRANT_CONTAINER_NAME,
"-p", `${QDRANT_PORT}:6333`,
"-p", `${QDRANT_GRPC_PORT}:6334`,
"-v", "socraticode_qdrant_data:/qdrant/storage",
"--restart", "unless-stopped",
QDRANT_IMAGE,
]);
} catch (error) {
const msg = error instanceof Error ? error.message : String(error);
throw new Error(
`Failed to start Qdrant container. Is port ${QDRANT_PORT} already in use?\nDetails: ${msg}`,
);
}
onProgress?.("Waiting for Qdrant to be ready...");
await waitForService(`http://${QDRANT_HOST}:${QDRANT_PORT}/healthz`, "Qdrant");
}
// ── Readiness cache ──────────────────────────────────────────────────────
const READINESS_TTL_MS = 60_000; // 60 seconds
let qdrantReadyAt = 0;
/** Reset the cached readiness state (e.g. after infra error or explicit restart) */
export function resetQdrantReadinessCache(): void {
qdrantReadyAt = 0;
}
/** Ensure Qdrant is ready.
* - managed mode: ensure Docker is available, image is pulled, container is running.
* - external mode: validate config and health-check the remote endpoint.
*/
export async function ensureQdrantReady(onProgress?: InfraProgressCallback): Promise<{ started: boolean; pulled: boolean }> {
// Fast path: recently verified as ready
if (Date.now() - qdrantReadyAt < READINESS_TTL_MS) {
return { started: false, pulled: false };
}
if (QDRANT_MODE === "external") {
return ensureExternalQdrantReady(onProgress);
}
// ── managed mode: Docker-managed container ────────────────────────────
let pulled = false;
let started = false;
if (!(await isDockerAvailable())) {
throw new Error(
"Docker is not available. Please install Docker Desktop (https://www.docker.com/products/docker-desktop/) and make sure it is running.",
);
}
onProgress?.("Checking Qdrant vector database...");
if (!(await isQdrantImagePresent())) {
await pullQdrantImage(onProgress);
pulled = true;
}
if (!(await isQdrantRunning())) {
await startQdrant(onProgress);
started = true;
}
qdrantReadyAt = Date.now();
return { started, pulled };
}
/** Validate config and health-check an externally managed Qdrant instance. */
async function ensureExternalQdrantReady(onProgress?: InfraProgressCallback): Promise<{ started: boolean; pulled: boolean }> {
// Require at least one of: QDRANT_URL or a non-default QDRANT_HOST
if (!QDRANT_URL && QDRANT_HOST === "localhost") {
throw new Error(
"QDRANT_MODE=external requires QDRANT_URL (e.g. https://xyz.aws.cloud.qdrant.io:6333) " +
"or QDRANT_HOST to be set to your remote Qdrant server's hostname.",
);
}
const baseUrl = QDRANT_URL ? QDRANT_URL.replace(/\/$/, "") : `http://${QDRANT_HOST}:${QDRANT_PORT}`;
const healthUrl = `${baseUrl}/healthz`;
// If an api-key is configured, refuse to send it over a non-TLS connection.
// Localhost loopback URLs are accepted because some users run authenticated
// Qdrant locally during development. The URL is parsed (rather than checked
// with startsWith) so that hostnames like "http://localhost.evil.com" are
// not mistaken for loopback. Placed before the try/catch so the specific
// error is not masked by the generic "Cannot reach" message below.
if (QDRANT_API_KEY) {
let parsed: URL | null = null;
try {
parsed = new URL(baseUrl);
} catch {
/* fall through: the unparseable URL will fail the reachability check */
}
const isHttps = parsed?.protocol === "https:";
const isLoopback = parsed
? ["localhost", "127.0.0.1", "[::1]"].includes(parsed.hostname)
: false;
if (parsed && !isHttps && !isLoopback) {
throw new Error(
`QDRANT_API_KEY is set but ${baseUrl} is not HTTPS. ` +
"Refusing to send the API key over a non-TLS connection. " +
"Use https://... for remote or cloud Qdrant (a localhost URL is also accepted for local development).",
);
}
}
onProgress?.(`Checking external Qdrant at ${baseUrl}...`);
logger.info("Checking external Qdrant", { url: baseUrl });
// Qdrant Cloud requires authentication on every endpoint, including /healthz
// (returns 403 without an api-key header). Locally run Qdrant typically does
// not, so the header is sent only when QDRANT_API_KEY is configured.
const init: RequestInit | undefined = QDRANT_API_KEY
? { headers: { "api-key": QDRANT_API_KEY } }
: undefined;
try {
// 5 retries × 1 s — fast fail for misconfiguration, brief grace for transient flakiness
await waitForService(healthUrl, "Qdrant", 5, 1000, init);
} catch {
throw new Error(
`Cannot reach external Qdrant at ${baseUrl}.\n` +
"Verify that QDRANT_URL (or QDRANT_HOST/QDRANT_PORT) and QDRANT_API_KEY (if required) are correct and the server is reachable.",
);
}
qdrantReadyAt = Date.now();
return { started: false, pulled: false };
}
// ── Ollama ────────────────────────────────────────────────────────────────
/** Check if the Ollama Docker image is pulled */
export async function isOllamaImagePresent(): Promise<boolean> {
try {
const { stdout } = await run("docker", ["images", "--format", "{{.Repository}}:{{.Tag}}", OLLAMA_IMAGE]);
return stdout.includes("ollama/ollama");
} catch {
return false;
}
}
/** Pull the Ollama Docker image (may take several minutes on first launch) */
export async function pullOllamaImage(onProgress?: InfraProgressCallback): Promise<void> {
onProgress?.("Downloading Ollama Docker image (first time only, may take a few minutes)...");
logger.info("Pulling Ollama Docker image", { image: OLLAMA_IMAGE });
try {
await run("docker", ["pull", OLLAMA_IMAGE], 10 * 60_000); // 10 minute timeout for image pull
} catch (error) {
const msg = error instanceof Error ? error.message : String(error);
if (msg.includes("timed out")) {
throw new Error(
`Ollama image download timed out after 10 minutes. Check your network connection and try again.`,
);
}
throw new Error(
`Failed to download Ollama image. Check your network connection and available disk space.\nDetails: ${msg}`,
);
}
}
/** Check if the Ollama container is running */
export async function isOllamaRunning(): Promise<boolean> {
try {
const { stdout } = await run("docker", [
"ps", "--filter", `name=${OLLAMA_CONTAINER_NAME}`, "--format", "{{.Names}}",
]);
return stdout.trim().includes(OLLAMA_CONTAINER_NAME);
} catch {
return false;
}
}
/** Check if the Ollama container exists (even if stopped) */
async function doesOllamaContainerExist(): Promise<boolean> {
try {
const { stdout } = await run("docker", [
"ps", "-a", "--filter", `name=${OLLAMA_CONTAINER_NAME}`, "--format", "{{.Names}}",
]);
return stdout.trim().includes(OLLAMA_CONTAINER_NAME);
} catch {
return false;
}
}
/** Start the Ollama container, creating it if necessary */
export async function startOllama(onProgress?: InfraProgressCallback): Promise<void> {
if (await isOllamaRunning()) return;
if (await doesOllamaContainerExist()) {
onProgress?.("Starting existing Ollama container...");
logger.info("Starting existing Ollama container");
await run("docker", ["start", OLLAMA_CONTAINER_NAME]);
onProgress?.("Waiting for Ollama to be ready...");
await waitForService(`${OLLAMA_HOST}/api/tags`, "Ollama");
return;
}
onProgress?.("Creating and starting Ollama container...");
logger.info("Creating and starting Ollama container", { port: OLLAMA_PORT });
try {
await run("docker", [
"run", "-d",
"--name", OLLAMA_CONTAINER_NAME,
"-p", `${OLLAMA_PORT}:11434`,
"-v", "socraticode_ollama_data:/root/.ollama",
"--restart", "unless-stopped",
OLLAMA_IMAGE,
]);
} catch (error) {
const msg = error instanceof Error ? error.message : String(error);
throw new Error(
`Failed to start Ollama container. Is port ${OLLAMA_PORT} already in use?\nDetails: ${msg}`,
);
}
onProgress?.("Waiting for Ollama to be ready...");
await waitForService(`${OLLAMA_HOST}/api/tags`, "Ollama");
}
let ollamaContainerReadyAt = 0;
/** Reset the cached readiness state for Ollama container */
export function resetOllamaContainerReadinessCache(): void {
ollamaContainerReadyAt = 0;
}
/** Ensure Docker is available, Ollama image is pulled, and Ollama container is running */
export async function ensureOllamaContainerReady(onProgress?: InfraProgressCallback): Promise<{ started: boolean; pulled: boolean }> {
// Fast path: recently verified as ready
if (Date.now() - ollamaContainerReadyAt < READINESS_TTL_MS) {
return { started: false, pulled: false };
}
let pulled = false;
let started = false;
if (!(await isDockerAvailable())) {
throw new Error(
"Docker is not available. Please install Docker Desktop (https://www.docker.com/products/docker-desktop/) and make sure it is running.",
);
}
onProgress?.("Checking Ollama embedding service...");
if (!(await isOllamaImagePresent())) {
await pullOllamaImage(onProgress);
pulled = true;
}
if (!(await isOllamaRunning())) {
await startOllama(onProgress);
started = true;
}
ollamaContainerReadyAt = Date.now();
return { started, pulled };
}
// ── Shared ────────────────────────────────────────────────────────────────
/** Wait for an HTTP service to respond with 200.
*
* `init` is forwarded to fetch(), allowing callers to attach headers (e.g. an
* `api-key` header for Qdrant Cloud, whose /healthz endpoint requires auth).
*/
async function waitForService(url: string, serviceName: string, retries = 30, delayMs = 1000, init?: RequestInit): Promise<void> {
logger.info(`Waiting for ${serviceName} to be ready`, { url });
for (let i = 0; i < retries; i++) {
try {
const resp = await fetch(url, init);
if (resp.ok) {
logger.info(`${serviceName} is ready`);
return;
}
} catch {
// not ready yet
}
await new Promise((r) => setTimeout(r, delayMs));
}
throw new Error(`${serviceName} did not become ready in time (${retries * delayMs / 1000}s)`);
}