96 lines
2.9 KiB
PHP
96 lines
2.9 KiB
PHP
<?php
|
|
|
|
namespace Paymenter\Extensions\Servers\Proxmox\Livewire;
|
|
|
|
use Paymenter\Extensions\Servers\Proxmox\Models\OS;
|
|
use Paymenter\Extensions\Servers\Proxmox\Models\Server;
|
|
use Paymenter\Extensions\Servers\Proxmox\Proxmox;
|
|
|
|
class Reinstall extends Component
|
|
{
|
|
public $os;
|
|
|
|
public bool $visible = false;
|
|
|
|
public $confirmReinstall = false;
|
|
|
|
public bool $isProtected = false;
|
|
|
|
public function mount(Server $server): void
|
|
{
|
|
parent::mount($server);
|
|
$this->os = $server->os_id;
|
|
$this->checkProtectedStatus();
|
|
}
|
|
|
|
protected function checkProtectedStatus(): void
|
|
{
|
|
try {
|
|
$proxmox = new Proxmox;
|
|
$config = $proxmox->request('/nodes/' . $this->server->node->name . '/' . $this->server->node->vmType() . '/' . $this->server->vm_id . '/config')->json()['data'] ?? [];
|
|
// Protected can be 1, '1', true, or missing/null
|
|
$this->isProtected = isset($config['protection']) && ($config['protection'] == 1 || $config['protection'] === '1' || $config['protection'] === true);
|
|
} catch (\Throwable $e) {
|
|
// If we can't check, assume not protected
|
|
$this->isProtected = false;
|
|
}
|
|
}
|
|
|
|
public function reinstall()
|
|
{
|
|
// Check protected status again before proceeding
|
|
$this->checkProtectedStatus();
|
|
|
|
if ($this->isProtected) {
|
|
$this->notify('Cannot reinstall: This server is protected in Proxmox', 'error');
|
|
return;
|
|
}
|
|
|
|
$this->validate([
|
|
'os' => 'required',
|
|
]);
|
|
if (!$this->confirmReinstall) {
|
|
$this->notify('Please confirm the reinstall', 'error');
|
|
|
|
return;
|
|
}
|
|
$this->server->os_id = $this->os;
|
|
$this->server->save();
|
|
|
|
try {
|
|
$proxmox = new Proxmox;
|
|
$proxmox->reinstall($this->server);
|
|
} catch (\Throwable $e) {
|
|
$this->notify('Reinstall failed: ' . $e->getMessage(), 'error');
|
|
return;
|
|
}
|
|
|
|
$this->redirect(route('proxmox.index'));
|
|
}
|
|
|
|
public function render()
|
|
{
|
|
if (!$this->server->node) {
|
|
return view('proxmox::reinstall', [
|
|
'osList' => collect(),
|
|
'isProtected' => $this->isProtected,
|
|
]);
|
|
}
|
|
|
|
// assuming Server has relation `node()` -> Node model
|
|
$locationId = $this->server->node->location_id;
|
|
$isLxc = $this->server->node->is_lxc ?? false;
|
|
|
|
// Filter OS templates by location AND type (LXC templates for LXC nodes, KVM for KVM)
|
|
$osList = \Paymenter\Extensions\Servers\Proxmox\Models\Location::findOrFail($locationId)
|
|
->os()
|
|
->where('is_lxc', $isLxc)
|
|
->get();
|
|
|
|
return view('proxmox::reinstall', [
|
|
'osList' => $osList,
|
|
'isProtected' => $this->isProtected,
|
|
]);
|
|
}
|
|
}
|