Continue evaluating/reaping images even if one of them fails (#95)

This commit is contained in:
Salah Al Saleh
2022-07-27 09:15:59 -07:00
committed by GitHub
parent 5d3ae58d9c
commit 24aa192358

View File

@@ -39,51 +39,55 @@ async function main(argv): Promise<any> {
const images = await smembersAsync("current.images");
console.log(` there are ${images.length} total images to evaluate`);
for (const image of images) {
const expireAt = await hgetAsync(image, "expires");
try {
const expireAt = await hgetAsync(image, "expires");
if (+expireAt > now) {
const minutesLeft = (+expireAt - now) / 1000 / 60;
console.log(`not expiring ${image} for another ~${Math.round(minutesLeft)} minute(s)`);
continue;
}
const imageAndTag = image.split(":");
const headers = {
"Accept": "application/vnd.docker.distribution.manifest.v2+json",
};
// Get the manifest from the tag
const getOptions = {
method: "HEAD",
uri: `https://ttl.sh/v2/${imageAndTag[0]}/manifests/${imageAndTag[1]}`,
headers,
resolveWithFullResponse: true,
simple: false,
}
const getResponse = await rp(getOptions);
if (getResponse.statusCode == 404) {
if (+expireAt > now) {
const minutesLeft = (+expireAt - now) / 1000 / 60;
console.log(`not expiring ${image} for another ~${Math.round(minutesLeft)} minute(s)`);
continue;
}
const imageAndTag = image.split(":");
const headers = {
"Accept": "application/vnd.docker.distribution.manifest.v2+json",
};
// Get the manifest from the tag
const getOptions = {
method: "HEAD",
uri: `https://ttl.sh/v2/${imageAndTag[0]}/manifests/${imageAndTag[1]}`,
headers,
resolveWithFullResponse: true,
simple: false,
}
const getResponse = await rp(getOptions);
if (getResponse.statusCode == 404) {
await sremAsync("current.images", image);
await delAsync(image);
continue;
}
const deleteURI = `https://ttl.sh/v2/${imageAndTag[0]}/manifests/${getResponse.headers.etag.replace(/"/g,"")}`;
// Remove from the registry
const options = {
method: "DELETE",
uri: deleteURI,
headers,
resolveWithFullResponse: true,
simple: false,
}
await rp(options);
console.log(`expiring ${image}`);
await sremAsync("current.images", image);
await delAsync(image);
continue;
} catch(err) {
console.log(`failed to evaluate image ${image}:`, err);
}
const deleteURI = `https://ttl.sh/v2/${imageAndTag[0]}/manifests/${getResponse.headers.etag.replace(/"/g,"")}`;
// Remove from the registry
const options = {
method: "DELETE",
uri: deleteURI,
headers,
resolveWithFullResponse: true,
simple: false,
}
await rp(options);
console.log(`expiring ${image}`);
await sremAsync("current.images", image);
await delAsync(image);
}
},
start: true,