- 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.
114 lines
3.8 KiB
PHP
114 lines
3.8 KiB
PHP
<?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',
|
|
};
|
|
}
|
|
}
|