130 lines
3.9 KiB
PHP
130 lines
3.9 KiB
PHP
<?php
|
|
|
|
namespace App\Filament\SuperAdmin\Pages;
|
|
|
|
use App\Models\InfrastructureActionLog;
|
|
use App\Services\Dokploy\DokployClient;
|
|
use BackedEnum;
|
|
use Filament\Notifications\Notification;
|
|
use Filament\Pages\Page;
|
|
use Illuminate\Support\Arr;
|
|
|
|
class DokployDeployments extends Page
|
|
{
|
|
protected static string|BackedEnum|null $navigationIcon = 'heroicon-o-wrench-screwdriver';
|
|
|
|
protected static ?string $navigationLabel = 'Infrastructure';
|
|
|
|
protected static ?string $title = 'Infrastructure Controls';
|
|
|
|
protected string $view = 'filament.super-admin.pages.dokploy-deployments';
|
|
|
|
public array $applications = [];
|
|
|
|
public array $recentLogs = [];
|
|
|
|
public ?string $dokployWebUrl = null;
|
|
|
|
public function mount(DokployClient $client): void
|
|
{
|
|
$this->dokployWebUrl = config('dokploy.web_url');
|
|
$this->refreshApplications($client);
|
|
$this->refreshLogs();
|
|
}
|
|
|
|
public function reload(string $applicationId): void
|
|
{
|
|
$this->performAction($applicationId, 'reload');
|
|
}
|
|
|
|
public function redeploy(string $applicationId): void
|
|
{
|
|
$this->performAction($applicationId, 'redeploy');
|
|
}
|
|
|
|
protected function performAction(string $applicationId, string $action): void
|
|
{
|
|
$client = app(DokployClient::class);
|
|
|
|
if (! $this->isKnownApplication($applicationId)) {
|
|
Notification::make()
|
|
->danger()
|
|
->title('Unknown service')
|
|
->body("The application ID {$applicationId} is not configured.")
|
|
->send();
|
|
|
|
return;
|
|
}
|
|
|
|
try {
|
|
$action === 'reload'
|
|
? $client->reloadApplication($applicationId, auth()->user())
|
|
: $client->redeployApplication($applicationId, auth()->user());
|
|
|
|
Notification::make()
|
|
->success()
|
|
->title(ucfirst($action).' requested')
|
|
->body("Dokploy accepted the {$action} action for {$applicationId}.")
|
|
->send();
|
|
} catch (\Throwable $exception) {
|
|
Notification::make()
|
|
->danger()
|
|
->title('Dokploy request failed')
|
|
->body($exception->getMessage())
|
|
->send();
|
|
}
|
|
|
|
$this->refreshApplications($client);
|
|
$this->refreshLogs();
|
|
}
|
|
|
|
protected function refreshApplications(DokployClient $client): void
|
|
{
|
|
$applicationMap = config('dokploy.applications', []);
|
|
$results = [];
|
|
|
|
foreach ($applicationMap as $label => $id) {
|
|
try {
|
|
$status = $client->applicationStatus($id);
|
|
$application = Arr::get($status, 'application', []);
|
|
|
|
$results[] = [
|
|
'label' => ucfirst($label),
|
|
'application_id' => $id,
|
|
'status' => Arr::get($application, 'applicationStatus', 'unknown'),
|
|
];
|
|
} catch (\Throwable $e) {
|
|
$results[] = [
|
|
'label' => ucfirst($label),
|
|
'application_id' => $id,
|
|
'status' => 'error',
|
|
];
|
|
}
|
|
}
|
|
|
|
$this->applications = $results;
|
|
}
|
|
|
|
protected function refreshLogs(): void
|
|
{
|
|
$this->recentLogs = InfrastructureActionLog::query()
|
|
->with('user')
|
|
->latest()
|
|
->limit(5)
|
|
->get()
|
|
->map(fn ($log) => [
|
|
'created_at' => $log->created_at->diffForHumans(),
|
|
'user' => $log->user?->name ?? 'System',
|
|
'service_id' => $log->service_id,
|
|
'action' => $log->action,
|
|
'status_code' => $log->status_code,
|
|
])
|
|
->toArray();
|
|
}
|
|
|
|
protected function isKnownApplication(string $applicationId): bool
|
|
{
|
|
return in_array($applicationId, array_values(config('dokploy.applications', [])), true);
|
|
}
|
|
}
|