111 lines
3.6 KiB
PHP
111 lines
3.6 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\VmRebuildPasswordService;
|
|
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;
|
|
|
|
/**
|
|
* RebuildChangePasswordJob
|
|
*
|
|
* Generates a fresh credential and applies it to the new VM via the shared
|
|
* VsphereHostSshService. Saves the encrypted + plaintext copy onto the
|
|
* existing ModuleVmware row.
|
|
*/
|
|
final class RebuildChangePasswordJob 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 int $newOsId,
|
|
public readonly string $newVmId,
|
|
public readonly int $reinstallLogId,
|
|
) {
|
|
}
|
|
|
|
public function handle(
|
|
VmRebuildService $rebuildService,
|
|
VmRebuildPasswordService $passwordService,
|
|
): void {
|
|
Log::info('RebuildChangePasswordJob: start', [
|
|
'module_vmware_id' => $this->moduleVmwareId,
|
|
'new_vm_id' => $this->newVmId,
|
|
]);
|
|
|
|
$ctx = $rebuildService
|
|
->buildContext($this->moduleVmwareId, $this->newOsId, $this->newVmId);
|
|
|
|
$passwordService->rotate($ctx, $this->reinstallLogId);
|
|
|
|
RebuildAddonInstallJob::dispatch(
|
|
$this->moduleVmwareId,
|
|
$this->newOsId,
|
|
$this->newVmId,
|
|
$this->reinstallLogId,
|
|
)->onQueue('default');
|
|
|
|
Log::info('RebuildChangePasswordJob: completed, addon job dispatched', [
|
|
'module_vmware_id' => $this->moduleVmwareId,
|
|
'new_vm_id' => $this->newVmId,
|
|
]);
|
|
|
|
$vmAfter = ModuleVmware::find($this->moduleVmwareId);
|
|
(new Telegram)->SendRebuildStepNotification(
|
|
$ctx->vmName,
|
|
$ctx->ip,
|
|
'Đổi mật khẩu' . ($ctx->isWindows ? ' & Port RDP' : ''),
|
|
true,
|
|
$ctx->isWindows ? "Port RDP mới: {$vmAfter?->port}" : null,
|
|
);
|
|
}
|
|
|
|
public function failed(Throwable $e): void
|
|
{
|
|
Log::error('RebuildChangePasswordJob: failed — 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' => 'ChangePassword failed: ' . $e->getMessage(),
|
|
'updated_at' => now(),
|
|
]);
|
|
|
|
$vm = ModuleVmware::with('customer')->find($this->moduleVmwareId);
|
|
(new Telegram)->SendRebuildStepNotification(
|
|
$vm->name ?? "VM #{$this->moduleVmwareId}",
|
|
$vm->ip ?? '',
|
|
'Đổi mật khẩu & Port',
|
|
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,
|
|
]);
|
|
}
|
|
}
|