From 7bfeb6c177d619af7b208da4bfbb2273fbfa2ce6 Mon Sep 17 00:00:00 2001 From: Andras Bacsai Date: Fri, 7 Jul 2023 21:35:29 +0200 Subject: [PATCH] feat: notify user of disk cleanup init --- app/Console/Kernel.php | 4 ++-- app/Jobs/DockerCleanupJob.php | 15 ++++++++++++--- app/Models/Server.php | 6 +++++- config/version.php | 2 +- versions.json | 2 +- 5 files changed, 21 insertions(+), 8 deletions(-) diff --git a/app/Console/Kernel.php b/app/Console/Kernel.php index 1d20b7606..cbd91f662 100644 --- a/app/Console/Kernel.php +++ b/app/Console/Kernel.php @@ -14,12 +14,12 @@ protected function schedule(Schedule $schedule): void { if (isDev()) { $schedule->command('horizon:snapshot')->everyMinute(); - // $schedule->job(new DockerCleanupJob)->everyOddHour(); + $schedule->job(new DockerCleanupJob)->everyOddHour(); // $schedule->job(new InstanceAutoUpdateJob(true))->everyMinute(); } else { $schedule->command('horizon:snapshot')->everyFiveMinutes(); - $schedule->job(new DockerCleanupJob)->everyTenMinutes(); $schedule->job(new ProxyCheckJob)->everyFiveMinutes(); + $schedule->job(new DockerCleanupJob)->everyTenMinutes(); $schedule->job(new InstanceAutoUpdateJob)->everyTenMinutes(); } } diff --git a/app/Jobs/DockerCleanupJob.php b/app/Jobs/DockerCleanupJob.php index 117a9bd8a..96b2d668f 100644 --- a/app/Jobs/DockerCleanupJob.php +++ b/app/Jobs/DockerCleanupJob.php @@ -36,16 +36,25 @@ public function handle(): void } else { $docker_root_filesystem = instant_remote_process(['stat --printf=%m $(docker info --format "{{json .DockerRootDir}}" |sed \'s/"//g\')'], $server); } - $disk_usage = json_decode(instant_remote_process(['df -hP | awk \'BEGIN {printf"{\"disks\":["}{if($1=="Filesystem")next;if(a)printf",";printf"{\"mount\":\""$6"\",\"size\":\""$2"\",\"used\":\""$3"\",\"avail\":\""$4"\",\"use%\":\""$5"\"}";a++;}END{print"]}";}\''], $server), true); - $mount_point = collect(data_get($disk_usage, 'disks'))->where('mount', $docker_root_filesystem)->first(); - if (Str::of(data_get($mount_point, 'use%'))->trim()->replace('%', '')->value() >= $server->settings->cleanup_after_percentage) { + $disk_percentage_before = $this->get_disk_usage($server, $docker_root_filesystem); + if ($disk_percentage_before >= $server->settings->cleanup_after_percentage) { instant_remote_process(['docker image prune -af'], $server); instant_remote_process(['docker container prune -f --filter "label=coolify.managed=true"'], $server); instant_remote_process(['docker builder prune -af'], $server); + $disk_percentage_after = $this->get_disk_usage($server, $docker_root_filesystem); + if ($disk_percentage_after < $disk_percentage_before) { + ray('Saved ' . ($disk_percentage_before - $disk_percentage_after) . '% disk space on ' . $server->name); + } } } } catch (\Exception $e) { Log::error($e->getMessage()); } } + + private function get_disk_usage(Server $server, string $docker_root_filesystem) { + $disk_usage = json_decode(instant_remote_process(['df -hP | awk \'BEGIN {printf"{\"disks\":["}{if($1=="Filesystem")next;if(a)printf",";printf"{\"mount\":\""$6"\",\"size\":\""$2"\",\"used\":\""$3"\",\"avail\":\""$4"\",\"use%\":\""$5"\"}";a++;}END{print"]}";}\''], $server), true); + $mount_point = collect(data_get($disk_usage, 'disks'))->where('mount', $docker_root_filesystem)->first(); + return Str::of(data_get($mount_point, 'use%'))->trim()->replace('%', '')->value(); + } } \ No newline at end of file diff --git a/app/Models/Server.php b/app/Models/Server.php index d3fabe967..0d21e80b0 100644 --- a/app/Models/Server.php +++ b/app/Models/Server.php @@ -84,6 +84,10 @@ public function muxFilename() { return "{$this->ip}_{$this->port}_{$this->user}"; } + public function team() + { + return $this->belongsTo(Team::class); + } static public function ownedByCurrentTeam(array $select = ['*']) { $selectArray = collect($select)->concat(['id']); @@ -102,4 +106,4 @@ static public function destinationsByServer(string $server_id) $swarmDocker = collect($server->swarmDockers->all()); return $standaloneDocker->concat($swarmDocker); } -} +} \ No newline at end of file diff --git a/config/version.php b/config/version.php index 3a9c12a30..743126446 100644 --- a/config/version.php +++ b/config/version.php @@ -1,3 +1,3 @@