CluPilotCloud/app/Livewire/Admin/ConfirmRemoveHost.php

56 lines
1.6 KiB
PHP

<?php
namespace App\Livewire\Admin;
use App\Models\Host;
use App\Services\Wireguard\WireguardHub;
use Illuminate\Support\Facades\Cache;
use LivewireUI\Modal\ModalComponent;
/**
* Confirmation for the destructive "remove host" action (R5). Deregisters the
* CluPilot record only — it does not touch the physical server.
*/
class ConfirmRemoveHost extends ModalComponent
{
public string $uuid;
public string $name = '';
public function mount(string $uuid): void
{
$this->uuid = $uuid;
$this->name = Host::query()->where('uuid', $uuid)->value('name') ?? '';
}
public function remove(WireguardHub $hub)
{
$host = Host::query()->where('uuid', $this->uuid)->first();
if ($host !== null) {
// Delete each run under its runner lock so an in-flight worker can't
// keep mutating the server or write an event against a deleted run.
foreach ($host->runs()->get() as $run) {
Cache::lock('run:'.$run->uuid, 30)->block(15, function () use ($run) {
$run->delete(); // cascades run_resources + step_events
});
}
// Drop the hub peer so its wg_ip can't later be reused while a stale
// peer still routes to the removed server.
if (filled($host->wg_pubkey)) {
$hub->removePeer($host->wg_pubkey);
}
$host->delete();
}
return $this->redirectRoute('admin.hosts', navigate: true);
}
public function render()
{
return view('livewire.admin.confirm-remove-host');
}
}