mirror of
https://github.com/coollabsio/coolify.git
synced 2026-11-06 13:10:37 -04:00
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: peaklabs-dev <122374094+peaklabs-dev@users.noreply.github.com> Co-authored-by: Aditya Tripathi <aditya@climactic.co>
56 lines
1.7 KiB
PHP
56 lines
1.7 KiB
PHP
<?php
|
|
|
|
namespace App\Actions\ImageScan;
|
|
|
|
use App\Jobs\PrepareImageScannerJob;
|
|
use App\Models\InstanceSettings;
|
|
use App\Models\Server;
|
|
use App\Services\ImageScan\ImageScanningRefused;
|
|
use Illuminate\Support\Facades\Cache;
|
|
use Lorisleiva\Actions\Concerns\AsAction;
|
|
|
|
/**
|
|
* Turns image scanning of a server on or off. Turning it on gets the scanner on the server.
|
|
*/
|
|
class SetServerImageScanning
|
|
{
|
|
use AsAction;
|
|
|
|
/**
|
|
* @throws ImageScanningRefused When the instance does not allow scanning or another instance manages the server.
|
|
*/
|
|
public function handle(Server $server, bool $enabled): void
|
|
{
|
|
if (! $enabled) {
|
|
$server->settings->update(['is_image_scan_enabled' => false]);
|
|
PrepareImageScannerJob::forget($server);
|
|
|
|
return;
|
|
}
|
|
|
|
self::ensureManagedHere($server);
|
|
if (! InstanceSettings::get()->is_image_scanning_allowed) {
|
|
throw new ImageScanningRefused('Image scanning is turned off for this Coolify instance.');
|
|
}
|
|
|
|
$server->settings->update(['is_image_scan_enabled' => true]);
|
|
self::prepareScanner($server);
|
|
}
|
|
|
|
/**
|
|
* Gets the scanner on the server again, for a server that scans images.
|
|
*/
|
|
public static function prepareScanner(Server $server): void
|
|
{
|
|
Cache::put(PrepareImageScannerJob::stateCacheKey($server), PrepareImageScannerJob::STATE_PULLING, now()->addHour());
|
|
PrepareImageScannerJob::dispatch($server);
|
|
}
|
|
|
|
public static function ensureManagedHere(Server $server): void
|
|
{
|
|
if ($server->isTransferredAway()) {
|
|
throw new ImageScanningRefused('This server is managed by another Coolify instance.');
|
|
}
|
|
}
|
|
}
|