docker: rework image pruning

with our new retagging approach, the Digest ID remains <null> because
this is only set by docker if truly fetched from the registry.

this means that redis container always gets removed...
This commit is contained in:
Girish Ramakrishnan
2024-12-14 20:47:35 +01:00
parent bd107e849b
commit 8e6890b4d6
5 changed files with 19 additions and 38 deletions

View File

@@ -22,7 +22,6 @@ const apps = require('./apps.js'),
dashboard = require('./dashboard.js'),
database = require('./database.js'),
debug = require('debug')('box:platform'),
docker = require('./docker.js'),
dockerProxy = require('./dockerproxy.js'),
fs = require('fs'),
infra = require('./infra_version.js'),
@@ -46,37 +45,6 @@ function getStatus() {
return { message: gStatusMessage };
}
async function pruneInfraImages() {
debug('pruneInfraImages: checking existing images');
// cannot blindly remove all unused images since redis image may not be used
const imageRefs = Object.keys(infra.images).map(addon => infra.images[addon]);
const [error, output] = await safe(shell.bash('docker images --digests --format "{{.ID}} {{.Repository}} {{.Tag}} {{.Digest}}"', { encoding: 'utf8' }));
if (error) {
debug(`Failed to list images ${error.message}`);
throw error;
}
const lines = output.trim().split('\n');
for (const imageRef of imageRefs) {
const parsedRef = docker.parseImageRef(imageRef);
for (const line of lines) {
if (!line) continue;
const [, repo, tag, digest] = line.split(' '); // [ ID, Repo, Tag, Digest ]
const normalizedRepo = repo.replace('registry.ipv6.docker.com/', '').replace('registry-1.docker.io/', '').replace('registry.docker.com/', '');
if (!parsedRef.fullRepositoryName.endsWith(normalizedRepo)) continue; // some other repo
if (imageRef === `${repo}:${tag}@${digest}`) continue; // the image we want to keep
const imageIdToPrune = tag === '<none>' ? `${repo}@${digest}` : `${repo}:${tag}`; // untagged, use digest
console.log(`pruneInfraImages: removing unused image of ${imageRef}: ${imageIdToPrune}`);
const [error] = await safe(shell.spawn('docker', [ 'rmi', imageIdToPrune ], {}));
if (error) console.log(`Error removing image ${imageIdToPrune}: ${error.mesage}`);
}
}
}
async function pruneVolumes() {
debug('pruneVolumes: remove all unused local volumes');
@@ -138,11 +106,7 @@ async function onInfraReady(infraChanged) {
debug(`onInfraReady: platform is ready. infra changed: ${infraChanged}`);
gStatusMessage = 'Ready';
if (infraChanged) {
await safe(pruneInfraImages(), { debug }); // ignore error
await safe(pruneVolumes(), { debug }); // ignore error
}
if (infraChanged) await safe(pruneVolumes(), { debug }); // ignore error
await apps.schedulePendingTasks(AuditSource.PLATFORM);
await appTaskManager.start();
}