116 lines
3.5 KiB
PHP
116 lines
3.5 KiB
PHP
<?php
|
|
|
|
namespace App\Jobs\VirtualMachine\Create;
|
|
|
|
|
|
use App\Http\Services\Telegram;
|
|
use App\Http\Services\vCenter as ServiceVCenter;
|
|
use App\Models\ModuleVmware;
|
|
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;
|
|
use Illuminate\Support\Facades\Log;
|
|
use Illuminate\Support\Str;
|
|
use Throwable;
|
|
|
|
class CreateVMJobs implements ShouldQueue
|
|
{
|
|
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
|
|
|
protected $data = null;
|
|
protected $push = null;
|
|
protected $telegram = null;
|
|
public int $tries = 1;
|
|
public $message = "";
|
|
public $timeout = 0;
|
|
|
|
/**
|
|
* Create a new job instance.
|
|
*
|
|
* @return void
|
|
*/
|
|
public function __construct($product = 0)
|
|
{
|
|
$this->data = $product;
|
|
$this->telegram = new Telegram;
|
|
}
|
|
|
|
|
|
public function handle(ServiceVCenter $serviceVCenter)
|
|
{
|
|
|
|
$product = ModuleVmware::with([
|
|
'Order.OrderItem',
|
|
'Customer',
|
|
'IpAddress.Network',
|
|
'IpAddress.Host.OneDatastore',
|
|
'IpAddress.Host.Datacenter.vCenter','Os.OsGroup'
|
|
])->find($this->data);
|
|
|
|
$serviceVCenter->token = $product->IpAddress->Host->Datacenter->vCenter->token;
|
|
$serviceVCenter->apiUri = $product->IpAddress->Host->Datacenter->vCenter->host;
|
|
|
|
$CheckToken = $serviceVCenter->checkApiExits();
|
|
|
|
if (! $CheckToken)
|
|
{
|
|
$token = $serviceVCenter->loginServer($product->IpAddress->Host->Datacenter->vCenter);
|
|
|
|
if ($token)
|
|
{
|
|
$product->IpAddress->Host->Datacenter->vCenter->update(['token' => $token]);
|
|
$serviceVCenter->token = $token;
|
|
}
|
|
}
|
|
|
|
$name = $product->IpAddress->ip . '_' . str_replace('-','_',Str::slug($product->Customer->firstname));
|
|
|
|
$jobs_check = new GetVirtualMachine($name,$product,$serviceVCenter);
|
|
|
|
$vmware_id = $jobs_check->handle();
|
|
|
|
if ($vmware_id === false)
|
|
{
|
|
Log::channel('detail')->info("Bắt đầu tạo: " . time());
|
|
$vmware_id = $serviceVCenter->CreateVirtualMachine($product);
|
|
Log::channel('detail')->info("Kết thúc: " . time());
|
|
if (isset($vmware_id['error']) && $vmware_id['error'] === false) {
|
|
$vmware_id = $vmware_id['data'];
|
|
}
|
|
}
|
|
|
|
if ($vmware_id && is_string($vmware_id)) {
|
|
|
|
$product->update(['vmware_id' => $vmware_id]);
|
|
|
|
$jobs_GetStatus = new CheckStatusVirtualMachine($vmware_id,$product,$serviceVCenter);
|
|
|
|
dispatch($jobs_GetStatus)->delay(now()->addSeconds(30));
|
|
|
|
} else {
|
|
|
|
$this->message = "Notification: " . json_encode($vmware_id);
|
|
|
|
throw new \Exception(json_encode($vmware_id));
|
|
}
|
|
|
|
}
|
|
public function failed(Throwable $exception): void
|
|
{
|
|
Log::channel('Custom')->info("\r\n");
|
|
Log::channel('Custom')->info("=================================");
|
|
Log::channel('Custom')->info("Thông báo lỗi lúc: " .Carbon::now());
|
|
Log::channel('Custom')->info("Notification Create VirtualMachine: ");
|
|
Log::channel('Custom')->info("========");
|
|
Log::channel('Custom')->info($exception->getMessage());
|
|
Log::channel('Custom')->info("=================================");
|
|
|
|
$this->telegram->SendErrorMessage($exception->getMessage());
|
|
|
|
}
|
|
|
|
}
|