109 lines
3.1 KiB
PHP
109 lines
3.1 KiB
PHP
<?php
|
|
|
|
namespace App\Jobs\VirtualMachine\Action;
|
|
|
|
use App\Http\Services\PusherNotification;
|
|
use App\Http\Services\Telegram;
|
|
use App\Http\Services\vCenter as ServiceVCenter;
|
|
use App\Jobs\VirtualMachine\Create\CreateVMJobs;
|
|
use App\Models\ModuleVmware;
|
|
use Exception;
|
|
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 Throwable;
|
|
|
|
class RemoveVirtualMachine implements ShouldQueue
|
|
{
|
|
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
|
|
|
public int $tries = 1;
|
|
public int $timeout = 0;
|
|
protected $data = null;
|
|
protected $push = null;
|
|
protected $telegram = null;
|
|
|
|
|
|
public function __construct($product = 0)
|
|
{
|
|
|
|
$this->data = $product;
|
|
$this->push = new PusherNotification;
|
|
$this->telegram = new Telegram;
|
|
}
|
|
|
|
|
|
/**
|
|
* Execute the job.
|
|
*
|
|
* @return void
|
|
*/
|
|
public function handle(ServiceVCenter $serviceVCenter)
|
|
{
|
|
$product = ModuleVmware::with([
|
|
'IpAddress.Host.Datacenter.vCenter'
|
|
])->find($this->data);
|
|
|
|
$serviceVCenter->token = $product->IpAddress->Host->Datacenter->vCenter->token;
|
|
$serviceVCenter->apiUri = $product->IpAddress->Host->Datacenter->vCenter->host;
|
|
|
|
|
|
$VirtualMachine = $serviceVCenter->CheckVirtualMachine($product->vmware_id);
|
|
|
|
if ($VirtualMachine === "Login_Again") {
|
|
$token = $serviceVCenter->loginServer($product->IpAddress->Host->Datacenter->vCenter);
|
|
|
|
if ($token) {
|
|
$product->IpAddress->Host->Datacenter->vCenter->update(['token' => $token]);
|
|
$serviceVCenter->token = $token;
|
|
}
|
|
$VirtualMachine = $serviceVCenter->CheckVirtualMachine($product->vmware_id);
|
|
}
|
|
|
|
if ($VirtualMachine['error']) {
|
|
throw new Exception(json_encode($VirtualMachine['message']));
|
|
}
|
|
|
|
if ($VirtualMachine['data']['operations_ready'] === true) {
|
|
|
|
$Status = $serviceVCenter->ShutdownGuestVirtualMachine($product->vmware_id);
|
|
|
|
sleep(20);
|
|
|
|
$VirtualMachine = $serviceVCenter->CheckVirtualMachine($product->vmware_id);
|
|
}
|
|
|
|
if ($VirtualMachine['data']['operations_ready'] === true) {
|
|
|
|
do {
|
|
sleep(10);
|
|
|
|
$VirtualMachine = $serviceVCenter->CheckVirtualMachine($product->vmware_id);
|
|
|
|
Log::channel('Custom')->info(json_encode($VirtualMachine));
|
|
}
|
|
while ($VirtualMachine['data']['operations_ready'] !== false);
|
|
|
|
}
|
|
|
|
$StatusVirtualMachine = $serviceVCenter->DeleteVirtualMachine($product->vmware_id);
|
|
|
|
if ($StatusVirtualMachine['error']) {
|
|
throw new Exception(json_encode($Status['data']));
|
|
}
|
|
|
|
$jobs = new CreateVMJobs($product->id);
|
|
|
|
dispatch($jobs)->delay(now()->addSeconds(5));
|
|
|
|
}
|
|
|
|
public function failed(Throwable $exception)
|
|
{
|
|
$this->telegram->SendErrorMessage($exception->getMessage());
|
|
}
|
|
}
|