feat: implement JSON-based IP configuration scripts for Windows and Linux and integrate service activation in provisioning flow

This commit is contained in:
pbao9
2026-05-15 09:08:44 +07:00
parent 41de3744a8
commit f158a84e8b
7 changed files with 282 additions and 114 deletions
+72 -34
View File
@@ -91,8 +91,10 @@ 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
* Windows script params (7): VMName IP MaskBits Gateway DNS GuestUser GuestPassword
* Linux script params (9): VCenter VCUser VCPass VMName IP Gateway DNS GuestUser GuestPassword
*
* Script output: JSON {"status":"ok"} or {"status":"error","message":"..."}
*/
public function assignIp(
string $vmId,
@@ -104,24 +106,43 @@ final class VsphereHostSshService
string $vcenterPassword,
string $guestPassword,
string $osFamily,
string $dns,
string $guestUser,
): string {
$script = strtoupper($osFamily) === VmProvisionService::OS_WINDOWS
? self::SCRIPT_ASSIGN_WINDOWS
: self::SCRIPT_ASSIGN_LINUX;
$isWindows = strtoupper($osFamily) === VmProvisionService::OS_WINDOWS;
$script = $isWindows ? 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),
);
if ($isWindows) {
// assignIPWindows.ps1: VMName IP MaskBits Gateway DNS GuestUser GuestPassword
$remoteCmd = sprintf(
'cd %s && powershell -ExecutionPolicy Bypass -NonInteractive -File %s %s %s %s %s %s %s %s',
self::ASSIGN_DIR,
$script,
$this->warg($vmId),
$this->warg($ip),
$this->warg($cidr),
$this->warg($gateway),
$this->warg($dns),
$this->warg($guestUser),
$this->warg($guestPassword),
);
} else {
// assignIPLinux.ps1: VCenter VCUser VCPass VMName IP Gateway DNS GuestUser GuestPassword
$remoteCmd = sprintf(
'cd %s && powershell -ExecutionPolicy Bypass -NonInteractive -File %s %s %s %s %s %s %s %s %s %s',
self::ASSIGN_DIR,
$script,
$this->warg($vcenterHost),
$this->warg($vcenterUser),
$this->warg($vcenterPassword),
$this->warg($vmId),
$this->warg($ip),
$this->warg($gateway),
$this->warg($dns),
$this->warg($guestUser),
$this->warg($guestPassword),
);
}
Log::info('VsphereHostSshService: executing assignIp', [
'vm_id' => $vmId,
@@ -145,8 +166,10 @@ final class VsphereHostSshService
* 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
* Windows script params (4): VMName IP GuestUser GuestPassword
* Linux script params (7): VCenter VCUser VCPass VMName IP GuestUser GuestPassword
*
* Script output: JSON {"status":"ok"} or {"status":"error","message":"..."}
*/
public function verifyIpInGuest(
string $vmId,
@@ -156,22 +179,37 @@ final class VsphereHostSshService
string $vcenterPassword,
string $guestPassword,
string $osFamily,
string $guestUser,
): string {
$script = strtoupper($osFamily) === VmProvisionService::OS_WINDOWS
? self::SCRIPT_VERIFY_WINDOWS
: self::SCRIPT_VERIFY_LINUX;
$isWindows = strtoupper($osFamily) === VmProvisionService::OS_WINDOWS;
$script = $isWindows ? 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),
);
if ($isWindows) {
// verifyIPWindows.ps1: VMName IP GuestUser GuestPassword
$remoteCmd = sprintf(
'cd %s && powershell -ExecutionPolicy Bypass -NonInteractive -File %s %s %s %s %s',
self::ASSIGN_DIR,
$script,
$this->warg($vmId),
$this->warg($ip),
$this->warg($guestUser),
$this->warg($guestPassword),
);
} else {
// verifyIPLinux.ps1: VCenter VCUser VCPass VMName IP GuestUser GuestPassword
$remoteCmd = sprintf(
'cd %s && powershell -ExecutionPolicy Bypass -NonInteractive -File %s %s %s %s %s %s %s %s',
self::ASSIGN_DIR,
$script,
$this->warg($vcenterHost),
$this->warg($vcenterUser),
$this->warg($vcenterPassword),
$this->warg($vmId),
$this->warg($ip),
$this->warg($guestUser),
$this->warg($guestPassword),
);
}
Log::info('VsphereHostSshService: executing verifyIpInGuest', [
'vm_id' => $vmId,