mirror of
https://github.com/pelican-dev/panel.git
synced 2025-05-20 12:14:45 +02:00
Command to cleanup docker images (#495)
* add command to cleanup docker images * automatically cleanup images daily * fix request * fix empty check * run pint
This commit is contained in:
parent
56484a2282
commit
dfba8e3993
60
app/Console/Commands/Maintenance/PruneImagesCommand.php
Normal file
60
app/Console/Commands/Maintenance/PruneImagesCommand.php
Normal file
@ -0,0 +1,60 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Console\Commands\Maintenance;
|
||||||
|
|
||||||
|
use App\Models\Node;
|
||||||
|
use Exception;
|
||||||
|
use Illuminate\Console\Command;
|
||||||
|
use Illuminate\Support\Facades\Http;
|
||||||
|
|
||||||
|
class PruneImagesCommand extends Command
|
||||||
|
{
|
||||||
|
protected $signature = 'p:maintenance:prune-images {node?}';
|
||||||
|
|
||||||
|
protected $description = 'Clean up all dangling docker images to clear up disk space.';
|
||||||
|
|
||||||
|
public function handle(): void
|
||||||
|
{
|
||||||
|
$node = $this->argument('node');
|
||||||
|
|
||||||
|
if (empty($node)) {
|
||||||
|
$nodes = Node::all();
|
||||||
|
/** @var Node $node */
|
||||||
|
foreach ($nodes as $node) {
|
||||||
|
$this->cleanupImages($node);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
$this->cleanupImages((int) $node);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private function cleanupImages(int|Node $node): void
|
||||||
|
{
|
||||||
|
if (!$node instanceof Node) {
|
||||||
|
$node = Node::query()->findOrFail($node);
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
$response = Http::daemon($node)
|
||||||
|
->connectTimeout(5)
|
||||||
|
->timeout(30)
|
||||||
|
->delete('/api/system/docker/image/prune')
|
||||||
|
->json() ?? [];
|
||||||
|
|
||||||
|
if (empty($response) || $response['ImagesDeleted'] === null) {
|
||||||
|
$this->warn("Node {$node->id}: No images to clean up.");
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$count = count($response['ImagesDeleted']);
|
||||||
|
|
||||||
|
$useBinaryPrefix = config('panel.use_binary_prefix');
|
||||||
|
$space = round($useBinaryPrefix ? $response['SpaceReclaimed'] / 1024 / 1024 : $response['SpaceReclaimed'] / 1000 / 1000, 2) . ($useBinaryPrefix ? ' MiB' : ' MB');
|
||||||
|
|
||||||
|
$this->info("Node {$node->id}: Cleaned up {$count} dangling docker images. ({$space})");
|
||||||
|
} catch (Exception $exception) {
|
||||||
|
$this->error($exception->getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -10,6 +10,7 @@ use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
|
|||||||
use App\Console\Commands\Schedule\ProcessRunnableCommand;
|
use App\Console\Commands\Schedule\ProcessRunnableCommand;
|
||||||
use App\Console\Commands\Maintenance\PruneOrphanedBackupsCommand;
|
use App\Console\Commands\Maintenance\PruneOrphanedBackupsCommand;
|
||||||
use App\Console\Commands\Maintenance\CleanServiceBackupFilesCommand;
|
use App\Console\Commands\Maintenance\CleanServiceBackupFilesCommand;
|
||||||
|
use App\Console\Commands\Maintenance\PruneImagesCommand;
|
||||||
|
|
||||||
class Kernel extends ConsoleKernel
|
class Kernel extends ConsoleKernel
|
||||||
{
|
{
|
||||||
@ -31,7 +32,9 @@ class Kernel extends ConsoleKernel
|
|||||||
|
|
||||||
// Execute scheduled commands for servers every minute, as if there was a normal cron running.
|
// Execute scheduled commands for servers every minute, as if there was a normal cron running.
|
||||||
$schedule->command(ProcessRunnableCommand::class)->everyMinute()->withoutOverlapping();
|
$schedule->command(ProcessRunnableCommand::class)->everyMinute()->withoutOverlapping();
|
||||||
|
|
||||||
$schedule->command(CleanServiceBackupFilesCommand::class)->daily();
|
$schedule->command(CleanServiceBackupFilesCommand::class)->daily();
|
||||||
|
$schedule->command(PruneImagesCommand::class)->daily();
|
||||||
|
|
||||||
$schedule->job(new NodeStatistics())->everyFiveSeconds()->withoutOverlapping();
|
$schedule->job(new NodeStatistics())->everyFiveSeconds()->withoutOverlapping();
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user