client = new Client(); $this->shell = new PowerShellService($this->client); $this->power = new PowerManagementService($this->client); $this->vm = new VirtualMachineService($this->client, $this->shell); $this->snapshot = new SnapshotService($this->client, $this->shell); $this->resource = new ResourceService($this->client); $this->template = new TemplateService($this->client); $this->vmConfig = new VMConfigurationService($this->client, $this->shell); } // ─── Auth & Setup ──────────────────────────────────────────────────────── public function loginServer(\App\Models\Vcenter $vcenterModel): bool|string { $result = $this->client->loginServer($vcenterModel); $this->token = $this->client->token; $this->apiUri = $this->client->apiUri; $this->username = $this->client->getUsername(); $this->password = $this->client->getPassword(); return $result; } public function checkApiExits(): bool { return $this->client->checkApiExits(); } public function setConnection(string $apiUri, string $token): static { $this->apiUri = $apiUri; $this->token = $token; $this->client->apiUri = $apiUri; $this->client->token = $token; return $this; } protected function SetConfig($data = []): static { $this->client->SetConfig($data); $this->username = $this->client->getUsername(); $this->password = $this->client->getPassword(); return $this; } public function resolveVCenterFromProduct(\App\Models\ModuleVmware $product): ?\App\Models\vCenter { return $this->client->resolveVCenterFromProduct($product); } // ─── Resources ─────────────────────────────────────────────────────────── public function getAllDatacenter() { return $this->resource->getAllDatacenter(); } public function getAllHost($datacenter = '') { return $this->resource->getAllHost($datacenter); } public function getAllFolder($datacenter = '') { return $this->resource->getAllFolder($datacenter); } public function getAllResourcePool($host = '', $datacenter = '') { return $this->resource->getAllResourcePool($host, $datacenter); } public function getAllDataStore($host = '') { return $this->resource->getAllDataStore($host); } public function loadDatastore($data = [], $host_id = '') { return $this->resource->loadDatastore($host_id); } public function loadNetWork($data = [], $host_id = '', $datacenter_id = '') { return $this->resource->loadNetWork($host_id, $datacenter_id); } public function getAllNetwork($host = '', $datacenter = '') { return $this->resource->getNetwork($host, $datacenter); } public function getNetwork($host = '', $datacenter = ''): array|bool|string { return $this->resource->getNetwork($host, $datacenter); } // ─── Templates ─────────────────────────────────────────────────────────── public function getAllTemplate() { return $this->template->getAllTemplate(); } public function getAllVersion($library = ''): bool|array|string { return $this->template->getAllVersion($library); } public function getTemplateFromDatastore($datastore = '') { return $this->template->getTemplateFromDatastore($datastore); } // ─── VM Lifecycle ───────────────────────────────────────────────────────── public function getAllVirtualMachine($datacenter = '', $host = '') { return $this->vm->getAllVirtualMachine($datacenter, $host); } public function findVirtualMachineByName(string $vmName): ?array { return $this->vm->findVirtualMachineByName($vmName); } public function FindVirtualMachine($name = ''): string|bool { return $this->vm->FindVirtualMachine($name); } public function CreateVirtualMachine($product_data): array|string { return $this->vm->CreateVirtualMachine($product_data); } public function CheckVirtualMachine($vmware_id = ''): string|array { return $this->vm->CheckVirtualMachine($vmware_id); } public function GetDetailVirtualMachine($vmware_id = ''): array { return $this->vm->GetDetailVirtualMachine($vmware_id); } public function GetIPAddressVirtualMachine($vmware_id = ''): array { return $this->vm->GetIPAddressVirtualMachine($vmware_id); } public function GetNetworkVirtualMachine($vmware_id = ''): array { return $this->vm->GetNetworkVirtualMachine($vmware_id); } public function ConfigNetworkVirtualMachine($vmware_id = '', $config = []): int { return $this->vm->ConfigNetworkVirtualMachine($vmware_id, $config); } public function GetVMStats($vmware_id = ''): array { return $this->vm->GetVMStats($vmware_id); } public function reBuildVPS(string $vmName): array|string { return $this->vm->reBuildVPS($vmName); } public function UpgradeMemoryVirtualMachine($vmware_id = '', $ram = 0): array|string { return $this->vm->UpgradeMemoryVirtualMachine($vmware_id, $ram); } public function UpgradeCPUVirtualMachine($vmware_id = '', $cpu = 0): array|string { return $this->vm->UpgradeCPUVirtualMachine($vmware_id, $cpu); } public function UpgradeDiskVirtualMachine($product, $disk = 0): array|string { return $this->vm->UpgradeDiskVirtualMachine($product, $disk); } // ─── Power Management ──────────────────────────────────────────────────── public function StartVirtualMachine($product, $vmware_id = ''): array|string { return $this->power->StartVirtualMachine($product, $vmware_id); } public function ShutdownGuestVirtualMachine($product, $vmware_id = ''): array|string { return $this->power->ShutdownGuestVirtualMachine($product, $vmware_id); } public function PowerOffVirtualMachine($vmware_id = ''): array|string { return $this->power->PowerOffVirtualMachine($vmware_id); } public function RebootVirtualMachine($product, $vmware_id = ''): array|string { return $this->power->RebootVirtualMachine($product, $vmware_id); } public function restartVM($product, $vmware_id = ''): array|string { return $this->power->restartVM($product, $vmware_id); } public function SuspendVirtualMachine($vmware_id = ''): array|string { return $this->power->SuspendVirtualMachine($vmware_id); } public function getVmPowerState($product, string $vmware_id = ''): array|string { return $this->power->getVmPowerState($product, $vmware_id); } public function DeleteVirtualMachine($vmware_id = ''): array|string { return $this->power->DeleteVirtualMachine($vmware_id); } public function GetConsoleTickets($vmware_id = ''): array|string { return $this->power->GetConsoleTickets($vmware_id); } // ─── Snapshots ─────────────────────────────────────────────────────────── public function CreateSnapShot($product, ?string $snapshotName = null): string|array { return $this->snapshot->CreateSnapShot($product, $snapshotName); } public function getSnapShot($product): string|array { return $this->snapshot->getSnapShot($product); } public function RemoveSnapShot($product, $snap = ''): string|array { return $this->snapshot->RemoveSnapShot($product, $snap); } public function RevertSnapShot($product, $snap = ''): string|array { return $this->snapshot->RevertSnapShot($product, $snap); } // ─── VM OS Configuration ───────────────────────────────────────────────── public function ConfigVirtualMachineUbuntu($vmware_id = '', $product = null, $password = ''): void { $this->vmConfig->ConfigVirtualMachineUbuntu($vmware_id, $product, $password); } public function ConfigCentOSVersion8($vmware_id = '', $product = [], $password = ''): void { $this->vmConfig->ConfigCentOSVersion8($vmware_id, $product, $password); } public function ConfigCentOSVersion7($vmware_id = '', $product = [], $password = ''): void { $this->vmConfig->ConfigCentOSVersion7($vmware_id, $product, $password); } public function ConfigWindowsVersion10($vmware_id = '', $product = [], $password = ''): void { $this->vmConfig->ConfigWindowsVersion10($vmware_id, $product, $password); } public function ConfigWindowsServer($vmware_id = '', $product = [], $password = ''): void { $this->vmConfig->ConfigWindowsServer($vmware_id, $product, $password); } public function ConfigFixWindowsClick($vmware_id = '', $product = []): void { $this->vmConfig->ConfigFixWindowsClick($vmware_id, $product); } public function ConfigRockyLinuxVersion9($vmware_id = '', $product = [], $password = ''): void { $this->vmConfig->ConfigRockyLinuxVersion9($vmware_id, $product, $password); } // ─── Credentials & PowerShell ──────────────────────────────────────────── public function resetGuestPasswordViaPs1( string $vmName, string $guestUser, string $guestPassword, string $newPassword, string $targetUser = 'Administrator', ): array|string { return $this->shell->resetGuestPasswordViaPs1($vmName, $guestUser, $guestPassword, $newPassword, $targetUser); } public function changeRDPPortWindows( string $vmName, string $guestUser, string $guestPassword, string $newPort, ): array|string { return $this->shell->changeRDPPortWindows($vmName, $guestUser, $guestPassword, $newPort); } public function changeIpVM( string $vmName, string $vmwareId, string $ipAddress, string $gateway, string $osType, string $guestUser = '', string $guestPassword = '', string $dns = '8.8.8.8', string $hostname = '', ): string { return $this->shell->changeIpVM( $vmName, $vmwareId, $ipAddress, $gateway, $osType, $guestUser, $guestPassword, $dns, $hostname, ); } public function getWindowsRdpPort(string $vmwareId, string $guestUser, string $guestPassword): ?int { return $this->shell->getWindowsRdpPort($vmwareId, $guestUser, $guestPassword); } // ─── Legacy VPS Direct SSH ──────────────────────────────────────────────── // These methods use direct SSH to guest VM, not via jumphost. protected function loginVPS(array|null $data = []): ?SSH2 { try { $user = $data['user'] ?? 'root'; $ip = $data['ip'] ?? '103.42.58.37'; $password = $data['password'] ?? 'TGS@1232'; $this->process = new SSH2($ip, 22, 10); if (!$this->process->login($user, $password)) { throw new Exception("Cannot login to VPS with IP: $ip, User: $user"); } return $this->process; } catch (Exception $exception) { Log::error('Login VPS failed: ' . $exception->getMessage()); $this->process = null; return null; } } public function change_password_linux( array $data = ['ip' => '10.10.10.173', 'user' => 'admin', 'password' => 'TGS@123', 'new_password' => ''], ): bool { try { if (empty($data['new_password'])) { throw new Exception('New password cannot be empty.'); } $this->loginVPS($data); if (!$this->process) { throw new Exception('Cannot login to VPS.'); } $username = $data['user'] ?? 'root'; $newPassword = $data['new_password']; $this->process->exec('echo ' . escapeshellarg("$username:$newPassword") . ' | chpasswd'); return true; } catch (Exception $exception) { Log::error('Failed to change password: ' . $exception->getMessage()); return false; } } public function change_password_windows( array $data = [ 'ip' => '103.42.58.37', 'user' => 'administrator', 'password' => 'TGS@123', 'new_password' => '', ], ): bool { try { if (empty($data['new_password'])) { throw new Exception('New password cannot be empty.'); } $this->loginVPS($data); if (!$this->process) { throw new Exception('Cannot login to VPS.'); } $username = $data['user'] ?? 'administrator'; $newPassword = $data['new_password']; $response = $this->process->exec("net user \"$username\" \"$newPassword\""); if (stripos($response, 'successfully') === false) { throw new Exception("Failed to change password. Response: $response"); } return true; } catch (Exception $exception) { Log::error('Failed to change password: ' . $exception->getMessage()); return false; } } public function extend_disk_centos_7(): bool { try { $this->loginVPS(); if (!$this->process) { throw new Exception('Cannot login to VPS'); } $checkDisk = $this->process->exec("lsblk | grep -w 'sda2'"); if (empty(trim($checkDisk))) { throw new Exception('Disk /dev/sda2 not found'); } $this->process->exec('sudo tee /sys/class/block/sda/device/rescan'); $this->process->exec('sudo pvcreate /dev/sda2'); $this->process->exec('sudo vgextend /dev/centos /dev/sda2'); $this->process->exec('sudo lvextend /dev/centos/root /dev/sda2'); $this->process->exec('sudo xfs_growfs /dev/centos/root'); return true; } catch (Exception $exception) { Log::error('Failed to extend disk: ' . $exception->getMessage()); return false; } } public function extend_disk_centos_8(): bool { try { $this->loginVPS(); if (!$this->process) { throw new Exception('Cannot login to VPS'); } $checkDisk = $this->process->exec("lsblk | grep -w 'sda3'"); if (empty(trim($checkDisk))) { throw new Exception('Disk /dev/sda3 not found'); } $this->process->exec('sudo pvcreate /dev/sda3'); $this->process->exec('sudo vgextend /dev/cl /dev/sda3'); $this->process->exec('sudo lvextend /dev/cl/root /dev/sd3'); $this->process->exec('sudo xfs_growfs /dev/cl/root'); return true; } catch (Exception $exception) { Log::error('Failed to extend disk: ' . $exception->getMessage()); return false; } } public function extend_disk_ubuntu(): bool { try { $this->loginVPS(); if (!$this->process) { throw new Exception('Cannot login to VPS'); } $checkDisk = $this->process->exec("lsblk | grep -w 'sda3'"); if (empty(trim($checkDisk))) { throw new Exception('Disk /dev/sda3 not found'); } $this->process->exec('sudo tee /sys/class/block/sda/device/rescan'); $this->process->exec('sudo growpart /dev/sda 3'); $this->process->exec('sudo pvresize /dev/sda3'); $this->process->exec('sudo lvresize -l+100%FREE --resizefs /dev/mapper/ubuntu--vg-ubuntu--lv'); return true; } catch (Exception $exception) { Log::error('Failed to extend disk: ' . $exception->getMessage()); return false; } } }