70 lines
2.2 KiB
PHP
70 lines
2.2 KiB
PHP
<?php
|
|
|
|
namespace App\Services\Hosting\Provisioning;
|
|
|
|
use App\Services\Hosting\Plesk\PleskClient;
|
|
use App\Services\Hosting\Proxmox\ProxmoxClient;
|
|
use App\Services\Hosting\Traefik\TraefikGenerator;
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
class ProvisioningRollback
|
|
{
|
|
private array $completed = [];
|
|
|
|
public function __construct(
|
|
private readonly ProxmoxClient $proxmox,
|
|
private readonly PleskClient $plesk,
|
|
private readonly TraefikGenerator $traefik,
|
|
) {}
|
|
|
|
public function mark(string $step, array $context = []): void
|
|
{
|
|
$this->completed[] = ['step' => $step, 'context' => $context];
|
|
}
|
|
|
|
public function execute(): void
|
|
{
|
|
Log::warning('Provisioning rollback started', ['steps' => count($this->completed)]);
|
|
|
|
foreach (array_reverse($this->completed) as $entry) {
|
|
try {
|
|
match ($entry['step']) {
|
|
'traefik_route' => $this->rollbackTraefik($entry['context']),
|
|
'plesk_dns' => $this->rollbackDns($entry['context']),
|
|
'proxmox_vm_started', 'proxmox_vm_created' => $this->rollbackVm($entry['context']),
|
|
default => null,
|
|
};
|
|
} catch (\Throwable $e) {
|
|
Log::error('Rollback step failed', [
|
|
'step' => $entry['step'],
|
|
'error' => $e->getMessage(),
|
|
]);
|
|
}
|
|
}
|
|
}
|
|
|
|
private function rollbackTraefik(array $context): void
|
|
{
|
|
if (! empty($context['domain'])) {
|
|
$this->traefik->removeCustomerRoute($context['domain']);
|
|
Log::info('Rollback: Traefik route removed', $context);
|
|
}
|
|
}
|
|
|
|
private function rollbackDns(array $context): void
|
|
{
|
|
if (! empty($context['domain']) && isset($context['subdomain'])) {
|
|
$this->plesk->deleteARecord($context['domain'], $context['subdomain']);
|
|
Log::info('Rollback: Plesk DNS removed', $context);
|
|
}
|
|
}
|
|
|
|
private function rollbackVm(array $context): void
|
|
{
|
|
if (! empty($context['vmid'])) {
|
|
$this->proxmox->deleteVM((int) $context['vmid']);
|
|
Log::info('Rollback: Proxmox VM deleted', $context);
|
|
}
|
|
}
|
|
}
|