115 lines
3.9 KiB
PHP
115 lines
3.9 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Jobs\VirtualMachine\Rebuild;
|
|
|
|
use App\Http\Services\Telegram;
|
|
use App\Jobs\Mail\Customer\VmRebuildFailedJob;
|
|
use App\Models\ModuleVmware;
|
|
use App\Services\VirtualMachine\Rebuild\VmRebuildIpService;
|
|
use App\Services\VirtualMachine\Rebuild\VmRebuildService;
|
|
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;
|
|
use Throwable;
|
|
|
|
/**
|
|
* RebuildAssignIpJob
|
|
*
|
|
* Reuses the existing service IP on the new VM (no allocation, no pool
|
|
* mutation, no ownership change). Idempotent: re-running it simply runs the
|
|
* assign + verify scripts again with the same parameters.
|
|
*/
|
|
final class RebuildAssignIpJob implements ShouldQueue
|
|
{
|
|
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
|
|
|
public int $tries = 3;
|
|
public int $timeout = 600;
|
|
public int $backoff = 30;
|
|
|
|
public function __construct(
|
|
public readonly int $moduleVmwareId,
|
|
public readonly int $newOsId,
|
|
public readonly string $newVmId,
|
|
public readonly int $reinstallLogId,
|
|
) {
|
|
}
|
|
|
|
public function handle(
|
|
VmRebuildService $rebuildService,
|
|
VmRebuildIpService $ipService,
|
|
): void {
|
|
Log::info('RebuildAssignIpJob: start', [
|
|
'module_vmware_id' => $this->moduleVmwareId,
|
|
'new_vm_id' => $this->newVmId,
|
|
'attempt' => $this->attempts(),
|
|
]);
|
|
|
|
$ctx = $rebuildService
|
|
->buildContext($this->moduleVmwareId, $this->newOsId, $this->newVmId);
|
|
|
|
try {
|
|
$ipService->assignAndVerify($ctx);
|
|
} catch (Throwable $e) {
|
|
// Surface the actual PS1 output into the reinstall log so admin
|
|
// can see WHY it failed without diving into laravel.log.
|
|
\App\Models\ReinstallLog::where('id', $this->reinstallLogId)->update([
|
|
'error_message' => 'AssignIp attempt ' . $this->attempts() . ': ' . $e->getMessage(),
|
|
'updated_at' => now(),
|
|
]);
|
|
throw $e;
|
|
}
|
|
|
|
RebuildChangePasswordJob::dispatch(
|
|
$this->moduleVmwareId,
|
|
$this->newOsId,
|
|
$this->newVmId,
|
|
$this->reinstallLogId,
|
|
)->onQueue('default');
|
|
|
|
Log::info('RebuildAssignIpJob: completed, password job dispatched', [
|
|
'module_vmware_id' => $this->moduleVmwareId,
|
|
'new_vm_id' => $this->newVmId,
|
|
]);
|
|
|
|
(new Telegram)->SendRebuildStepNotification($ctx->vmName, $ctx->ip, 'Gán địa chỉ IP', true);
|
|
}
|
|
|
|
public function failed(Throwable $e): void
|
|
{
|
|
Log::error('RebuildAssignIpJob: all retries exhausted — old VM preserved', [
|
|
'module_vmware_id' => $this->moduleVmwareId,
|
|
'new_vm_id' => $this->newVmId,
|
|
'error' => $e->getMessage(),
|
|
]);
|
|
|
|
\App\Models\ReinstallLog::where('id', $this->reinstallLogId)->update([
|
|
'status' => \App\Models\ReinstallLog::STATUS_FAILED,
|
|
'error_message' => 'AssignIp failed: ' . $e->getMessage(),
|
|
'updated_at' => now(),
|
|
]);
|
|
|
|
$vm = ModuleVmware::with('customer')->find($this->moduleVmwareId);
|
|
(new Telegram)->SendRebuildStepNotification(
|
|
$vm->name ?? "VM #{$this->moduleVmwareId}",
|
|
$vm->ip ?? '',
|
|
'Gán địa chỉ IP',
|
|
false,
|
|
$e->getMessage(),
|
|
);
|
|
|
|
VmRebuildFailedJob::dispatch([
|
|
'customer_email' => $vm?->customer?->email,
|
|
'customer_name' => $vm?->customer?->fullname,
|
|
'vm_name' => $vm->name ?? "VM #{$this->moduleVmwareId}",
|
|
'ip' => $vm->ip ?? '',
|
|
'module_vmware_id' => $this->moduleVmwareId,
|
|
]);
|
|
}
|
|
}
|