$this->moduleVmwareId, 'vm_id' => $this->vmId, 'os_family' => $this->osFamily, ]); // [2] Generate plain-text password (not hashed) $newPassword = $provision->generatePassword(16); // [3] Load vCenter credentials from database $vcenter = vCenter::first(); if (!$vcenter) { throw new \RuntimeException('No vCenter record found in module_vcenter'); } $vcenterHost = $vcenter->host; $vcenterUser = $vcenter->user; $vcenterPass = SignRSA::DecryptFunc($vcenter->password); // [4–5] SSH → vSphere host → run PS1 with 5 params $output = $ssh->changePassword( $this->vmId, $newPassword, $this->osFamily, $vcenterHost, $vcenterUser, $vcenterPass, ); // [6] Parse output — throw on FAIL so job retries $this->assertSuccess($output); // [7] Save encrypted + plain password (IP already assigned by AssignIpToGuestJob) $provision->updatePassword($this->moduleVmwareId, $newPassword); // Provisioning complete — activate the service record Service::where('vm_id', $this->moduleVmwareId)->update(['status' => 'active']); // [8] Send credentials email — IP + new password are both ready at this point SendVmCredentialsEmailJob::dispatch($this->moduleVmwareId); Log::info('ChangePasswordViaVsphereJob: completed', [ 'module_vmware_id' => $this->moduleVmwareId, 'vm_id' => $this->vmId, ]); } // ───────────────────────────────────────────────────────────────────────── public function failed(\Throwable $e): void { Log::error('ChangePasswordViaVsphereJob: all retries exhausted', [ 'module_vmware_id' => $this->moduleVmwareId, 'vm_id' => $this->vmId, 'error' => $e->getMessage(), ]); ModuleVmware::where('id', $this->moduleVmwareId)->update(['status' => 0]); Service::where('vm_id', $this->moduleVmwareId)->update(['status' => 'failed']); } // ───────────────────────────────────────────────────────────────────────── /** * PS1 scripts output exactly "SUCCESS" or "FAIL". * Throw RuntimeException on FAIL → triggers job retry. * * @throws \RuntimeException */ private function assertSuccess(string $output): void { $trimmed = trim($output); if (str_contains(strtoupper($trimmed), 'SUCCESS')) { return; } throw new \RuntimeException( "PS1 script returned failure for VM {$this->vmId}. Output: {$trimmed}", ); } }