Refactor VsphereHostSshService to streamline remote command execution; remove redundant conditional logic for PowerShell commands

This commit is contained in:
baodev0
2026-07-13 16:09:19 +07:00
parent 71878b4684
commit eb6e8e9e80
+34 -83
View File
@@ -58,24 +58,13 @@ final class VsphereHostSshService
: self::SCRIPT_LINUX;
$powerCliVmId = $this->powerCliVmId($vmId);
$remoteCmd = strtoupper($osFamily) === VmProvisionService::OS_WINDOWS
? $this->windowsPowerShellCommand(self::SCRIPT_DIR, $script, [
$powerCliVmId,
$newPassword,
$vcenterHost,
$vcenterUser,
$vcenterPassword,
])
: sprintf(
'cd %s && powershell -ExecutionPolicy Bypass -NonInteractive -File %s %s %s %s %s %s',
self::SCRIPT_DIR,
$script,
$this->warg($powerCliVmId),
$this->warg($newPassword),
$this->warg($vcenterHost),
$this->warg($vcenterUser),
$this->warg($vcenterPassword),
);
$remoteCmd = $this->windowsPowerShellCommand(self::SCRIPT_DIR, $script, [
$powerCliVmId,
$newPassword,
$vcenterHost,
$vcenterUser,
$vcenterPassword,
]);
Log::info('VsphereHostSshService: executing script', [
'vm_id' => $vmId,
@@ -121,30 +110,16 @@ final class VsphereHostSshService
$script = $isWindows ? self::SCRIPT_ASSIGN_WINDOWS : self::SCRIPT_ASSIGN_LINUX;
$powerCliVmId = $this->powerCliVmId($vmId);
$remoteCmd = $isWindows
? $this->windowsPowerShellCommand(self::ASSIGN_DIR, $script, [
$powerCliVmId,
$ip,
$gateway,
$cidr,
$vcenterHost,
$vcenterUser,
$vcenterPassword,
$guestPassword,
])
: sprintf(
'cd %s && powershell -ExecutionPolicy Bypass -NonInteractive -File %s %s %s %s %s %s %s %s %s',
self::ASSIGN_DIR,
$script,
$this->warg($powerCliVmId),
$this->warg($ip),
$this->warg($gateway),
$this->warg($cidr),
$this->warg($vcenterHost),
$this->warg($vcenterUser),
$this->warg($vcenterPassword),
$this->warg($guestPassword),
);
$remoteCmd = $this->windowsPowerShellCommand(self::ASSIGN_DIR, $script, [
$powerCliVmId,
$ip,
$gateway,
$cidr,
$vcenterHost,
$vcenterUser,
$vcenterPassword,
$guestPassword,
]);
Log::info('VsphereHostSshService: executing assignIp', [
'vm_id' => $vmId,
@@ -188,26 +163,14 @@ final class VsphereHostSshService
$script = $isWindows ? self::SCRIPT_VERIFY_WINDOWS : self::SCRIPT_VERIFY_LINUX;
$powerCliVmId = $this->powerCliVmId($vmId);
$remoteCmd = $isWindows
? $this->windowsPowerShellCommand(self::ASSIGN_DIR, $script, [
$powerCliVmId,
$ip,
$vcenterHost,
$vcenterUser,
$vcenterPassword,
$guestPassword,
])
: sprintf(
'cd %s && powershell -ExecutionPolicy Bypass -NonInteractive -File %s %s %s %s %s %s %s',
self::ASSIGN_DIR,
$script,
$this->warg($powerCliVmId),
$this->warg($ip),
$this->warg($vcenterHost),
$this->warg($vcenterUser),
$this->warg($vcenterPassword),
$this->warg($guestPassword),
);
$remoteCmd = $this->windowsPowerShellCommand(self::ASSIGN_DIR, $script, [
$powerCliVmId,
$ip,
$vcenterHost,
$vcenterUser,
$vcenterPassword,
$guestPassword,
]);
Log::info('VsphereHostSshService: executing verifyIpInGuest', [
'vm_id' => $vmId,
@@ -321,26 +284,14 @@ final class VsphereHostSshService
: self::SCRIPT_CHANGE_PWD_LINUX;
$powerCliVmId = $this->powerCliVmId($vmId);
$remoteCmd = strtoupper($osFamily) === VmProvisionService::OS_WINDOWS
? $this->windowsPowerShellCommand(self::CHANGE_PWD_DIR, $script, [
$powerCliVmId,
$newPassword,
$vcenterHost,
$vcenterUser,
$vcenterPassword,
$currentGuestPassword,
])
: sprintf(
'cd %s && powershell -ExecutionPolicy Bypass -NonInteractive -File %s %s %s %s %s %s %s',
self::CHANGE_PWD_DIR,
$script,
$this->warg($powerCliVmId),
$this->warg($newPassword),
$this->warg($vcenterHost),
$this->warg($vcenterUser),
$this->warg($vcenterPassword),
$this->warg($currentGuestPassword),
);
$remoteCmd = $this->windowsPowerShellCommand(self::CHANGE_PWD_DIR, $script, [
$powerCliVmId,
$newPassword,
$vcenterHost,
$vcenterUser,
$vcenterPassword,
$currentGuestPassword,
]);
Log::info('VsphereHostSshService: changePasswordCustomer', [
'vm_id' => $vmId,
@@ -366,7 +317,7 @@ final class VsphereHostSshService
private function windowsPowerShellCommand(string $workingDirectory, string $script, array $arguments): string
{
return sprintf(
'cd %s && powershell -ExecutionPolicy Bypass -File %s %s',
'powershell -ExecutionPolicy Bypass -NonInteractive -File "%s\\%s" %s',
$workingDirectory,
$script,
implode(' ', array_map(