143 lines
4.9 KiB
PHP
143 lines
4.9 KiB
PHP
<?php
|
|
|
|
namespace App\Livewire\Services;
|
|
|
|
use App\Livewire\Concerns\WithFleetContext;
|
|
use App\Services\FleetService;
|
|
use Illuminate\Support\Str;
|
|
use Livewire\Attributes\Layout;
|
|
use Livewire\Attributes\On;
|
|
use Livewire\Component;
|
|
use Throwable;
|
|
|
|
#[Layout('layouts.app')]
|
|
class Index extends Component
|
|
{
|
|
use WithFleetContext;
|
|
|
|
/** Active server name (from fleet context). */
|
|
public string $server = '—';
|
|
|
|
public bool $connected = false;
|
|
|
|
public bool $ready = false;
|
|
|
|
/** wire:model search over service name + description */
|
|
public string $search = '';
|
|
|
|
/** @var array<int, array{name:string,status:string,sub:string,enabled:bool,desc:string}> */
|
|
public array $services = [];
|
|
|
|
/** @var array<int, array{time:string,unit:string,level:string,text:string}> */
|
|
public array $journal = [];
|
|
|
|
public function mount(): void
|
|
{
|
|
$this->server = $this->activeServer()?->name ?? '—';
|
|
}
|
|
|
|
/** Lazy: the SSH read runs after the shell renders (wire:init), behind a skeleton. */
|
|
public function load(FleetService $fleet): void
|
|
{
|
|
$active = $this->activeServer();
|
|
|
|
if ($active && $active->credential_exists) {
|
|
try {
|
|
$data = $fleet->systemd($active);
|
|
$this->services = $data['services'];
|
|
$this->journal = $data['journal'];
|
|
$this->connected = true;
|
|
} catch (Throwable) {
|
|
$this->connected = false;
|
|
}
|
|
}
|
|
|
|
$this->ready = true;
|
|
}
|
|
|
|
/**
|
|
* Service control (R5): opens the wire-elements/modal confirm dialog. The
|
|
* modal writes the AuditEvent and re-dispatches `serviceConfirmed`, which
|
|
* applyService() handles. SSH exec (systemctl) is wired in behind this later.
|
|
*/
|
|
public function confirm(string $op, int $index): void
|
|
{
|
|
$name = $this->filteredServices[$index]['name'] ?? null;
|
|
if ($name === null) {
|
|
return;
|
|
}
|
|
|
|
[$heading, $phrase, $label, $action, $icon] = match ($op) {
|
|
'start' => [__('services.confirm_start_heading'), __('services.confirm_start_phrase'), __('services.confirm_start_label'), 'service.start', 'power'],
|
|
'stop' => [__('services.confirm_stop_heading'), __('services.confirm_stop_phrase'), __('services.confirm_stop_label'), 'service.stop', 'power'],
|
|
default => [__('services.confirm_restart_heading'), __('services.confirm_restart_phrase'), __('services.confirm_restart_label'), 'service.restart', 'rotate'],
|
|
};
|
|
|
|
$this->dispatch('openModal',
|
|
component: 'modals.confirm-action',
|
|
arguments: [
|
|
'heading' => $heading,
|
|
'body' => __('services.confirm_body', ['name' => $name, 'phrase' => $phrase, 'server' => $this->server]),
|
|
'confirmLabel' => $label,
|
|
'danger' => $op === 'stop',
|
|
'icon' => $icon,
|
|
'auditAction' => $action,
|
|
'auditTarget' => "{$name} · {$this->server}",
|
|
'event' => 'serviceConfirmed',
|
|
'params' => ['op' => $op, 'name' => $name],
|
|
'notify' => __('services.confirm_notify', ['name' => $name, 'label' => $label]),
|
|
],
|
|
);
|
|
}
|
|
|
|
/** Performs the confirmed systemctl action over SSH, then reloads real state. */
|
|
#[On('serviceConfirmed')]
|
|
public function applyService(string $op, string $name, FleetService $fleet): void
|
|
{
|
|
$active = $this->activeServer();
|
|
|
|
if (! $active || ! $active->credential_exists) {
|
|
return;
|
|
}
|
|
|
|
try {
|
|
$res = $fleet->serviceAction($active, $op, $name);
|
|
} catch (Throwable $e) {
|
|
$this->dispatch('notify', message: __('services.action_error', ['name' => $name, 'error' => $e->getMessage()]));
|
|
|
|
return;
|
|
}
|
|
|
|
$this->dispatch('notify', message: $res['ok']
|
|
? __('services.action_done', ['name' => $name, 'op' => $op])
|
|
: __('services.action_failed', ['name' => $name, 'output' => Str::limit($res['output'] ?: __('services.no_permission'), 90)]));
|
|
|
|
try {
|
|
$this->services = $fleet->systemd($active)['services'];
|
|
} catch (Throwable) {
|
|
// keep current list on reload failure
|
|
}
|
|
}
|
|
|
|
/** @return array<int, array{name:string,status:string,sub:string,enabled:bool,desc:string}> */
|
|
public function getFilteredServicesProperty(): array
|
|
{
|
|
$q = trim(mb_strtolower($this->search));
|
|
|
|
if ($q === '') {
|
|
return $this->services;
|
|
}
|
|
|
|
return array_values(array_filter(
|
|
$this->services,
|
|
fn (array $s): bool => str_contains(mb_strtolower($s['name']), $q)
|
|
|| str_contains(mb_strtolower($s['desc']), $q)
|
|
));
|
|
}
|
|
|
|
public function render()
|
|
{
|
|
return view('livewire.services.index')->title(__('services.title'));
|
|
}
|
|
}
|