Files
portal-api/app/Http/Services/vCenter/Concerns/UpgradesVirtualMachine.php
T
2026-09-28 08:26:29 +07:00

213 lines
7.3 KiB
PHP

<?php
namespace App\Http\Services\vCenter\Concerns;
use App\Support\TlsHttpClientFactory;
use Illuminate\Support\Facades\Log;
use Exception;
trait UpgradesVirtualMachine
{
public function UpgradeMemoryVirtualMachine($vmware_id = '', $ram = 0): array|string
{
$check = $this->client->checkApiExits();
if ($check) {
$RAMConfig = [
"spec" => [
"size_MiB" => $ram * 1024,
]
];
$response = TlsHttpClientFactory::vCenter()
->withHeaders([
'vmware-api-session-id' => $this->client->token
])
->patch("https://{$this->client->apiUri}/rest/vcenter/vm/$vmware_id/hardware/memory", $RAMConfig);
if ($response->status() === 200) {
return [
'error' => false,
'data' => 'success'
];
}
return [
'error' => true,
'data' => $response['value']
];
}
return "Login_Again";
}
public function UpgradeCPUVirtualMachine($vmware_id = '', $cpu = 0): array|string
{
$check = $this->client->checkApiExits();
if ($check) {
$CPUConfig = [
"spec" => [
"cores_per_socket" => 1,
"count" => $cpu
]
];
$response = TlsHttpClientFactory::vCenter()
->withHeaders([
'vmware-api-session-id' => $this->client->token
])
->patch("https://{$this->client->apiUri}/rest/vcenter/vm/$vmware_id/hardware/cpu", $CPUConfig);
if ($response->status() === 200) {
return [
'error' => false,
'data' => 'success'
];
}
return [
'error' => true,
'data' => $response['value']
];
}
return "Login_Again";
}
public function UpgradeDiskVirtualMachine($product, $disk = 0): array|string
{
$vmware_id = $product->vmware_id;
$check = $this->client->checkApiExits();
if ($check) {
try {
Log::channel('Custom')->info("===================================================================");
Log::channel('Custom')->info("Upgrade Disk with VirtualMachine ID # - $vmware_id");
Log::channel('Custom')->info("\r\n");
$Command = "\$vm = 'VirtualMachine-$vmware_id'; \$disksize = $disk; cd /; C:\CLISave\diskresize.ps1;";
Log::channel('Custom')->info("$Command");
$this->powerShellService->executePowerCLI($Command);
Log::channel('Custom')->info("\r\n");
Log::channel('Custom')->info("After run Command |" . now());
Log::channel('Custom')->info("===================================================================");
return [
'error' => false
];
} catch (\Exception $error) {
Log::channel('Custom')->info("\r\n");
Log::channel('Custom')->info("===================================================================");
Log::channel('Custom')->info("Error Log: ");
Log::channel('Custom')->info($error->getMessage());
Log::channel('Custom')->info("===================================================================");
return [
'error' => true,
'message' => $error->getMessage()
];
}
}
return "Login_Again";
}
private function updateVMHardware($vmId, $addon, $base_cpu, $base_ram): void
{
try {
Log::info("=== BẮT ĐẦU CẬP NHẬT ADDON ===");
$new_cpu = $base_cpu + $addon['cpu'];
$new_ram_mib = ($base_ram + $addon['ram']) * 1024;
if ($addon['cpu'] > 0) {
$this->updateVMCPU($vmId, $new_cpu);
}
if ($addon['ram'] > 0) {
$this->updateVMMemory($vmId, $new_ram_mib);
}
Log::info("=== HOÀN THÀNH CẬP NHẬT ADDON ===");
} catch (Exception $e) {
Log::error("❌ Lỗi khi cập nhật cấu hình phần cứng VM {$vmId}: " . $e->getMessage());
}
}
private function updateVMCPU($vmId, $cpuCount): bool
{
try {
$coresPerSocket = 1;
if ($cpuCount > 1) {
$maxCoresPerSocket = floor($cpuCount / 2);
for ($i = $maxCoresPerSocket; $i >= 1; $i--) {
if ($cpuCount % $i === 0) {
$coresPerSocket = $i;
break;
}
}
if ($coresPerSocket === 1 && $cpuCount > 2) {
$coresPerSocket = $cpuCount;
}
}
$payload = [
'spec' => [
'count' => $cpuCount,
'cores_per_socket' => $coresPerSocket,
'hot_add_enabled' => true
]
];
$response = TlsHttpClientFactory::vCenter()
->timeout(30)
->withHeaders([
'vmware-api-session-id' => $this->client->token,
'Content-Type' => 'application/json',
])
->patch("https://{$this->client->apiUri}/rest/vcenter/vm/{$vmId}/hardware/cpu", $payload);
if ($response->successful()) {
Log::info("✅ API call cập nhật CPU thành công cho VM {$vmId}");
return true;
} else {
Log::error("❌ API call cập nhật CPU thất bại cho VM {$vmId}", [
'status' => $response->status(),
'response' => $response->body()
]);
return false;
}
} catch (Exception $e) {
Log::error("❌ Exception khi cập nhật CPU cho VM {$vmId}: " . $e->getMessage());
return false;
}
}
private function updateVMMemory($vmId, $ramSizeMB): bool
{
try {
$payload = [
'spec' => [
'size_MiB' => $ramSizeMB
]
];
$response = TlsHttpClientFactory::vCenter()
->timeout(30)
->withHeaders([
'vmware-api-session-id' => $this->client->token,
'Content-Type' => 'application/json',
])
->patch("https://{$this->client->apiUri}/rest/vcenter/vm/{$vmId}/hardware/memory", $payload);
if ($response->successful()) {
Log::info("✅ API call cập nhật RAM thành công cho VM {$vmId}");
return true;
} else {
Log::error("❌ API call cập nhật RAM thất bại cho VM {$vmId}", [
'status' => $response->status(),
'response' => $response->body()
]);
return false;
}
} catch (Exception $e) {
Log::error("❌ Exception khi cập nhật RAM cho VM {$vmId}: " . $e->getMessage());
return false;
}
}
}