- 2.x - scripts

This commit is contained in:
Saeed Vaziry
2024-10-06 20:49:59 +02:00
parent c24b4b7333
commit 8b2338cb41
32 changed files with 936 additions and 79 deletions

View File

@ -0,0 +1,86 @@
<?php
namespace App\Web\Pages\Scripts;
use App\Actions\Script\ExecuteScript;
use App\Models\Script;
use App\Models\Server;
use App\Web\Components\Page;
use Filament\Actions\Action;
use Filament\Forms\Components\Select;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Get;
use Filament\Support\Enums\MaxWidth;
use Illuminate\Contracts\Support\Htmlable;
class Executions extends Page
{
protected static bool $shouldRegisterNavigation = false;
protected static ?string $slug = 'scripts/{script}/executions';
public Script $script;
public function getTitle(): string|Htmlable
{
return $this->script->name.' - Executions';
}
public static function canAccess(): bool
{
return auth()->user()?->can('view', get_from_route(Script::class, 'script')) ?? false;
}
public function getWidgets(): array
{
return [
[Widgets\ScriptExecutionsList::class, ['script' => $this->script]],
];
}
protected function getHeaderActions(): array
{
$form = [
Select::make('server')
->options(function () {
return auth()->user()?->currentProject?->servers?->pluck('name', 'id') ?? [];
})
->rules(fn (Get $get) => ExecuteScript::rules($get())['server'])
->searchable()
->reactive(),
Select::make('user')
->rules(fn (Get $get) => ExecuteScript::rules($get())['user'])
->native(false)
->options(function (Get $get) {
$options = [
'root' => 'root',
];
$server = Server::query()->find($get('server'));
if ($server) {
$options[$server->ssh_user] = $server->ssh_user;
}
return $options;
}),
];
foreach ($this->script->getVariables() as $variable) {
$form[] = TextInput::make($variable)
->label($variable)
->rules(fn (Get $get) => ExecuteScript::rules($get())['variables.*']);
}
return [
Action::make('execute')
->icon('heroicon-o-bolt')
->modalWidth(MaxWidth::Large)
->form($form)
->action(function (array $data) {
app(ExecuteScript::class)->execute($this->script, $data);
$this->dispatch('$refresh');
}),
];
}
}

View File

@ -0,0 +1,74 @@
<?php
namespace App\Web\Pages\Scripts;
use App\Actions\Script\CreateScript;
use App\Models\Script;
use App\Web\Components\Page;
use App\Web\Fields\CodeEditorField;
use Filament\Actions\Action;
use Filament\Forms\Components\Checkbox;
use Filament\Forms\Components\TextInput;
use Filament\Support\Enums\MaxWidth;
class Index extends Page
{
protected static ?string $slug = 'scripts';
protected static ?string $navigationIcon = 'heroicon-o-bolt';
protected static ?int $navigationSort = 2;
protected static ?string $title = 'Scripts';
public static function getNavigationItemActiveRoutePattern(): string
{
return static::getRouteName().'*';
}
public static function canAccess(): bool
{
return auth()->user()?->can('viewAny', Script::class) ?? false;
}
public function getWidgets(): array
{
return [
[Widgets\ScriptsList::class],
];
}
protected function getHeaderActions(): array
{
return [
Action::make('read-the-docs')
->label('Read the Docs')
->icon('heroicon-o-document-text')
->color('gray')
->url('https://vitodeploy.com/other/scripts.html')
->openUrlInNewTab(),
Action::make('create')
->label('Create a Script')
->icon('heroicon-o-plus')
->authorize('create', Script::class)
->modalWidth(MaxWidth::ThreeExtraLarge)
->form([
TextInput::make('name')
->rules(CreateScript::rules()['name']),
CodeEditorField::make('content')
->rules(CreateScript::rules()['content'])
->helperText('You can use variables like ${VARIABLE_NAME} in the script. The variables will be asked when executing the script'),
Checkbox::make('global')
->label('Is Global (Accessible in all projects)'),
])
->modalSubmitActionLabel('Create')
->action(function (array $data) {
run_action($this, function () use ($data) {
app(CreateScript::class)->create(auth()->user(), $data);
$this->dispatch('$refresh');
});
}),
];
}
}

