Refactor VM provisioning pipeline into focused services and add IP pool

management
This commit is contained in:
baodev0
2026-05-29 12:03:35 +07:00
parent 6bb4a47bfb
commit 7ae237ee9d
32 changed files with 1170 additions and 1200 deletions
-72
View File
@@ -1,72 +0,0 @@
<?php
namespace App\Console\Commands;
use App\Http\Services\WinRMClient;
use Exception;
use Illuminate\Console\Command;
use phpseclib3\Net\SSH2;
class Test extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'app:test';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Command description';
/**
* Execute the console command.
*/
public function handle()
{
try {
// $ssh = new SSH2('103.42.58.37', 22, 10);
// if (!$ssh->login('root', 'TGS@123')) {
// exit('Login Failed');
// }
$username = "administrator";
$password = 'TGS@123';
$client = new WinRMClient('10.10.10.173', 'administrator', 'Thegioiso@123**$$8899TgTk2');
// $output = $client->execute_command("net user \"$username\" \"$password\"");
$output = $client->execute_command("dir c:\\");
echo $output;
// $client = new WinRMClient('192.168.1.100', 'username', 'password');
// Rename HostName
// echo $ssh->exec("hostnamectl set-hostname NguyenKhoi_Dev");
// Cập nhật IP tĩnh
// $ssh->exec("nmcli con mod ens192 ipv4.address 103.42.58.37/24;");
// $ssh->exec("nmcli con mod ens192 ipv4.gateway 103.42.58.1;");
// Resize disk Centos 7
// $ssh->exec("sudo pvcreate /dev/sda2;");
// $ssh->exec("sudo vgextend /dev/centos /dev/sda2;");
// $ssh->exec("sudo lvextend /dev/centos/root /dev/sda2");
// $ssh->exec("xfs_growfs /dev/centos/root");
// Resize disk Centos 8
// $ssh->exec("sudo growpart /dev/sda 3");
// $ssh->exec("sudo pvresize /dev/sda3");
// $ssh->exec("sudo lvresize -l+100%FREE --resizefs /dev/mapper/ubuntu--vg-ubuntu--lv");
// 'growpart /dev/sda 3; pvresize /dev/sda3; lvresize -l+100%FREE --resizefs /dev/mapper/ubuntu--vg-ubuntu--lv;
// Change Pasword
// $ssh->exec("echo \"root:TGS@123\" | chpasswd");
// echo $ssh->exec("sudo tee /sys/class/block/sda/device/rescan");
// echo $ssh->exec("lsblk");
} catch (Exception $exception) {
echo $exception->getMessage();
}
}
}
-91
View File
@@ -1,91 +0,0 @@
<?php
namespace App\Console\Commands;
use App\Http\Services\vCenter;
use App\Models\Vcenter as VcenterModel;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Log;
class TestAddonUpdate extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'test:addon-update
{vm_id : VM ID to test}
{--cpu=1 : CPU addon amount}
{--ram=1 : RAM addon amount in GB}
{--disk=0 : Disk addon amount in GB}
{--vcenter=1 : vCenter ID}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Test addon update functionality for a specific VM';
/**
* Execute the console command.
*/
public function handle()
{
$vmId = $this->argument('vm_id');
$cpuAddon = (int) $this->option('cpu');
$ramAddon = (int) $this->option('ram');
$diskAddon = (int) $this->option('disk');
$vcenterId = (int) $this->option('vcenter');
$this->info("🧪 Testing addon update for VM: {$vmId}");
$this->info("Addon config: CPU={$cpuAddon}, RAM={$ramAddon}GB, Disk={$diskAddon}GB");
try {
// Lấy vCenter model
$vcenterModel = VcenterModel::find($vcenterId);
if (!$vcenterModel) {
$this->error("❌ Không tìm thấy vCenter với ID: {$vcenterId}");
return 1;
}
$this->info("📡 Connecting to vCenter: {$vcenterModel->host}");
// Khởi tạo vCenter service
$vCenter = new vCenter();
// Login to vCenter
if (!$vCenter->loginServer($vcenterModel)) {
$this->error("❌ Không thể login vào vCenter");
return 1;
}
$this->info("✅ Login thành công");
// Test addon update
$addon = [
'cpu' => $cpuAddon,
'ram' => $ramAddon,
'disk' => $diskAddon
];
$result = $vCenter->testAddonUpdate($vmId, $addon, 1, 1);
if ($result['error']) {
$this->error("❌ Test thất bại: " . $result['message']);
return 1;
}
$this->info("✅ Test thành công: " . $result['message']);
$this->info("📋 Kiểm tra logs để xem chi tiết");
return 0;
} catch (\Exception $e) {
$this->error("❌ Exception: " . $e->getMessage());
Log::error("TestAddonUpdate command error: " . $e->getMessage());
return 1;
}
}
}
@@ -1,91 +0,0 @@
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use App\Models\Order;
use App\Models\OrderItem;
use App\Models\Invoice;
use App\Jobs\VirtualMachine\Create\CreateVirtualMachineJob;
use Illuminate\Support\Facades\Log;
class TestFullVMProcess extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'test:full-vm-process {order_id}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Test full VM creation process and VM ID saving';
/**
* Execute the console command.
*/
public function handle()
{
$orderId = $this->argument('order_id');
$this->info("Testing full VM process for Order #{$orderId}");
try {
// Tìm order
$order = Order::with(['orderItems.addons', 'customer'])->find($orderId);
if (!$order) {
$this->error("Order #{$orderId} not found!");
return 1;
}
$this->info("Found order: {$order->order_num}");
$this->info("Customer: {$order->customer->email}");
$this->info("Order items count: " . $order->orderItems->count());
// Tìm invoice
$invoice = Invoice::where('order_id', $orderId)->first();
if (!$invoice) {
$this->error("Invoice for Order #{$orderId} not found!");
return 1;
}
$this->info("Found invoice: #{$invoice->id}");
$this->info("Current VM ID: " . ($invoice->vm_id ?? 'NULL'));
// Test từng order item
foreach ($order->orderItems as $item) {
$this->info("\n--- Testing Order Item #{$item->id} ---");
$this->info("Product Type: {$item->product_type}");
$this->info("Server Name: {$item->server_name}");
$this->info("Addons count: " . $item->addons->count());
if ($item->addons->count() > 0) {
$this->info("Addons:");
foreach ($item->addons as $addon) {
$this->info(" - {$addon->addonItems->name}: {$addon->qty} {$addon->addonItems->prefix}");
}
}
// Dispatch job để test
$this->info("Dispatching CreateVirtualMachineJob...");
CreateVirtualMachineJob::dispatch($item);
$this->info("Job dispatched successfully!");
}
$this->info("\n✅ Full VM process test completed!");
$this->info("Check logs for detailed information about the process.");
return 0;
} catch (\Exception $e) {
$this->error("Error: " . $e->getMessage());
Log::error('TestFullVMProcess error: ' . $e->getMessage());
return 1;
}
}
}
-149
View File
@@ -1,149 +0,0 @@
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use App\Models\Invoice;
use App\Http\Services\vCenter;
use Illuminate\Support\Facades\Log;
class TestVMInfo extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'test:vm-info {invoice_id}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Test getting VM information from vCenter API';
/**
* Execute the console command.
*/
public function handle()
{
$invoiceId = $this->argument('invoice_id');
$this->info("Testing VM info for Invoice #{$invoiceId}");
try {
// Tìm invoice
$invoice = Invoice::with(['invoiceItem.addons', 'invoiceItem.os.osGroup', 'customer'])->find($invoiceId);
if (!$invoice) {
$this->error("Invoice #{$invoiceId} not found!");
return 1;
}
$this->info("Found invoice: #{$invoice->id}");
$this->info("VM ID: " . ($invoice->vm_id ?? 'NULL'));
if (!$invoice->vm_id) {
$this->error("Invoice #{$invoiceId} doesn't have VM ID!");
return 1;
}
// Lấy thông tin vCenter
$orderItem = $invoice->invoiceItem->first();
if (!$orderItem) {
$this->error("No order item found for invoice #{$invoiceId}!");
return 1;
}
$product = $orderItem->getProductAttribute();
if (!$product) {
$this->error("No product found for order item #{$orderItem->id}!");
return 1;
}
$host = $product->host;
$datacenter = $host?->datacenter;
$vcenter = $datacenter?->vcenter;
if (!$vcenter) {
$this->error("No vCenter information found!");
return 1;
}
$this->info("vCenter Host: {$vcenter->host}");
$this->info("vCenter User: {$vcenter->user}");
// Test vCenter service
$vCenterService = new vCenter();
$this->info("Attempting to login to vCenter...");
if (!$vCenterService->loginServer($vcenter)) {
$this->error("Failed to login to vCenter!");
return 1;
}
$this->info("✅ Successfully logged in to vCenter");
// Test getting VM details
$this->info("Getting VM details...");
$vmDetail = $vCenterService->GetDetailVirtualMachine($invoice->vm_id);
if ($vmDetail['error'] === false) {
$this->info("✅ Successfully got VM details");
$this->info("VM Name: " . ($vmDetail['data']['name'] ?? 'Unknown'));
$this->info("VM Version: " . ($vmDetail['data']['version'] ?? 'Unknown'));
} else {
$this->error("❌ Failed to get VM details: " . ($vmDetail['message'] ?? 'Unknown error'));
return 1;
}
// Test getting power status
$this->info("Getting power status...");
$powerStatus = $vCenterService->CheckVirtualMachine($invoice->vm_id);
if ($powerStatus['error'] === false) {
$this->info("✅ Successfully got power status");
$this->info("Power State: " . ($powerStatus['data']['state'] ?? 'Unknown'));
} else {
$this->warn("⚠️ Failed to get power status: " . ($powerStatus['message'] ?? 'Unknown error'));
}
// Test getting IP address
$this->info("Getting IP address...");
$ipInfo = $vCenterService->GetIPAddressVirtualMachine($invoice->vm_id);
if ($ipInfo['error'] === false) {
$this->info("✅ Successfully got IP info");
$this->info("IP Info data structure:");
$this->line(json_encode($ipInfo['data'], JSON_PRETTY_PRINT));
if (!empty($ipInfo['data']['ip_addresses'])) {
$ipAddresses = $ipInfo['data']['ip_addresses'];
$this->info("IP Addresses type: " . gettype($ipAddresses));
$this->info("IP Addresses value: " . json_encode($ipAddresses));
if (is_array($ipAddresses)) {
$this->info("IP Addresses: " . implode(', ', $ipAddresses));
} else {
$this->info("IP Addresses: " . $ipAddresses);
}
} else {
$this->info("No IP addresses found");
}
} else {
$this->warn("⚠️ Failed to get IP info: " . ($ipInfo['message'] ?? 'Unknown error'));
}
// Display full VM data
$this->info("\n=== Full VM Data ===");
$this->line(json_encode($vmDetail['data'], JSON_PRETTY_PRINT));
return 0;
} catch (\Exception $e) {
$this->error("Error: " . $e->getMessage());
Log::error('TestVMInfo error: ' . $e->getMessage());
return 1;
}
}
}
-80
View File
@@ -1,80 +0,0 @@
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use App\Models\Invoice;
use App\Models\Order;
use Illuminate\Support\Facades\Log;
class TestVMSave extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'test:vm-save {order_id} {vm_id}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Test saving VM ID to invoice table';
/**
* Execute the console command.
*/
public function handle()
{
$orderId = $this->argument('order_id');
$vmId = $this->argument('vm_id');
$this->info("Testing VM ID save for Order #{$orderId} with VM ID: {$vmId}");
try {
// Tìm order
$order = Order::find($orderId);
if (!$order) {
$this->error("Order #{$orderId} not found!");
return 1;
}
$this->info("Found order: {$order->order_num}");
// Tìm invoice tương ứng
$invoice = Invoice::where('order_id', $orderId)->first();
if (!$invoice) {
$this->error("Invoice for Order #{$orderId} not found!");
return 1;
}
$this->info("Found invoice: #{$invoice->id}");
// Lưu vm_id
$oldVmId = $invoice->vm_id;
$invoice->update(['vm_id' => $vmId]);
$this->info("✅ Successfully updated VM ID:");
$this->info(" Old VM ID: " . ($oldVmId ?? 'NULL'));
$this->info(" New VM ID: {$vmId}");
// Verify
$invoice->refresh();
if ($invoice->vm_id === $vmId) {
$this->info("✅ Verification successful - VM ID saved correctly");
} else {
$this->error("❌ Verification failed - VM ID not saved correctly");
return 1;
}
return 0;
} catch (\Exception $e) {
$this->error("Error: " . $e->getMessage());
Log::error('TestVMSave error: ' . $e->getMessage());
return 1;
}
}
}