update bug

This commit is contained in:
Pham Bao
2025-07-29 22:49:14 +07:00
parent b048edee11
commit 21a2dfe855
15 changed files with 540 additions and 158 deletions
+108
View File
@@ -2124,6 +2124,114 @@ class vCenter
}
}
/**
* Assign IP to Windows VM using PowerShell script
*/
public function assignIPWindows($product, $vmware_id = ''): array|string
{
try {
// Kiểm tra null safety
if (!$product || !$product->ipAddress || !$product->ipAddress->host || !$product->ipAddress->host->datacenter || !$product->ipAddress->host->datacenter->vCenter) {
Log::error('❌ assignIPWindows: Missing required data', [
'vmware_id' => $vmware_id,
'product_id' => $product?->id ?? 'unknown',
'ip_address' => $product?->ipAddress?->ip ?? 'unknown',
'host' => $product?->ipAddress?->host?->name ?? 'unknown',
'datacenter' => $product?->ipAddress?->host?->datacenter?->name ?? 'unknown',
'vcenter' => $product?->ipAddress?->host?->datacenter?->vCenter?->host ?? 'unknown'
]);
return [
'error' => true,
'data' => 'Missing required data for IP assignment'
];
}
$this->SetConfig($product->ipAddress->host->datacenter->vCenter);
$vmware_name = (string) $product->name;
$ipAddress = $product->ipAddress->ip;
$gateway = $product->ipAddress->gateway;
$netmask = $product->ipAddress->netmask ?? '255.255.255.0';
$dns = $product->ipAddress->dns ?? '8.8.8.8';
// Convert netmask to prefix length (e.g., 255.255.255.0 = 24)
$maskBits = $this->convertNetmaskToPrefixLength($netmask);
Log::channel('Custom')->info("===================================================================");
Log::channel('Custom')->info("Assign IP to Windows VM - $vmware_name");
Log::channel('Custom')->info("IP: $ipAddress, Gateway: $gateway, DNS: $dns, MaskBits: $maskBits");
Log::channel('Custom')->info("\r\n");
// Create PowerShell command with dynamic values
$Command = "\$vmName = '$vmware_name'; \$IP = '$ipAddress'; \$MaskBits = $maskBits; \$Gateway = '$gateway'; \$DNS = '$dns'; cd /; C:\CLISave\AssignIP\assignIPWindows.ps1;";
Log::channel('Custom')->info("Executing Command: $Command");
$output = $this->Command($Command);
Log::channel('Custom')->info("Script output: " . $output);
Log::channel('Custom')->info("===================================================================");
// Check if execution was successful
if (strpos($output, '🎉') !== false || strpos($output, 'successfully') !== false) {
return [
'error' => false,
'data' => 'IP assigned successfully',
'output' => $output
];
} else {
return [
'error' => true,
'data' => 'Failed to assign IP',
'output' => $output
];
}
} catch (\Exception $e) {
Log::channel('Custom')->error("Exception in assignIPWindows: " . $e->getMessage());
return [
'error' => true,
'data' => $e->getMessage()
];
}
}
/**
* Convert netmask to prefix length (e.g., 255.255.255.0 = 24)
*/
private function convertNetmaskToPrefixLength($netmask): int
{
$netmaskMap = [
'255.255.255.255' => 32,
'255.255.255.254' => 31,
'255.255.255.252' => 30,
'255.255.255.248' => 29,
'255.255.255.240' => 28,
'255.255.255.224' => 27,
'255.255.255.192' => 26,
'255.255.255.128' => 25,
'255.255.255.0' => 24,
'255.255.254.0' => 23,
'255.255.252.0' => 22,
'255.255.248.0' => 21,
'255.255.240.0' => 20,
'255.255.224.0' => 19,
'255.255.192.0' => 18,
'255.255.128.0' => 17,
'255.255.0.0' => 16,
'255.254.0.0' => 15,
'255.252.0.0' => 14,
'255.248.0.0' => 13,
'255.240.0.0' => 12,
'255.224.0.0' => 11,
'255.192.0.0' => 10,
'255.128.0.0' => 9,
'255.0.0.0' => 8
];
return $netmaskMap[$netmask] ?? 24; // Default to /24 if not found
}
/**
* Parse snapshot output from PowerShell text format
*/