View File

@ -0,0 +1,79 @@
<?php
namespace App\Web\Pages\Scripts\Widgets;
use App\Models\Script;
use App\Models\ScriptExecution;
use App\Web\Pages\Servers\View;
use Filament\Tables\Actions\Action;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Table;
use Filament\Widgets\TableWidget as Widget;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\View\ComponentAttributeBag;
class ScriptExecutionsList extends Widget
{
protected $listeners = ['$refresh'];
public Script $script;
protected function getTableQuery(): Builder
{
return ScriptExecution::query()->where('script_id', $this->script->id);
}
protected function applyDefaultSortingToTableQuery(Builder $query): Builder
{
return $query->latest('created_at');
}
protected function getTableColumns(): array
{
return [
TextColumn::make('server')
->formatStateUsing(function (ScriptExecution $record) {
return $record->getServer()?->name ?? 'Unknown';
})
->url(function (ScriptExecution $record) {
$server = $record->getServer();
return $server ? View::getUrl(['server' => $server]) : null;
})
->searchable()
->sortable(),
TextColumn::make('created_at')
->label('Executed At')
->formatStateUsing(fn (ScriptExecution $record) => $record->created_at_by_timezone)
->searchable()
->sortable(),
TextColumn::make('status')
->label('Status')
->badge()
->color(fn (ScriptExecution $record) => ScriptExecution::$statusColors[$record->status])
->sortable(),
];
}
public function getTable(): Table
{
return $this->table
->heading('')
->actions([
Action::make('view')
->hiddenLabel()
->tooltip('View')
->icon('heroicon-o-eye')
->authorize(fn (ScriptExecution $record) => auth()->user()->can('view', $record->serverLog))
->modalHeading('View Log')
->modalContent(function (ScriptExecution $record) {
return view('components.console-view', [
'slot' => $record->serverLog?->getContent(),
'attributes' => new ComponentAttributeBag,
]);
})
->modalSubmitAction(false)
->modalCancelActionLabel('Close'),
]);
}
}

View File

@ -0,0 +1,89 @@
<?php
namespace App\Web\Pages\Scripts\Widgets;
use App\Actions\Script\EditScript;
use App\Models\Script;
use App\Web\Fields\CodeEditorField;
use App\Web\Pages\Scripts\Executions;
use Filament\Forms\Components\Checkbox;
use Filament\Forms\Components\TextInput;
use Filament\Support\Enums\MaxWidth;
use Filament\Tables\Actions\DeleteAction;
use Filament\Tables\Actions\EditAction;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Table;
use Filament\Widgets\TableWidget as Widget;
use Illuminate\Database\Eloquent\Builder;
class ScriptsList extends Widget
{
protected $listeners = ['$refresh'];
protected function getTableQuery(): Builder
{
return Script::getByProjectId(auth()->user()->current_project_id, auth()->user()->id);
}
protected function getTableColumns(): array
{
return [
TextColumn::make('name')
->searchable()
->sortable(),
TextColumn::make('id')
->label('Global')
->badge()
->color(fn ($record) => $record->project_id ? 'gray' : 'success')
->formatStateUsing(function (Script $record) {
return $record->project_id ? 'No' : 'Yes';
}),
TextColumn::make('created_at')
->label('Created At')
->formatStateUsing(fn (Script $record) => $record->created_at_by_timezone)
->searchable()
->sortable(),
];
}
public function getTable(): Table
{
return $this->table
->heading('')
->recordUrl(fn (Script $record) => Executions::getUrl(['script' => $record]))
->actions([
EditAction::make('edit')
->label('Edit')
->modalHeading('Edit Script')
->mutateRecordDataUsing(function (array $data, Script $record) {
return [
'name' => $record->name,
'content' => $record->content,
'global' => $record->project_id === null,
];
})
->form([
TextInput::make('name')
->rules(EditScript::rules()['name']),
CodeEditorField::make('content')
->rules(EditScript::rules()['content'])
->helperText('You can use variables like ${VARIABLE_NAME} in the script. The variables will be asked when executing the script'),
Checkbox::make('global')
->label('Is Global (Accessible in all projects)'),
])
->authorize(fn (Script $record) => auth()->user()->can('update', $record))
->using(function (array $data, Script $record) {
app(EditScript::class)->edit($record, auth()->user(), $data);
$this->dispatch('$refresh');
})
->modalWidth(MaxWidth::ThreeExtraLarge),
DeleteAction::make('delete')
->label('Delete')
->modalHeading('Delete Script')
->authorize(fn (Script $record) => auth()->user()->can('delete', $record))
->using(function (array $data, Script $record) {
$record->delete();
}),
]);
}
}

