77 lines
2.3 KiB
TypeScript
Raw Normal View History

2022-03-02 14:53:43 +01:00
import { asyncExecShell, getEngine, version } from '$lib/common';
2022-02-10 15:47:44 +01:00
import { prisma } from '$lib/database';
2022-04-06 21:01:47 +02:00
export default async function (): Promise<void> {
2022-02-24 10:11:48 +01:00
const destinationDockers = await prisma.destinationDocker.findMany();
const engines = [...new Set(destinationDockers.map(({ engine }) => engine))];
for (const engine of engines) {
2022-04-30 13:54:19 +02:00
let lowDiskSpace = false;
const host = getEngine(engine);
2022-03-02 14:53:43 +01:00
try {
2022-04-30 13:54:19 +02:00
const { stdout } = await asyncExecShell(
`DOCKER_HOST=${host} docker exec coolify sh -c 'df -kPT /'`
2022-03-02 14:53:43 +01:00
);
2022-04-30 13:54:19 +02:00
let lines = stdout.trim().split('\n');
let header = lines[0];
let regex =
/^Filesystem\s+|Type\s+|1024-blocks|\s+Used|\s+Available|\s+Capacity|\s+Mounted on\s*$/g;
const boundaries = [];
let match;
while ((match = regex.exec(header))) {
boundaries.push(match[0].length);
}
boundaries[boundaries.length - 1] = -1;
const data = lines.slice(1).map((line) => {
const cl = boundaries.map((boundary) => {
const column = boundary > 0 ? line.slice(0, boundary) : line;
line = line.slice(boundary);
return column.trim();
});
return {
capacity: Number.parseInt(cl[5], 10) / 100
};
});
if (data.length > 0) {
const { capacity } = data[0];
if (capacity > 0.8) {
lowDiskSpace = true;
}
2022-03-02 15:52:06 +01:00
}
2022-03-02 14:53:43 +01:00
} catch (error) {
2022-04-30 13:54:19 +02:00
console.log(error);
2022-04-12 20:57:49 +02:00
}
2022-04-30 13:54:19 +02:00
console.log(`Is LowDiskSpace detected? ${lowDiskSpace}`);
if (lowDiskSpace) {
// Cleanup old coolify images
try {
let { stdout: images } = await asyncExecShell(
`DOCKER_HOST=${host} docker images coollabsio/coolify --filter before="coollabsio/coolify:${version}" -q | xargs `
);
images = images.trim();
if (images) {
await asyncExecShell(`DOCKER_HOST=${host} docker rmi -f ${images}`);
}
} catch (error) {
//console.log(error);
}
try {
await asyncExecShell(`DOCKER_HOST=${host} docker container prune -f`);
} catch (error) {
//console.log(error);
}
try {
await asyncExecShell(`DOCKER_HOST=${host} docker image prune -f --filter "until=2h"`);
} catch (error) {
//console.log(error);
}
// Cleanup old images older than a day
try {
await asyncExecShell(`DOCKER_HOST=${host} docker image prune --filter "until=72h" -a -f`);
} catch (error) {
//console.log(error);
}
2022-03-02 14:53:43 +01:00
}
2022-02-10 15:47:44 +01:00
}
}