mailwolt/app/Console/Commands/MailwoltRestart.php

122 lines
4.1 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
class MailwoltRestart extends Command
{
protected $signature = 'mailwolt:restart-services';
protected $description = 'Restart or reload MailWolt-related system services';
public function handle(): int
{
$units = config('mailwolt.units', []);
foreach ($units as $u) {
$base = (string)($u['name'] ?? '');
$unit = str_ends_with($base, '.service') ? $base : $base . '.service';
$action = (string)($u['action'] ?? 'try-reload-or-restart');
$this->info("{$unit} ({$action})");
// Existiert die Unit?
$existsCmd = sprintf('systemctl status %s >/dev/null 2>&1', escapeshellarg($unit));
exec($existsCmd, $_o, $existsRc);
if ($existsRc !== 0) {
$this->line(" {$unit} existiert nicht übersprungen");
continue;
}
// Restart/Reload via sudo (ohne Passwort)
$cmd = sprintf('sudo -n /usr/bin/systemctl %s %s', escapeshellarg($action), escapeshellarg($unit));
exec($cmd . ' 2>&1', $out, $rc);
foreach ($out as $line) {
$this->line(" $line");
}
$rc === 0 ? $this->line(' [✓] Erfolgreich') : $this->warn(" [!] Fehler (rc={$rc})");
}
return self::SUCCESS;
}
}
//class MailwoltRestart extends Command
//{
// protected $signature = 'mailwolt:restart-services';
// protected $description = 'Restart or reload MailWolt-related system services';
//
// public function handle(): int
// {
// $units = config('mailwolt.units', []);
// $allowed = ['reload','restart','try-reload-or-restart'];
//
// foreach ($units as $u) {
// $base = (string)($u['name'] ?? '');
// $unit = str_ends_with($base, '.service') ? $base : $base . '.service';
// $action = $u['action'] ?? 'try-reload-or-restart';
// if (!in_array($action, $allowed, true)) $action = 'try-reload-or-restart';
//
// // existiert Unit überhaupt?
// $probe = Process::fromShellCommandline("systemctl status $unit >/dev/null 2>&1");
// $probe->run();
// if ($probe->getExitCode() !== 0) {
// $this->warn("→ {$unit} existiert nicht übersprungen");
// continue;
// }
//
// $this->info("→ {$unit} ({$action})");
// $p = new Process(['sudo','-n','/usr/bin/systemctl',$action,$unit]);
// $p->setTimeout(15);
// $p->run();
//
// if (!$p->isSuccessful()) {
// $this->warn(" [!] Fehler (rc={$p->getExitCode()})");
// Log::warning('service restart failed', [
// 'unit'=>$unit,'action'=>$action,
// 'out'=>$p->getOutput(), 'err'=>$p->getErrorOutput()
// ]);
// } else {
// $this->line(" [✓] Erfolgreich");
// }
// }
// return self::SUCCESS;
// }
//}
//
//use Illuminate\Console\Command;
//
//class MailwoltRestart extends Command
//{
// protected $signature = 'mailwolt:restart-services';
// protected $description = 'Restart or reload MailWolt-related system services';
//
// public function handle(): int
// {
// $units = config('mailwolt.units', []);
//
// foreach ($units as $u) {
// $unit = rtrim($u['name'] ?? '', '.service') . '.service';
// $action = $u['action'] ?? 'try-reload-or-restart';
//
// $cmd = sprintf('sudo -n /usr/bin/systemctl %s %s', escapeshellarg($action), escapeshellarg($unit));
// $this->info("→ {$unit} ({$action})");
//
// exec($cmd . ' 2>&1', $out, $rc);
// foreach ($out as $line) {
// $this->line(" $line");
// }
//
// if ($rc !== 0) {
// $this->warn(" [!] Fehler beim Neustart von {$unit} (rc={$rc})");
// } else {
// $this->line(" [✓] Erfolgreich");
// }
// }
//
// return self::SUCCESS;
// }
//}