View File

@ -7,7 +7,7 @@
use App\Models\SshKey;
use Exception;
use Filament\Notifications\Notification;
use Filament\Tables\Actions\Action;
use Filament\Tables\Actions\DeleteAction;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Table;
use Filament\Widgets\TableWidget;
@ -21,10 +21,11 @@ class SshKeysList extends TableWidget
protected function getTableQuery(): Builder
{
return SshKey::query()->whereHas(
'servers',
fn (Builder $query) => $query->where('server_id', $this->server->id)
);
return SshKey::withTrashed()
->whereHas(
'servers',
fn (Builder $query) => $query->where('server_id', $this->server->id)
);
}
protected static ?string $heading = '';
@ -47,14 +48,10 @@ public function getTable(): Table
{
return $this->table
->actions([
Action::make('delete')
->icon('heroicon-o-trash')
->tooltip('Delete')
->color('danger')
DeleteAction::make('delete')
->hiddenLabel()
->requiresConfirmation()
->authorize(fn (SshKey $record) => auth()->user()->can('deleteServer', [SshKey::class, $this->server]))
->action(function (SshKey $record) {
->using(function (SshKey $record) {
try {
app(DeleteKeyFromServer::class)->delete($this->server, $record);
} catch (Exception $e) {

View File

@ -0,0 +1,65 @@
<?php
namespace App\Web\Pages\Settings\NotificationChannels\Actions;
use App\Actions\NotificationChannels\AddChannel;
use Exception;
use Filament\Forms\Components\Checkbox;
use Filament\Forms\Components\Select;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Get;
use Filament\Notifications\Notification;
class Create
{
public static function form(): array
{
return [
Select::make('provider')
->options(
collect(config('core.notification_channels_providers'))
->mapWithKeys(fn ($provider) => [$provider => $provider])
)
->live()
->reactive()
->rules(fn (Get $get) => AddChannel::rules($get())['provider']),
TextInput::make('label')
->rules(fn (Get $get) => AddChannel::rules($get())['label']),
TextInput::make('webhook_url')
->label('Webhook URL')
->validationAttribute('Webhook URL')
->visible(fn (Get $get) => AddChannel::rules($get())['webhook_url'] ?? false)
->rules(fn (Get $get) => AddChannel::rules($get())['webhook_url']),
TextInput::make('email')
->visible(fn (Get $get) => AddChannel::rules($get())['email'] ?? false)
->rules(fn (Get $get) => AddChannel::rules($get())['email']),
TextInput::make('bot_token')
->label('Bot Token')
->visible(fn (Get $get) => AddChannel::rules($get())['bot_token'] ?? false)
->rules(fn (Get $get) => AddChannel::rules($get())['bot_token']),
TextInput::make('chat_id')
->label('Chat ID')
->visible(fn (Get $get) => AddChannel::rules($get())['chat_id'] ?? false)
->rules(fn (Get $get) => AddChannel::rules($get())['chat_id']),
Checkbox::make('global')
->label('Is Global (Accessible in all projects)'),
];
}
/**
* @throws Exception
*/
public static function action(array $data): void
{
try {
app(AddChannel::class)->add(auth()->user(), $data);
} catch (Exception $e) {
Notification::make()
->title($e->getMessage())
->danger()
->send();
throw $e;
}
}
}

View File

@ -0,0 +1,27 @@
<?php
namespace App\Web\Pages\Settings\NotificationChannels\Actions;
use App\Actions\NotificationChannels\EditChannel;
use App\Models\NotificationChannel;
use Filament\Forms\Components\Checkbox;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Get;
class Edit
{
public static function form(): array
{
return [
TextInput::make('label')
->rules(fn (Get $get) => EditChannel::rules($get())['label']),
Checkbox::make('global')
->label('Is Global (Accessible in all projects)'),
];
}
public static function action(NotificationChannel $channel, array $data): void
{
app(EditChannel::class)->edit($channel, auth()->user(), $data);
}
}

View File

@ -0,0 +1,52 @@
<?php
namespace App\Web\Pages\Settings\NotificationChannels;
use App\Models\NotificationChannel;
use App\Web\Components\Page;
use Filament\Actions\Action;
use Filament\Support\Enums\MaxWidth;
class Index extends Page
{
protected static ?string $navigationGroup = 'Settings';
protected static ?string $slug = 'settings/notification-channels';
protected static ?string $title = 'Notification Channels';
protected static ?string $navigationIcon = 'heroicon-o-bell';
protected static ?int $navigationSort = 8;
public static function canAccess(): bool
{
return auth()->user()?->can('viewAny', NotificationChannel::class) ?? false;
}
public function getWidgets(): array
{
return [
[Widgets\NotificationChannelsList::class],
];
}
protected function getHeaderActions(): array
{
return [
Action::make('add')
->label('Add new Channel')
->icon('heroicon-o-plus')
->modalHeading('Add a new Channel')
->modalSubmitActionLabel('Add')
->form(Actions\Create::form())
->authorize('create', NotificationChannel::class)
->modalWidth(MaxWidth::Large)
->action(function (array $data) {
Actions\Create::action($data);
$this->dispatch('$refresh');
}),
];
}
}

View File

@ -0,0 +1,77 @@
<?php
namespace App\Web\Pages\Settings\NotificationChannels\Widgets;
use App\Models\NotificationChannel;
use App\Web\Pages\Settings\NotificationChannels\Actions\Edit;
use Filament\Support\Enums\MaxWidth;
use Filament\Tables\Actions\DeleteAction;
use Filament\Tables\Actions\EditAction;
use Filament\Tables\Columns\IconColumn;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Table;
use Filament\Widgets\TableWidget as Widget;
use Illuminate\Database\Eloquent\Builder;
class NotificationChannelsList extends Widget
{
protected $listeners = [
'$refresh' => 'refreshTable',
];
protected function getTableQuery(): Builder
{
return NotificationChannel::getByProjectId(auth()->user()->current_project_id);
}
protected function getTableColumns(): array
{
return [
IconColumn::make('provider')
->icon(fn (NotificationChannel $record) => 'icon-'.$record->provider)
->width(24),
TextColumn::make('label')
->default(fn (NotificationChannel $record) => $record->label)
->searchable()
->sortable(),
TextColumn::make('id')
->label('Global')
->badge()
->color(fn (NotificationChannel $record) => $record->project_id ? 'gray' : 'success')
->formatStateUsing(function (NotificationChannel $record) {
return $record->project_id ? 'No' : 'Yes';
}),
TextColumn::make('created_at')
->label('Created At')
->formatStateUsing(fn (NotificationChannel $record) => $record->created_at_by_timezone)
->searchable()
->sortable(),
];
}
public function getTable(): Table
{
return $this->table
->heading('')
->actions([
EditAction::make('edit')
->modalHeading('Edit Notification Channel')
->mutateRecordDataUsing(function (array $data, NotificationChannel $record) {
return [
'label' => $record->label,
'global' => ! $record->project_id,
];
})
->form(Edit::form())
->authorize(fn (NotificationChannel $record) => auth()->user()->can('update', $record))
->using(fn (array $data, NotificationChannel $record) => Edit::action($record, $data))
->modalWidth(MaxWidth::Medium),
DeleteAction::make('delete')
->modalHeading('Delete Notification Channel')
->authorize(fn (NotificationChannel $record) => auth()->user()->can('delete', $record))
->using(function (array $data, NotificationChannel $record) {
//
}),
]);
}
}

View File

@ -4,7 +4,7 @@
use App\Actions\Projects\DeleteProject;
use App\Models\Project;
use App\Web\Pages\Servers\Page;
use App\Web\Components\Page;
use App\Web\Pages\Settings\Projects\Widgets\AddUser;
use App\Web\Pages\Settings\Projects\Widgets\ProjectUsersList;
use App\Web\Pages\Settings\Projects\Widgets\UpdateProject;
@ -19,9 +19,11 @@ class Settings extends Page
protected static ?string $title = 'Project Settings';
protected static bool $shouldRegisterNavigation = false;
public static function canAccess(): bool
{
return auth()->user()?->can('update', request()->route('project')) ?? false;
return auth()->user()?->can('update', get_from_route(Project::class, 'project')) ?? false;
}
public Project $project;
@ -62,7 +64,7 @@ protected function getHeaderActions(): array
try {
app(DeleteProject::class)->delete(auth()->user(), $record);
$this->redirectRoute('filament.app.resources.projects.index');
$this->redirectRoute(Index::getUrl());
} catch (Exception $e) {
Notification::make()
->title($e->getMessage())

View File

@ -0,0 +1,63 @@
<?php
namespace App\Web\Pages\Settings\SSHKeys;
use App\Actions\SshKey\CreateSshKey;
use App\Models\SshKey;
use App\Web\Components\Page;
use Filament\Actions\CreateAction;
use Filament\Forms\Components\Textarea;
use Filament\Forms\Components\TextInput;
use Filament\Support\Enums\MaxWidth;
class Index extends Page
{
protected static ?string $navigationGroup = 'Settings';
protected static ?string $slug = 'settings/ssh-keys';
protected static ?string $title = 'SSH Keys';
protected static ?string $navigationIcon = 'heroicon-o-key';
protected static ?int $navigationSort = 9;
public static function canAccess(): bool
{
return auth()->user()?->can('viewAny', SshKey::class) ?? false;
}
public function getWidgets(): array
{
return [
[Widgets\SshKeysList::class],
];
}
protected function getHeaderActions(): array
{
return [
CreateAction::make('add')
->label('Add Key')
->icon('heroicon-o-plus')
->modalHeading('Add a new Key')
->modalSubmitActionLabel('Add')
->createAnother(false)
->form([
TextInput::make('name')
->label('Name')
->rules(CreateSshKey::rules()['name']),
Textarea::make('public_key')
->label('Public Key')
->rules(CreateSshKey::rules()['public_key']),
])
->authorize('create', SshKey::class)
->modalWidth(MaxWidth::Large)
->using(function (array $data) {
app(CreateSshKey::class)->create(auth()->user(), $data);
$this->dispatch('$refresh');
}),
];
}
}

View File

@ -0,0 +1,53 @@
<?php
namespace App\Web\Pages\Settings\SSHKeys\Widgets;
use App\Models\SshKey;
use Filament\Tables\Actions\DeleteAction;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Table;
use Filament\Widgets\TableWidget;
use Illuminate\Database\Eloquent\Builder;
class SshKeysList extends TableWidget
{
protected $listeners = ['$refresh'];
protected function getTableQuery(): Builder
{
return SshKey::query()->where('user_id', auth()->id());
}
protected static ?string $heading = '';
protected function getTableColumns(): array
{
return [
TextColumn::make('name')
->sortable()
->searchable(),
TextColumn::make('public_key')
->tooltip('Copy')
->limit(20)
->copyable(),
TextColumn::make('created_at')
->sortable(),
];
}
public function getTable(): Table
{
return $this->table
->actions([
DeleteAction::make('delete')
->requiresConfirmation()
->authorize(fn (SshKey $record) => auth()->user()->can('delete', $record))
->action(function (SshKey $record) {
run_action($this, function () use ($record) {
$record->delete();
$this->dispatch('$refresh');
});
}),
]);
}
}

View File

@ -18,7 +18,7 @@ class Index extends Page
protected static ?string $navigationIcon = 'heroicon-o-tag';
protected static ?int $navigationSort = 7;
protected static ?int $navigationSort = 10;
public static function canAccess(): bool
{