Add a load of fallbacks and rework the docker bits
All checks were successful
Build and deploy / deploy (push) Successful in 1m58s
All checks were successful
Build and deploy / deploy (push) Successful in 1m58s
This commit is contained in:
@@ -18,38 +18,46 @@ export const dockerRouter = createTRPCRouter({
|
||||
|
||||
// curl -fsSL -v -H "Accept: application/json" "hub.docker.com/v2/repositories/linuxserver/code-server/tags?n=25&ordering=last_updated"
|
||||
|
||||
return Promise.all(
|
||||
const dockerInfo = await Promise.all(
|
||||
containers.map(async (container) => {
|
||||
const version = (
|
||||
await docker.getImage(container.Image).inspect()
|
||||
).RepoTags[0]?.split(":")[1];
|
||||
const latest = {
|
||||
version: version,
|
||||
hash: "N/A",
|
||||
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,
|
||||
};
|
||||
if (version && isSemver(version)) {
|
||||
latest.version = await getLatestSemverTag(container.Image);
|
||||
latest.hash = await getLatestHash(
|
||||
`${container.Image.split(":")[0]}:${latest.version}`,
|
||||
);
|
||||
} else {
|
||||
latest.hash = await getLatestHash(container.Image);
|
||||
|
||||
let latest: {
|
||||
hash?: string;
|
||||
tag?: string;
|
||||
} = {
|
||||
hash: "",
|
||||
tag: "",
|
||||
};
|
||||
if (imageName) {
|
||||
latest = await getLatest(imageName, current.tag);
|
||||
}
|
||||
|
||||
return {
|
||||
...container,
|
||||
current: {
|
||||
version,
|
||||
hash: (
|
||||
await docker.getImage(container.Image).inspect()
|
||||
).RepoDigests[0]
|
||||
?.split(":")[1]
|
||||
?.substring(0, 12),
|
||||
},
|
||||
containerName: container.Names[0]?.replace("/", ""),
|
||||
imageName,
|
||||
current,
|
||||
latest,
|
||||
};
|
||||
}),
|
||||
);
|
||||
|
||||
return dockerInfo;
|
||||
}),
|
||||
});
|
||||
|
||||
@@ -58,27 +66,58 @@ function isSemver(tag: string): boolean {
|
||||
return !!semver.valid(removeV);
|
||||
}
|
||||
|
||||
async function getLatestSemverTag(image: string): Promise<string> {
|
||||
async function getLatestSemverTag(image: string, tag?: string): Promise<{ hash: string; tag?: string }> {
|
||||
try {
|
||||
const allTags =
|
||||
await $`bin/regctl tag ls --exclude 'version.*' --exclude 'unstable.*' --exclude '.*rc.*' --exclude 'lib.*' --exclude 'release.*' --exclude 'arm.*
|
||||
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")
|
||||
.slice(-20)
|
||||
.filter((tag) => isSemver(tag));
|
||||
return semver.rsort(semverTags)[0] ?? "Error";
|
||||
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) {
|
||||
return "Error";
|
||||
console.error((ex as unknown as Error).message);
|
||||
return {
|
||||
hash: "Error",
|
||||
tag: "Error",
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
async function getLatestHash(image: string): Promise<string> {
|
||||
async function getLatest(image: string, tag?: string): Promise<{ hash: string; tag?: string }> {
|
||||
try {
|
||||
const latestImage = await $`bin/regctl image digest ${image}`.text();
|
||||
const latestHash = latestImage.split(":")?.[1];
|
||||
return latestHash?.substring(0, 12) ?? "Error";
|
||||
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) {
|
||||
return "Error";
|
||||
console.error((ex as unknown as Error).message);
|
||||
return {
|
||||
hash: "Error",
|
||||
tag: "Error",
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user