feat: Enhance vCenter integration with resource pool and folder support

- Added methods to ResourceService for fetching resource pools and updated existing methods to include datacenter filtering.
- Modified CreateVirtualMachine to accept folder and resource pool parameters from product data.
- Updated various jobs to synchronize resource pools and include them in the provisioning process.
- Introduced new ModuleResourcePool model and migration for resource pool management.
- Enhanced existing models (ModuleDatastore, ModuleNetwork, Os) to include relationships with vCenter and resource pools.
- Added netmask field to IpPool and updated related logic for IP reservation.
- Improved error handling and logging in VM creation and IP reservation processes.
- Updated API routes to include sync functionality for vCenter.
This commit is contained in:
baodev0
2026-07-21 21:37:28 +07:00
parent 2d43e95855
commit 5d3ed57b32
33 changed files with 708 additions and 152 deletions
@@ -119,22 +119,26 @@ class CreateVirtualMachineJob implements ShouldQueue
}
$vmLogger->step('VM clone started', [
'order_item_id' => $this->item->id,
'product_id' => $product->id,
'template' => $infra->template,
'host_id' => $infra->host->id,
'datastore_id' => $infra->datastore?->id,
'vcenter_id' => $infra->vcenter->id,
'order_item_id' => $this->item->id,
'product_id' => $product->id,
'template' => $infra->template,
'host_id' => $infra->host->id,
'datastore_id' => $infra->datastore?->id,
'vcenter_id' => $infra->vcenter->id,
'folder' => $infra->folderId,
'resource_pool' => $infra->resourcePoolId,
]);
$result = $vCenter->CreateVirtualMachine([
'item' => $this->item,
'host' => $infra->host,
'datacenter' => $infra->datacenter,
'vcenter' => $infra->vcenter,
'template' => $infra->template,
'addon' => $addonData,
'datastore' => $infra->datastore,
'item' => $this->item,
'host' => $infra->host,
'datacenter' => $infra->datacenter,
'vcenter' => $infra->vcenter,
'template' => $infra->template,
'addon' => $addonData,
'datastore' => $infra->datastore,
'folder' => $infra->folderId,
'resource_pool' => $infra->resourcePoolId,
]);
if (is_array($result) && ($result['error'] ?? false)) {
@@ -90,12 +90,14 @@ class AssignIpToGuestJob implements ShouldQueue
return;
}
// DNS + network selector still come from product template; gateway + CIDR
// are now read from the chosen ip_pools row at reservation time.
// Network preferably comes from the OS's own binding (module_network_id);
// falls back to the product's ipAddress template for legacy OS entries
// that were never assigned a network. Gateway + CIDR are read from the
// chosen ip_pools row at reservation time.
$orderItem = $moduleVmware->order?->orderItems?->first();
$osModel = $orderItem?->os_id ? Os::with('osGroup.host')->find($orderItem->os_id) : null;
$networkId = $orderItem?->product?->ipAddress?->module_network_id ?? 1;
$networkId = $osModel?->module_network_id ?? $orderItem?->product?->ipAddress?->module_network_id ?? 1;
$dns = $orderItem?->product?->ipAddress?->dns ?? '8.8.8.8';
$hostId = $osModel?->osGroup?->host?->id ?? 1;
$serviceId = Service::where('vm_id', $this->moduleVmwareId)->value('id');
@@ -334,15 +336,9 @@ class AssignIpToGuestJob implements ShouldQueue
string $dns,
): ?array {
return DB::transaction(function () use ($networkId, $hostId, $dns) {
// Lock existing rows to prevent another job from picking the same IP
$usedIps = ModuleIpAddress::where('module_network_id', $networkId)
->lockForUpdate()
->pluck('ip')
->toArray();
// ip_pools.status is the single source of truth for availability.
$ipPool = IpPool::where('module_network_id', $networkId)
->whereIn('status', ['free', 'reserved'])
->whereNotIn('ip_address', $usedIps)
->lockForUpdate()
->inRandomOrder()
->first();
@@ -356,10 +352,16 @@ class AssignIpToGuestJob implements ShouldQueue
$cidr = (int) ($ipPool->cidr ?: 24);
$gateway = (string) ($ipPool->gateway ?? '');
$netmask = $this->cidrToNetmask($cidr);
$netmask = $ipPool->netmask ?: $this->cidrToNetmask($cidr);
// Create a pending reservation — acts as a distributed lock on this IP
$reservation = ModuleIpAddress::create([
// module_ip_address mirrors ip_pools — update the existing synced row
// (created when the IP was added to ip_pools) instead of inserting a new one.
$reservation = ModuleIpAddress::where('ip', $ipPool->ip_address)
->where('module_network_id', $networkId)
->lockForUpdate()
->first();
$attrs = [
'vm_name' => '__pending__',
'module_host_id' => $hostId,
'module_network_id' => $networkId,
@@ -368,7 +370,13 @@ class AssignIpToGuestJob implements ShouldQueue
'netmask' => $netmask,
'dns' => $dns,
'status' => 0, // 0 = pending/reserved, not yet confirmed
]);
];
if ($reservation) {
$reservation->update($attrs);
} else {
$reservation = ModuleIpAddress::create($attrs);
}
return [
'reservation' => $reservation,
@@ -408,13 +416,13 @@ class AssignIpToGuestJob implements ShouldQueue
}
/**
* Release a failed reservation by deleting the pending ModuleIpAddress row.
* Release a failed reservation by resetting the mirrored ModuleIpAddress row.
* This returns the IP to the available pool.
*/
private function releaseIp(ModuleIpAddress $reservation, ?int $ipPoolId, VmProvisionLogger $logger): void
{
try {
$reservation->delete();
$reservation->update(['vm_name' => null, 'status' => 0]);
if ($ipPoolId) {
IpPool::where('id', $ipPoolId)->update(['status' => 'free']);
}
@@ -34,6 +34,7 @@ class SyncAllPackage implements ShouldQueue
SyncDatastore::dispatch();
SyncNetwork::dispatch();
SyncFolder::dispatch();
SyncResourcePool::dispatch();
SyncVirtualMachine::dispatch();
}
}
@@ -93,7 +93,7 @@ class SyncDatacenter implements ShouldQueue
ModuleDatacenter::updateOrCreate(
['datacenter_id' => $_item['datacenter']],
['name' => $_item['name'], 'vcenter_id' => $item->id]
['name' => $_item['name'], 'vcenter_id' => $item->id, 'status' => 1]
);
}
}
@@ -100,8 +100,8 @@ class SyncDatastore implements ShouldQueue
'vcenter_host_id' => $hostItem->host_id
]);
// Use ResourceService to get datastores
$datastore = $resourceService->loadDatastore($hostItem->host_id);
// Use ResourceService to get datastores, scoped to this host AND datacenter
$datastore = $resourceService->loadDatastore($hostItem->host_id, $_item->datacenter_id);
if ($datastore === "Login_Again") {
$this->jobLog('Token expired → relogin');
@@ -109,7 +109,7 @@ class SyncDatastore implements ShouldQueue
if ($token) {
// Retry once
$datastore = $resourceService->loadDatastore($hostItem->host_id);
$datastore = $resourceService->loadDatastore($hostItem->host_id, $_item->datacenter_id);
} else {
$this->jobLog('Relogin failed');
continue;
@@ -135,6 +135,8 @@ class SyncDatastore implements ShouldQueue
'name' => $_subItem['name'],
'type' => $_subItem['type'],
'space' => floor($_subItem['free_space'] / 1073741824),
'vcenter_id' => $item->id,
'status' => 1,
]
);
}
@@ -84,6 +84,7 @@ class SyncHost implements ShouldQueue
'name' => $_subItem['name'],
'module_datacenter_id' => $_item['id'],
'template' => $template[0] ?? null,
'status' => 1,
]
);
}
+7 -2
View File
@@ -38,6 +38,7 @@ class SyncNetwork implements ShouldQueue
*/
public function handle()
{
$networkCount = 0;
foreach ($this->data as $item) {
if (!$item->token) {
@@ -62,13 +63,13 @@ class SyncNetwork implements ShouldQueue
if ($allHost) {
foreach ($allHost as $hostItem) {
$network = $this->vCenter->loadNetWork($item, $hostItem->host_id);
$network = $this->vCenter->loadNetWork($item, $hostItem->host_id, $_item->datacenter_id);
if ($network === "Login_Again") {
$token = $this->vCenter->loginServer($item);
if ($token) {
vCenter::where('id', $item->id)->update(['token' => $token]);
$this->vCenter->token = $token;
$network = $this->vCenter->loadNetWork($item, $hostItem->host_id);
$network = $this->vCenter->loadNetWork($item, $hostItem->host_id, $_item->datacenter_id);
}
}
@@ -82,8 +83,11 @@ class SyncNetwork implements ShouldQueue
[
'name' => $_subItem['name'],
'type' => $_subItem['type'],
'vcenter_id' => $item->id,
'status' => 1,
]
);
$networkCount++;
}
}
}
@@ -94,6 +98,7 @@ class SyncNetwork implements ShouldQueue
(new \App\Http\Services\Telegram())->SendJobRunInfo('SyncNetwork', 'Success', [
'message' => 'Sync network completed successfully',
'synced_vcenters' => $this->data->count(),
'synced_network_count' => $networkCount,
]);
}
}
@@ -0,0 +1,92 @@
<?php
namespace App\Jobs\VirtualMachine\Sync;
use App\Http\Services\Telegram;
use App\Http\Services\vCenter as ServiceVCenter;
use App\Models\ModuleDatacenter;
use App\Models\ModuleHost;
use App\Models\ModuleResourcePool;
use App\Models\vCenter;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
class SyncResourcePool implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
protected $vCenter = null;
protected $data = null;
protected Telegram $telegram;
public function __construct()
{
$this->data = vCenter::select('id', 'host', 'user', 'password', 'token')->get();
$this->vCenter = new ServiceVCenter;
$this->telegram = new Telegram();
}
public function handle()
{
$resourcePoolCount = 0;
foreach ($this->data as $item) {
$token = $this->vCenter->loginServer($item);
if (!$token) {
continue;
}
$dataCenter = ModuleDatacenter::where('vcenter_id', $item->id)
->select('id', 'datacenter_id')
->get();
foreach ($dataCenter as $_item) {
$allHost = ModuleHost::where('module_datacenter_id', $_item->id)
->select('id', 'name', 'host_id')
->get();
foreach ($allHost as $hostItem) {
$resourcePool = $this->vCenter->getAllResourcePool($hostItem->host_id, $_item->datacenter_id);
if ($resourcePool === 'Login_Again') {
$token = $this->vCenter->loginServer($item);
if ($token) {
$resourcePool = $this->vCenter->getAllResourcePool($hostItem->host_id, $_item->datacenter_id);
} else {
continue;
}
}
if (!is_array($resourcePool)) {
continue;
}
foreach ($resourcePool as $_subItem) {
ModuleResourcePool::updateOrCreate(
[
'resource_pool' => $_subItem['resource_pool'],
'module_host_id' => $hostItem->id,
],
[
'name' => $_subItem['name'],
'vcenter_id' => $item->id,
'status' => 1,
]
);
$resourcePoolCount++;
}
}
}
}
$this->telegram->SendJobRunInfo('SyncResourcePool', 'Success', [
'message' => 'Sync resource pool completed successfully',
'synced_vcenters' => $this->data->count(),
'synced_resource_pool_count' => $resourcePoolCount,
]);
}
}