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:
@@ -23,6 +23,8 @@ class VmInfrastructureContext
|
||||
public readonly int $vmPort,
|
||||
public readonly ?ModuleDatastore $datastore = null,
|
||||
public readonly ?ModuleNetwork $network = null,
|
||||
public readonly string $folderId = '',
|
||||
public readonly string $resourcePoolId = '',
|
||||
) {
|
||||
}
|
||||
}
|
||||
|
||||
@@ -43,11 +43,11 @@ class VmActiveProviderService
|
||||
|
||||
/** type → [modelClass, parentColumn|null (null = global), childTypes[]] */
|
||||
private const MAP = [
|
||||
self::TYPE_VCENTER => [vCenter::class, null, [self::TYPE_DATACENTER]],
|
||||
self::TYPE_VCENTER => [vCenter::class, null, [self::TYPE_DATACENTER, self::TYPE_DATASTORE, self::TYPE_NETWORK]],
|
||||
self::TYPE_DATACENTER => [ModuleDatacenter::class, 'vcenter_id', [self::TYPE_HOST]],
|
||||
self::TYPE_HOST => [ModuleHost::class, 'module_datacenter_id', [self::TYPE_DATASTORE, self::TYPE_NETWORK]],
|
||||
self::TYPE_DATASTORE => [ModuleDatastore::class, 'module_host_id', []],
|
||||
self::TYPE_NETWORK => [ModuleNetwork::class, 'module_host_id', []],
|
||||
self::TYPE_HOST => [ModuleHost::class, 'module_datacenter_id', []],
|
||||
self::TYPE_DATASTORE => [ModuleDatastore::class, 'vcenter_id', []],
|
||||
self::TYPE_NETWORK => [ModuleNetwork::class, 'vcenter_id', []],
|
||||
];
|
||||
|
||||
public function __construct(private readonly VmProviderSettingsService $providerSettings)
|
||||
@@ -77,16 +77,16 @@ class VmActiveProviderService
|
||||
->first();
|
||||
}
|
||||
|
||||
public function activeDatastore(int $hostId): ?ModuleDatastore
|
||||
public function activeDatastore(int $vcenterId): ?ModuleDatastore
|
||||
{
|
||||
return ModuleDatastore::where('module_host_id', $hostId)
|
||||
return ModuleDatastore::where('vcenter_id', $vcenterId)
|
||||
->where('status', 1)
|
||||
->first();
|
||||
}
|
||||
|
||||
public function activeNetwork(int $hostId): ?ModuleNetwork
|
||||
public function activeNetwork(int $vcenterId): ?ModuleNetwork
|
||||
{
|
||||
return ModuleNetwork::where('module_host_id', $hostId)
|
||||
return ModuleNetwork::where('vcenter_id', $vcenterId)
|
||||
->where('status', 1)
|
||||
->first();
|
||||
}
|
||||
@@ -113,14 +113,14 @@ class VmActiveProviderService
|
||||
throw new RuntimeException("Datacenter active id={$datacenter->id} chưa có Host nào active.");
|
||||
}
|
||||
|
||||
$datastore = $this->activeDatastore($host->id);
|
||||
$datastore = $this->activeDatastore($vcenter->id);
|
||||
if (!$datastore) {
|
||||
throw new RuntimeException("Host active id={$host->id} chưa có Datastore nào active.");
|
||||
throw new RuntimeException("vCenter active id={$vcenter->id} chưa có Datastore nào active.");
|
||||
}
|
||||
|
||||
$network = $this->activeNetwork($host->id);
|
||||
$network = $this->activeNetwork($vcenter->id);
|
||||
if (!$network) {
|
||||
throw new RuntimeException("Host active id={$host->id} chưa có Network nào active.");
|
||||
throw new RuntimeException("vCenter active id={$vcenter->id} chưa có Network nào active.");
|
||||
}
|
||||
|
||||
return compact('vcenter', 'datacenter', 'host', 'datastore', 'network');
|
||||
|
||||
@@ -10,25 +10,29 @@ use App\Services\VirtualMachine\Dto\VmInfrastructureContext;
|
||||
use RuntimeException;
|
||||
|
||||
/**
|
||||
* Resolves the provisioning target by walking the ACTIVE chain only:
|
||||
* Resolves the provisioning target by walking the ACTIVE chain:
|
||||
*
|
||||
* active vCenter → active Datacenter → active Host → active Datastore + Network
|
||||
*
|
||||
* Product / OS bindings are now informational only — the host/datacenter/vcenter
|
||||
* configured per-product no longer drive VM creation. The whole platform clones
|
||||
* onto whatever single chain is currently marked status=1.
|
||||
* Product/OS host binding is informational only — it no longer drives VM
|
||||
* creation. Datastore, network, resource pool and folder are exceptions: if
|
||||
* the OS has datastore_id / module_network_id / resource_pool_id / folder_id
|
||||
* set, they override the active chain / global default (an OS can target a
|
||||
* different datastore/IP pool/resource pool/folder than whatever happens to
|
||||
* be "active" or configured as the system-wide default).
|
||||
*/
|
||||
class VmInfrastructureResolver
|
||||
{
|
||||
public function __construct(
|
||||
private readonly VmProvisionLogger $logger,
|
||||
private readonly VmActiveProviderService $activeProvider,
|
||||
private readonly VmProviderSettingsService $providerSettings,
|
||||
) {
|
||||
}
|
||||
|
||||
public function resolve(OrderItem $item, $product): VmInfrastructureContext
|
||||
{
|
||||
$os = Os::with('osGroup')->find($item->os_id);
|
||||
$os = Os::with(['osGroup', 'network', 'datastore', 'resourcePool', 'folder'])->find($item->os_id);
|
||||
|
||||
if (!$os) {
|
||||
throw new RuntimeException(
|
||||
@@ -36,15 +40,25 @@ class VmInfrastructureResolver
|
||||
);
|
||||
}
|
||||
|
||||
$chain = $this->activeProvider->resolveChain();
|
||||
$chain = $this->activeProvider->resolveChain();
|
||||
$network = $os->network ?? $chain['network'];
|
||||
$datastore = $os->datastore ?? $chain['datastore'];
|
||||
$folderId = $os->folder?->folder ?? $this->providerSettings->defaultFolder();
|
||||
$resourcePoolId = $os->resourcePool?->resource_pool ?? $this->providerSettings->defaultResourcePool();
|
||||
|
||||
$this->logger->step('Infrastructure resolved from active chain', [
|
||||
'order_item_id' => $item->id,
|
||||
'vcenter_id' => $chain['vcenter']->id,
|
||||
'datacenter_id' => $chain['datacenter']->id,
|
||||
'host_id' => $chain['host']->id,
|
||||
'datastore_id' => $chain['datastore']->id,
|
||||
'network_id' => $chain['network']->id,
|
||||
'order_item_id' => $item->id,
|
||||
'vcenter_id' => $chain['vcenter']->id,
|
||||
'datacenter_id' => $chain['datacenter']->id,
|
||||
'host_id' => $chain['host']->id,
|
||||
'datastore_id' => $datastore->id,
|
||||
'datastore_from_os' => $os->datastore !== null,
|
||||
'network_id' => $network->id,
|
||||
'network_from_os' => $os->network !== null,
|
||||
'folder_id' => $folderId,
|
||||
'folder_from_os' => $os->folder !== null,
|
||||
'resource_pool_id' => $resourcePoolId,
|
||||
'resource_pool_from_os' => $os->resourcePool !== null,
|
||||
]);
|
||||
|
||||
$osGroupName = $os->osGroup?->name ?? '';
|
||||
@@ -59,8 +73,10 @@ class VmInfrastructureResolver
|
||||
isWindows: $isWindows,
|
||||
vmUser: $isWindows ? 'administrator' : 'root',
|
||||
vmPort: $isWindows ? 3389 : 22,
|
||||
datastore: $chain['datastore'],
|
||||
network: $chain['network'],
|
||||
datastore: $datastore,
|
||||
network: $network,
|
||||
folderId: $folderId,
|
||||
resourcePoolId: $resourcePoolId,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -24,18 +24,9 @@ class VmIpReservationService
|
||||
public function reserve(int $networkId, int $hostId, string $dns): ?array
|
||||
{
|
||||
return DB::transaction(function () use ($networkId, $hostId, $dns) {
|
||||
// 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();
|
||||
|
||||
// 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', $allUsedIps)
|
||||
->lockForUpdate()
|
||||
->inRandomOrder()
|
||||
->first();
|
||||
@@ -48,9 +39,16 @@ class VmIpReservationService
|
||||
|
||||
$cidr = (int) ($ipPool->cidr ?: 24);
|
||||
$gateway = (string) ($ipPool->gateway ?? '');
|
||||
$netmask = $this->cidrToNetmask($cidr);
|
||||
$netmask = $ipPool->netmask ?: $this->cidrToNetmask($cidr);
|
||||
|
||||
$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,
|
||||
@@ -59,7 +57,13 @@ class VmIpReservationService
|
||||
'netmask' => $netmask,
|
||||
'dns' => $dns,
|
||||
'status' => 0,
|
||||
]);
|
||||
];
|
||||
|
||||
if ($reservation) {
|
||||
$reservation->update($attrs);
|
||||
} else {
|
||||
$reservation = ModuleIpAddress::create($attrs);
|
||||
}
|
||||
|
||||
return [
|
||||
'reservation' => $reservation,
|
||||
@@ -73,7 +77,7 @@ class VmIpReservationService
|
||||
public function release(ModuleIpAddress $reservation, ?int $ipPoolId): void
|
||||
{
|
||||
try {
|
||||
$reservation->delete();
|
||||
$reservation->update(['vm_name' => null, 'status' => 0]);
|
||||
if ($ipPoolId) {
|
||||
IpPool::where('id', $ipPoolId)->update(['status' => 'free']);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user