125 lines
4.4 KiB
PHP
125 lines
4.4 KiB
PHP
<?php
|
|
|
|
namespace App\Jobs\VirtualMachine\Create;
|
|
|
|
use App\Http\Facade\HelperFacade as Helper;
|
|
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()
|
|
{
|
|
$PasswordVirtualMachine = Helper::general_password();
|
|
$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());
|
|
}
|
|
|
|
switch (true) {
|
|
case (str_contains($this->data->Os->OsGroup->name, "Windows") && str_contains($this->data->Os->version, 10)):
|
|
$usersName = 'administrator';
|
|
$port = 533892;
|
|
$this->serviceVCenter->ConfigWindowsVersion10($this->vmware_id, $this->data, $PasswordVirtualMachine);
|
|
break;
|
|
|
|
case (str_contains($this->data->Os->OsGroup->name, "Windows")):
|
|
$usersName = 'administrator';
|
|
$port = 53389;
|
|
$this->serviceVCenter->ConfigWindowsServer($this->vmware_id, $this->data,
|
|
$PasswordVirtualMachine);
|
|
|
|
break;
|
|
|
|
case (str_contains($this->data->Os->OsGroup->name, "CentOS") && str_contains($this->data->Os->version, 7)):
|
|
$usersName = 'root';
|
|
$port = 22;
|
|
$this->serviceVCenter->ConfigCentOSVersion7($this->vmware_id, $this->data,
|
|
$PasswordVirtualMachine);
|
|
|
|
break;
|
|
|
|
case (str_contains($this->data->Os->OsGroup->name, "CentOS") && str_contains($this->data->Os->version, 8)):
|
|
$usersName = 'root';
|
|
$port = 22;
|
|
$this->serviceVCenter->ConfigCentOSVersion8($this->vmware_id, $this->data,
|
|
$PasswordVirtualMachine);
|
|
|
|
break;
|
|
|
|
case (str_contains($this->data->Os->OsGroup->name, "Ubuntu")):
|
|
$usersName = 'root';
|
|
$port = 22;
|
|
$this->serviceVCenter->ConfigVirtualMachineUbuntu($this->vmware_id, $this->data,
|
|
$PasswordVirtualMachine);
|
|
break;
|
|
|
|
case (str_contains($this->data->Os->OsGroup->name, "Rocky")):
|
|
$usersName = 'root';
|
|
$port = 22;
|
|
$this->serviceVCenter->ConfigRockyLinuxVersion9($this->vmware_id, $this->data,
|
|
$PasswordVirtualMachine);
|
|
break;
|
|
}
|
|
sleep(20);
|
|
|
|
$VirtualMachine = $this->serviceVCenter->CheckVirtualMachine($this->vmware_id);
|
|
|
|
if ($VirtualMachine['data']['operations_ready'] === false) {
|
|
do {
|
|
sleep(10);
|
|
|
|
$VirtualMachine = $this->serviceVCenter->CheckVirtualMachine($this->vmware_id);
|
|
|
|
Log::channel('Custom')->info(json_encode($VirtualMachine));
|
|
} while ($VirtualMachine['data']['operations_ready'] !== true);
|
|
|
|
}
|
|
if (str_contains($this->data->Os->OsGroup->name, "Windows"))
|
|
{
|
|
$this->serviceVCenter->ConfigFixWindowsClick($this->vmware_id, $this->data);
|
|
}
|
|
$jobs_CheckIP = new CheckIPAddress($this->vmware_id,$this->data,['user' => $usersName,'password' => $PasswordVirtualMachine,'port' => $port],$this->serviceVCenter);
|
|
dispatch($jobs_CheckIP)->delay(now()->addSeconds(5));
|
|
|
|
}
|
|
}
|