diff --git a/src/lib/nim.test.ts b/src/lib/nim.test.ts index fa251765334..77219e12565 100644 --- a/src/lib/nim.test.ts +++ b/src/lib/nim.test.ts @@ -268,4 +268,123 @@ describe("nim", () => { } }); }); + + describe("isNgcLoggedIn", () => { + const fs = require("fs"); + const os = require("os"); + + function mockDockerConfig(config: string | null) { + const origReadFileSync = fs.readFileSync; + const origHomedir = os.homedir; + os.homedir = () => "/mock-home"; + if (config === null) { + fs.readFileSync = () => { throw new Error("ENOENT"); }; + } else { + fs.readFileSync = (p: string, ...args: unknown[]) => { + if (typeof p === "string" && p.includes(".docker/config.json")) return config; + return origReadFileSync(p, ...args); + }; + } + return () => { + fs.readFileSync = origReadFileSync; + os.homedir = origHomedir; + }; + } + + it("returns true when credHelpers has nvcr.io", () => { + const restore = mockDockerConfig(JSON.stringify({ credHelpers: { "nvcr.io": "secretservice" } })); + try { + expect(nim.isNgcLoggedIn()).toBe(true); + } finally { + restore(); + } + }); + + it("returns true when auths has nvcr.io with auth field", () => { + const restore = mockDockerConfig(JSON.stringify({ auths: { "nvcr.io": { auth: "dXNlcjpwYXNz" } } })); + try { + expect(nim.isNgcLoggedIn()).toBe(true); + } finally { + restore(); + } + }); + + it("returns true when auths has https://nvcr.io with auth field", () => { + const restore = mockDockerConfig( + JSON.stringify({ auths: { "https://nvcr.io": { auth: "dXNlcjpwYXNz" } } }), + ); + try { + expect(nim.isNgcLoggedIn()).toBe(true); + } finally { + restore(); + } + }); + + it("returns false when auths has nvcr.io but empty entry", () => { + const restore = mockDockerConfig(JSON.stringify({ auths: { "nvcr.io": {} } })); + try { + expect(nim.isNgcLoggedIn()).toBe(false); + } finally { + restore(); + } + }); + + it("returns false when config file is missing", () => { + const restore = mockDockerConfig(null); + try { + expect(nim.isNgcLoggedIn()).toBe(false); + } finally { + restore(); + } + }); + + it("returns false when config has malformed JSON", () => { + const restore = mockDockerConfig("not json"); + try { + expect(nim.isNgcLoggedIn()).toBe(false); + } finally { + restore(); + } + }); + + it("returns false when auths is empty and no credHelpers", () => { + const restore = mockDockerConfig(JSON.stringify({ auths: {} })); + try { + expect(nim.isNgcLoggedIn()).toBe(false); + } finally { + restore(); + } + }); + + it("returns true when empty nvcr.io marker exists and credsStore is set (Docker Desktop)", () => { + const restore = mockDockerConfig( + JSON.stringify({ credsStore: "desktop", auths: { "nvcr.io": {} } }), + ); + try { + expect(nim.isNgcLoggedIn()).toBe(true); + } finally { + restore(); + } + }); + + it("returns false when credsStore is set but no nvcr.io marker (not logged in)", () => { + const restore = mockDockerConfig( + JSON.stringify({ credsStore: "desktop", auths: {} }), + ); + try { + expect(nim.isNgcLoggedIn()).toBe(false); + } finally { + restore(); + } + }); + + it("returns false when empty nvcr.io marker exists but no credsStore", () => { + const restore = mockDockerConfig(JSON.stringify({ auths: { "nvcr.io": {} } })); + try { + expect(nim.isNgcLoggedIn()).toBe(false); + } finally { + restore(); + } + }); + }); }); diff --git a/src/lib/nim.ts b/src/lib/nim.ts index d30db81fa42..d83e6399d26 100644 --- a/src/lib/nim.ts +++ b/src/lib/nim.ts @@ -176,6 +176,47 @@ export function detectGpu(): GpuDetection | null { return null; } +// Check if Docker has stored credentials for nvcr.io. +// Docker Desktop (macOS/Windows/WSL) stores creds in the OS keychain and +// leaves an empty marker entry { "nvcr.io": {} } in auths after a successful +// login. That marker plus a global credsStore is treated as logged in. +export function isNgcLoggedIn(): boolean { + try { + const os = require("os"); + const fs = require("fs"); + const path = require("path"); + const config = path.join(os.homedir(), ".docker", "config.json"); + const data = fs.readFileSync(config, "utf-8"); + const parsed = JSON.parse(data); + if (parsed?.credHelpers?.["nvcr.io"]) return true; + const auths = parsed?.auths || {}; + const entry = auths["nvcr.io"] || auths["https://nvcr.io"]; + if (entry?.auth) return true; + if (entry && parsed?.credsStore) return true; + return false; + } catch { + return false; + } +} + +// NGC expects literal "$oauthtoken" as the username for API key authentication. +export function dockerLoginNgc(apiKey: string): boolean { + const { spawnSync } = require("child_process"); + const result = spawnSync("docker", ["login", "nvcr.io", "-u", "$oauthtoken", "--password-stdin"], { + input: apiKey, + encoding: "utf-8", + stdio: ["pipe", "pipe", "pipe"], + }); + if (result.error) { + console.error(` Docker error: ${result.error.message}`); + return false; + } + if (result.status !== 0 && result.stderr) { + console.error(` Docker login error: ${result.stderr.trim()}`); + } + return result.status === 0; +} + export function pullNimImage(model: string): string { const image = getImageForModel(model); if (!image) { diff --git a/src/lib/onboard.ts b/src/lib/onboard.ts index f4e690c95c4..745d274ddf6 100644 --- a/src/lib/onboard.ts +++ b/src/lib/onboard.ts @@ -4050,6 +4050,34 @@ async function setupNim(gpu) { } model = sel.name; + // Ensure Docker is logged in to NGC registry before pulling NIM images. + if (!nim.isNgcLoggedIn()) { + if (isNonInteractive()) { + console.error( + " Docker is not logged in to nvcr.io. In non-interactive mode, run `docker login nvcr.io` first and retry.", + ); + process.exit(1); + } + console.log(""); + console.log(" NGC API Key required to pull NIM images."); + console.log(" Get one from: https://org.ngc.nvidia.com/setup/api-key"); + console.log(""); + let ngcKey = normalizeCredentialValue(await prompt(" NGC API Key: ", { secret: true })); + if (!ngcKey) { + console.error(" NGC API Key is required for Local NIM."); + process.exit(1); + } + if (!nim.dockerLoginNgc(ngcKey)) { + console.error(" Failed to login to NGC registry. Check your API key and try again."); + console.log(""); + ngcKey = normalizeCredentialValue(await prompt(" NGC API Key: ", { secret: true })); + if (!ngcKey || !nim.dockerLoginNgc(ngcKey)) { + console.error(" NGC login failed. Cannot pull NIM images."); + process.exit(1); + } + } + } + console.log(` Pulling NIM image for ${model}...`); nim.pullNimImage(model); diff --git a/test/onboard-selection.test.ts b/test/onboard-selection.test.ts index 04305e33c10..bd078f9c2ff 100644 --- a/test/onboard-selection.test.ts +++ b/test/onboard-selection.test.ts @@ -3066,6 +3066,7 @@ nimMod.pullNimImage = () => {}; nimMod.containerName = () => "nemoclaw-nim-test"; nimMod.startNimContainerByName = () => "container-123"; nimMod.waitForNimHealth = () => true; +nimMod.isNgcLoggedIn = () => true; // Select option 7 (nim-local), then model 1 const answers = ["7", "1"];