update show ip & reinstall vps 50%
This commit is contained in:
@@ -11,11 +11,13 @@ use App\Http\Services\vCenter;
|
||||
use App\Models\ModuleSnapshot;
|
||||
use App\Models\ModuleVmware;
|
||||
use App\Models\Order;
|
||||
use App\Models\Os;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
use Illuminate\Support\Facades\Validator;
|
||||
use Illuminate\Validation\Validator as ValidatorInstance;
|
||||
|
||||
class ServiceController extends Controller
|
||||
{
|
||||
@@ -146,8 +148,11 @@ class ServiceController extends Controller
|
||||
|
||||
// Lấy thông tin IP
|
||||
$ipAddress = 'N/A';
|
||||
if ($ipInfo['error'] === false && !empty($ipInfo['data']['ip_addresses'])) {
|
||||
$ipAddress = $ipInfo['data']['ip_addresses'][0] ?? 'N/A';
|
||||
if ($ipInfo['error'] === false && !empty($ipInfo['data']['ip_address'])) {
|
||||
$ipAddress = $ipInfo['data']['ip_address'] ?? 'N/A';
|
||||
} elseif ($ipInfo['error'] === false && isset($ipInfo['data']['value']['ip_address'])) {
|
||||
// Fallback for nested structure
|
||||
$ipAddress = $ipInfo['data']['value']['ip_address'] ?? 'N/A';
|
||||
}
|
||||
|
||||
// Tính toán thông tin phần cứng
|
||||
@@ -414,7 +419,6 @@ class ServiceController extends Controller
|
||||
->onQueue('snapshots');
|
||||
|
||||
return redirect()->back()->with('success', 'Đã gửi yêu cầu tạo snapshot. Quá trình sẽ mất 3-5 phút, vui lòng chờ đợi.');
|
||||
|
||||
} catch (\Exception $e) {
|
||||
Log::error('❌ Lỗi khi tạo snapshot', [
|
||||
'product_id' => $request->product_id ?? 'not_provided',
|
||||
@@ -527,7 +531,6 @@ class ServiceController extends Controller
|
||||
'data' => $responseData,
|
||||
'cached' => false
|
||||
]);
|
||||
|
||||
} catch (\Exception $e) {
|
||||
Log::error('Lỗi khi lấy danh sách snapshot', [
|
||||
'user_id' => $user->id ?? 'guest',
|
||||
@@ -618,7 +621,6 @@ class ServiceController extends Controller
|
||||
'error' => false,
|
||||
'message' => 'Đã gửi yêu cầu xóa snapshot. Quá trình sẽ mất 1-2 phút, vui lòng chờ đợi.'
|
||||
]);
|
||||
|
||||
} catch (\Exception $e) {
|
||||
Log::error('❌ Lỗi khi xóa snapshot', [
|
||||
'product_id' => $request->product_id ?? 'not_provided',
|
||||
@@ -710,7 +712,6 @@ class ServiceController extends Controller
|
||||
'error' => false,
|
||||
'message' => 'Đã gửi yêu cầu khôi phục snapshot. Quá trình sẽ mất 2-3 phút, vui lòng chờ đợi.'
|
||||
]);
|
||||
|
||||
} catch (\Exception $e) {
|
||||
Log::error('❌ Lỗi khi khôi phục snapshot', [
|
||||
'product_id' => $request->product_id ?? 'not_provided',
|
||||
@@ -930,6 +931,183 @@ class ServiceController extends Controller
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public function reinstallVM(Request $request, vCenter $vCenterService)
|
||||
{
|
||||
try {
|
||||
// Debug: Log request data
|
||||
Log::info('reinstallVM called', [
|
||||
'request_data' => $request->all(),
|
||||
'user_id' => customer()?->id ?? 'not_logged_in'
|
||||
]);
|
||||
|
||||
$user = customer();
|
||||
if (!$user) {
|
||||
Log::warning('User not logged in for reinstallVM');
|
||||
return response()->json([
|
||||
'error' => true,
|
||||
'message' => __('Chưa đăng nhập. Vui lòng đăng nhập để sử dụng tính năng này.')
|
||||
], 401);
|
||||
}
|
||||
|
||||
$validator = Validator::make($request->all(), [
|
||||
'invoice_id' => 'required|integer',
|
||||
]);
|
||||
|
||||
if ($validator->fails()) {
|
||||
Log::warning('Validation failed for reinstallVM', [
|
||||
'errors' => $validator->errors()->toArray()
|
||||
]);
|
||||
return response()->json([
|
||||
'error' => true,
|
||||
'message' => __('Dữ liệu không hợp lệ'),
|
||||
'errors' => $validator->errors()
|
||||
], 422);
|
||||
}
|
||||
|
||||
// Debug: Log invoice lookup
|
||||
Log::info('Looking for invoice', ['invoice_id' => $request->invoice_id]);
|
||||
|
||||
$invoice = Invoice::find($request->invoice_id);
|
||||
|
||||
// Debug: Log invoice result
|
||||
Log::info('Invoice lookup result', [
|
||||
'invoice_id' => $request->invoice_id,
|
||||
'found' => $invoice ? 'yes' : 'no',
|
||||
'invoice_data' => $invoice ? $invoice->toArray() : null
|
||||
]);
|
||||
|
||||
if (!$invoice) {
|
||||
return response()->json([
|
||||
'error' => true,
|
||||
'message' => __('Không tìm thấy hóa đơn')
|
||||
], 404);
|
||||
}
|
||||
|
||||
// Debug: Log product lookup
|
||||
$invoiceItem = $invoice->invoiceItem->first();
|
||||
Log::info('Invoice item lookup', [
|
||||
'invoice_item_found' => $invoiceItem ? 'yes' : 'no',
|
||||
'invoice_item_data' => $invoiceItem ? $invoiceItem->toArray() : null
|
||||
]);
|
||||
|
||||
$productId = $invoiceItem?->getProductAttribute()?->id;
|
||||
Log::info('Product lookup', [
|
||||
'product_id' => $productId,
|
||||
'product_data' => $invoiceItem?->getProductAttribute()?->toArray() ?? null
|
||||
]);
|
||||
|
||||
$vmware = $productId;
|
||||
$osId = $request->os_id;
|
||||
$userId = $user->id;
|
||||
|
||||
Log::info('VM lookup parameters', [
|
||||
'vmware_id' => $vmware?->vmware_id ?? 'null',
|
||||
'os_id' => $osId,
|
||||
'user_id' => $userId
|
||||
]);
|
||||
|
||||
// Tìm product và kiểm tra quyền sở hữu
|
||||
$vm = ModuleVmware::where('vmware_id', $vmware->vmware_id)
|
||||
->where('customer_id', $userId)
|
||||
->first();
|
||||
|
||||
Log::info('VM lookup result', [
|
||||
'vm_found' => $vm ? 'yes' : 'no',
|
||||
'vm_data' => $vm ? $vm->toArray() : null
|
||||
]);
|
||||
if (!$vm) {
|
||||
return response()->json([
|
||||
'error' => true,
|
||||
'message' => __('Không tìm thấy VPS hoặc bạn không có quyền truy cập')
|
||||
], 404);
|
||||
}
|
||||
|
||||
// Kiểm tra OS có tồn tại và hợp lệ không
|
||||
$os = Os::find($osId);
|
||||
if (!$os) {
|
||||
return response()->json([
|
||||
'error' => true,
|
||||
'message' => __('Hệ điều hành không tồn tại')
|
||||
], 404);
|
||||
}
|
||||
|
||||
// Kiểm tra OS có được phép cho VPS này không
|
||||
$allowedOs = $vm->operatingSystems()->where('os.id', $osId)->first();
|
||||
if (!$allowedOs) {
|
||||
return response()->json([
|
||||
'error' => true,
|
||||
'message' => __('Hệ điều hành này không được phép cho VPS này')
|
||||
], 403);
|
||||
}
|
||||
|
||||
// Debug: Log before vCenter call
|
||||
Log::info('About to call vCenter DeleteVirtualMachine', [
|
||||
'vmware_id' => $vm->vmware_id,
|
||||
'os_id' => $osId
|
||||
]);
|
||||
|
||||
// Gọi vCenter service để xóa VM
|
||||
$result = $vCenterService->DeleteVirtualMachine($vm->vmware_id);
|
||||
|
||||
// Debug: Log vCenter result
|
||||
Log::info('vCenter DeleteVirtualMachine result', [
|
||||
'result' => $result,
|
||||
'is_array' => is_array($result),
|
||||
'has_error_key' => is_array($result) && isset($result['error']),
|
||||
'error_value' => is_array($result) ? ($result['error'] ?? 'not_set') : 'not_array'
|
||||
]);
|
||||
|
||||
if (is_array($result) && isset($result['error']) && $result['error'] === false) {
|
||||
// Cập nhật OS trong database
|
||||
$vm->update([
|
||||
'os_id' => $osId
|
||||
]);
|
||||
|
||||
// Clear VM info cache
|
||||
Cache::forget('vm_info_' . $vm->vmware_id);
|
||||
|
||||
// Log hoạt động
|
||||
Log::info('VM reinstall initiated', [
|
||||
'user_id' => $userId,
|
||||
'product_id' => $vm->id,
|
||||
'old_os_id' => $vm->os_id,
|
||||
'new_os_id' => $osId,
|
||||
'os_name' => $os->version
|
||||
]);
|
||||
|
||||
Log::info('Returning success response');
|
||||
return response()->json([
|
||||
'error' => false,
|
||||
'message' => __('Yêu cầu cài lại máy ảo đã được gửi thành công. Quá trình sẽ mất 5-10 phút.')
|
||||
]);
|
||||
} else {
|
||||
$msg = is_array($result) && isset($result['data']) ? $result['data'] : __('Không thể cài lại máy ảo');
|
||||
Log::warning('vCenter call failed', [
|
||||
'result' => $result,
|
||||
'message' => $msg
|
||||
]);
|
||||
return response()->json([
|
||||
'error' => true,
|
||||
'message' => $msg
|
||||
], 500);
|
||||
}
|
||||
} catch (\Exception $e) {
|
||||
Log::error('Lỗi khi cài lại máy ảo', [
|
||||
'user_id' => $user->id ?? 'guest',
|
||||
'product_id' => $request->product_id ?? 'not_provided',
|
||||
'os_id' => $request->os_id ?? 'not_provided',
|
||||
'error' => $e->getMessage(),
|
||||
'file' => $e->getFile(),
|
||||
'line' => $e->getLine()
|
||||
]);
|
||||
return response()->json([
|
||||
'error' => true,
|
||||
'message' => __('Có lỗi xảy ra khi cài lại máy ảo')
|
||||
], 500);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sanitize input data
|
||||
*/
|
||||
@@ -1016,7 +1194,6 @@ class ServiceController extends Controller
|
||||
}
|
||||
|
||||
return $result['data'] ?? [];
|
||||
|
||||
} catch (\Exception $e) {
|
||||
Log::error('Lỗi khi lấy snapshot từ vCenter', [
|
||||
'product_id' => $product->id,
|
||||
|
||||
Reference in New Issue
Block a user