update change port
This commit is contained in:
Vendored
BIN
Binary file not shown.
@@ -73,31 +73,21 @@ class ChangeRemotePortJob implements ShouldQueue
|
||||
throw new \RuntimeException('SSH runner login failed.');
|
||||
}
|
||||
if (is_string($result)) {
|
||||
// Script trả non-JSON → bắn lỗi kèm raw để debug
|
||||
throw new \RuntimeException('Script returned non-JSON output: ' . $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* Chuẩn hoá $row:
|
||||
* - Script 1-VM: trả 1 object: ['vm'=>.., 'status'=>.., 'message'=>.., 'newPort'=>..]
|
||||
* - (Phòng hờ) Nếu lỡ trả array nhiều item, chọn item có 'vm' trùng $vmName, không thì lấy phần tử đầu.
|
||||
*/
|
||||
$row = null;
|
||||
|
||||
if (is_array($result)) {
|
||||
// case: object đơn
|
||||
if (isset($result['vm']) && isset($result['status'])) {
|
||||
$row = $result;
|
||||
} else {
|
||||
// case: mảng các object
|
||||
// Tìm theo vmName
|
||||
foreach ($result as $it) {
|
||||
if (is_array($it) && (($it['vm'] ?? null) === $vmName)) {
|
||||
$row = $it;
|
||||
break;
|
||||
}
|
||||
}
|
||||
// fallback: phần tử đầu tiên nếu chưa có
|
||||
if ($row === null && isset($result[0]) && is_array($result[0])) {
|
||||
$row = $result[0];
|
||||
}
|
||||
@@ -109,33 +99,23 @@ class ChangeRemotePortJob implements ShouldQueue
|
||||
throw new \RuntimeException('Invalid script response.');
|
||||
}
|
||||
|
||||
// Đọc trường returned
|
||||
$status = strtolower((string) ($row['status'] ?? ''));
|
||||
$message = (string) ($row['message'] ?? '');
|
||||
$newPort = (string) ($row['newPort'] ?? $this->newPort);
|
||||
|
||||
// Xét thành công: status === 'ok'
|
||||
$ok = ($status === 'ok');
|
||||
|
||||
Log::info('ChangeRemotePortJob: script result', [
|
||||
'vm' => $vmName,
|
||||
'status' => $status,
|
||||
'message' => $message,
|
||||
'newPort' => $newPort,
|
||||
'raw' => $row,
|
||||
ModuleVmware::where('id', $product->id)->update([
|
||||
'port' => (int) $newPort,
|
||||
'updated_at' => now(),
|
||||
]);
|
||||
|
||||
if (!$ok) {
|
||||
// Nếu muốn cho phép 'warning' pass, có thể đổi điều kiện ở trên hoặc thêm nhánh dưới:
|
||||
// if ($status === 'warning' && str_contains(strtolower($message), 'rdp port is now')) { $ok = true; }
|
||||
throw new \RuntimeException($message !== '' ? $message : ('Change RDP port failed with status: ' . $status));
|
||||
}
|
||||
|
||||
// Gửi mail thông báo
|
||||
Mail::to($this->email)->send(
|
||||
new ChangePortRemote(vmName: $vmName, newPort: (string) $newPort)
|
||||
);
|
||||
|
||||
Log::info('ChangeRemotePortJob: success', ['vm' => $vmName, 'newPort' => $newPort]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -40,15 +40,10 @@ class ConfigCLICommand implements ShouldQueue
|
||||
$usersName = '';
|
||||
$port = '';
|
||||
|
||||
// Lưu password vào database ngay khi tạo
|
||||
try {
|
||||
$vm = \App\Models\ModuleVmware::where('vmware_id', $this->vmware_id)->first();
|
||||
if ($vm) {
|
||||
$vm->update(['password' => \App\Http\Facade\SignRSA::EncryptFunc($PasswordVirtualMachine)]);
|
||||
Log::info('✅ Đã lưu password vào database', [
|
||||
'vm_id' => $this->vmware_id,
|
||||
'password' => $PasswordVirtualMachine
|
||||
]);
|
||||
}
|
||||
} catch (\Exception $e) {
|
||||
Log::error('❌ Lỗi khi lưu password: ' . $e->getMessage());
|
||||
|
||||
@@ -43,7 +43,7 @@ class CreateVirtualMachineJob implements ShouldQueue
|
||||
public function handle(vCenter $vCenter, Telegram $telegram)
|
||||
{
|
||||
try {
|
||||
$product = $this->item->getProductAttribute(); // dùng helper trong OrderItem
|
||||
$product = $this->item->getProductAttribute();
|
||||
|
||||
if (!$product) {
|
||||
$errorMessage = "Không tìm thấy sản phẩm từ OrderItem #{$this->item->id}";
|
||||
@@ -139,22 +139,6 @@ class CreateVirtualMachineJob implements ShouldQueue
|
||||
'addon' => $addonData,
|
||||
]);
|
||||
|
||||
Log::info('Thông tin vCenter khi tạo máy ảo', [
|
||||
'product' => $product,
|
||||
'item' => $this->item,
|
||||
'host_id' => $host?->id,
|
||||
'host_name' => $host?->name,
|
||||
'host_vcenter_id' => $host?->vcenter_id,
|
||||
'datacenter_id' => $datacenter?->id,
|
||||
'datacenter_name' => $datacenter?->name,
|
||||
'datacenter_vcenter_id' => $datacenter?->vcenter_id,
|
||||
'vcenter' => $vcenter,
|
||||
'vcenter_id' => $vcenter?->id,
|
||||
'vcenter_host' => $vcenter?->host,
|
||||
'vcenter_user' => $vcenter?->user,
|
||||
'vcenter_password' => SignRSA::DecryptFunc($vcenter?->password)
|
||||
]);
|
||||
|
||||
if (is_array($result) && ($result['error'] ?? false)) {
|
||||
$errorMessage = 'Tạo máy ảo thất bại: ' . json_encode($result);
|
||||
Log::warning($errorMessage);
|
||||
@@ -214,11 +198,9 @@ class CreateVirtualMachineJob implements ShouldQueue
|
||||
$customer = $this->item->order->customer;
|
||||
$serverName = $this->item->server_name ?? 'VM-' . $vmId;
|
||||
|
||||
// 1. Lấy IP available từ IPPool
|
||||
$networkId = $product->ipAddress?->module_network_id ?? 1;
|
||||
$availableIP = $this->getAvailableIPFromPool($networkId);
|
||||
|
||||
// 2. Tạo ModuleIpAddress với IP từ pool
|
||||
$moduleIpAddress = ModuleIpAddress::create([
|
||||
'vm_name' => $serverName,
|
||||
'module_host_id' => $host->id,
|
||||
@@ -230,7 +212,6 @@ class CreateVirtualMachineJob implements ShouldQueue
|
||||
'status' => 1 // 1 = Assigned
|
||||
]);
|
||||
|
||||
// 3. Tạo ModuleVmware với IP address vừa tạo
|
||||
$moduleVmware = ModuleVmware::create([
|
||||
'name' => $serverName,
|
||||
'module_ip_address_id' => $moduleIpAddress->id,
|
||||
@@ -249,26 +230,8 @@ class CreateVirtualMachineJob implements ShouldQueue
|
||||
'status' => 1
|
||||
]);
|
||||
|
||||
Log::info('✅ Lưu dữ liệu VM và IP address thành công', [
|
||||
'vmware_id' => $vmId,
|
||||
'module_vmware_id' => $moduleVmware->id,
|
||||
'module_ip_address_id' => $moduleIpAddress->id,
|
||||
'assigned_ip' => $availableIP,
|
||||
'name' => $serverName,
|
||||
'order_item_id' => $this->item->id
|
||||
]);
|
||||
|
||||
// Dispatch AssignIPJob để chạy sau 5 phút
|
||||
AssignIPJob::dispatch($vmId, $moduleVmware->id, $moduleIpAddress->ip)
|
||||
->delay(now()->addMinutes(5));
|
||||
|
||||
Log::info('✅ Đã dispatch AssignIPJob để chạy sau 5 phút', [
|
||||
'vm_id' => $vmId,
|
||||
'module_vmware_id' => $moduleVmware->id,
|
||||
'assigned_ip' => $moduleIpAddress->ip,
|
||||
'delay_minutes' => 5
|
||||
]);
|
||||
|
||||
} catch (\Exception $e) {
|
||||
Log::error('❌ Lỗi khi lưu dữ liệu VM và IP address: ' . $e->getMessage(), [
|
||||
'vmware_id' => $vmId,
|
||||
@@ -286,7 +249,6 @@ class CreateVirtualMachineJob implements ShouldQueue
|
||||
}
|
||||
}
|
||||
|
||||
// Gửi thông báo thành công
|
||||
$telegram->SendJobRunInfo(
|
||||
'CreateVirtualMachineJob',
|
||||
'success',
|
||||
@@ -325,9 +287,6 @@ class CreateVirtualMachineJob implements ShouldQueue
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get available IP from IPPool that doesn't exist in ModuleIpAddress
|
||||
*/
|
||||
private function getAvailableIPFromPool(int $networkId): string
|
||||
{
|
||||
// Lấy tất cả IP từ IPPool theo network_id, order by ip_address
|
||||
@@ -335,39 +294,20 @@ class CreateVirtualMachineJob implements ShouldQueue
|
||||
->orderBy('ip_address')
|
||||
->get();
|
||||
|
||||
Log::info('✅ Lấy IP available từ pool', [
|
||||
'ip_pool_list' => $ipPoolList,
|
||||
'network_id' => $networkId
|
||||
]);
|
||||
|
||||
if ($ipPoolList->isEmpty()) {
|
||||
// Nếu không có IP trong pool, trả về IP default
|
||||
return '0.0.0.0';
|
||||
}
|
||||
|
||||
// Lấy tất cả IP đã được assign trong ModuleIpAddress
|
||||
$assignedIPs = ModuleIpAddress::where('module_network_id', $networkId)
|
||||
->pluck('ip')
|
||||
->toArray();
|
||||
|
||||
// Tìm IP đầu tiên chưa được assign
|
||||
foreach ($ipPoolList as $ipPool) {
|
||||
if (!in_array($ipPool->ip_address, $assignedIPs)) {
|
||||
Log::info('✅ Lấy IP available từ pool', [
|
||||
'ip' => $ipPool->ip_address,
|
||||
'network_id' => $networkId
|
||||
]);
|
||||
return $ipPool->ip_address;
|
||||
}
|
||||
}
|
||||
|
||||
// Nếu tất cả IP đã được assign, log warning và trả về IP default
|
||||
Log::warning('⚠️ Không có IP available trong pool', [
|
||||
'network_id' => $networkId,
|
||||
'total_ips_in_pool' => $ipPoolList->count(),
|
||||
'assigned_ips' => count($assignedIPs)
|
||||
]);
|
||||
|
||||
return '0.0.0.0';
|
||||
}
|
||||
|
||||
|
||||
@@ -63,16 +63,11 @@ class SyncDatastore implements ShouldQueue
|
||||
}
|
||||
|
||||
$dataCenter = ModuleDatacenter::where('vcenter_id', $item->id)->select('id', 'datacenter_id')->get();
|
||||
Log::info('Datacenter SyncDatastore', ['data' => $dataCenter]);
|
||||
foreach ($dataCenter as $_item) {
|
||||
|
||||
$allHost = ModuleHost::where('module_datacenter_id', $_item->id)->select('id', 'name')->get();
|
||||
|
||||
Log::info('Allhost Data', ['data' => $allHost]);
|
||||
if ($allHost) {
|
||||
foreach ($allHost as $hostItem) {
|
||||
$datastore = $this->vCenter->loadDatastore($item, $hostItem->name);
|
||||
Log::info('Datastore', ['data' => $datastore]);
|
||||
|
||||
if ($datastore === "Login_Again") {
|
||||
$token = $this->vCenter->loginServer($item);
|
||||
@@ -81,8 +76,6 @@ class SyncDatastore implements ShouldQueue
|
||||
$this->vCenter->token = $token;
|
||||
}
|
||||
}
|
||||
$datastore = $this->vCenter->loadDatastore($item, $hostItem->name);
|
||||
|
||||
if ($datastore) {
|
||||
|
||||
foreach ($datastore as $_subItem) {
|
||||
|
||||
Reference in New Issue
Block a user