- Introduced `ip` and `dns` columns to the `module_vmware` table to store assigned IP addresses and DNS configurations. - Implemented `ChangePasswordViaSshJob` for secure password changes via SSH. - Created `ChangePasswordJob` for handling password changes during VM provisioning. - Developed `ChangePasswordViaVsphereJob` to reset VM guest OS passwords using PowerShell scripts executed on the vSphere host. - Added `SendVmCredentialsEmailJob` to send final VM credentials to customers. - Implemented `WaitVmReadyJob` to poll vSphere API until VMware Tools are running and guest IP is available. - Created `VmProvisionService` for managing VM provisioning tasks, including password generation and updates. - Developed `VsphereHostSshService` for executing PowerShell scripts on the vSphere host via SSH. - Added `IpPoolSeeder` to populate IP pools for VM service initialization.
134 lines
4.4 KiB
PHP
134 lines
4.4 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Services;
|
|
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
/**
|
|
* VsphereHostSshService
|
|
*
|
|
* Executes pre-deployed PowerShell scripts on the vSphere Windows host
|
|
* via native SSH (sshpass). No phpseclib dependency.
|
|
*
|
|
* Scripts in C:/CLISave/ accept 5 positional params:
|
|
* VM_ID NEW_PASSWORD VCENTER_HOST VCENTER_USER VCENTER_PASSWORD
|
|
*
|
|
* Scripts only output "SUCCESS" or "FAIL".
|
|
*/
|
|
final class VsphereHostSshService
|
|
{
|
|
private const HOST = '10.10.10.173';
|
|
private const PORT = 22;
|
|
private const USER = 'administrator';
|
|
private const PASS = 'Thegioiso@123**$$8899TgTk2';
|
|
|
|
private const SCRIPT_DIR = 'C:\\CLISave'; // backslash required for Windows cmd.exe
|
|
private const SCRIPT_LINUX = 'changePasswordLinux.ps1';
|
|
private const SCRIPT_WINDOWS = 'changePasswordWindows.ps1';
|
|
|
|
private const CONNECT_TIMEOUT = 20;
|
|
private const MAX_RETRIES = 5;
|
|
private const RETRY_DELAY = 5;
|
|
|
|
// ─────────────────────────────────────────────────────────────────────────
|
|
|
|
/**
|
|
* Run the OS-appropriate PS1 script on the vSphere host.
|
|
*
|
|
* @throws \RuntimeException on SSH failure
|
|
*/
|
|
public function changePassword(
|
|
string $vmId,
|
|
string $newPassword,
|
|
string $osFamily,
|
|
string $vcenterHost,
|
|
string $vcenterUser,
|
|
string $vcenterPassword,
|
|
): string {
|
|
$script = strtoupper($osFamily) === VmProvisionService::OS_WINDOWS
|
|
? self::SCRIPT_WINDOWS
|
|
: self::SCRIPT_LINUX;
|
|
|
|
// Remote Windows cmd.exe command — all args double-quoted
|
|
$remoteCmd = sprintf(
|
|
'cd %s && powershell -ExecutionPolicy Bypass -NonInteractive -File %s %s %s %s %s %s',
|
|
self::SCRIPT_DIR,
|
|
$script,
|
|
$this->warg($vmId),
|
|
$this->warg($newPassword),
|
|
$this->warg($vcenterHost),
|
|
$this->warg($vcenterUser),
|
|
$this->warg($vcenterPassword),
|
|
);
|
|
|
|
Log::info('VsphereHostSshService: executing script', [
|
|
'vm_id' => $vmId,
|
|
'script' => $script,
|
|
'vcenter_host' => $vcenterHost,
|
|
]);
|
|
|
|
$output = $this->sshExec($remoteCmd);
|
|
|
|
Log::info('VsphereHostSshService: script output', [
|
|
'vm_id' => $vmId,
|
|
'output' => trim($output),
|
|
]);
|
|
|
|
return $output;
|
|
}
|
|
|
|
// ─────────────────────────────────────────────────────────────────────────
|
|
|
|
/**
|
|
* Run a command on the vSphere SSH host via sshpass.
|
|
* Retries up to MAX_RETRIES times on connection failure.
|
|
*
|
|
* @throws \RuntimeException
|
|
*/
|
|
private function sshExec(string $remoteCmd): string
|
|
{
|
|
// Full native SSH command executed on local shell
|
|
$sshCmd = sprintf(
|
|
'sshpass -p %s ssh -o StrictHostKeyChecking=no -o ConnectTimeout=%d -p %d %s@%s %s 2>&1',
|
|
escapeshellarg(self::PASS),
|
|
self::CONNECT_TIMEOUT,
|
|
self::PORT,
|
|
escapeshellarg(self::USER),
|
|
escapeshellarg(self::HOST),
|
|
escapeshellarg($remoteCmd),
|
|
);
|
|
|
|
for ($attempt = 1; $attempt <= self::MAX_RETRIES; $attempt++) {
|
|
$output = shell_exec($sshCmd);
|
|
|
|
if ($output === null) {
|
|
Log::warning('VsphereHostSshService: shell_exec returned null', [
|
|
'attempt' => $attempt,
|
|
]);
|
|
} else {
|
|
Log::info('VsphereHostSshService: SSH executed', ['attempt' => $attempt]);
|
|
return $output;
|
|
}
|
|
|
|
if ($attempt < self::MAX_RETRIES) {
|
|
sleep(self::RETRY_DELAY);
|
|
}
|
|
}
|
|
|
|
throw new \RuntimeException(
|
|
'SSH exec to ' . self::HOST . ' failed after ' . self::MAX_RETRIES . ' attempts',
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Windows cmd.exe safe argument: wrap in double quotes, escape inner double quotes.
|
|
* Password charset excludes " and % so wrapping is always safe.
|
|
*/
|
|
private function warg(string $value): string
|
|
{
|
|
return '"' . str_replace('"', '\\"', $value) . '"';
|
|
}
|
|
}
|