fix: cleanup stucked containers

This commit is contained in:
Andras Bacsai 2023-01-20 09:40:29 +01:00
parent 8b57a2b055
commit 27021538d8

View File

@ -197,7 +197,13 @@ const host = '0.0.0.0';
await copySSLCertificates(); await copySSLCertificates();
}, 10000); }, 10000);
await Promise.all([getTagsTemplates(), getArch(), getIPAddress(), configureRemoteDockers()]); await Promise.all([
getTagsTemplates(),
getArch(),
getIPAddress(),
configureRemoteDockers(),
cleanupStuckedContainers()
]);
} catch (error) { } catch (error) {
console.error(error); console.error(error);
process.exit(1); process.exit(1);
@ -311,6 +317,42 @@ async function getArch() {
} catch (error) {} } catch (error) {}
} }
async function cleanupStuckedContainers() {
try {
const destinationDockers = await prisma.destinationDocker.findMany();
let enginesDone = new Set();
for (const destination of destinationDockers) {
if (enginesDone.has(destination.engine) || enginesDone.has(destination.remoteIpAddress))
return;
if (destination.engine) {
enginesDone.add(destination.engine);
}
if (destination.remoteIpAddress) {
if (!destination.remoteVerified) continue;
enginesDone.add(destination.remoteIpAddress);
}
const { stdout: containers } = await executeCommand({
dockerId: destination.id,
command: `docker container ps -a --filter "label=coolify.managed=true" --format '{{ .Names}}'`
});
if (containers) {
const containersArray = containers.trim().split('\n');
if (containersArray.length > 0) {
for (const container of containersArray) {
const application = await prisma.application.findFirst({ where: { id: container } });
const service = await prisma.service.findFirst({ where: { id: container } });
const database = await prisma.database.findFirst({ where: { id: container } });
if (!application && !service && !database) {
await executeCommand({ command: `docker container rm -f ${container}` });
}
}
}
}
}
} catch (error) {
console.log(error);
}
}
async function configureRemoteDockers() { async function configureRemoteDockers() {
try { try {
const remoteDocker = await prisma.destinationDocker.findMany({ const remoteDocker = await prisma.destinationDocker.findMany({
@ -543,9 +585,13 @@ async function cleanupStorage() {
let enginesDone = new Set(); let enginesDone = new Set();
for (const destination of destinationDockers) { for (const destination of destinationDockers) {
if (enginesDone.has(destination.engine) || enginesDone.has(destination.remoteIpAddress)) return; if (enginesDone.has(destination.engine) || enginesDone.has(destination.remoteIpAddress)) return;
if (destination.engine) enginesDone.add(destination.engine); if (destination.engine) {
if (destination.remoteIpAddress) enginesDone.add(destination.remoteIpAddress); enginesDone.add(destination.engine);
let force = false; }
if (destination.remoteIpAddress) {
if (!destination.remoteVerified) continue;
enginesDone.add(destination.remoteIpAddress);
}
let lowDiskSpace = false; let lowDiskSpace = false;
try { try {
let stdout = null; let stdout = null;