Refactor deployment process and enhance VM IP reservation logic; update email template for VM creation failure notifications

This commit is contained in:
baodev0
2026-07-13 11:53:00 +07:00
parent c1008d579c
commit 3403cb9ec5
4 changed files with 223 additions and 82 deletions
@@ -24,18 +24,18 @@ class VmIpReservationService
public function reserve(int $networkId, int $hostId, string $dns): ?array
{
return DB::transaction(function () use ($networkId, $hostId, $dns) {
// ponytail: exclude only IPs with an actual VM assigned, not stale __pending__ reservations
$usedIps = ModuleIpAddress::where('module_network_id', $networkId)
->where('vm_name', '!=', '__pending__')
// Exclude IPs that have any active reservation (real VM or pending).
// Also accept status='reserved' in ip_pools so stale reservations
// (ip_pool row stuck as 'reserved' with no matching module_ip_addresses row)
// are recoverable without manual DB cleanup.
$allUsedIps = ModuleIpAddress::where('module_network_id', $networkId)
->lockForUpdate()
->pluck('ip')
->toArray();
$ipPool = IpPool::where('module_network_id', $networkId)
->where('status', 'free')
->whereNotNull('gateway')
->where('gateway', '!=', '')
->whereNotIn('ip_address', $usedIps)
->whereIn('status', ['free', 'reserved'])
->whereNotIn('ip_address', $allUsedIps)
->lockForUpdate()
->inRandomOrder()
->first();