update change port

This commit is contained in:
pbao9
2026-01-17 11:27:54 +07:00
parent a79f0041f4
commit 4f2fdbaaa5
14 changed files with 70 additions and 147 deletions
@@ -43,7 +43,7 @@ class CreateVirtualMachineJob implements ShouldQueue
public function handle(vCenter $vCenter, Telegram $telegram)
{
try {
$product = $this->item->getProductAttribute(); // dùng helper trong OrderItem
$product = $this->item->getProductAttribute();
if (!$product) {
$errorMessage = "Không tìm thấy sản phẩm từ OrderItem #{$this->item->id}";
@@ -139,22 +139,6 @@ class CreateVirtualMachineJob implements ShouldQueue
'addon' => $addonData,
]);
Log::info('Thông tin vCenter khi tạo máy ảo', [
'product' => $product,
'item' => $this->item,
'host_id' => $host?->id,
'host_name' => $host?->name,
'host_vcenter_id' => $host?->vcenter_id,
'datacenter_id' => $datacenter?->id,
'datacenter_name' => $datacenter?->name,
'datacenter_vcenter_id' => $datacenter?->vcenter_id,
'vcenter' => $vcenter,
'vcenter_id' => $vcenter?->id,
'vcenter_host' => $vcenter?->host,
'vcenter_user' => $vcenter?->user,
'vcenter_password' => SignRSA::DecryptFunc($vcenter?->password)
]);
if (is_array($result) && ($result['error'] ?? false)) {
$errorMessage = 'Tạo máy ảo thất bại: ' . json_encode($result);
Log::warning($errorMessage);
@@ -214,11 +198,9 @@ class CreateVirtualMachineJob implements ShouldQueue
$customer = $this->item->order->customer;
$serverName = $this->item->server_name ?? 'VM-' . $vmId;
// 1. Lấy IP available từ IPPool
$networkId = $product->ipAddress?->module_network_id ?? 1;
$availableIP = $this->getAvailableIPFromPool($networkId);
// 2. Tạo ModuleIpAddress với IP từ pool
$moduleIpAddress = ModuleIpAddress::create([
'vm_name' => $serverName,
'module_host_id' => $host->id,
@@ -230,7 +212,6 @@ class CreateVirtualMachineJob implements ShouldQueue
'status' => 1 // 1 = Assigned
]);
// 3. Tạo ModuleVmware với IP address vừa tạo
$moduleVmware = ModuleVmware::create([
'name' => $serverName,
'module_ip_address_id' => $moduleIpAddress->id,
@@ -249,26 +230,8 @@ class CreateVirtualMachineJob implements ShouldQueue
'status' => 1
]);
Log::info('✅ Lưu dữ liệu VM và IP address thành công', [
'vmware_id' => $vmId,
'module_vmware_id' => $moduleVmware->id,
'module_ip_address_id' => $moduleIpAddress->id,
'assigned_ip' => $availableIP,
'name' => $serverName,
'order_item_id' => $this->item->id
]);
// Dispatch AssignIPJob để chạy sau 5 phút
AssignIPJob::dispatch($vmId, $moduleVmware->id, $moduleIpAddress->ip)
->delay(now()->addMinutes(5));
Log::info('✅ Đã dispatch AssignIPJob để chạy sau 5 phút', [
'vm_id' => $vmId,
'module_vmware_id' => $moduleVmware->id,
'assigned_ip' => $moduleIpAddress->ip,
'delay_minutes' => 5
]);
} catch (\Exception $e) {
Log::error('❌ Lỗi khi lưu dữ liệu VM và IP address: ' . $e->getMessage(), [
'vmware_id' => $vmId,
@@ -286,7 +249,6 @@ class CreateVirtualMachineJob implements ShouldQueue
}
}
// Gửi thông báo thành công
$telegram->SendJobRunInfo(
'CreateVirtualMachineJob',
'success',
@@ -325,9 +287,6 @@ class CreateVirtualMachineJob implements ShouldQueue
}
}
/**
* Get available IP from IPPool that doesn't exist in ModuleIpAddress
*/
private function getAvailableIPFromPool(int $networkId): string
{
// Lấy tất cả IP từ IPPool theo network_id, order by ip_address
@@ -335,39 +294,20 @@ class CreateVirtualMachineJob implements ShouldQueue
->orderBy('ip_address')
->get();
Log::info('✅ Lấy IP available từ pool', [
'ip_pool_list' => $ipPoolList,
'network_id' => $networkId
]);
if ($ipPoolList->isEmpty()) {
// Nếu không có IP trong pool, trả về IP default
return '0.0.0.0';
}
// Lấy tất cả IP đã được assign trong ModuleIpAddress
$assignedIPs = ModuleIpAddress::where('module_network_id', $networkId)
->pluck('ip')
->toArray();
// Tìm IP đầu tiên chưa được assign
foreach ($ipPoolList as $ipPool) {
if (!in_array($ipPool->ip_address, $assignedIPs)) {
Log::info('✅ Lấy IP available từ pool', [
'ip' => $ipPool->ip_address,
'network_id' => $networkId
]);
return $ipPool->ip_address;
}
}
// Nếu tất cả IP đã được assign, log warning và trả về IP default
Log::warning('⚠️ Không có IP available trong pool', [
'network_id' => $networkId,
'total_ips_in_pool' => $ipPoolList->count(),
'assigned_ips' => count($assignedIPs)
]);
return '0.0.0.0';
}