diff --git a/app/Console/Commands/Egg/CheckEggUpdatesCommand.php b/app/Console/Commands/Egg/CheckEggUpdatesCommand.php new file mode 100644 index 000000000..f2d5e589e --- /dev/null +++ b/app/Console/Commands/Egg/CheckEggUpdatesCommand.php @@ -0,0 +1,46 @@ +update_url)) { + $this->comment("{$egg->name}: Skipping (no update url set)"); + + continue; + } + + $currentJson = json_decode($exporterService->handle($egg->id)); + unset($currentJson->exported_at); + + $updatedJson = json_decode(file_get_contents($egg->update_url)); + unset($updatedJson->exported_at); + + if (md5(json_encode($currentJson)) === md5(json_encode($updatedJson))) { + $this->info("{$egg->name}: Up-to-date"); + cache()->put("eggs.{$egg->uuid}.update", false, now()->addHour()); + } else { + $this->warn("{$egg->name}: Found update"); + cache()->put("eggs.{$egg->uuid}.update", true, now()->addHour()); + } + } catch (Exception $exception) { + $this->error("{$egg->name}: Error ({$exception->getMessage()})"); + } + } + } +} diff --git a/app/Console/Kernel.php b/app/Console/Kernel.php index 4a9bbee17..3c2fc96fa 100644 --- a/app/Console/Kernel.php +++ b/app/Console/Kernel.php @@ -2,15 +2,16 @@ namespace App\Console; +use App\Console\Commands\Egg\CheckEggUpdatesCommand; +use App\Console\Commands\Maintenance\CleanServiceBackupFilesCommand; +use App\Console\Commands\Maintenance\PruneImagesCommand; +use App\Console\Commands\Maintenance\PruneOrphanedBackupsCommand; +use App\Console\Commands\Schedule\ProcessRunnableCommand; use App\Jobs\NodeStatistics; use App\Models\ActivityLog; use Illuminate\Console\Scheduling\Schedule; use Illuminate\Database\Console\PruneCommand; use Illuminate\Foundation\Console\Kernel as ConsoleKernel; -use App\Console\Commands\Schedule\ProcessRunnableCommand; -use App\Console\Commands\Maintenance\PruneOrphanedBackupsCommand; -use App\Console\Commands\Maintenance\CleanServiceBackupFilesCommand; -use App\Console\Commands\Maintenance\PruneImagesCommand; class Kernel extends ConsoleKernel { @@ -35,6 +36,7 @@ class Kernel extends ConsoleKernel $schedule->command(CleanServiceBackupFilesCommand::class)->daily(); $schedule->command(PruneImagesCommand::class)->daily(); + $schedule->command(CheckEggUpdatesCommand::class)->hourly(); $schedule->job(new NodeStatistics())->everyFiveSeconds()->withoutOverlapping(); diff --git a/app/Filament/Resources/EggResource/Pages/ListEggs.php b/app/Filament/Resources/EggResource/Pages/ListEggs.php index 1a6a3b650..792eaa162 100644 --- a/app/Filament/Resources/EggResource/Pages/ListEggs.php +++ b/app/Filament/Resources/EggResource/Pages/ListEggs.php @@ -14,7 +14,7 @@ use Filament\Forms\Components\Tabs\Tab; use Filament\Forms\Components\TextInput; use Filament\Notifications\Notification; use Filament\Resources\Pages\ListRecords; -use Filament\Tables; +use Filament\Tables\Actions\Action; use Filament\Tables\Actions\BulkActionGroup; use Filament\Tables\Actions\DeleteBulkAction; use Filament\Tables\Actions\EditAction; @@ -49,7 +49,7 @@ class ListEggs extends ListRecords ]) ->actions([ EditAction::make(), - Tables\Actions\Action::make('export') + Action::make('export') ->icon('tabler-download') ->label('Export') ->color('primary') @@ -57,6 +57,38 @@ class ListEggs extends ListRecords echo $service->handle($egg->id); }, 'egg-' . $egg->getKebabName() . '.json')) ->authorize(fn () => auth()->user()->can('export egg')), + Action::make('update') + ->icon('tabler-cloud-download') + ->label('Update') + ->color('success') + ->requiresConfirmation() + ->modalHeading('Are you sure you want to update this egg?') + ->modalDescription('If you made any changes to the egg they will be overwritten!') + ->modalIconColor('danger') + ->modalSubmitAction(fn (Actions\StaticAction $action) => $action->color('danger')) + ->action(function (Egg $egg) { + try { + app(EggImporterService::class)->fromUrl($egg->update_url, $egg); + cache()->forget("eggs.{$egg->uuid}.update"); + } catch (Exception $exception) { + Notification::make() + ->title('Egg Update failed') + ->body($exception->getMessage()) + ->danger() + ->send(); + + report($exception); + + return; + } + + Notification::make() + ->title('Egg updated') + ->success() + ->send(); + }) + ->authorize(fn () => auth()->user()->can('import egg')) + ->visible(fn (Egg $egg) => cache()->get("eggs.{$egg->uuid}.update", false)), ]) ->bulkActions([ BulkActionGroup::make([