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; : self::SCRIPT_LINUX;
$powerCliVmId = $this->powerCliVmId($vmId); $powerCliVmId = $this->powerCliVmId($vmId);
$remoteCmd = strtoupper($osFamily) === VmProvisionService::OS_WINDOWS $remoteCmd = $this->windowsPowerShellCommand(self::SCRIPT_DIR, $script, [
? $this->windowsPowerShellCommand(self::SCRIPT_DIR, $script, [ $powerCliVmId,
$powerCliVmId, $newPassword,
$newPassword, $vcenterHost,
$vcenterHost, $vcenterUser,
$vcenterUser, $vcenterPassword,
$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),
);
Log::info('VsphereHostSshService: executing script', [ Log::info('VsphereHostSshService: executing script', [
'vm_id' => $vmId, 'vm_id' => $vmId,
@@ -121,30 +110,16 @@ final class VsphereHostSshService
$script = $isWindows ? self::SCRIPT_ASSIGN_WINDOWS : self::SCRIPT_ASSIGN_LINUX; $script = $isWindows ? self::SCRIPT_ASSIGN_WINDOWS : self::SCRIPT_ASSIGN_LINUX;
$powerCliVmId = $this->powerCliVmId($vmId); $powerCliVmId = $this->powerCliVmId($vmId);
$remoteCmd = $isWindows $remoteCmd = $this->windowsPowerShellCommand(self::ASSIGN_DIR, $script, [
? $this->windowsPowerShellCommand(self::ASSIGN_DIR, $script, [ $powerCliVmId,
$powerCliVmId, $ip,
$ip, $gateway,
$gateway, $cidr,
$cidr, $vcenterHost,
$vcenterHost, $vcenterUser,
$vcenterUser, $vcenterPassword,
$vcenterPassword, $guestPassword,
$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),
);
Log::info('VsphereHostSshService: executing assignIp', [ Log::info('VsphereHostSshService: executing assignIp', [
'vm_id' => $vmId, 'vm_id' => $vmId,
@@ -188,26 +163,14 @@ final class VsphereHostSshService
$script = $isWindows ? self::SCRIPT_VERIFY_WINDOWS : self::SCRIPT_VERIFY_LINUX; $script = $isWindows ? self::SCRIPT_VERIFY_WINDOWS : self::SCRIPT_VERIFY_LINUX;
$powerCliVmId = $this->powerCliVmId($vmId); $powerCliVmId = $this->powerCliVmId($vmId);
$remoteCmd = $isWindows $remoteCmd = $this->windowsPowerShellCommand(self::ASSIGN_DIR, $script, [
? $this->windowsPowerShellCommand(self::ASSIGN_DIR, $script, [ $powerCliVmId,
$powerCliVmId, $ip,
$ip, $vcenterHost,
$vcenterHost, $vcenterUser,
$vcenterUser, $vcenterPassword,
$vcenterPassword, $guestPassword,
$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),
);
Log::info('VsphereHostSshService: executing verifyIpInGuest', [ Log::info('VsphereHostSshService: executing verifyIpInGuest', [
'vm_id' => $vmId, 'vm_id' => $vmId,
@@ -321,26 +284,14 @@ final class VsphereHostSshService
: self::SCRIPT_CHANGE_PWD_LINUX; : self::SCRIPT_CHANGE_PWD_LINUX;
$powerCliVmId = $this->powerCliVmId($vmId); $powerCliVmId = $this->powerCliVmId($vmId);
$remoteCmd = strtoupper($osFamily) === VmProvisionService::OS_WINDOWS $remoteCmd = $this->windowsPowerShellCommand(self::CHANGE_PWD_DIR, $script, [
? $this->windowsPowerShellCommand(self::CHANGE_PWD_DIR, $script, [ $powerCliVmId,
$powerCliVmId, $newPassword,
$newPassword, $vcenterHost,
$vcenterHost, $vcenterUser,
$vcenterUser, $vcenterPassword,
$vcenterPassword, $currentGuestPassword,
$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),
);
Log::info('VsphereHostSshService: changePasswordCustomer', [ Log::info('VsphereHostSshService: changePasswordCustomer', [
'vm_id' => $vmId, 'vm_id' => $vmId,
@@ -366,7 +317,7 @@ final class VsphereHostSshService
private function windowsPowerShellCommand(string $workingDirectory, string $script, array $arguments): string private function windowsPowerShellCommand(string $workingDirectory, string $script, array $arguments): string
{ {
return sprintf( return sprintf(
'cd %s && powershell -ExecutionPolicy Bypass -File %s %s', 'powershell -ExecutionPolicy Bypass -NonInteractive -File "%s\\%s" %s',
$workingDirectory, $workingDirectory,
$script, $script,
implode(' ', array_map( implode(' ', array_map(