feat: Implement IP assignment and verification for VM guests; add related PowerShell scripts

This commit is contained in:
pbao9
2026-04-18 08:51:02 +07:00
parent f4145088e1
commit 8aef607e1f
8 changed files with 777 additions and 59 deletions
+112
View File
@@ -28,6 +28,12 @@ final class VsphereHostSshService
private const SCRIPT_LINUX = 'changePasswordLinux.ps1';
private const SCRIPT_WINDOWS = 'changePasswordWindows.ps1';
private const ASSIGN_DIR = 'C:\\CLISave\\AssignIP';
private const SCRIPT_ASSIGN_LINUX = 'assignIPLinux.ps1';
private const SCRIPT_ASSIGN_WINDOWS = 'assignIPWindows.ps1';
private const SCRIPT_VERIFY_LINUX = 'verifyIPLinux.ps1';
private const SCRIPT_VERIFY_WINDOWS = 'verifyIPWindows.ps1';
private const CONNECT_TIMEOUT = 20;
private const MAX_RETRIES = 5;
private const RETRY_DELAY = 5;
@@ -81,6 +87,112 @@ final class VsphereHostSshService
// ─────────────────────────────────────────────────────────────────────────
/**
* Assign a static IP to the VM guest OS via PS1 script.
* Calls assignIPLinux.ps1 or assignIPWindows.ps1 in C:\CLISave\AssignIP\.
*
* Script params: vm_id ip gateway cidr vcenter_host vcenter_user vcenter_password guest_password
* Script output: SUCCESS | FAIL
*/
public function assignIp(
string $vmId,
string $ip,
string $gateway,
string $cidr,
string $vcenterHost,
string $vcenterUser,
string $vcenterPassword,
string $guestPassword,
string $osFamily,
): string {
$script = strtoupper($osFamily) === VmProvisionService::OS_WINDOWS
? self::SCRIPT_ASSIGN_WINDOWS
: self::SCRIPT_ASSIGN_LINUX;
$remoteCmd = sprintf(
'cd %s && powershell -ExecutionPolicy Bypass -NonInteractive -File %s %s %s %s %s %s %s %s %s',
self::ASSIGN_DIR,
$script,
$this->warg($vmId),
$this->warg($ip),
$this->warg($gateway),
$this->warg($cidr),
$this->warg($vcenterHost),
$this->warg($vcenterUser),
$this->warg($vcenterPassword),
$this->warg($guestPassword),
);
Log::info('VsphereHostSshService: executing assignIp', [
'vm_id' => $vmId,
'ip' => $ip,
'script' => $script,
'os' => $osFamily,
]);
$output = $this->sshExec($remoteCmd);
Log::info('VsphereHostSshService: assignIp output', [
'vm_id' => $vmId,
'ip' => $ip,
'output' => trim($output),
]);
return $output;
}
/**
* Verify that an IP is active inside the VM guest OS.
* Calls verifyIPLinux.ps1 or verifyIPWindows.ps1 in C:\CLISave\AssignIP\.
*
* Script params: vm_id ip vcenter_host vcenter_user vcenter_password guest_password
* Script output: SUCCESS | FAIL
*/
public function verifyIpInGuest(
string $vmId,
string $ip,
string $vcenterHost,
string $vcenterUser,
string $vcenterPassword,
string $guestPassword,
string $osFamily,
): string {
$script = strtoupper($osFamily) === VmProvisionService::OS_WINDOWS
? self::SCRIPT_VERIFY_WINDOWS
: self::SCRIPT_VERIFY_LINUX;
$remoteCmd = sprintf(
'cd %s && powershell -ExecutionPolicy Bypass -NonInteractive -File %s %s %s %s %s %s %s',
self::ASSIGN_DIR,
$script,
$this->warg($vmId),
$this->warg($ip),
$this->warg($vcenterHost),
$this->warg($vcenterUser),
$this->warg($vcenterPassword),
$this->warg($guestPassword),
);
Log::info('VsphereHostSshService: executing verifyIpInGuest', [
'vm_id' => $vmId,
'ip' => $ip,
'script' => $script,
'os' => $osFamily,
]);
$output = $this->sshExec($remoteCmd);
Log::info('VsphereHostSshService: verifyIpInGuest output', [
'vm_id' => $vmId,
'ip' => $ip,
'output' => trim($output),
]);
return $output;
}
// ─────────────────────────────────────────────────────────────────────────
/**
* Run a command on the vSphere SSH host via sshpass.
* Retries up to MAX_RETRIES times on connection failure.