This commit is contained in:
baodev0
2026-06-22 08:43:32 +07:00
parent 3243473843
commit 0c436153c6
22 changed files with 1389 additions and 95 deletions
@@ -4,22 +4,31 @@ declare(strict_types=1);
namespace App\Services\VirtualMachine;
use App\Models\ModuleHost;
use App\Models\OrderItem;
use App\Models\Os;
use App\Services\VirtualMachine\Dto\VmInfrastructureContext;
use RuntimeException;
/**
* Resolves the provisioning target by walking the ACTIVE chain only:
*
* 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.
*/
class VmInfrastructureResolver
{
public function __construct(private readonly VmProvisionLogger $logger)
{
public function __construct(
private readonly VmProvisionLogger $logger,
private readonly VmActiveProviderService $activeProvider,
) {
}
public function resolve(OrderItem $item, $product): VmInfrastructureContext
{
$os = Os::with(['host.datacenter.vCenter', 'osGroup.host.datacenter.vCenter'])
->find($item->os_id);
$os = Os::with('osGroup')->find($item->os_id);
if (!$os) {
throw new RuntimeException(
@@ -27,63 +36,31 @@ class VmInfrastructureResolver
);
}
// Priority 1: host attached directly to OS
$host = $os->host;
$datacenter = $host?->datacenter;
$vcenter = $datacenter?->vCenter;
$chain = $this->activeProvider->resolveChain();
// Priority 2: host from OS group fallback
if (!$host || !$datacenter || !$vcenter) {
$groupHost = $os->osGroup?->host;
$host = $host ?? $groupHost;
$datacenter = $datacenter ?? $groupHost?->datacenter;
$vcenter = $vcenter ?? $groupHost?->datacenter?->vCenter;
if ($groupHost) {
$this->logger->step('Infrastructure fallback: osGroup host', [
'os_id' => $os->id,
'os_group_id' => $os->os_group_id,
'host_id' => $groupHost->id,
]);
}
}
// Priority 3: host from product (Provisioning tab)
if ((!$host || !$datacenter || !$vcenter) && $product && $product->module_host_id) {
$productHost = ModuleHost::with('datacenter.vCenter')->find($product->module_host_id);
if ($productHost) {
$host = $host ?? $productHost;
$datacenter = $datacenter ?? $productHost->datacenter;
$vcenter = $vcenter ?? $productHost->datacenter?->vCenter;
$this->logger->step('Infrastructure fallback: product host', [
'product_id' => $product->id,
'host_id' => $productHost->id,
'datacenter_id' => $productHost->datacenter?->id,
]);
}
}
if (!$vcenter || !$host || !$datacenter) {
throw new RuntimeException(
"Thiếu thông tin host/datacenter/vcenter từ sản phẩm ID #" . ($product->id ?? 'unknown') . ". "
. "Vui lòng cấu hình vCenter trong tab Provisioning của sản phẩm, "
. "hoặc gán host cho OS group của OS được chọn."
);
}
$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,
]);
$osGroupName = $os->osGroup?->name ?? '';
$isWindows = str_contains(strtolower($osGroupName), 'windows');
return new VmInfrastructureContext(
os: $os,
host: $host,
datacenter: $datacenter,
vcenter: $vcenter,
host: $chain['host'],
datacenter: $chain['datacenter'],
vcenter: $chain['vcenter'],
template: $os->template_id,
isWindows: $isWindows,
vmUser: $isWindows ? 'administrator' : 'root',
vmPort: $isWindows ? 3389 : 22,
datastore: $chain['datastore'],
network: $chain['network'],
);
}
}