netgescon-day0/app/Console/Commands/NetgesconNodeRefreshCommand.php

141 lines
5.2 KiB
PHP

<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Artisan;
class NetgesconNodeRefreshCommand extends Command
{
protected $signature = 'netgescon:node-refresh
{--remote=origin : Remote Git da usare}
{--branch=main : Branch da allineare}
{--force : Continua anche con working tree sporco lato sync}
{--skip-migrate : Salta php artisan migrate --force}
{--skip-view-cache : Salta rebuild view cache}
{--progress-file= : File JSON dove scrivere lo stato avanzamento}';
protected $description = 'Aggiorna il nodo da Gitea e applica le manutenzioni post-sync senza usare il terminale';
public function handle(): int
{
$remote = trim((string) $this->option('remote')) ?: 'origin';
$branch = trim((string) $this->option('branch')) ?: 'main';
$force = (bool) $this->option('force');
$skipMigrate = (bool) $this->option('skip-migrate');
$skipViewCache = (bool) $this->option('skip-view-cache');
$this->writeProgress(2, 'Avvio refresh nodo da Gitea', 'running');
$gitParams = [
'--remote' => $remote,
'--branch' => $branch,
];
if ($force) {
$gitParams['--force'] = true;
}
$this->info("Sync Git da {$remote}/{$branch}...");
$this->writeProgress(15, 'Sync Git da Gitea in corso', 'running');
$gitExit = Artisan::call('netgescon:git-sync', $gitParams);
$gitOutput = trim((string) Artisan::output());
if ($gitExit !== 0) {
$this->error('Sync Git fallita.');
$this->line($gitOutput);
$this->writeProgress(100, 'Sync Git fallita', 'failed', $gitExit);
return self::FAILURE;
}
$fullOutput = [
'NODE REFRESH: netgescon:git-sync',
$gitOutput,
];
if (! $skipMigrate) {
$this->info('Esecuzione migrate --force...');
$this->writeProgress(55, 'Applicazione migration', 'running');
$migrateExit = Artisan::call('migrate', ['--force' => true]);
$migrateOutput = trim((string) Artisan::output());
$fullOutput[] = 'NODE REFRESH: php artisan migrate --force';
$fullOutput[] = $migrateOutput;
if ($migrateExit !== 0) {
$this->error('Migration fallita.');
$this->line($migrateOutput);
$this->writeProgress(100, 'Migration fallita', 'failed', $migrateExit);
return self::FAILURE;
}
}
$this->info('Pulizia cache applicative...');
$this->writeProgress(72, 'Pulizia cache applicative', 'running');
$optimizeExit = Artisan::call('optimize:clear');
$optimizeOutput = trim((string) Artisan::output());
$fullOutput[] = 'NODE REFRESH: php artisan optimize:clear';
$fullOutput[] = $optimizeOutput;
if ($optimizeExit !== 0) {
$this->error('optimize:clear fallito.');
$this->line($optimizeOutput);
$this->writeProgress(100, 'Pulizia cache fallita', 'failed', $optimizeExit);
return self::FAILURE;
}
if (! $skipViewCache) {
$this->info('Ricostruzione view cache...');
$this->writeProgress(88, 'Ricostruzione view cache', 'running');
$viewClearExit = Artisan::call('view:clear');
$viewClearOutput = trim((string) Artisan::output());
$fullOutput[] = 'NODE REFRESH: php artisan view:clear';
$fullOutput[] = $viewClearOutput;
if ($viewClearExit !== 0) {
$this->error('view:clear fallito.');
$this->line($viewClearOutput);
$this->writeProgress(100, 'view:clear fallito', 'failed', $viewClearExit);
return self::FAILURE;
}
$viewCacheExit = Artisan::call('view:cache');
$viewCacheOutput = trim((string) Artisan::output());
$fullOutput[] = 'NODE REFRESH: php artisan view:cache';
$fullOutput[] = $viewCacheOutput;
if ($viewCacheExit !== 0) {
$this->error('view:cache fallita.');
$this->line($viewCacheOutput);
$this->writeProgress(100, 'view:cache fallita', 'failed', $viewCacheExit);
return self::FAILURE;
}
}
$this->line(implode(PHP_EOL . PHP_EOL, array_filter($fullOutput, static fn(string $chunk): bool => trim($chunk) !== '')));
$this->writeProgress(100, 'Refresh nodo completato', 'completed', 0);
return self::SUCCESS;
}
private function writeProgress(int $percent, string $message, string $status, ?int $exitCode = null): void
{
$path = trim((string) $this->option('progress-file'));
if ($path === '') {
return;
}
@file_put_contents($path, json_encode([
'timestamp' => now()->toIso8601String(),
'percent' => max(0, min(100, $percent)),
'message' => $message,
'status' => $status,
'exit_code' => $exitCode,
], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES));
}
}