Files
portal-api/app/Http/Services/vCenter/VMConfigurationService.php
T

196 lines
9.0 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 ConfigVirtualMachineUbuntu($vmware_id = '', $product = [], $password = ''): void
{
$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("===================================================================");
Log::channel('Custom')->info("Configuring Ubuntu for VM #ID $VirtualMachine");
$cmd1 = "\$vm = get-vm -Id '$VirtualMachine';\$hostname ='$HostName'; \$ipadd = '$IPAddress'; \$gw = '$Gateway'; \$password = \"'$password'\"; cd /; C:\CLISave\createUbuntu1line.ps1;";
Log::channel('Custom')->info($cmd1);
$this->powerShellService->executePowerCLI($cmd1);
$cmd2 = "\$vm = get-vm -Id '$VirtualMachine'; \$ipadd = '$IPAddress'; \$gw = '$Gateway'; \$password = \"'$password'\"; cd /; C:\CLISave\changeLinux.ps1;";
Log::channel('Custom')->info($cmd2);
$this->powerShellService->executePowerCLI($cmd2);
Log::channel('Custom')->info("Configuration completed at " . Carbon::now());
Log::channel('Custom')->info("===================================================================");
} catch (Exception $error) {
Log::channel('Custom')->error("Error configuring Ubuntu: " . $error->getMessage());
}
}
public function ConfigCentOSVersion8($vmware_id = '', $product = [], $password = ''): void
{
$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 CentOS 8 for VM #ID $VirtualMachine");
$cmd = "\$vm = get-vm -Id '$VirtualMachine';\$hostname ='$HostName'; \$ipadd = '$IPAddress'; \$gw = '$Gateway'; \$password = \"'$password'\"; cd /; C:\CLISave\createCentOS81line.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 CentOS 8: " . $error->getMessage());
}
}
public function ConfigCentOSVersion7($vmware_id = '', $product = [], $password = ''): void
{
$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 CentOS 7 for VM #ID $VirtualMachine");
$cmd = "\$vm = get-vm -Id '$VirtualMachine'; \$hostname ='$HostName';\$ipadd = '$IPAddress'; \$gw = '$Gateway'; \$password = \"'$password'\"; cd /; C:\CLISave\createCentOS71line.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 CentOS 7: " . $error->getMessage());
}
}
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;
}
}