mailwolt/app/Console/Commands/MigrateConfigNames.php

65 lines
2.3 KiB
PHP

<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
class MigrateConfigNames extends Command
{
protected $signature = 'clubird:migrate-config-names {--dry-run : Nur anzeigen was gemacht würde}';
protected $description = 'Benennt server-seitige Config-Dateien von mailwolt-* auf generische Namen um';
private array $renames = [
'/etc/rspamd/local.d/mailwolt-actions.conf' => '/etc/rspamd/local.d/actions.conf',
'/etc/dovecot/conf.d/99-mailwolt-tls.conf' => '/etc/dovecot/conf.d/99-tls.conf',
'/etc/postfix/mailwolt-tls.cf' => '/etc/postfix/tls.cf',
'/etc/fail2ban/jail.d/mailwolt-whitelist.local' => '/etc/fail2ban/jail.d/whitelist.local',
'/etc/fail2ban/jail.d/00-mailwolt-defaults.local'=> '/etc/fail2ban/jail.d/00-defaults.local',
];
public function handle(): int
{
$dry = $this->option('dry-run');
foreach ($this->renames as $old => $new) {
if (!file_exists($old)) {
$this->line(" <fg=gray>übersprungen</> {$old} (nicht vorhanden)");
continue;
}
if (file_exists($new) && !is_link($new)) {
$this->line(" <fg=yellow>bereits vorhanden</> {$new}");
continue;
}
if ($dry) {
$this->line(" <fg=cyan>[dry-run]</> mv {$old}{$new}");
continue;
}
// Als root direkt umbenennen; als www-data via sudo (sudo mv muss in sudoers sein)
if (posix_getuid() === 0) {
$ok = @rename($old, $new);
$errMsg = error_get_last()['message'] ?? '';
} else {
@exec('sudo -n mv ' . escapeshellarg($old) . ' ' . escapeshellarg($new) . ' 2>&1', $out, $rc);
$ok = ($rc === 0);
$errMsg = implode(' ', $out);
}
if ($ok) {
$this->info(" umbenannt: {$old}{$new}");
} else {
$this->error(" FEHLER bei {$old}: {$errMsg}");
$this->line(" → manuell: <fg=yellow>mv {$old} {$new}</>");
}
}
if (!$dry) {
$this->info('Fertig. Dienste ggf. neu starten: rspamd, dovecot, postfix, fail2ban');
}
return self::SUCCESS;
}
}