From 8b57a2b055157587cf0c57b2892f9a43c7b30839 Mon Sep 17 00:00:00 2001 From: Andras Bacsai Date: Fri, 20 Jan 2023 09:26:48 +0100 Subject: [PATCH] fix: cleanup function --- apps/api/src/index.ts | 4 +- apps/api/src/lib/common.ts | 80 +++++--------------------- apps/api/src/routes/api/v1/handlers.ts | 2 +- package.json | 2 +- 4 files changed, 18 insertions(+), 70 deletions(-) diff --git a/apps/api/src/index.ts b/apps/api/src/index.ts index 1ab6acd47..44d46f8a3 100644 --- a/apps/api/src/index.ts +++ b/apps/api/src/index.ts @@ -591,6 +591,8 @@ async function cleanupStorage() { } } } catch (error) {} - await cleanupDockerStorage(destination.id, lowDiskSpace, force); + if (lowDiskSpace) { + await cleanupDockerStorage(destination.id); + } } } diff --git a/apps/api/src/lib/common.ts b/apps/api/src/lib/common.ts index 02f932613..3eeced3f7 100644 --- a/apps/api/src/lib/common.ts +++ b/apps/api/src/lib/common.ts @@ -19,7 +19,7 @@ import { saveBuildLog, saveDockerRegistryCredentials } from './buildPacks/common import { scheduler } from './scheduler'; import type { ExecaChildProcess } from 'execa'; -export const version = '3.12.14'; +export const version = '3.12.15'; export const isDev = process.env.NODE_ENV === 'development'; export const sentryDSN = 'https://409f09bcb7af47928d3e0f46b78987f3@o1082494.ingest.sentry.io/4504236622217216'; @@ -1714,78 +1714,24 @@ export function convertTolOldVolumeNames(type) { } } -export async function cleanupDockerStorage(dockerId, lowDiskSpace, force) { - // Cleanup old coolify images +export async function cleanupDockerStorage(dockerId) { + // Cleanup images that are not used by any container try { - let { stdout: images } = await executeCommand({ - dockerId, - command: `docker images coollabsio/coolify --filter before="coollabsio/coolify:${version}" -q | xargs -r`, - shell: true - }); - - images = images.trim(); - if (images) { - await executeCommand({ - dockerId, - command: `docker rmi -f ${images}" -q | xargs -r`, - shell: true - }); - } + await executeCommand({ dockerId, command: `docker image prune -af` }); } catch (error) {} - if (lowDiskSpace || force) { - // Cleanup images that are not used - try { - await executeCommand({ dockerId, command: `docker image prune -f` }); - } catch (error) {} - const { numberOfDockerImagesKeptLocally } = await prisma.setting.findUnique({ - where: { id: '0' } - }); - const { stdout: images } = await executeCommand({ + // Prune coolify managed containers + try { + await executeCommand({ dockerId, - command: `docker images|grep -v ""|grep -v REPOSITORY|awk '{print $1, $2}'`, - shell: true + command: `docker container prune -f --filter "label=coolify.managed=true"` }); - const imagesArray = images.trim().replaceAll(' ', ':').split('\n'); - const imagesSet = new Set(imagesArray.map((image) => image.split(':')[0])); - let deleteImage = []; - for (const image of imagesSet) { - let keepImage = []; - for (const image2 of imagesArray) { - if (image2.startsWith(image)) { - if (force) { - deleteImage.push(image2); - continue; - } - if (keepImage.length >= numberOfDockerImagesKeptLocally) { - deleteImage.push(image2); - } else { - keepImage.push(image2); - } - } - } - } - for (const image of deleteImage) { - try { - await executeCommand({ dockerId, command: `docker image rm -f ${image}` }); - } catch (error) { - console.log(error); - } - } + } catch (error) {} - // Prune coolify managed containers - try { - await executeCommand({ - dockerId, - command: `docker container prune -f --filter "label=coolify.managed=true"` - }); - } catch (error) {} - - // Cleanup build caches - try { - await executeCommand({ dockerId, command: `docker builder prune -a -f` }); - } catch (error) {} - } + // Cleanup build caches + try { + await executeCommand({ dockerId, command: `docker builder prune -af` }); + } catch (error) {} } export function persistentVolumes(id, persistentStorage, config) { diff --git a/apps/api/src/routes/api/v1/handlers.ts b/apps/api/src/routes/api/v1/handlers.ts index 382635800..52d5e4e38 100644 --- a/apps/api/src/routes/api/v1/handlers.ts +++ b/apps/api/src/routes/api/v1/handlers.ts @@ -59,7 +59,7 @@ export async function cleanupManually(request: FastifyRequest) { const destination = await prisma.destinationDocker.findUnique({ where: { id: serverId } }); - await cleanupDockerStorage(destination.id, true, true); + await cleanupDockerStorage(destination.id); return {}; } catch ({ status, message }) { return errorHandler({ status, message }); diff --git a/package.json b/package.json index 9ace8b3e1..88034d7b7 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "coolify", "description": "An open-source & self-hostable Heroku / Netlify alternative.", - "version": "3.12.14", + "version": "3.12.15", "license": "Apache-2.0", "repository": "github:coollabsio/coolify", "scripts": {