feat: Add IP address and DNS fields to module_vmware table

- Introduced `ip` and `dns` columns to the `module_vmware` table to store assigned IP addresses and DNS configurations.
- Implemented `ChangePasswordViaSshJob` for secure password changes via SSH.
- Created `ChangePasswordJob` for handling password changes during VM provisioning.
- Developed `ChangePasswordViaVsphereJob` to reset VM guest OS passwords using PowerShell scripts executed on the vSphere host.
- Added `SendVmCredentialsEmailJob` to send final VM credentials to customers.
- Implemented `WaitVmReadyJob` to poll vSphere API until VMware Tools are running and guest IP is available.
- Created `VmProvisionService` for managing VM provisioning tasks, including password generation and updates.
- Developed `VsphereHostSshService` for executing PowerShell scripts on the vSphere host via SSH.
- Added `IpPoolSeeder` to populate IP pools for VM service initialization.
This commit is contained in:
pbao9
2026-04-16 23:21:27 +07:00
parent d68fda2cf0
commit 5043bea79a
16 changed files with 1599 additions and 172 deletions
@@ -0,0 +1,138 @@
<?php
declare(strict_types=1);
namespace App\Jobs\VirtualMachine\Provision;
use App\Services\VmProvisionService;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Log;
/**
* ChangePasswordJob
*
* Connects to the VM via SSH (Linux) or WinRM (Windows) using the
* template default password (TGS@123), then:
* [7] Generates a new secure 16-char password
* [8] Changes the password on the OS
* [9] Verifies the new password works (re-login)
* [10] Saves encrypted password + plain_password to module_vmware
* [11] Dispatches SendVmCredentialsEmailJob
*
* Uses the REAL IP reported by VMware Tools (from WaitVmReadyJob),
* NOT the IP pool address.
*
* Up to 3 job-level retries if an unexpected error occurs.
*/
class ChangePasswordJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
public int $tries = 3;
/** SSH + WinRM operations should complete within 2 min */
public int $timeout = 120;
public function __construct(
public readonly int $moduleVmwareId,
public readonly string $vmId,
public readonly string $guestIp, // real IP from VMware Tools
public readonly string $osFamily, // VmProvisionService::OS_LINUX | OS_WINDOWS
public readonly string $username, // root | Administrator
) {}
public function handle(VmProvisionService $provision): void
{
Log::info('ChangePasswordJob: starting', [
'vm_id' => $this->vmId,
'ip' => $this->guestIp,
'os_family' => $this->osFamily,
'username' => $this->username,
]);
// [7] Generate secure password
$newPassword = $provision->generatePassword(16);
// [8] Change password on OS
$this->changePassword($provision, $newPassword);
// [9] Verify new credentials work (critical — throws on failure)
$this->verifyPassword($provision, $newPassword);
// [10] Persist encrypted password + guest IP to DB
$provision->savePassword($this->moduleVmwareId, $this->guestIp, $newPassword);
// [11] Send credentials email
SendVmCredentialsEmailJob::dispatch($this->moduleVmwareId);
Log::info('ChangePasswordJob: completed', [
'vm_id' => $this->vmId,
'module_vmware_id' => $this->moduleVmwareId,
]);
}
public function failed(\Throwable $e): void
{
Log::error('ChangePasswordJob: failed after max retries', [
'vm_id' => $this->vmId,
'module_vmware_id' => $this->moduleVmwareId,
'error' => $e->getMessage(),
]);
\App\Models\ModuleVmware::where('id', $this->moduleVmwareId)
->update(['status' => 0]);
}
// -------------------------------------------------------------------------
private function changePassword(VmProvisionService $provision, string $newPassword): void
{
if ($this->osFamily === VmProvisionService::OS_WINDOWS) {
Log::info('ChangePasswordJob: changing password via WinRM', [
'vm_id' => $this->vmId,
'ip' => $this->guestIp,
]);
$provision->winrmChangePassword(
$this->guestIp,
$this->username,
$newPassword,
$this->vmId,
);
} else {
Log::info('ChangePasswordJob: changing password via SSH', [
'vm_id' => $this->vmId,
'ip' => $this->guestIp,
]);
$provision->sshChangePassword(
$this->guestIp,
$this->username,
$newPassword,
$this->vmId,
);
}
}
private function verifyPassword(VmProvisionService $provision, string $newPassword): void
{
Log::info('ChangePasswordJob: verifying new credentials', [
'vm_id' => $this->vmId,
'ip' => $this->guestIp,
]);
if ($this->osFamily === VmProvisionService::OS_WINDOWS) {
$provision->verifyWinrmLogin($this->guestIp, $this->username, $newPassword, $this->vmId);
} else {
$provision->verifySshLogin($this->guestIp, $this->username, $newPassword, $this->vmId);
}
Log::info('ChangePasswordJob: credential verification passed', [
'vm_id' => $this->vmId,
]);
}
}
@@ -0,0 +1,131 @@
<?php
declare(strict_types=1);
namespace App\Jobs\VirtualMachine\Provision;
use App\Http\Facade\SignRSA;
use App\Models\ModuleVmware;
use App\Models\vCenter;
use App\Services\VmProvisionService;
use App\Services\VsphereHostSshService;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Log;
/**
* ChangePasswordViaVsphereJob
*
* Resets the VM guest OS password via:
* Laravel → SSH (10.10.10.173) → PowerShell PS1 → Invoke-VMScript (guest OS)
*
* Flow:
* [1] VM already exists, vm_id known
* [2] Generate random 16-char password (plain text)
* [3] Load vCenter credentials from module_vcenter
* [4] SSH into vSphere host
* [5] Execute changePassword{Linux|Windows}.ps1 with 5 params
* [6] Parse "SUCCESS" / "FAIL" from script output
* [7] On SUCCESS → save encrypted password to DB, dispatch email
* [8] On FAIL → throw exception → job retries
*/
class ChangePasswordViaVsphereJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
public int $tries = 5;
public int $timeout = 180;
public int $backoff = 60;
public function __construct(
public readonly int $moduleVmwareId,
public readonly string $vmId,
public readonly string $osFamily, // VmProvisionService::OS_LINUX | OS_WINDOWS
) {}
// ─────────────────────────────────────────────────────────────────────────
public function handle(VmProvisionService $provision, VsphereHostSshService $ssh): void
{
Log::info('ChangePasswordViaVsphereJob: starting', [
'module_vmware_id' => $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 set during VM creation
$provision->updatePassword($this->moduleVmwareId, $newPassword);
// [8] Send credentials email (loads fresh VM data from DB)
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]);
}
// ─────────────────────────────────────────────────────────────────────────
/**
* 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}",
);
}
}
@@ -0,0 +1,113 @@
<?php
declare(strict_types=1);
namespace App\Jobs\VirtualMachine\Provision;
use App\Jobs\Mail\SendInformationVirtualMachine;
use App\Models\ModuleVmware;
use App\Models\Setting;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Log;
/**
* SendVmCredentialsEmailJob
*
* Sends the final VM credential email to the customer.
* Always loads fresh data from DB so it gets the real IP and
* the final password saved by ChangePasswordJob.
*
* This is step [11] in the provisioning flow.
*/
class SendVmCredentialsEmailJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
public int $tries = 3;
public int $timeout = 60;
public function __construct(
public readonly int $moduleVmwareId,
) {}
public function handle(): void
{
$vm = ModuleVmware::with(['customer', 'os.osGroup', 'order.orderItems'])->find($this->moduleVmwareId);
if (!$vm) {
Log::error('SendVmCredentialsEmailJob: ModuleVmware not found', [
'module_vmware_id' => $this->moduleVmwareId,
]);
return;
}
$customer = $vm->customer;
if (!$customer?->email) {
Log::error('SendVmCredentialsEmailJob: customer or email missing', [
'module_vmware_id' => $this->moduleVmwareId,
]);
return;
}
$setting = Setting::where('key', 'setting')->first();
$settingValue = $setting ? json_decode((string) $setting->value) : null;
// Resolve billing info from order items
$orderItem = $vm->order?->orderItems?->first();
$months = (int) ($orderItem?->months ?? 0);
$emailData = [
'company_name' => $settingValue->company ?? 'Thế Giới Số',
'company_logo' => $settingValue->logo ?? '',
'client_name' => trim($customer->firstname . ' ' . $customer->lastname),
'product_name' => $orderItem?->getProductAttribute()?->name ?? 'Cloud VPS',
'product_billing' => $this->billingLabel($months),
'product_price' => $orderItem ? number_format((float) $orderItem->price, 0, ',', '.') : 'N/A',
'service_host' => $vm->ip ?? 'N/A',
'service_username' => $vm->user,
'service_port' => $vm->port,
'service_password' => $vm->plain_password ?? '(xem tài khoản quản lý)',
'os_name' => $vm->os
? (($vm->os->osGroup?->name ?? '') . ' ' . $vm->os->version)
: 'Unknown OS',
'service_due_exp' => $vm->next_due_date
? date('d/m/Y', (int) $vm->next_due_date)
: 'N/A',
];
SendInformationVirtualMachine::dispatch($emailData, $customer);
Log::info('SendVmCredentialsEmailJob: email dispatched', [
'module_vmware_id' => $this->moduleVmwareId,
'customer_email' => $customer->email,
'ip' => $vm->ip,
]);
}
public function failed(\Throwable $e): void
{
Log::error('SendVmCredentialsEmailJob: failed', [
'module_vmware_id' => $this->moduleVmwareId,
'error' => $e->getMessage(),
]);
}
// -------------------------------------------------------------------------
private function billingLabel(?int $months): string
{
return match ($months) {
1 => 'Hàng tháng',
3 => '3 Tháng',
6 => '6 Tháng',
12 => 'Hàng năm',
24 => '2 Năm',
default => ($months ?? 0) . ' Tháng',
};
}
}
@@ -0,0 +1,138 @@
<?php
declare(strict_types=1);
namespace App\Jobs\VirtualMachine\Provision;
use App\Http\Services\vCenter as VCenterService;
use App\Services\VmProvisionService;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Log;
/**
* WaitVmReadyJob
*
* Polls vSphere REST API every 15 s until VMware Tools is running
* and the guest IP is available. No direct TCP port check is required
* because password changes are performed via Invoke-VMScript (vSphere
* path), not direct SSH/WinRM into the VM.
*
* Max 40 attempts × 15 s ≈ 10 minutes before giving up.
* On success → dispatches ChangePasswordViaVsphereJob.
*/
class WaitVmReadyJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
/** 40 attempts × 15 s release delay ≈ 10 min total */
public int $tries = 40;
/** Each poll attempt must finish within 30 s */
public int $timeout = 30;
public function __construct(
public readonly int $moduleVmwareId,
public readonly string $vmId,
public readonly int $vcenterId,
public readonly string $osFamily, // VmProvisionService::OS_LINUX | OS_WINDOWS
public readonly string $username, // root | Administrator
) {}
// -------------------------------------------------------------------------
public function handle(VCenterService $vCenterService, VmProvisionService $provision): void
{
$attempt = $this->attempts();
Log::info('WaitVmReadyJob: polling', [
'vm_id' => $this->vmId,
'attempt' => $attempt,
'os_family' => $this->osFamily,
]);
// ── 1. Login to vCenter ───────────────────────────────────────────────
$vcenter = \App\Models\vCenter::find($this->vcenterId);
if (!$vcenter) {
Log::error('WaitVmReadyJob: vCenter model not found', ['vcenter_id' => $this->vcenterId]);
$this->fail(new \RuntimeException("vCenter id={$this->vcenterId} not found"));
return;
}
try {
$provision->loginVCenter($vcenter, $vCenterService);
} catch (\Throwable $e) {
Log::warning('WaitVmReadyJob: vCenter login failed, will retry', [
'vm_id' => $this->vmId,
'error' => $e->getMessage(),
]);
$this->releaseOrFail(15, $attempt);
return;
}
// ── 2. Check VMware Tools + guest IP ─────────────────────────────────
// Invoke-VMScript requires VMware Tools to be running inside the guest.
$guest = $provision->getVmGuestInfo($vCenterService, $this->vmId);
Log::info('WaitVmReadyJob: guest info', [
'vm_id' => $this->vmId,
'attempt' => $attempt,
'tools_running' => $guest['tools_running'],
'ip_address' => $guest['ip_address'],
]);
if (!$guest['tools_running'] || $guest['ip_address'] === null) {
Log::info('WaitVmReadyJob: VMware Tools not ready yet', ['vm_id' => $this->vmId]);
$this->releaseOrFail(15, $attempt);
return;
}
$guestIp = $guest['ip_address'];
// ── 3. VM ready — dispatch password-change job ────────────────────────
Log::info('WaitVmReadyJob: VMware Tools ready, dispatching ChangePasswordViaVsphereJob', [
'vm_id' => $this->vmId,
'guest_ip' => $guestIp,
]);
ChangePasswordViaVsphereJob::dispatch(
$this->moduleVmwareId,
$this->vmId,
$guestIp,
$this->osFamily,
$this->username,
);
}
// -------------------------------------------------------------------------
public function failed(\Throwable $e): void
{
Log::error('WaitVmReadyJob: exceeded max attempts, marking VM failed', [
'vm_id' => $this->vmId,
'module_vmware_id' => $this->moduleVmwareId,
'error' => $e->getMessage(),
]);
\App\Models\ModuleVmware::where('id', $this->moduleVmwareId)
->update(['status' => 0]);
}
// -------------------------------------------------------------------------
private function releaseOrFail(int $delaySeconds, int $attempt): void
{
if ($attempt >= $this->tries) {
$this->fail(new \RuntimeException(
"VM {$this->vmId} not ready after {$this->tries} attempts (~10 min)",
));
return;
}
$this->release($delaySeconds);
}
}