update fix deadcode, security
This commit is contained in:
@@ -1,94 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace App\Jobs\VirtualMachine\Create;
|
||||
|
||||
use Illuminate\Bus\Queueable;
|
||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||
use Illuminate\Foundation\Bus\Dispatchable;
|
||||
use Illuminate\Queue\InteractsWithQueue;
|
||||
use Illuminate\Queue\SerializesModels;
|
||||
|
||||
class CheckIPAddress implements ShouldQueue
|
||||
{
|
||||
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
||||
|
||||
|
||||
protected string $vmware_id;
|
||||
protected $serviceVCenter;
|
||||
protected $data;
|
||||
protected array $info;
|
||||
|
||||
/**
|
||||
* Create a new job instance.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct($vmware_id, $data,$virtual_info,$service)
|
||||
{
|
||||
$this->vmware_id = $vmware_id;
|
||||
$this->serviceVCenter = $service;
|
||||
$this->data = $data;
|
||||
$this->info = $virtual_info;
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the job.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function handle()
|
||||
{
|
||||
$DetailVirtualMachine = $this->serviceVCenter->GetIPAddressVirtualMachine($this->vmware_id);
|
||||
|
||||
if ($DetailVirtualMachine['error']) {
|
||||
throw new \Exception(json_encode($DetailVirtualMachine['message']));
|
||||
}
|
||||
|
||||
// Get expected IP address with null safety
|
||||
$expectedIP = null;
|
||||
|
||||
// Try to get IP from old relationship first
|
||||
if ($this->data && isset($this->data->IpAddress) && $this->data->IpAddress) {
|
||||
$expectedIP = $this->data->IpAddress->ip;
|
||||
}
|
||||
|
||||
// If no IP from old relationship, try to get from ModuleVmware
|
||||
if (!$expectedIP && $this->data) {
|
||||
// Try to find ModuleVmware by vmware_id
|
||||
$moduleVmware = \App\Models\ModuleVmware::with('ipAddress')
|
||||
->where('vmware_id', $this->vmware_id)
|
||||
->first();
|
||||
|
||||
if ($moduleVmware && $moduleVmware->ipAddress) {
|
||||
$expectedIP = $moduleVmware->ipAddress->ip;
|
||||
}
|
||||
}
|
||||
|
||||
// If still no expected IP, log warning but don't fail
|
||||
if (!$expectedIP) {
|
||||
\Illuminate\Support\Facades\Log::warning('⚠️ Không tìm thấy expected IP cho VM', [
|
||||
'vmware_id' => $this->vmware_id,
|
||||
'data_class' => $this->data ? get_class($this->data) : 'null',
|
||||
'has_ip_address_relation' => isset($this->data->IpAddress) && $this->data->IpAddress ? 'yes' : 'no'
|
||||
]);
|
||||
|
||||
// Skip IP check and continue to next job
|
||||
$jobs_configDB = new ConfigDatabase($this->data, $this->info);
|
||||
dispatch($jobs_configDB)->delay(now()->addSeconds(5));
|
||||
return;
|
||||
}
|
||||
|
||||
// Compare actual vs expected IP
|
||||
if ($DetailVirtualMachine['data']['ip_address'] !== $expectedIP) {
|
||||
\Illuminate\Support\Facades\Log::warning('⚠️ IP address mismatch', [
|
||||
'vmware_id' => $this->vmware_id,
|
||||
'actual_ip' => $DetailVirtualMachine['data']['ip_address'],
|
||||
'expected_ip' => $expectedIP
|
||||
]);
|
||||
throw new \Exception('IP address mismatch: actual=' . $DetailVirtualMachine['data']['ip_address'] . ', expected=' . $expectedIP);
|
||||
}
|
||||
$jobs_configDB = new ConfigDatabase($this->data,$this->info);
|
||||
dispatch($jobs_configDB)->delay(now()->addSeconds(5));
|
||||
|
||||
}
|
||||
}
|
||||
@@ -1,54 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace App\Jobs\VirtualMachine\Create;
|
||||
|
||||
use Illuminate\Bus\Queueable;
|
||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||
use Illuminate\Foundation\Bus\Dispatchable;
|
||||
use Illuminate\Queue\InteractsWithQueue;
|
||||
use Illuminate\Queue\SerializesModels;
|
||||
|
||||
class CheckStatusVirtualMachine implements ShouldQueue
|
||||
{
|
||||
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
||||
|
||||
protected $vmware_id;
|
||||
protected $serviceVCenter;
|
||||
protected $data;
|
||||
|
||||
/**
|
||||
* Create a new job instance.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct($vmware_id, $data,$service)
|
||||
{
|
||||
$this->vmware_id = $vmware_id;
|
||||
$this->serviceVCenter = $service;
|
||||
$this->data = $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the job.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function handle()
|
||||
{
|
||||
$status = $this->serviceVCenter->CheckVirtualMachine($this->vmware_id);
|
||||
|
||||
if ($status['error'] === true) {
|
||||
throw new \Exception(json_encode($status['message']));
|
||||
}
|
||||
if ($status['data']['operations_ready'] === false) {
|
||||
do {
|
||||
sleep(10);
|
||||
$status = $this->serviceVCenter->CheckVirtualMachine($this->vmware_id);
|
||||
} while ($status['data']['operations_ready'] !== true);
|
||||
}
|
||||
|
||||
$jobs_configNetwork = new GetNetworkVirtualMachine($this->vmware_id,$this->data,$this->serviceVCenter);
|
||||
dispatch($jobs_configNetwork)->delay(now()->addSeconds(5));
|
||||
|
||||
}
|
||||
}
|
||||
@@ -1,208 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace App\Jobs\VirtualMachine\Create;
|
||||
|
||||
use App\Http\Facade\HelperFacade as Helper;
|
||||
use App\Jobs\Mail\SendInformationVirtualMachine;
|
||||
use App\Models\Setting;
|
||||
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;
|
||||
|
||||
class ConfigCLICommand implements ShouldQueue
|
||||
{
|
||||
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
||||
|
||||
protected string $vmware_id;
|
||||
protected $serviceVCenter;
|
||||
protected $data;
|
||||
|
||||
/**
|
||||
* Create a new job instance.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct($vmware_id, $data,$service)
|
||||
{
|
||||
$this->vmware_id = $vmware_id;
|
||||
$this->serviceVCenter = $service;
|
||||
$this->data = $data;
|
||||
}
|
||||
/**
|
||||
* Execute the job.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function handle()
|
||||
{
|
||||
// Generate new password
|
||||
$PasswordVirtualMachine = Helper::general_password();
|
||||
$usersName = '';
|
||||
$port = '';
|
||||
|
||||
// Get VM with relationships
|
||||
$vm = \App\Models\ModuleVmware::with(['ipAddress', 'os.osGroup'])
|
||||
->where('vmware_id', $this->vmware_id)
|
||||
->first();
|
||||
|
||||
if (!$vm) {
|
||||
Log::error('❌ ConfigCLICommand: VM not found', ['vmware_id' => $this->vmware_id]);
|
||||
return;
|
||||
}
|
||||
|
||||
// Determine OS type and config
|
||||
$osGroupName = $vm->os?->osGroup?->name ?? '';
|
||||
$osVersion = $vm->os?->version ?? '';
|
||||
|
||||
try {
|
||||
switch (true) {
|
||||
case (str_contains($osGroupName, "Windows") && str_contains($osVersion, 10)):
|
||||
$usersName = 'administrator';
|
||||
$port = 3389;
|
||||
$this->serviceVCenter->ConfigWindowsVersion10($this->vmware_id, $vm, $PasswordVirtualMachine);
|
||||
break;
|
||||
|
||||
case (str_contains($osGroupName, "Windows")):
|
||||
$usersName = 'administrator';
|
||||
$port = 3389;
|
||||
$this->serviceVCenter->ConfigWindowsServer($this->vmware_id, $vm, $PasswordVirtualMachine);
|
||||
break;
|
||||
|
||||
case (str_contains($osGroupName, "CentOS") && str_contains($osVersion, 7)):
|
||||
$usersName = 'root';
|
||||
$port = 22;
|
||||
$this->serviceVCenter->ConfigCentOSVersion7($this->vmware_id, $vm, $PasswordVirtualMachine);
|
||||
break;
|
||||
|
||||
case (str_contains($osGroupName, "CentOS") && str_contains($osVersion, 8)):
|
||||
$usersName = 'root';
|
||||
$port = 22;
|
||||
$this->serviceVCenter->ConfigCentOSVersion8($this->vmware_id, $vm, $PasswordVirtualMachine);
|
||||
break;
|
||||
|
||||
case (str_contains($osGroupName, "Ubuntu")):
|
||||
$usersName = 'root';
|
||||
$port = 22;
|
||||
$this->serviceVCenter->ConfigVirtualMachineUbuntu($this->vmware_id, $vm, $PasswordVirtualMachine);
|
||||
break;
|
||||
|
||||
case (str_contains($osGroupName, "Rocky")):
|
||||
$usersName = 'root';
|
||||
$port = 22;
|
||||
$this->serviceVCenter->ConfigRockyLinuxVersion9($this->vmware_id, $vm, $PasswordVirtualMachine);
|
||||
break;
|
||||
|
||||
default:
|
||||
Log::warning('❌ ConfigCLICommand: Unknown OS', [
|
||||
'vmware_id' => $this->vmware_id,
|
||||
'os_group' => $osGroupName,
|
||||
'os_version' => $osVersion,
|
||||
]);
|
||||
return;
|
||||
}
|
||||
|
||||
// Wait for VM to be ready
|
||||
sleep(20);
|
||||
|
||||
$VirtualMachine = $this->serviceVCenter->CheckVirtualMachine($this->vmware_id);
|
||||
|
||||
if ($VirtualMachine['data']['operations_ready'] === false) {
|
||||
$maxAttempts = 30; // 5 minutes max (30 * 10 seconds)
|
||||
$attempt = 0;
|
||||
do {
|
||||
sleep(10);
|
||||
$VirtualMachine = $this->serviceVCenter->CheckVirtualMachine($this->vmware_id);
|
||||
Log::channel('Custom')->info(json_encode($VirtualMachine));
|
||||
$attempt++;
|
||||
} while ($VirtualMachine['data']['operations_ready'] !== true && $attempt < $maxAttempts);
|
||||
}
|
||||
|
||||
// Fix Windows click if Windows
|
||||
if (str_contains($osGroupName, "Windows")) {
|
||||
$this->serviceVCenter->ConfigFixWindowsClick($this->vmware_id, $vm);
|
||||
}
|
||||
|
||||
// ✅ Save password to DB AFTER successful config
|
||||
$vm->update([
|
||||
'password' => \App\Http\Facade\SignRSA::EncryptFunc($PasswordVirtualMachine),
|
||||
'plain_password' => $PasswordVirtualMachine,
|
||||
'port' => $port,
|
||||
'user' => $usersName,
|
||||
]);
|
||||
|
||||
Log::info('✅ ConfigCLICommand: Password changed and saved', [
|
||||
'vmware_id' => $this->vmware_id,
|
||||
'username' => $usersName,
|
||||
'port' => $port,
|
||||
]);
|
||||
|
||||
// Send email with new password to customer
|
||||
$this->sendPasswordEmail($vm, $PasswordVirtualMachine, $usersName, $port);
|
||||
|
||||
// Dispatch CheckIP job
|
||||
$jobs_CheckIP = new CheckIPAddress(
|
||||
$this->vmware_id,
|
||||
$vm,
|
||||
['user' => $usersName, 'password' => $PasswordVirtualMachine, 'port' => $port],
|
||||
$this->serviceVCenter
|
||||
);
|
||||
dispatch($jobs_CheckIP)->delay(now()->addSeconds(5));
|
||||
|
||||
} catch (\Exception $e) {
|
||||
Log::error('❌ ConfigCLICommand: Error during configuration', [
|
||||
'vmware_id' => $this->vmware_id,
|
||||
'error' => $e->getMessage(),
|
||||
'trace' => $e->getTraceAsString(),
|
||||
]);
|
||||
throw $e;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Send email with new password to customer
|
||||
*/
|
||||
private function sendPasswordEmail($vm, string $password, string $username, int $port): void
|
||||
{
|
||||
try {
|
||||
$customer = $vm->customer;
|
||||
if (!$customer) {
|
||||
Log::error('ConfigCLICommand: Cannot find customer', ['vm_id' => $vm->id]);
|
||||
return;
|
||||
}
|
||||
|
||||
$setting = Setting::where('key', 'setting')->first();
|
||||
$settingValue = $setting ? json_decode($setting->value) : null;
|
||||
|
||||
$emailData = [
|
||||
'company_name' => $settingValue->company ?? 'Thế Giới Số',
|
||||
'company_logo' => $settingValue->logo ?? '',
|
||||
'client_name' => trim($customer->firstname . ' ' . $customer->lastname),
|
||||
'product_name' => 'Cloud VPS',
|
||||
'product_billing' => 'N/A',
|
||||
'product_price' => 'N/A',
|
||||
'service_host' => $vm->ipAddress?->ip ?? 'N/A',
|
||||
'service_username' => $username,
|
||||
'service_port' => $port,
|
||||
'service_password' => $password,
|
||||
'os_name' => $vm->os?->osGroup?->name . ' ' . $vm->os?->version ?? 'Unknown OS',
|
||||
'service_due_exp' => $vm->next_due_date ? date('d/m/Y', $vm->next_due_date) : 'N/A',
|
||||
];
|
||||
|
||||
SendInformationVirtualMachine::dispatch($emailData, $customer)
|
||||
->delay(now()->addSeconds(10));
|
||||
|
||||
Log::info('✅ ConfigCLICommand: Password email dispatched', [
|
||||
'customer_email' => $customer->email,
|
||||
'vmware_id' => $vm->vmware_id,
|
||||
]);
|
||||
} catch (\Throwable $e) {
|
||||
Log::error('❌ ConfigCLICommand: Failed to dispatch password email: ' . $e->getMessage(), [
|
||||
'vmware_id' => $vm->vmware_id,
|
||||
'error' => $e->getMessage(),
|
||||
]);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,84 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace App\Jobs\VirtualMachine\Create;
|
||||
|
||||
use App\Events\Create_VirtualMachine_Success;
|
||||
use App\Http\Facade\SignRSA;
|
||||
use App\Http\Facade\HelperFacade as Helper;
|
||||
use App\Http\Services\Telegram;
|
||||
use App\Jobs\Mail\SendInformationVirtualMachine;
|
||||
use App\Models\Setting;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Bus\Queueable;
|
||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||
use Illuminate\Foundation\Bus\Dispatchable;
|
||||
use Illuminate\Queue\InteractsWithQueue;
|
||||
use Illuminate\Queue\SerializesModels;
|
||||
|
||||
class ConfigDatabase implements ShouldQueue
|
||||
{
|
||||
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
||||
|
||||
protected $data;
|
||||
protected $info;
|
||||
protected $telegram;
|
||||
/**
|
||||
* Create a new job instance.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct($data,$info)
|
||||
{
|
||||
$this->data = $data;
|
||||
$this->info = $info;
|
||||
$this->telegram = new Telegram();
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the job.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function handle()
|
||||
{
|
||||
$this->data->update([
|
||||
'name' => $this->data->ipaddress->ip . '_' . $this->data->cannotustomer->firstname,
|
||||
'port' => $this->info['port'],
|
||||
'user' => $this->info['user'],
|
||||
'password' => SignRSA::EncryptFunc($this->info['password']),
|
||||
'status' => 1
|
||||
]);
|
||||
|
||||
$this->data->ipAddress->update([
|
||||
'vm_name' => $this->data->IpAddress->ip . '_' . $this->data->Customer->firstname,
|
||||
'status' => 1
|
||||
]);
|
||||
|
||||
$setting = Setting::where('key', 'setting')->first();
|
||||
|
||||
$data = [
|
||||
'company_name' => json_decode($setting->value)->company,
|
||||
'company_logo' => json_decode($setting->value)->logo,
|
||||
'client_name' => $this->data->Customer->firstname . " " . $this->data->Customer->lastname,
|
||||
'product_name' => $this->data->GetInvoiceItem()->GetItemProduct()->name,
|
||||
'product_billing' => Helper::billing_cycle($this->data->GetInvoiceItem()->billing),
|
||||
'product_price' => number_format($this->data->GetInvoiceItem()->amount, 0, ',', '.'),
|
||||
'service_host' => $this->data->IpAddress->ip,
|
||||
'service_username' => $this->info['user'],
|
||||
'service_port' => $this->info['port'],
|
||||
'service_password' => $this->info['password'],
|
||||
'os_name' => $this->data->Os->OsGroup->name . " " . $this->data->Os->version,
|
||||
'service_due_exp' => Carbon::parse($this->data->next_due_date)->format('d/m/Y')
|
||||
];
|
||||
|
||||
// Note: Email is now sent by ConfigCLICommand after password is changed
|
||||
// $mail = new SendInformationVirtualMachine($data, $this->data->Customer);
|
||||
// dispatch($mail)->delay(now()->addSeconds(5));
|
||||
|
||||
Event(new Create_VirtualMachine_Success("Máy chủ của bạn đã được khởi tạo thành công!",
|
||||
$this->data->customer->id));
|
||||
|
||||
$this->telegram->SendInformation($this->data, $this->info['user'], $this->info['password'], $this->info['port']);
|
||||
}
|
||||
}
|
||||
@@ -1,61 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace App\Jobs\VirtualMachine\Create;
|
||||
|
||||
use Illuminate\Bus\Queueable;
|
||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||
use Illuminate\Foundation\Bus\Dispatchable;
|
||||
use Illuminate\Queue\InteractsWithQueue;
|
||||
use Illuminate\Queue\SerializesModels;
|
||||
|
||||
class GetNetworkVirtualMachine implements ShouldQueue
|
||||
{
|
||||
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
||||
|
||||
protected string $vmware_id;
|
||||
protected $serviceVCenter;
|
||||
protected $data;
|
||||
|
||||
/**
|
||||
* Create a new job instance.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct($vmware_id, $data, $service)
|
||||
{
|
||||
$this->vmware_id = $vmware_id;
|
||||
$this->serviceVCenter = $service;
|
||||
$this->data = $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the job.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function handle()
|
||||
{
|
||||
|
||||
$NetworkForVirtualMachine = $this->serviceVCenter->GetNetworkVirtualMachine($this->vmware_id);
|
||||
|
||||
if ($NetworkForVirtualMachine['error']) {
|
||||
throw new \Exception(json_encode($NetworkForVirtualMachine['message']));
|
||||
}
|
||||
$Network = $this->data->IpAddress->Network->network;
|
||||
if ($Network !== $NetworkForVirtualMachine['data']['network']) {
|
||||
$ConfigNetwork = [
|
||||
"spec" => [
|
||||
"backing" => [
|
||||
"type" => $this->data->IpAddress->Network->type,
|
||||
"network" => $this->data->IpAddress->Network->network
|
||||
]
|
||||
]
|
||||
];
|
||||
$this->serviceVCenter->ConfigNetworkVirtualMachine($this->vmware_id,$ConfigNetwork);
|
||||
|
||||
}
|
||||
|
||||
$jobs_configCLI = new ConfigCLICommand($this->vmware_id,$this->data,$this->serviceVCenter);
|
||||
dispatch($jobs_configCLI)->delay(now()->addSeconds(5));
|
||||
}
|
||||
}
|
||||
@@ -1,57 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace App\Jobs\VirtualMachine\Create;
|
||||
|
||||
use App\Http\Services\vCenter as ServiceVCenter;
|
||||
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;
|
||||
|
||||
class GetVirtualMachine implements ShouldQueue
|
||||
{
|
||||
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
||||
|
||||
protected string $name;
|
||||
protected $data;
|
||||
protected $response = '';
|
||||
protected ServiceVCenter $serviceVCenter;
|
||||
/**
|
||||
* Create a new job instance.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct($name = '', $data,$service)
|
||||
{
|
||||
//
|
||||
$this->name = $name;
|
||||
$this->data = $data;
|
||||
$this->serviceVCenter = $service;
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the job.
|
||||
*
|
||||
* @return bool|string
|
||||
*/
|
||||
public function handle()
|
||||
{
|
||||
|
||||
$vmware_id = $this->serviceVCenter->FindVirtualMachine($this->name);
|
||||
|
||||
Log::channel('detail')->info("==================");
|
||||
|
||||
Log::channel('detail')->info($vmware_id);
|
||||
|
||||
return $vmware_id;
|
||||
|
||||
}
|
||||
|
||||
public function failed(\Throwable $error)
|
||||
{
|
||||
Log::channel('error')->info($error->getMessage());
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user