You've already forked Atomcms-edit
Initial commit
This commit is contained in:
Executable
+272
@@ -0,0 +1,272 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Console\Commands;
|
||||
|
||||
use App\Enums\AlertChannel;
|
||||
use App\Enums\AlertType;
|
||||
use App\Services\AlertService;
|
||||
use App\Services\NitroUpdateService;
|
||||
use Illuminate\Console\Command;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
|
||||
class NitroUpdateCommand extends Command
|
||||
{
|
||||
#[\Override]
|
||||
protected $signature = 'nitro:auto
|
||||
{--force : Forceer update ook al is het niet de geplande tijd}
|
||||
{--build-only : Alleen build en deploy uitvoeren}
|
||||
{--full : Volledige reset en reinstall}
|
||||
{--repair : Probeer Nitro te repareren}
|
||||
{--diagnose : Toon diagnose informatie}';
|
||||
|
||||
#[\Override]
|
||||
protected $description = 'Automatische Nitro client en renderer updates';
|
||||
|
||||
private const string CACHE_KEY_LAST_UPDATE = 'nitro_last_update';
|
||||
|
||||
public function handle(AlertService $alertService, NitroUpdateService $nitroService): int
|
||||
{
|
||||
$this->info('Nitro update check...');
|
||||
|
||||
if ($this->option('diagnose')) {
|
||||
return $this->diagnose($nitroService);
|
||||
}
|
||||
|
||||
if ($this->option('repair')) {
|
||||
return $this->repair($alertService, $nitroService);
|
||||
}
|
||||
|
||||
$isForced = $this->option('force');
|
||||
$buildOnly = $this->option('build-only');
|
||||
$full = $this->option('full');
|
||||
|
||||
if ($buildOnly) {
|
||||
return $this->buildOnly($alertService, $nitroService);
|
||||
}
|
||||
|
||||
if (! $isForced && ! $this->isScheduledTime()) {
|
||||
$this->line('Niet de geplande tijd, overslaan.');
|
||||
|
||||
return Command::SUCCESS;
|
||||
}
|
||||
|
||||
$repo = setting('nitro_github_url', 'duckietm/Nitro-V3 (default)');
|
||||
$this->info("GitHub: {$repo}");
|
||||
|
||||
if ($full) {
|
||||
return $this->fullReinstall($alertService, $nitroService);
|
||||
}
|
||||
|
||||
$status = $nitroService->getStatus();
|
||||
|
||||
$this->info('Client: ' . ($status['client_installed'] ? '✅' : '❌'));
|
||||
$this->info('Renderer: ' . ($status['renderer_installed'] ? '✅' : '❌'));
|
||||
$this->info('Build: ' . ($status['build_exists'] ? '✅' : '❌'));
|
||||
$this->info('Deployed: ' . ($status['deployed'] ? '✅' : '❌'));
|
||||
|
||||
if (! $status['client_installed'] || ! $status['renderer_installed']) {
|
||||
$this->warn('Nitro niet volledig geïnstalleerd. Voer --full uit voor volledige installatie.');
|
||||
if ($this->confirm('Wil je nu volledig installeren?')) {
|
||||
return $this->fullReinstall($alertService, $nitroService);
|
||||
}
|
||||
|
||||
return Command::SUCCESS;
|
||||
}
|
||||
|
||||
$this->line('✅ Nitro client is up-to-date.');
|
||||
|
||||
if ($isForced) {
|
||||
$this->warn('Force update aangevraagd...');
|
||||
|
||||
return $this->fullReinstall($alertService, $nitroService);
|
||||
}
|
||||
|
||||
Cache::put(self::CACHE_KEY_LAST_UPDATE, now()->toIso8601String());
|
||||
|
||||
return Command::SUCCESS;
|
||||
}
|
||||
|
||||
private function diagnose(NitroUpdateService $nitroService): int
|
||||
{
|
||||
$this->info('🔍 Nitro diagnose...');
|
||||
$diagnosis = $nitroService->diagnose();
|
||||
|
||||
$this->newLine();
|
||||
$this->info('=== Controle Resultaten ===');
|
||||
|
||||
$checks = $diagnosis['checks'] ?? [];
|
||||
foreach ($checks as $key => $value) {
|
||||
if (is_bool($value)) {
|
||||
$this->line(($value ? '✅' : '❌') . ' ' . $key);
|
||||
} elseif (is_array($value)) {
|
||||
$this->line($key . ': ' . count($value) . ' items');
|
||||
} else {
|
||||
$this->line($key . ': ' . $value);
|
||||
}
|
||||
}
|
||||
|
||||
if (! empty($diagnosis['issues'])) {
|
||||
$this->newLine();
|
||||
$this->error('=== Problemen ===');
|
||||
foreach ($diagnosis['issues'] as $issue) {
|
||||
$this->line('❌ ' . $issue);
|
||||
}
|
||||
}
|
||||
|
||||
if (! empty($diagnosis['recommendations'])) {
|
||||
$this->newLine();
|
||||
$this->info('=== Aanbevelingen ===');
|
||||
foreach ($diagnosis['recommendations'] as $rec) {
|
||||
$this->line('💡 ' . $rec);
|
||||
}
|
||||
}
|
||||
|
||||
return Command::SUCCESS;
|
||||
}
|
||||
|
||||
private function repair(AlertService $alertService, NitroUpdateService $nitroService): int
|
||||
{
|
||||
$this->warn('🔧 Nitro repair modus gestart...');
|
||||
$this->info('Dit zal de Nitro installatie controleren en repareren.');
|
||||
|
||||
try {
|
||||
$repairResult = $nitroService->repair();
|
||||
|
||||
if ($repairResult['success']) {
|
||||
$this->info('✅ Repair succesvol!');
|
||||
foreach ($repairResult['actions'] ?? [] as $action) {
|
||||
$this->line(' - ' . $action);
|
||||
}
|
||||
$alertService->send(
|
||||
AlertType::EMULATOR_UPDATE,
|
||||
'Nitro client gerepareerd',
|
||||
['actions' => $repairResult['actions'] ?? []],
|
||||
AlertChannel::DISCORD,
|
||||
);
|
||||
|
||||
return Command::SUCCESS;
|
||||
}
|
||||
|
||||
$this->error('❌ Repair mislukt: ' . ($repairResult['error'] ?? 'Onbekende fout'));
|
||||
if (! empty($repairResult['actions'])) {
|
||||
$this->line('Uitgevoerde acties:');
|
||||
foreach ($repairResult['actions'] as $action) {
|
||||
$this->line(' - ' . $action);
|
||||
}
|
||||
}
|
||||
if (! empty($repairResult['errors'])) {
|
||||
$this->line('Fouten:');
|
||||
foreach ($repairResult['errors'] as $error) {
|
||||
$this->line(' ❌ ' . $error);
|
||||
}
|
||||
}
|
||||
$alertService->send(
|
||||
AlertType::EMULATOR_ERROR,
|
||||
'Nitro repair mislukt: ' . ($repairResult['error'] ?? 'Onbekende fout'),
|
||||
[],
|
||||
AlertChannel::DISCORD,
|
||||
);
|
||||
|
||||
return Command::FAILURE;
|
||||
|
||||
} catch (\Exception $e) {
|
||||
$this->error('❌ Repair exception: ' . $e->getMessage());
|
||||
|
||||
return Command::FAILURE;
|
||||
}
|
||||
}
|
||||
|
||||
private function buildOnly(AlertService $alertService, NitroUpdateService $nitroService): int
|
||||
{
|
||||
$this->info('Build en deploy uitvoeren...');
|
||||
|
||||
$buildResult = $nitroService->buildClient();
|
||||
|
||||
if (! $buildResult['success']) {
|
||||
$this->error('Build mislukt: ' . ($buildResult['error'] ?? 'Onbekend'));
|
||||
|
||||
return Command::FAILURE;
|
||||
}
|
||||
|
||||
$nitroService->deployClient();
|
||||
$nitroService->generateConfigs();
|
||||
|
||||
$this->info('Client succesvol gedeployed!');
|
||||
$alertService->send(
|
||||
AlertType::EMULATOR_UPDATE,
|
||||
'Nitro client opnieuw gedeployed',
|
||||
[],
|
||||
AlertChannel::DISCORD,
|
||||
);
|
||||
|
||||
return Command::SUCCESS;
|
||||
}
|
||||
|
||||
private function fullReinstall(AlertService $alertService, NitroUpdateService $nitroService): int
|
||||
{
|
||||
$this->warn('Volledige reinstall wordt uitgevoerd...');
|
||||
|
||||
try {
|
||||
$result = $nitroService->updateNitro();
|
||||
|
||||
if ($result['success']) {
|
||||
$this->info('Nitro succesvol opnieuw geïnstalleerd!');
|
||||
$alertService->send(
|
||||
AlertType::EMULATOR_UPDATE,
|
||||
'Nitro client succesvol geüpdatet en gedeployed',
|
||||
[],
|
||||
AlertChannel::DISCORD,
|
||||
);
|
||||
|
||||
return Command::SUCCESS;
|
||||
}
|
||||
|
||||
$this->error('Reinstall mislukt: ' . ($result['error'] ?? 'Onbekende fout'));
|
||||
$alertService->send(
|
||||
AlertType::CRITICAL_ERROR,
|
||||
'Nitro Update Mislukt: ' . ($result['error'] ?? 'Onbekende fout'),
|
||||
[],
|
||||
AlertChannel::DISCORD,
|
||||
);
|
||||
|
||||
return Command::FAILURE;
|
||||
|
||||
} catch (\Exception $e) {
|
||||
$this->error('Reinstall exception: ' . $e->getMessage());
|
||||
|
||||
return Command::FAILURE;
|
||||
}
|
||||
}
|
||||
|
||||
private function isScheduledTime(): bool
|
||||
{
|
||||
$scheduleTime = setting('nitro_auto_update_schedule', '03:00');
|
||||
$scheduleDays = setting('nitro_auto_update_days', '0,6');
|
||||
$enabled = setting('nitro_auto_update_enabled', false);
|
||||
|
||||
if (! $enabled) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$now = now();
|
||||
$currentTime = $now->format('H:i');
|
||||
$currentDay = (int) $now->dayOfWeek;
|
||||
|
||||
$allowedDays = array_map(intval(...), explode(',', $scheduleDays));
|
||||
|
||||
if (! in_array($currentDay, $allowedDays)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ($currentTime !== $scheduleTime) {
|
||||
$minuteDiff = abs(strtotime($currentTime) - strtotime((string) $scheduleTime)) / 60;
|
||||
if ($minuteDiff > 5) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user