|
| 1 | +import fs from "fs"; |
| 2 | +import path from "path"; |
| 3 | +import { Compose } from "@dappnode/types"; |
| 4 | + |
| 5 | +/** |
| 6 | + * Given a GitHub release tag (e.g. "v1.17.0", "n8n@2.10.3"), resolves the |
| 7 | + * correct version format by checking the upstream Docker image registry. |
| 8 | + * |
| 9 | + * 1. Finds which compose service uses the given build arg |
| 10 | + * 2. Parses the Dockerfile to extract the Docker image that uses that arg |
| 11 | + * 3. Checks the Docker registry for tag existence (with/without prefix) |
| 12 | + * 4. Returns the version in the format that matches the Docker registry |
| 13 | + */ |
| 14 | +export async function resolveVersionFormat({ |
| 15 | + tag, |
| 16 | + arg, |
| 17 | + compose, |
| 18 | + dir |
| 19 | +}: { |
| 20 | + tag: string; |
| 21 | + arg: string; |
| 22 | + compose: Compose; |
| 23 | + dir: string; |
| 24 | +}): Promise<string> { |
| 25 | + const stripped = stripTagPrefix(tag); |
| 26 | + if (!stripped || stripped === tag) return tag; // No prefix to strip |
| 27 | + |
| 28 | + try { |
| 29 | + const dockerImage = getDockerImageForArg(compose, arg, dir); |
| 30 | + if (!dockerImage) return tag; |
| 31 | + |
| 32 | + const tagExists = await checkDockerTagExists(dockerImage, stripped); |
| 33 | + if (tagExists) return stripped; |
| 34 | + |
| 35 | + return tag; |
| 36 | + } catch (e) { |
| 37 | + console.warn(`Could not resolve version format for ${tag}, using as-is:`, e.message); |
| 38 | + return tag; |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +/** |
| 43 | + * Finds the Docker image that uses a given build arg by parsing the Dockerfile. |
| 44 | + */ |
| 45 | +function getDockerImageForArg( |
| 46 | + compose: Compose, |
| 47 | + arg: string, |
| 48 | + dir: string |
| 49 | +): string | null { |
| 50 | + for (const [, service] of Object.entries(compose.services)) { |
| 51 | + if ( |
| 52 | + typeof service.build !== "string" && |
| 53 | + service.build?.args && |
| 54 | + arg in service.build.args |
| 55 | + ) { |
| 56 | + const buildContext = service.build.context || "."; |
| 57 | + const dockerfileName = service.build.dockerfile || "Dockerfile"; |
| 58 | + const dockerfilePath = path.resolve(dir, buildContext, dockerfileName); |
| 59 | + |
| 60 | + if (!fs.existsSync(dockerfilePath)) continue; |
| 61 | + |
| 62 | + const content = fs.readFileSync(dockerfilePath, "utf-8"); |
| 63 | + return extractImageForArg(content, arg); |
| 64 | + } |
| 65 | + } |
| 66 | + return null; |
| 67 | +} |
| 68 | + |
| 69 | +/** |
| 70 | + * Parses a Dockerfile to find the FROM line that references the given ARG, |
| 71 | + * and extracts the Docker image name (without the tag). |
| 72 | + * |
| 73 | + * Handles patterns like: |
| 74 | + * FROM ethereum/client-go:${UPSTREAM_VERSION} |
| 75 | + * FROM ethereum/client-go:v${UPSTREAM_VERSION} |
| 76 | + * FROM ollama/ollama:${OLLAMA_VERSION#v} |
| 77 | + * FROM statusim/nimbus-eth2:multiarch-${UPSTREAM_VERSION} |
| 78 | + */ |
| 79 | +function extractImageForArg( |
| 80 | + dockerfileContent: string, |
| 81 | + arg: string |
| 82 | +): string | null { |
| 83 | + const lines = dockerfileContent.split("\n"); |
| 84 | + |
| 85 | + for (const line of lines) { |
| 86 | + const trimmed = line.trim(); |
| 87 | + if (!trimmed.startsWith("FROM") || !trimmed.includes(arg)) continue; |
| 88 | + |
| 89 | + // Match: FROM image:tag_pattern (with optional "AS stage") |
| 90 | + const match = trimmed.match(/^FROM\s+([^:\s]+)/i); |
| 91 | + if (match) return match[1]; |
| 92 | + } |
| 93 | + |
| 94 | + return null; |
| 95 | +} |
| 96 | + |
| 97 | +/** |
| 98 | + * Checks if a tag exists on a Docker registry using the Docker Hub v2 API. |
| 99 | + * Supports Docker Hub, ghcr.io, and gcr.io. |
| 100 | + */ |
| 101 | +async function checkDockerTagExists( |
| 102 | + image: string, |
| 103 | + tag: string |
| 104 | +): Promise<boolean> { |
| 105 | + const url = getRegistryTagUrl(image, tag); |
| 106 | + if (!url) return false; |
| 107 | + |
| 108 | + try { |
| 109 | + const response = await fetch(url); |
| 110 | + return response.ok; |
| 111 | + } catch { |
| 112 | + return false; |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +function getRegistryTagUrl(image: string, tag: string): string | null { |
| 117 | + // ghcr.io/org/image -> GitHub Container Registry |
| 118 | + if (image.startsWith("ghcr.io/")) { |
| 119 | + const imagePath = image.replace("ghcr.io/", ""); |
| 120 | + return `https://ghcr.io/v2/${imagePath}/manifests/${tag}`; |
| 121 | + } |
| 122 | + |
| 123 | + // gcr.io/project/image -> Google Container Registry |
| 124 | + if (image.startsWith("gcr.io/")) { |
| 125 | + const imagePath = image.replace("gcr.io/", ""); |
| 126 | + return `https://gcr.io/v2/${imagePath}/manifests/${tag}`; |
| 127 | + } |
| 128 | + |
| 129 | + // Docker Hub: library/image or org/image |
| 130 | + const dockerImage = image.includes("/") ? image : `library/${image}`; |
| 131 | + return `https://registry.hub.docker.com/v2/repositories/${dockerImage}/tags/${tag}`; |
| 132 | +} |
| 133 | + |
| 134 | +function stripTagPrefix(tag: string): string | null { |
| 135 | + const match = tag.match(/(\d+\.\d+\.\d+.*)$/); |
| 136 | + return match ? match[1] : null; |
| 137 | +} |
0 commit comments