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
BIN
View File
Binary file not shown.
@@ -49,22 +49,17 @@ class ServiceController extends Controller
return redirect()->route('service.index')->with('error', 'Không tìm thấy dịch vụ.');
}
// Kiểm tra quyền truy cập - chỉ cho phép customer sở hữu
if ($invoice->customer_id !== customer()->id) {
return redirect()->route('service.index')->with('error', 'Bạn không có quyền truy cập dịch vụ này.');
}
// Lấy thông tin VM nếu có - WITH CACHING AND BACKGROUND UPDATE
$vmInfo = null;
if ($invoice->vm_id) {
$vmInfo = $this->getVMInfoFromVCenter($invoice, $vCenterService);
// Dispatch background job to update VM info if cache is stale
$this->dispatchVMInfoUpdateJob($invoice);
}
$title = "Chi tiết dịch vụ #" . $invoice->id;
dd($vmInfo);
return view('frontend.service.show', [
'title' => $title,
'invoice' => $invoice,
@@ -82,11 +77,9 @@ class ServiceController extends Controller
*/
private function getVMInfoFromVCenter($invoice, $vCenterService)
{
// Cache key for this VM info
$cacheKey = "vm_info_{$invoice->vm_id}";
$cacheDuration = 300; // 5 minutes
// Try to get from cache first
if (Cache::has($cacheKey)) {
return Cache::get($cacheKey);
}
@@ -118,7 +111,6 @@ class ServiceController extends Controller
return $defaultInfo;
}
// Login vào vCenter - with timeout
$loginSuccess = false;
try {
$loginSuccess = $vCenterService->loginServer($vcenter);
@@ -132,7 +124,6 @@ class ServiceController extends Controller
return $defaultInfo;
}
// Lấy thông tin VM từ vCenter API - with timeout handling
$vmDetail = null;
$ipInfo = null;
@@ -180,7 +171,7 @@ class ServiceController extends Controller
'cpu' => $cpuCount . ' cores',
'ram' => $memoryGB . ' GB',
'disk' => $diskSizeGB . ' GB',
'uptime' => 'N/A', // Có thể lấy từ guest info nếu cần
'uptime' => 'N/A',
'last_updated' => now()->format('Y-m-d H:i:s'),
'vm_name' => $vmData['name'] ?? 'Unknown',
'vm_folder' => $vmData['folder'] ?? 'Unknown',
@@ -611,7 +602,7 @@ class ServiceController extends Controller
$validated = Validator::make($request->all(), [
'product_id' => 'required|string|max:255',
'new_port' => 'required|string|min:4|max:128',
'password' => 'required|string|min:8|max:128',
'password' => 'required|string|min:4|max:128',
]);
if ($validated->fails()) {
return response()->json(['error' => true, 'message' => __('Dữ liệu không hợp lệ'), 'errors' => $validated->errors()], 422);
+28 -18
View File
@@ -898,11 +898,6 @@ PS;
])
->patch("https://{$this->apiUri}/rest/vcenter/vm/{$vmId}/hardware/cpu", $payload);
Log::info("📥 Response CPU", [
'status' => $response->status(),
'body' => $response->body()
]);
if ($response->successful()) {
Log::info("✅ API call cập nhật CPU thành công cho VM {$vmId}");
return true;
@@ -938,8 +933,6 @@ PS;
]
];
Log::info("📤 Payload RAM", ['payload' => $payload]);
$response = Http::withoutVerifying()
->timeout(30)
->withHeaders([
@@ -948,11 +941,6 @@ PS;
])
->patch("https://{$this->apiUri}/rest/vcenter/vm/{$vmId}/hardware/memory", $payload);
Log::info("📥 Response RAM", [
'status' => $response->status(),
'body' => $response->body()
]);
if ($response->successful()) {
Log::info("✅ API call cập nhật RAM thành công cho VM {$vmId}");
return true;
@@ -2029,8 +2017,7 @@ PS;
// Kiểm tra hệ điều hành
if (str_contains($this->os, "Windows")) {
$user = $data['user'] ?? 'administartor';
$this->process = new WinRMClient($ip, $user, $password);
;
$this->process = new WinRMClient($ip, $user, $password);;
return $this->process;
}
@@ -2345,7 +2332,6 @@ PS;
'error' => false,
'message' => 'Test addon update completed'
];
} catch (\Exception $e) {
Log::error("🧪 TEST: Lỗi trong test addon update: " . $e->getMessage());
return [
@@ -2379,7 +2365,6 @@ PS;
'message' => 'Test getSnapShot completed',
'result' => $result
];
} catch (\Exception $e) {
Log::error("🧪 TEST: Lỗi trong test getSnapShot: " . $e->getMessage());
return [
@@ -2417,7 +2402,6 @@ PS;
$dns = $product->ipAddress->dns ?? '8.8.8.8';
$vmware_name = $product->name ?? $vmware_id;
$vCenter = $product->ipAddress->host->datacenter->vCenter ?? null;
} elseif ($product && isset($product->IpAddress) && $product->IpAddress) {
Log::info('✅ Using old Product structure for assignIPWindows', [
'product_id' => $product->id,
@@ -2482,7 +2466,6 @@ PS;
'output' => $output
];
}
} catch (\Exception $e) {
Log::channel('Custom')->error("Exception in assignIPWindows: " . $e->getMessage());
return [
@@ -2659,4 +2642,31 @@ PS;
return $snapshots;
}
/*
* Get Window Remote Port
*/
public function getWindowsRdpPort(string $vmwareId, string $guestUser, string $guestPassword): ?int
{
$ps = "(Get-ItemProperty 'HKLM:\\System\\CurrentControlSet\\Control\\Terminal Server\\WinStations\\RDP-Tcp' -Name PortNumber).PortNumber";
$cmd = "Invoke-VMScript -VM '$vmwareId' "
. "-ScriptText \"$ps\" "
. "-ScriptType Powershell "
. "-GuestUser '$guestUser' "
. "-GuestPassword '$guestPassword'";
$output = $this->Command($cmd);
if (!$output || str_contains($output, 'Login_Fail')) {
return null;
}
if (preg_match('/\b(\d{2,5})\b/', $output, $m)) {
return (int) $m[1];
}
return null;
}
}