127 lines
5.4 KiB
PHP
127 lines
5.4 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Services\vCenter;
|
|
|
|
use Illuminate\Support\Facades\Log;
|
|
use Carbon\Carbon;
|
|
use Exception;
|
|
|
|
class VMConfigurationService
|
|
{
|
|
protected Client $client;
|
|
protected PowerShellService $powerShellService;
|
|
|
|
public function __construct(Client $client, PowerShellService $powerShellService)
|
|
{
|
|
$this->client = $client;
|
|
$this->powerShellService = $powerShellService;
|
|
}
|
|
|
|
private function setConfig($vcenterModel)
|
|
{
|
|
// Setup client with vCenter credentials
|
|
$this->client->loginServer($vcenterModel);
|
|
}
|
|
|
|
public function ConfigWindowsVersion10($vmware_id = '', $product = [], $password = ''): void
|
|
{
|
|
if (!$this->validateProductData($product, 'ConfigWindowsVersion10', $vmware_id)) return;
|
|
|
|
$this->setConfig($product->IpAddress->Host->Datacenter->vCenter);
|
|
$VirtualMachine = "VirtualMachine-$vmware_id";
|
|
$IPAddress = $product->IpAddress->ip;
|
|
$Gateway = $product->IpAddress->gateway;
|
|
$HostName = strtolower($product->hostname);
|
|
|
|
try {
|
|
Log::channel('Custom')->info("Configuring Windows 10 for VM #ID $VirtualMachine");
|
|
|
|
$cmd = "\$vm = get-vm -Id '$VirtualMachine';\$hostname ='$HostName'; \$ipadd = '$IPAddress'; \$gw = '$Gateway'; \$password = \"'$password'\"; cd /; C:\CLISave\changeWindows10.ps1;";
|
|
Log::channel('Custom')->info($cmd);
|
|
$this->powerShellService->executePowerCLI($cmd);
|
|
|
|
Log::channel('Custom')->info("Configuration completed at " . Carbon::now());
|
|
} catch (Exception $error) {
|
|
Log::channel('Custom')->error("Error configuring Windows 10: " . $error->getMessage());
|
|
}
|
|
}
|
|
|
|
public function ConfigWindowsServer($vmware_id = '', $product = [], $password = ''): void
|
|
{
|
|
if (!$this->validateProductData($product, 'ConfigWindowsServer', $vmware_id)) return;
|
|
|
|
$this->setConfig($product->IpAddress->Host->Datacenter->vCenter);
|
|
$VirtualMachine = "VirtualMachine-$vmware_id";
|
|
$IPAddress = $product->IpAddress->ip;
|
|
$Gateway = $product->IpAddress->gateway;
|
|
$HostName = strtolower($product->hostname);
|
|
|
|
try {
|
|
Log::channel('Custom')->info("Configuring Windows Server for VM #ID $VirtualMachine");
|
|
|
|
$cmd = "\$vm = get-vm -Id '$VirtualMachine';\$hostname ='$HostName'; \$ipadd = '$IPAddress'; \$gw = '$Gateway'; \$password = \"'$password'\"; cd /; C:\CLISave\changeWindows1line.ps1;";
|
|
Log::channel('Custom')->info($cmd);
|
|
$this->powerShellService->executePowerCLI($cmd);
|
|
|
|
Log::channel('Custom')->info("Configuration completed at " . Carbon::now());
|
|
} catch (Exception $error) {
|
|
Log::channel('Custom')->error("Error configuring Windows Server: " . $error->getMessage());
|
|
}
|
|
}
|
|
|
|
public function ConfigFixWindowsClick($vmware_id = '', $product = []): void
|
|
{
|
|
if (!$this->validateProductData($product, 'ConfigFixWindowsClick', $vmware_id)) return;
|
|
|
|
$this->setConfig($product->IpAddress->Host->Datacenter->vCenter);
|
|
$VirtualMachine = "VirtualMachine-$vmware_id";
|
|
|
|
try {
|
|
Log::channel('Custom')->info("Fixing Windows Click for VM #ID $VirtualMachine");
|
|
|
|
$cmd = "\$vm = get-vm -Id '$VirtualMachine'; cd /; C:\CLISave\FixTwoClickWindows.ps1;";
|
|
Log::channel('Custom')->info($cmd);
|
|
$this->powerShellService->executePowerCLI($cmd);
|
|
|
|
Log::channel('Custom')->info("Configuration completed at " . Carbon::now());
|
|
} catch (Exception $error) {
|
|
Log::channel('Custom')->error("Error fixing Windows Click: " . $error->getMessage());
|
|
}
|
|
}
|
|
|
|
public function ConfigRockyLinuxVersion9($vmware_id = '', $product = [], $password = ''): void
|
|
{
|
|
if (!$this->validateProductData($product, 'ConfigRockyLinuxVersion9', $vmware_id)) return;
|
|
|
|
$this->setConfig($product->IpAddress->Host->Datacenter->vCenter);
|
|
$VirtualMachine = "VirtualMachine-$vmware_id";
|
|
$IPAddress = $product->IpAddress->ip;
|
|
$Gateway = $product->IpAddress->gateway;
|
|
$HostName = strtolower($product->hostname);
|
|
|
|
try {
|
|
Log::channel('Custom')->info("Configuring Rocky Linux 9 for VM #ID $VirtualMachine");
|
|
|
|
$cmd = "\$vm = get-vm -Id '$VirtualMachine'; \$hostname ='$HostName';\$ipadd = '$IPAddress'; \$gw = '$Gateway'; \$password = \"'$password'\"; cd /; C:\CLISave\createRocky1line.ps1;";
|
|
Log::channel('Custom')->info($cmd);
|
|
$this->powerShellService->executePowerCLI($cmd);
|
|
|
|
Log::channel('Custom')->info("Configuration completed at " . Carbon::now());
|
|
} catch (Exception $error) {
|
|
Log::channel('Custom')->error("Error configuring Rocky Linux 9: " . $error->getMessage());
|
|
}
|
|
}
|
|
|
|
private function validateProductData($product, $methodName, $vmware_id): bool
|
|
{
|
|
if (!$product || !$product->IpAddress || !$product->IpAddress->Host || !$product->IpAddress->Host->Datacenter || !$product->IpAddress->Host->Datacenter->vCenter) {
|
|
Log::error("❌ $methodName: Missing required data", [
|
|
'vmware_id' => $vmware_id,
|
|
'product_id' => $product?->id ?? 'unknown',
|
|
]);
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
}
|