Files
Virtnet.bond/extensions/Servers/Proxmox/Jobs/ReinstallJob.php
2025-11-30 11:57:22 +01:00

74 lines
2.3 KiB
PHP

<?php
namespace Paymenter\Extensions\Servers\Proxmox\Jobs;
use App\Helpers\ExtensionHelper;
use App\Helpers\NotificationHelper;
use Exception;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Str;
use Paymenter\Extensions\Servers\Proxmox\Models\Server;
use Paymenter\Extensions\Servers\Proxmox\Proxmox;
class ReinstallJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
public int $tries = 5;
public int $timeout = 600; // 10 minutes
public function __construct(public Server $server) {}
public function handle(): void
{
$proxmox = new Proxmox;
// Stop and delete existing VM if it exists
try {
$proxmox->requestAndWait(
'/nodes/' . $this->server->node->name . '/qemu/' . $this->server->vm_id . '/status/stop',
'post',
['vmid' => $this->server->vm_id],
$this->server->node->name
);
$proxmox->requestAndWait(
'/nodes/' . $this->server->node->name . '/qemu/' . $this->server->vm_id,
'delete',
[],
$this->server->node->name
);
} catch (Exception $e) {
if (!str_contains($e->getMessage(), 'does not exist')) {
throw $e;
}
}
// Generate a new password
$password = Str::password(16);
// Reinstall server — install() will now use the updated password
$settings = array_merge(ExtensionHelper::settingsToArray($this->server->service->product->settings), ExtensionHelper::getServiceProperties($this->server->service));
$proxmox->install($this->server, $settings, $password);
// Send notification to the user
NotificationHelper::serverCreatedNotification(
$this->server->service->user,
$this->server->service,
[
'password' => $password,
'ip' => $this->server->primaryIpv4?->ip,
'ipv6' => $this->server->primaryIpv6?->ip,
'hostname' => $this->server->hostname,
]
);
}
}