- sgc:production-check confere ambiente, https, PostgreSQL, migrações, MFA, pastas e pacotes partilhados (só leitura). - scripts/producao/backup.sh e restore-check.sh: pg_dump + ficheiros privados com hashes e contagens; ensaio de restauro em base temporária. - docs/producao.md: .env de produção, publicação, cópias, plano do piloto e resultado da bateria em PostgreSQL. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
94 lines
4.3 KiB
PHP
94 lines
4.3 KiB
PHP
<?php
|
|
|
|
namespace App\Console\Commands;
|
|
|
|
use App\Services\ProductModules;
|
|
use Illuminate\Console\Command;
|
|
use Illuminate\Database\Migrations\Migrator;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Laravel\Fortify\Features;
|
|
use Symfony\Component\Process\Process;
|
|
use Throwable;
|
|
|
|
/** Conferência da instalação de produção (fase 5). Só lê configuração e esquema; não altera dados. */
|
|
class SgcProductionCheck extends Command
|
|
{
|
|
protected $signature = 'sgc:production-check';
|
|
|
|
protected $description = 'Conferir se a instalação está pronta para produção (ambiente, base de dados, migrações, MFA, ficheiros); não altera dados';
|
|
|
|
private int $failures = 0;
|
|
|
|
public function handle(Migrator $migrator): int
|
|
{
|
|
$this->check(app()->environment('production'), 'APP_ENV=production');
|
|
$this->check(! config('app.debug'), 'APP_DEBUG=false');
|
|
$this->check(filled(config('app.key')), 'APP_KEY definida');
|
|
$https = str_starts_with((string) config('app.url'), 'https://');
|
|
$this->check($https, 'APP_URL em https');
|
|
$this->check(! $https || config('session.secure') === true, 'SESSION_SECURE_COOKIE=true');
|
|
$this->check(! in_array(config('session.driver'), ['array', 'cookie'], true), 'Sessões persistentes (SESSION_DRIVER)');
|
|
$this->check(config('cache.default') !== 'array', 'Cache persistente (CACHE_STORE)');
|
|
|
|
$this->database($migrator);
|
|
|
|
$this->check(Features::enabled(Features::twoFactorAuthentication()), 'MFA ligado (SGC_TWO_FACTOR_ENABLED=true)');
|
|
$this->check(array_filter((array) config('mfa.required_functions')) !== [], 'Funções com MFA obrigatório (SGC_MFA_REQUIRED_FUNCTIONS)');
|
|
|
|
foreach ([storage_path('app/private'), storage_path('logs'), storage_path('framework/sessions'), base_path('bootstrap/cache')] as $path) {
|
|
$this->check(is_dir($path) && is_writable($path), 'Pasta gravável: '.str_replace(base_path().'/', '', $path));
|
|
}
|
|
|
|
$runtime = new Process([PHP_BINARY, base_path('scripts/check-runtime-packages.php')], base_path());
|
|
$runtime->run();
|
|
$this->check($runtime->isSuccessful(), 'Pacotes partilhados conferidos (runtime:check)');
|
|
|
|
if (in_array(config('mail.default'), ['log', 'array'], true)) {
|
|
$this->warn('AVISO Email não configurado (MAIL_MAILER='.config('mail.default').'): avisos de prazos não saem da aplicação.');
|
|
}
|
|
$modules = collect(app(ProductModules::class)->states())->map(fn (bool $on, string $key) => $key.'='.($on ? 'ligado' : 'desligado'))->implode(', ');
|
|
$this->line('INFO Módulos: '.$modules.'; cancelamento de ordens='.(config('order_cancellation.enabled') ? 'ligado' : 'desligado'));
|
|
$this->line('INFO Confirme também: cron com `php artisan schedule:run` a cada minuto e cópias de segurança diárias (docs/producao.md).');
|
|
|
|
if ($this->failures > 0) {
|
|
$this->error($this->failures.' verificação(ões) falharam. A instalação não está pronta para produção.');
|
|
|
|
return self::FAILURE;
|
|
}
|
|
$this->info('Instalação pronta para produção. Não substitui o restauro de teste nem a aceitação do piloto.');
|
|
|
|
return self::SUCCESS;
|
|
}
|
|
|
|
private function database(Migrator $migrator): void
|
|
{
|
|
try {
|
|
DB::connection()->getPdo();
|
|
} catch (Throwable) {
|
|
$this->check(false, 'Ligação à base de dados');
|
|
|
|
return;
|
|
}
|
|
$this->check(DB::connection()->getDriverName() === 'pgsql', 'Base de dados PostgreSQL');
|
|
if (! $migrator->repositoryExists()) {
|
|
$this->check(false, 'Tabela de migrações presente');
|
|
|
|
return;
|
|
}
|
|
$files = $migrator->getMigrationFiles([database_path('migrations'), ...$migrator->paths()]);
|
|
$pending = array_diff(array_keys($files), $migrator->getRepository()->getRan());
|
|
$this->check($pending === [], $pending === [] ? 'Migrações aplicadas' : 'Migrações pendentes: '.count($pending).' ('.implode(', ', array_slice($pending, 0, 3)).')');
|
|
}
|
|
|
|
private function check(bool $passed, string $label): void
|
|
{
|
|
if ($passed) {
|
|
$this->line('OK '.$label);
|
|
|
|
return;
|
|
}
|
|
$this->failures++;
|
|
$this->line('<fg=red>FALHA</> '.$label);
|
|
}
|
|
}
|