mirror of
https://github.com/vitodeploy/vito.git
synced 2025-07-03 06:56:15 +00:00
2.x - firewall/metrics/services/cronjobs
This commit is contained in:
93
app/Web/Pages/Servers/Services/Index.php
Normal file
93
app/Web/Pages/Servers/Services/Index.php
Normal file
@ -0,0 +1,93 @@
|
||||
<?php
|
||||
|
||||
namespace App\Web\Pages\Servers\Services;
|
||||
|
||||
use App\Actions\Service\Install;
|
||||
use App\Models\Server;
|
||||
use App\Models\Service;
|
||||
use App\Web\Components\Page;
|
||||
use App\Web\Traits\PageHasServer;
|
||||
use Exception;
|
||||
use Filament\Actions\Action;
|
||||
use Filament\Forms\Components\Select;
|
||||
use Filament\Notifications\Notification;
|
||||
use Filament\Support\Enums\MaxWidth;
|
||||
|
||||
class Index extends Page
|
||||
{
|
||||
use PageHasServer;
|
||||
|
||||
protected static ?string $slug = 'servers/{server}/services';
|
||||
|
||||
protected static bool $shouldRegisterNavigation = false;
|
||||
|
||||
protected static ?string $title = 'Services';
|
||||
|
||||
public Server $server;
|
||||
|
||||
public static function canAccess(): bool
|
||||
{
|
||||
return auth()->user()?->can('viewAny', [Service::class, static::getServerFromRoute()]) ?? false;
|
||||
}
|
||||
|
||||
public function getWidgets(): array
|
||||
{
|
||||
return [
|
||||
[Widgets\ServicesList::class, ['server' => $this->server]],
|
||||
];
|
||||
}
|
||||
|
||||
protected function getHeaderActions(): array
|
||||
{
|
||||
$availableServices = [];
|
||||
foreach (config('core.service_handlers') as $key => $addOn) {
|
||||
if (! $this->server->services()->where('name', $key)->exists()) {
|
||||
$availableServices[$key] = $key;
|
||||
}
|
||||
}
|
||||
|
||||
return [
|
||||
Action::make('install')
|
||||
->label('Install Service')
|
||||
->icon('heroicon-o-archive-box-arrow-down')
|
||||
->modalWidth(MaxWidth::Large)
|
||||
->authorize(fn () => auth()->user()?->can('create', [Service::class, $this->server]))
|
||||
->form([
|
||||
Select::make('name')
|
||||
->searchable()
|
||||
->options($availableServices)
|
||||
->reactive()
|
||||
->rules(fn ($get) => Install::rules($get())['name']),
|
||||
Select::make('version')
|
||||
->options(function (callable $get) {
|
||||
if (! $get('name')) {
|
||||
return [];
|
||||
}
|
||||
|
||||
return collect(config("core.service_versions.{$get('name')}"))
|
||||
->mapWithKeys(fn ($version) => [$version => $version]);
|
||||
})
|
||||
->rules(fn ($get) => Install::rules($get())['version'])
|
||||
->reactive(),
|
||||
])
|
||||
->action(function (array $data) {
|
||||
$this->validate();
|
||||
|
||||
try {
|
||||
app(Install::class)->install($this->server, $data);
|
||||
|
||||
$this->redirect(self::getUrl(['server' => $this->server]));
|
||||
} catch (Exception $e) {
|
||||
Notification::make()
|
||||
->danger()
|
||||
->title($e->getMessage())
|
||||
->send();
|
||||
|
||||
throw $e;
|
||||
}
|
||||
|
||||
$this->dispatch('$refresh');
|
||||
}),
|
||||
];
|
||||
}
|
||||
}
|
117
app/Web/Pages/Servers/Services/Widgets/ServicesList.php
Normal file
117
app/Web/Pages/Servers/Services/Widgets/ServicesList.php
Normal file
@ -0,0 +1,117 @@
|
||||
<?php
|
||||
|
||||
namespace App\Web\Pages\Servers\Services\Widgets;
|
||||
|
||||
use App\Actions\Service\Manage;
|
||||
use App\Actions\Service\Uninstall;
|
||||
use App\Models\Server;
|
||||
use App\Models\Service;
|
||||
use App\Web\Pages\Servers\Services\Index;
|
||||
use Exception;
|
||||
use Filament\Notifications\Notification;
|
||||
use Filament\Tables\Actions\Action;
|
||||
use Filament\Tables\Actions\ActionGroup;
|
||||
use Filament\Tables\Columns\ImageColumn;
|
||||
use Filament\Tables\Columns\TextColumn;
|
||||
use Filament\Tables\Table;
|
||||
use Filament\Widgets\TableWidget;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
|
||||
class ServicesList extends TableWidget
|
||||
{
|
||||
public Server $server;
|
||||
|
||||
protected $listeners = ['$refresh'];
|
||||
|
||||
protected function getTableQuery(): Builder
|
||||
{
|
||||
return Service::query()->where('server_id', $this->server->id);
|
||||
}
|
||||
|
||||
protected static ?string $heading = 'Installed Services';
|
||||
|
||||
protected function getTableColumns(): array
|
||||
{
|
||||
return [
|
||||
ImageColumn::make('image_url')
|
||||
->label('Service')
|
||||
->size(24),
|
||||
TextColumn::make('name')
|
||||
->sortable(),
|
||||
TextColumn::make('version')
|
||||
->sortable(),
|
||||
TextColumn::make('status')
|
||||
->label('Status')
|
||||
->badge()
|
||||
->color(fn (Service $service) => Service::$statusColors[$service->status])
|
||||
->sortable(),
|
||||
TextColumn::make('created_at')
|
||||
->label('Installed At')
|
||||
->formatStateUsing(fn ($record) => $record->created_at_by_timezone),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws Exception
|
||||
*/
|
||||
public function getTable(): Table
|
||||
{
|
||||
return $this->table
|
||||
->actions([
|
||||
ActionGroup::make([
|
||||
$this->serviceAction('start'),
|
||||
$this->serviceAction('stop'),
|
||||
$this->serviceAction('restart'),
|
||||
$this->serviceAction('disable'),
|
||||
$this->serviceAction('enable'),
|
||||
$this->uninstallAction(),
|
||||
]),
|
||||
]);
|
||||
}
|
||||
|
||||
private function serviceAction(string $type): Action
|
||||
{
|
||||
return Action::make($type)
|
||||
->authorize(fn (Service $service) => auth()->user()?->can($type, $service))
|
||||
->label(ucfirst($type).' Service')
|
||||
->action(function (Service $service) use ($type) {
|
||||
try {
|
||||
app(Manage::class)->$type($service);
|
||||
} catch (Exception $e) {
|
||||
Notification::make()
|
||||
->danger()
|
||||
->title($e->getMessage())
|
||||
->send();
|
||||
|
||||
throw $e;
|
||||
}
|
||||
|
||||
$this->dispatch('$refresh');
|
||||
});
|
||||
}
|
||||
|
||||
private function uninstallAction(): Action
|
||||
{
|
||||
return Action::make('uninstall')
|
||||
->authorize(fn (Service $service) => auth()->user()?->can('delete', $service))
|
||||
->label('Uninstall Service')
|
||||
->color('danger')
|
||||
->requiresConfirmation()
|
||||
->action(function (Service $service) {
|
||||
try {
|
||||
app(Uninstall::class)->uninstall($service);
|
||||
|
||||
$this->redirect(Index::getUrl(['server' => $this->server]));
|
||||
} catch (Exception $e) {
|
||||
Notification::make()
|
||||
->danger()
|
||||
->title($e->getMessage())
|
||||
->send();
|
||||
|
||||
throw $e;
|
||||
}
|
||||
|
||||
$this->dispatch('$refresh');
|
||||
});
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user