mailwolt/app/Livewire/Ui/System/UpdateManager.php

73 lines
2.1 KiB
PHP

<?php
namespace App\Livewire\Ui\System;
use Livewire\Component;
class UpdateManager extends Component
{
public bool $running = false;
public string $log = '';
public ?int $rc = null;
public ?string $latest = null; // optional: von Cache o.ä.
public function mount(): void
{
$this->refreshState();
// Optional: latest Tag/Version aus Cache anzeigen
$this->latest = cache('mailwolt.update_available');
}
public function refreshState(): void
{
$state = @trim(@file_get_contents('/var/lib/mailwolt/update/state') ?: '');
$this->running = ($state === 'running');
$rcRaw = @trim(@file_get_contents('/var/lib/mailwolt/update/rc') ?: '');
$this->rc = is_numeric($rcRaw) ? (int)$rcRaw : null;
// letzte 200 Zeilen Log
$this->log = @shell_exec('tail -n 200 /var/log/mailwolt-update.log 2>/dev/null') ?? '';
}
public function runUpdate(): void
{
// Hinweis „Update verfügbar“ ausblenden
cache()->forget('mailwolt.update_available');
// Update im Hintergrund starten
@shell_exec('nohup sudo -n /usr/local/sbin/mw-update >/dev/null 2>&1 &');
$this->running = true;
$this->dispatch('toast',
type: 'info',
badge: 'Update',
title: 'Update gestartet',
text: 'Das System wird aktualisiert …',
duration: 6000
);
}
public function poll(): void
{
$before = $this->running;
$this->refreshState();
if ($before && !$this->running && $this->rc !== null) {
$ok = ($this->rc === 0);
$this->dispatch('toast',
type: $ok ? 'done' : 'warn',
badge: 'Update',
title: $ok ? 'Update abgeschlossen' : 'Update fehlgeschlagen',
text: $ok ? 'Die neue Version ist aktiv.' : "Fehlercode: {$this->rc}. Log prüfen.",
duration: 8000
);
}
}
public function render()
{
return view('livewire.ui.system.update-manager');
}
}