migrating tests (DatabaseUser, Firewall and Logs)

This commit is contained in:
Saeed Vaziry
2024-10-09 23:32:52 +02:00
parent 0007e6f00a
commit 93cee92568
12 changed files with 214 additions and 56 deletions

View File

@ -3,7 +3,6 @@
namespace App\Actions\Server;
use App\Models\Server;
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\ValidationException;
class CreateServerLog
@ -13,8 +12,6 @@ class CreateServerLog
*/
public function create(Server $server, array $input): void
{
$this->validate($input);
$server->logs()->create([
'is_remote' => true,
'name' => $input['path'],
@ -23,13 +20,10 @@ public function create(Server $server, array $input): void
]);
}
/**
* @throws ValidationException
*/
protected function validate(array $input): void
public static function rules(): array
{
Validator::make($input, [
return [
'path' => 'required',
])->validate();
];
}
}

View File

@ -155,9 +155,24 @@ public function upload(string $local, string $remote): void
if (! $this->connection) {
$this->connect(true);
}
$this->connection->put($remote, $local, SFTP::SOURCE_LOCAL_FILE);
}
/**
* @throws Throwable
*/
public function download(string $local, string $remote): void
{
$this->log = null;
if (! $this->connection) {
$this->connect(true);
}
$this->connection->get($remote, $local, SFTP::SOURCE_LOCAL_FILE);
}
/**
* @throws Exception
*/

View File

@ -5,10 +5,12 @@
use Exception;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
use Symfony\Component\HttpFoundation\StreamedResponse;
use Throwable;
/**
* @property int $server_id
@ -44,12 +46,14 @@ public static function boot(): void
parent::boot();
static::deleting(function (ServerLog $log) {
try {
if (Storage::disk($log->disk)->exists($log->name)) {
Storage::disk($log->disk)->delete($log->name);
if ($log->is_remote) {
try {
if (Storage::disk($log->disk)->exists($log->name)) {
Storage::disk($log->disk)->delete($log->name);
}
} catch (Exception $e) {
Log::error($e->getMessage(), ['exception' => $e]);
}
} catch (Exception $e) {
Log::error($e->getMessage(), ['exception' => $e]);
}
});
}
@ -69,8 +73,28 @@ public function site(): BelongsTo
return $this->belongsTo(Site::class);
}
/**
* @throws Throwable
*/
public function download(): StreamedResponse
{
if ($this->is_remote) {
$tmpName = $this->server->id.'-'.strtotime('now').'-'.$this->type.'.log';
$tmpPath = Storage::disk('local')->path($tmpName);
$this->server->ssh()->download($tmpPath, $this->name);
dispatch(function () use ($tmpPath) {
if (File::exists($tmpPath)) {
File::delete($tmpPath);
}
})
->delay(now()->addMinutes(5))
->onQueue('default');
return Storage::disk('local')->download($tmpName, str($this->name)->afterLast('/'));
}
return Storage::disk($this->disk)->download($this->name);
}

View File

@ -30,7 +30,7 @@ public function mount(): void
protected function getHeaderActions(): array
{
return [
Action::make('Create User')
Action::make('create')
->icon('heroicon-o-plus')
->modalWidth(MaxWidth::Large)
->authorize(fn () => auth()->user()?->can('create', [DatabaseUser::class, $this->server]))

View File

@ -3,11 +3,14 @@
namespace App\Web\Pages\Servers\Logs;
use App\Models\ServerLog;
use App\Web\Contracts\HasSecondSubNav;
use App\Web\Pages\Servers\Logs\Widgets\LogsList;
use App\Web\Pages\Servers\Page;
class Index extends Page
class Index extends Page implements HasSecondSubNav
{
use Traits\Navigation;
protected static ?string $slug = 'servers/{server}/logs';
protected static ?string $title = 'Logs';

View File

@ -0,0 +1,54 @@
<?php
namespace App\Web\Pages\Servers\Logs;
use App\Actions\Server\CreateServerLog;
use App\Models\ServerLog;
use App\Web\Contracts\HasSecondSubNav;
use App\Web\Pages\Servers\Logs\Widgets\LogsList;
use App\Web\Pages\Servers\Page;
use Filament\Actions\Action;
use Filament\Forms\Components\TextInput;
use Filament\Support\Enums\MaxWidth;
class RemoteLogs extends Page implements HasSecondSubNav
{
use Traits\Navigation;
protected static ?string $slug = 'servers/{server}/logs/remote';
protected static ?string $title = 'Remote Logs';
public function mount(): void
{
$this->authorize('viewAny', [ServerLog::class, $this->server]);
}
public function getWidgets(): array
{
return [
[LogsList::class, ['server' => $this->server, 'remote' => true]],
];
}
protected function getHeaderActions(): array
{
return [
Action::make('create')
->icon('heroicon-o-plus')
->modalWidth(MaxWidth::Large)
->authorize(fn () => auth()->user()?->can('create', [ServerLog::class, $this->server]))
->form([
TextInput::make('path')
->helperText('The full path of the log file on the server')
->rules(fn (callable $get) => CreateServerLog::rules()['path']),
])
->modalSubmitActionLabel('Create')
->action(function (array $data) {
app(CreateServerLog::class)->create($this->server, $data);
$this->dispatch('$refresh');
}),
];
}
}

View File

@ -0,0 +1,34 @@
<?php
namespace App\Web\Pages\Servers\Logs\Traits;
use App\Models\ServerLog;
use App\Web\Pages\Servers\Logs\Index;
use App\Web\Pages\Servers\Logs\RemoteLogs;
use Filament\Navigation\NavigationGroup;
use Filament\Navigation\NavigationItem;
trait Navigation
{
public function getSecondSubNavigation(): array
{
$items = [];
if (auth()->user()->can('viewAny', [ServerLog::class, $this->server])) {
$items[] = NavigationItem::make(Index::getNavigationLabel())
->icon('heroicon-o-square-3-stack-3d')
->isActiveWhen(fn () => request()->routeIs(Index::getRouteName()))
->url(Index::getUrl(parameters: ['server' => $this->server]));
$items[] = NavigationItem::make(RemoteLogs::getNavigationLabel())
->icon('heroicon-o-wifi')
->isActiveWhen(fn () => request()->routeIs(RemoteLogs::getRouteName()))
->url(RemoteLogs::getUrl(parameters: ['server' => $this->server]));
}
return [
NavigationGroup::make()
->items($items),
];
}
}

View File

@ -26,6 +26,8 @@ class LogsList extends Widget
public ?string $label = '';
public bool $remote = false;
protected $listeners = ['$refresh'];
protected function getTableQuery(): Builder
@ -36,7 +38,8 @@ protected function getTableQuery(): Builder
if ($this->site) {
$query->where('site_id', $this->site->id);
}
});
})
->where('is_remote', $this->remote);
}
protected function getTableColumns(): array