feat: implement JSON-based IP configuration scripts for Windows and Linux and integrate service activation in provisioning flow
This commit is contained in:
@@ -96,6 +96,9 @@ class AssignIpToGuestJob implements ShouldQueue
|
||||
$vcenterUser = $vcenter->user;
|
||||
$vcenterPass = SignRSA::DecryptFunc($vcenter->password);
|
||||
|
||||
// Guest OS default admin user per OS family
|
||||
$guestUser = strtoupper($this->osFamily) === 'WINDOWS' ? 'Administrator' : 'root';
|
||||
|
||||
// ─── Attempt loop ────────────────────────────────────────────────────
|
||||
|
||||
for ($attempt = 1; $attempt <= self::MAX_ATTEMPTS; $attempt++) {
|
||||
@@ -129,6 +132,8 @@ class AssignIpToGuestJob implements ShouldQueue
|
||||
vcenterPassword: $vcenterPass,
|
||||
guestPassword: $this->guestPassword,
|
||||
osFamily: $this->osFamily,
|
||||
dns: $dns,
|
||||
guestUser: $guestUser,
|
||||
);
|
||||
|
||||
// [6] If assign failed → release and try next IP
|
||||
@@ -151,6 +156,7 @@ class AssignIpToGuestJob implements ShouldQueue
|
||||
vcenterPassword: $vcenterPass,
|
||||
guestPassword: $this->guestPassword,
|
||||
osFamily: $this->osFamily,
|
||||
guestUser: $guestUser,
|
||||
);
|
||||
|
||||
// [8] If verification failed → release and try next IP
|
||||
@@ -329,10 +335,11 @@ class AssignIpToGuestJob implements ShouldQueue
|
||||
// Micro-helpers
|
||||
// ─────────────────────────────────────────────────────────────────────────
|
||||
|
||||
/** PS1 scripts output exactly "SUCCESS" or "FAIL". */
|
||||
/** PS1 scripts return JSON: {"status":"ok"} or {"status":"error","message":"..."} */
|
||||
private function isSuccess(string $output): bool
|
||||
{
|
||||
return str_contains(strtoupper(trim($output)), 'SUCCESS');
|
||||
$data = json_decode(trim($output), true);
|
||||
return is_array($data) && ($data['status'] ?? '') === 'ok';
|
||||
}
|
||||
|
||||
/** Convert dotted-decimal netmask to CIDR prefix length (e.g. 255.255.255.0 → 24). */
|
||||
|
||||
@@ -9,7 +9,6 @@ use App\Models\ModuleVmware;
|
||||
use App\Models\vCenter;
|
||||
use App\Services\VmProvisionService;
|
||||
use App\Services\VsphereHostSshService;
|
||||
use App\Jobs\VirtualMachine\Provision\AssignIpToGuestJob;
|
||||
use Illuminate\Bus\Queueable;
|
||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||
use Illuminate\Foundation\Bus\Dispatchable;
|
||||
@@ -88,13 +87,8 @@ class ChangePasswordViaVsphereJob implements ShouldQueue
|
||||
// [7] Save encrypted + plain password (IP will be set later by AssignIpToGuestJob)
|
||||
$provision->updatePassword($this->moduleVmwareId, $newPassword);
|
||||
|
||||
// [8] Assign real IP into guest OS using the new password
|
||||
AssignIpToGuestJob::dispatch(
|
||||
$this->moduleVmwareId,
|
||||
$this->vmId,
|
||||
$this->osFamily,
|
||||
$newPassword, // guest_password — MUST use new password, not template
|
||||
);
|
||||
// [8] Send VM credentials email (AssignIpToGuestJob bypassed temporarily)
|
||||
SendVmCredentialsEmailJob::dispatch($this->moduleVmwareId);
|
||||
|
||||
Log::info('ChangePasswordViaVsphereJob: completed', [
|
||||
'module_vmware_id' => $this->moduleVmwareId,
|
||||
|
||||
@@ -9,22 +9,24 @@ use App\Jobs\Mail\Admin\AdminOrderNotificationJob;
|
||||
use App\Jobs\Mail\SendInformationVirtualMachine;
|
||||
use App\Models\Invoice;
|
||||
use App\Models\ModuleVmware;
|
||||
use App\Models\Service;
|
||||
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\DB;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
/**
|
||||
* SendVmCredentialsEmailJob
|
||||
*
|
||||
* Sends the final VM credential email to the customer with an attached PDF invoice.
|
||||
* 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.
|
||||
* Final step in the VM provisioning chain:
|
||||
* [1] Activate service: services.status → 'active', link services.vm_id → module_vmware.id
|
||||
* [2] Activate VM: module_vmware.status → 1, link module_vmware.service_id → services.id
|
||||
* [3] Send credential email to customer with PDF invoice
|
||||
* [4] Notify admin via AdminOrderNotificationJob
|
||||
*/
|
||||
class SendVmCredentialsEmailJob implements ShouldQueue
|
||||
{
|
||||
@@ -39,7 +41,11 @@ class SendVmCredentialsEmailJob implements ShouldQueue
|
||||
|
||||
public function handle(Telegram $telegram): void
|
||||
{
|
||||
$vm = ModuleVmware::with(['customer', 'os.osGroup', 'order.orderItems'])->find($this->moduleVmwareId);
|
||||
$vm = ModuleVmware::with([
|
||||
'customer',
|
||||
'os.osGroup',
|
||||
'order.orderItems.service',
|
||||
])->find($this->moduleVmwareId);
|
||||
|
||||
if (!$vm) {
|
||||
$msg = "SendVmCredentialsEmailJob: ModuleVmware #{$this->moduleVmwareId} not found";
|
||||
@@ -57,13 +63,19 @@ class SendVmCredentialsEmailJob implements ShouldQueue
|
||||
return;
|
||||
}
|
||||
|
||||
// ── [1 & 2] Activate service + VM records ────────────────────────────
|
||||
|
||||
$orderItem = $vm->order?->orderItems?->first();
|
||||
$service = $orderItem?->service;
|
||||
|
||||
$this->activateService($vm, $service);
|
||||
|
||||
// ── [3] Build email payload ───────────────────────────────────────────
|
||||
|
||||
$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);
|
||||
|
||||
$months = (int) ($orderItem?->months ?? 0);
|
||||
$osGroupName = $vm->os?->osGroup?->name ?? '';
|
||||
$isWindows = str_contains(strtolower($osGroupName), 'windows');
|
||||
$osUsername = $isWindows ? 'Administrator' : 'root';
|
||||
@@ -80,15 +92,14 @@ class SendVmCredentialsEmailJob implements ShouldQueue
|
||||
'service_port' => $vm->port,
|
||||
'service_password' => $vm->plain_password ?? '(xem tài khoản quản lý)',
|
||||
'os_name' => $vm->os
|
||||
? (($osGroupName . ' ' . $vm->os->version))
|
||||
? ($osGroupName . ' ' . $vm->os->version)
|
||||
: 'Unknown OS',
|
||||
'service_due_exp' => $vm->next_due_date
|
||||
? date('d/m/Y', (int) $vm->next_due_date)
|
||||
: 'N/A',
|
||||
];
|
||||
|
||||
// Resolve invoice ID — PDF is generated inside SendInformationVirtualMachine::handle()
|
||||
// to avoid passing large binary blobs through the queue payload
|
||||
// PDF is generated inside the mail job — never pass binary blobs through queue payload
|
||||
$invoice = Invoice::where('order_id', $vm->order_id)->first();
|
||||
|
||||
SendInformationVirtualMachine::dispatch($emailData, $customer, $invoice?->id);
|
||||
@@ -97,10 +108,11 @@ class SendVmCredentialsEmailJob implements ShouldQueue
|
||||
'module_vmware_id' => $this->moduleVmwareId,
|
||||
'customer_email' => $customer->email,
|
||||
'ip' => $vm->ip,
|
||||
'invoice_id' => $invoice?->id,
|
||||
'invoice_id' => $invoice?->id,
|
||||
]);
|
||||
|
||||
// Notify admin with VM credentials after successful provisioning
|
||||
// ── [4] Admin notification ────────────────────────────────────────────
|
||||
|
||||
$order = $vm->order;
|
||||
AdminOrderNotificationJob::dispatch([
|
||||
'customer_name' => $customer->fullname ?? '',
|
||||
@@ -142,7 +154,46 @@ class SendVmCredentialsEmailJob implements ShouldQueue
|
||||
}
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// ─────────────────────────────────────────────────────────────────────────
|
||||
|
||||
/**
|
||||
* Activate the Service record and link it bidirectionally with ModuleVmware.
|
||||
*
|
||||
* services: status → 'active', vm_id → module_vmware.id
|
||||
* module_vmware: status → 1, service_id → services.id
|
||||
*/
|
||||
private function activateService(ModuleVmware $vm, ?Service $service): void
|
||||
{
|
||||
DB::transaction(function () use ($vm, $service) {
|
||||
if ($service) {
|
||||
$service->update([
|
||||
'status' => 'active',
|
||||
'vm_id' => $vm->id,
|
||||
'server_name' => $service->server_name ?: ($vm->hostname ?? $vm->name),
|
||||
]);
|
||||
|
||||
$vm->update([
|
||||
'status' => 1,
|
||||
'service_id' => $service->id,
|
||||
]);
|
||||
|
||||
Log::info('SendVmCredentialsEmailJob: service activated', [
|
||||
'module_vmware_id' => $vm->id,
|
||||
'service_id' => $service->id,
|
||||
'ip' => $vm->ip,
|
||||
]);
|
||||
} else {
|
||||
// Service record not found — still activate the VM so it is not stuck at pending
|
||||
$vm->update(['status' => 1]);
|
||||
|
||||
Log::warning('SendVmCredentialsEmailJob: no Service record found, activated module_vmware only', [
|
||||
'module_vmware_id' => $vm->id,
|
||||
]);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// ─────────────────────────────────────────────────────────────────────────
|
||||
|
||||
private function billingLabel(?int $months): string
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user