Add VM rebuild pipeline with isolated job chain and admin log API
This commit is contained in:
@@ -11,9 +11,12 @@ use App\Models\Service;
|
||||
use App\Repositories\Api\ServiceClient\ServiceClientRepository;
|
||||
use App\Services\ActivityLogger;
|
||||
use App\Services\Api\ServiceClient\ServiceClientService;
|
||||
use App\Services\VirtualMachine\Rebuild\VmRebuildService;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Throwable;
|
||||
|
||||
class ServiceController extends Controller
|
||||
{
|
||||
@@ -290,12 +293,12 @@ class ServiceController extends Controller
|
||||
]);
|
||||
}
|
||||
|
||||
public function rebuild(Request $request, string $uuid, vCenter $serviceVCenter): JsonResponse
|
||||
public function rebuild(Request $request, string $uuid, VmRebuildService $rebuildService): JsonResponse
|
||||
{
|
||||
$customer = $request->user();
|
||||
$payload = $request->validate([
|
||||
'os_id' => ['required', 'integer', 'exists:os,id'],
|
||||
'password' => ['nullable', 'string', 'max:255'],
|
||||
'password' => ['nullable', 'string', 'min:8', 'max:255'],
|
||||
]);
|
||||
|
||||
$service = Service::with(['product.osVersions', 'vmware'])
|
||||
@@ -330,27 +333,26 @@ class ServiceController extends Controller
|
||||
return response()->json(['success' => false, 'message' => 'Dịch vụ chưa được cấp phát VM'], 404);
|
||||
}
|
||||
|
||||
DB::table('reinstall_logs')->insert([
|
||||
'product_id' => $vm->id,
|
||||
'product_type' => 4,
|
||||
'custome_id' => $customer->id,
|
||||
'os_id' => $payload['os_id'],
|
||||
'status' => 'pending',
|
||||
'initiator' => 'client',
|
||||
'note' => 'Client rebuild via service UUID: ' . $uuid,
|
||||
'created_at' => now(),
|
||||
'updated_at' => now(),
|
||||
]);
|
||||
|
||||
$result = $serviceVCenter->reBuildVPS((string) $vm->name);
|
||||
if (is_string($result) || ($result['error'] ?? false)) {
|
||||
return response()->json(
|
||||
[
|
||||
'success' => false,
|
||||
'message' => is_string($result) ? $result : $result['message'] ?? 'Không thể rebuild VM',
|
||||
],
|
||||
400,
|
||||
try {
|
||||
$log = $rebuildService->dispatchRebuild(
|
||||
moduleVmwareId: (int) $vm->id,
|
||||
newOsId: (int) $payload['os_id'],
|
||||
initiatorCustomerId: (int) $customer->id,
|
||||
customPassword: !empty($payload['password']) ? (string) $payload['password'] : null,
|
||||
initiator: 'client',
|
||||
note: 'Client rebuild via service UUID: ' . $uuid,
|
||||
);
|
||||
} catch (Throwable $e) {
|
||||
Log::error('Client rebuild dispatch failed', [
|
||||
'service_uuid' => $uuid,
|
||||
'module_vmware_id' => $vm->id,
|
||||
'error' => $e->getMessage(),
|
||||
]);
|
||||
|
||||
return response()->json([
|
||||
'success' => false,
|
||||
'message' => 'Không thể gửi yêu cầu rebuild: ' . $e->getMessage(),
|
||||
], 400);
|
||||
}
|
||||
|
||||
$this->activityLogger->log(
|
||||
@@ -358,12 +360,21 @@ class ServiceController extends Controller
|
||||
logName: 'VM',
|
||||
description: "Customer {$customer->email} rebuild VM: {$vm->name} với OS ID: {$payload['os_id']} qua service UUID: {$uuid}",
|
||||
subject: $vm,
|
||||
newValues: ['os_id' => $payload['os_id'], 'service_uuid' => $uuid],
|
||||
newValues: [
|
||||
'os_id' => (int) $payload['os_id'],
|
||||
'service_uuid' => $uuid,
|
||||
'reinstall_log_id' => $log->id,
|
||||
'custom_password' => !empty($payload['password']),
|
||||
],
|
||||
);
|
||||
|
||||
return response()->json([
|
||||
'success' => true,
|
||||
'message' => 'Đã gửi yêu cầu rebuild VM',
|
||||
'data' => [
|
||||
'reinstall_log_id' => $log->id,
|
||||
'status' => $log->status,
|
||||
],
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user