124 lines
3.8 KiB
TypeScript
124 lines
3.8 KiB
TypeScript
import Docker from "dockerode";
|
|
import semver from "semver";
|
|
import { $ } from "zx";
|
|
|
|
import { createTRPCRouter, publicProcedure } from "@/server/api/trpc";
|
|
|
|
export const dockerRouter = createTRPCRouter({
|
|
list: publicProcedure.query(async () => {
|
|
const docker = new Docker({ socketPath: "/var/run/docker.sock" });
|
|
const containers = await docker.listContainers();
|
|
|
|
// curl -fsSL -v "https://quay.io/token?service=quay.io&scope=repository:linuxserver/code-server:pull"
|
|
// curl -fsSL -v -H "Accept: application/vnd.docker.distribution.manifest.list.v2+json" -H "Authorization: Bearer djE6bGludXhzZXJ2ZXIvY29kZS1zZXJ2ZXI6MTc0NTQ1NTk1MTY3NjM5ODA4Mg==" quay.io/v2/linuxserver.io/code-server/tags/list
|
|
|
|
// curl -fsSL -v -H "Accept: application/vnd.docker.distribution.manifest.list.v2+json" quay.io/v2/linuxserver.io/code-server/tags/list
|
|
// link: </v2/linuxserver.io/code-server/tags/list?n=100&last=4.18.0-ls180>; rel="next"
|
|
// link: </v2/linuxserver.io/code-server/tags/list?n=100&last=4.90.1-ls217>; rel="next"
|
|
|
|
// curl -fsSL -v -H "Accept: application/json" "hub.docker.com/v2/repositories/linuxserver/code-server/tags?n=25&ordering=last_updated"
|
|
|
|
const dockerInfo = await Promise.all(
|
|
containers.map(async (container) => {
|
|
const imageInspect = await docker.getImage(container.Image).inspect();
|
|
const imageDigest = imageInspect.RepoDigests[0];
|
|
const containerInspect = docker.getContainer(container.Id);
|
|
|
|
let imageTag = imageInspect.RepoTags[0]?.split(":")[1];
|
|
|
|
let imageName = imageDigest?.split("@")?.[0];
|
|
imageName ??= container.Image.split(":")[0] ?? undefined;
|
|
|
|
const imageHash = imageDigest?.split("@")?.[1]?.split(":")?.[1];
|
|
imageTag ??= (await containerInspect.inspect()).Config.Image.split(":")[1] ?? undefined;
|
|
|
|
const current = {
|
|
hash: imageHash?.substring(0, 12),
|
|
tag: imageTag,
|
|
};
|
|
|
|
let latest: {
|
|
hash?: string;
|
|
tag?: string;
|
|
} = {
|
|
hash: "",
|
|
tag: "",
|
|
};
|
|
if (imageName) {
|
|
latest = await getLatest(imageName, current.tag);
|
|
}
|
|
|
|
return {
|
|
containerName: container.Names[0]?.replace("/", ""),
|
|
imageName,
|
|
current,
|
|
latest,
|
|
};
|
|
}),
|
|
);
|
|
|
|
return dockerInfo;
|
|
}),
|
|
});
|
|
|
|
function isSemver(tag: string): boolean {
|
|
const removeV = tag.replace(/^v/i, "");
|
|
return !!semver.valid(removeV);
|
|
}
|
|
|
|
async function getLatestSemverTag(image: string, tag?: string): Promise<{ hash: string; tag?: string }> {
|
|
try {
|
|
const latest = {
|
|
hash: "",
|
|
tag: "",
|
|
};
|
|
|
|
const allTags = await $`bin/regctl tag ls --exclude 'version.*' --exclude 'unstable.*' --exclude '.*rc.*' --exclude 'lib.*' --exclude 'release.*' --exclude 'arm.*
|
|
' --exclude 'amd.*' ${image}`.text();
|
|
const semverTags = allTags.split("\n").filter((tag) => isSemver(tag));
|
|
const newestTag = semver.rsort(semverTags)[0];
|
|
|
|
let imageSh = image;
|
|
if (newestTag) {
|
|
imageSh += `:${newestTag}`;
|
|
latest.tag = newestTag;
|
|
}
|
|
const latestImage = await $`bin/regctl image digest ${imageSh}`.text();
|
|
latest.hash = latestImage.split(":")?.[1]?.substring(0, 12) ?? "Error";
|
|
|
|
return latest;
|
|
} catch (ex) {
|
|
console.error((ex as unknown as Error).message);
|
|
return {
|
|
hash: "Error",
|
|
tag: "Error",
|
|
};
|
|
}
|
|
}
|
|
|
|
async function getLatest(image: string, tag?: string): Promise<{ hash: string; tag?: string }> {
|
|
try {
|
|
let latest: { hash: string; tag?: string } = {
|
|
hash: "",
|
|
tag: tag,
|
|
};
|
|
if (tag && isSemver(tag)) {
|
|
latest = await getLatestSemverTag(image, tag);
|
|
} else {
|
|
let imageSh = image;
|
|
if (tag) {
|
|
imageSh += `:${tag}`;
|
|
}
|
|
const latestImage = await $`bin/regctl image digest ${imageSh}`.text();
|
|
latest.hash = latestImage.split(":")?.[1]?.substring(0, 12) ?? "Error";
|
|
}
|
|
return latest;
|
|
} catch (ex) {
|
|
console.error((ex as unknown as Error).message);
|
|
return {
|
|
hash: "Error",
|
|
tag: "Error",
|
|
};
|
|
}
|
|
}
|