180 lines
7.2 KiB
PHP
180 lines
7.2 KiB
PHP
<?php
|
|
|
|
namespace App\Jobs\VirtualMachine\Action;
|
|
|
|
use App\Events\NotificationStatus;
|
|
use App\Http\Services\PusherNotification;
|
|
use App\Http\Services\Telegram;
|
|
use App\Http\Services\vCenter as ServiceVCenter;
|
|
use App\Jobs\VirtualMachine\Handle\WaitStatusVirtualMachine;
|
|
use App\Models\ModuleVmware;
|
|
use App\Models\vCenter;
|
|
use Illuminate\Bus\Queueable;
|
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
|
use Illuminate\Foundation\Bus\Dispatchable;
|
|
use Illuminate\Queue\InteractsWithQueue;
|
|
use Illuminate\Queue\SerializesModels;
|
|
use Throwable;
|
|
|
|
class VMJobs implements ShouldQueue
|
|
{
|
|
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
|
|
|
public int $tries = 1;
|
|
public int $timeout = 0;
|
|
protected int $data;
|
|
protected $push = null;
|
|
protected $telegram = null;
|
|
protected $action = null;
|
|
protected $update = true;
|
|
|
|
|
|
public function __construct($product = 0, $action = '',$update = true)
|
|
{
|
|
|
|
$this->data = $product;
|
|
$this->push = new PusherNotification;
|
|
$this->telegram = new Telegram;
|
|
$this->action = $action;
|
|
$this->update = $update;
|
|
}
|
|
|
|
|
|
public function handle(ServiceVCenter $serviceVCenter)
|
|
{
|
|
|
|
$product = ModuleVmware::with([
|
|
'Customer',
|
|
'IpAddress.Host.Datacenter.vCenter'
|
|
])->find($this->data);
|
|
|
|
$serviceVCenter->token = $product->IpAddress->Host->Datacenter->vCenter->token;
|
|
$serviceVCenter->apiUri = $product->IpAddress->Host->Datacenter->vCenter->host;
|
|
$VirtualMachineStatus = null;
|
|
$status = 1;
|
|
$jobs = null;
|
|
switch ($this->action)
|
|
{
|
|
case "Suspend":
|
|
$VirtualMachineStatus = $serviceVCenter->SuspendVirtualMachine($product->vmware_id);
|
|
if ($VirtualMachineStatus === "Login_Again")
|
|
{
|
|
$token = $serviceVCenter->loginServer($product->IpAddress->Host->Datacenter->vCenter);
|
|
if ($token)
|
|
{
|
|
vCenter::where('id',$product->IpAddress->Host->Datacenter->vCenter->id)->update(['token' => $token]);
|
|
$serviceVCenter->token = $token;
|
|
}
|
|
$VirtualMachineStatus = $serviceVCenter->SuspendVirtualMachine($product->vmware_id);
|
|
}
|
|
$status = 2;
|
|
$jobs = new WaitStatusVirtualMachine($product->vmware_id,$serviceVCenter,$product->customer_id,true);
|
|
break;
|
|
case "Reboot":
|
|
$VirtualMachineStatus = $serviceVCenter->RebootVirtualMachine($product->vmware_id);
|
|
|
|
if ($VirtualMachineStatus === "Login_Again")
|
|
{
|
|
$token = $serviceVCenter->loginServer($product->IpAddress->Host->Datacenter->vCenter);
|
|
if ($token)
|
|
{
|
|
vCenter::where('id',$product->IpAddress->Host->Datacenter->vCenter->id)->update(['token' => $token]);
|
|
$serviceVCenter->token = $token;
|
|
}
|
|
$VirtualMachineStatus = $serviceVCenter->RebootVirtualMachine($product->vmware_id);
|
|
}
|
|
$jobs = new WaitStatusVirtualMachine($product->vmware_id,$serviceVCenter,$product->customer_id,false);
|
|
break;
|
|
case "Start":
|
|
$VirtualMachineStatus = $serviceVCenter->CheckVirtualMachine($product->vmware_id);
|
|
|
|
if ($VirtualMachineStatus === "Login_Again")
|
|
{
|
|
$token = $serviceVCenter->loginServer($product->IpAddress->Host->Datacenter->vCenter);
|
|
if ($token)
|
|
{
|
|
vCenter::where('id',$product->IpAddress->Host->Datacenter->vCenter->id)->update(['token' => $token]);
|
|
$serviceVCenter->token = $token;
|
|
}
|
|
$VirtualMachineStatus = $serviceVCenter->CheckVirtualMachine($product->vmware_id);
|
|
}
|
|
if ($VirtualMachineStatus['data']['operations_ready'] === true)
|
|
{
|
|
break;
|
|
}
|
|
$VirtualMachineStatus = $serviceVCenter->StartVirtualMachine($product->vmware_id);
|
|
$jobs = new WaitStatusVirtualMachine($product->vmware_id,$serviceVCenter,$product->customer_id,false);
|
|
break;
|
|
case "ShutdownGuest":
|
|
$VirtualMachineStatus = $serviceVCenter->CheckVirtualMachine($product->vmware_id);
|
|
|
|
if ($VirtualMachineStatus === "Login_Again")
|
|
{
|
|
$token = $serviceVCenter->loginServer($product->IpAddress->Host->Datacenter->vCenter);
|
|
if ($token)
|
|
{
|
|
vCenter::where('id',$product->IpAddress->Host->Datacenter->vCenter->id)
|
|
->update(['token' => $token]);
|
|
$serviceVCenter->token = $token;
|
|
}
|
|
$VirtualMachineStatus = $serviceVCenter->CheckVirtualMachine($product->vmware_id);
|
|
}
|
|
if ($VirtualMachineStatus['data']['operations_ready'] === false)
|
|
{
|
|
$status = 0;
|
|
break;
|
|
}
|
|
$VirtualMachineStatus = $serviceVCenter->ShutdownGuestVirtualMachine($product->vmware_id);
|
|
|
|
$jobs = new WaitStatusVirtualMachine($product->vmware_id,$serviceVCenter,$product->customer_id,true);
|
|
|
|
$status = 0;
|
|
break;
|
|
case "PowerOff":
|
|
$VirtualMachineStatus = $serviceVCenter->CheckVirtualMachine($product->vmware_id);
|
|
|
|
if ($VirtualMachineStatus === "Login_Again")
|
|
{
|
|
$token = $serviceVCenter->loginServer($product->IpAddress->Host->Datacenter->vCenter);
|
|
if ($token)
|
|
{
|
|
vCenter::where('id',$product->IpAddress->Host->Datacenter->vCenter->id)->update(['token' => $token]);
|
|
$serviceVCenter->token = $token;
|
|
}
|
|
$VirtualMachineStatus = $serviceVCenter->CheckVirtualMachine($product->vmware_id);
|
|
}
|
|
if ($VirtualMachineStatus['data']['operations_ready'] === false)
|
|
{
|
|
$status = 0;
|
|
break;
|
|
}
|
|
$VirtualMachineStatus = $serviceVCenter->PowerOffVirtualMachine($product->vmware_id);
|
|
$jobs = new WaitStatusVirtualMachine($product->vmware_id,$serviceVCenter,$product->customer_id,true);
|
|
$status = 0;
|
|
break;
|
|
}
|
|
|
|
if ($VirtualMachineStatus['error'])
|
|
{
|
|
$this->response = 0;
|
|
throw new \Exception(json_encode($VirtualMachineStatus['data']));
|
|
}
|
|
|
|
if ($this->update)
|
|
{
|
|
$product->update(
|
|
['status' => $status]
|
|
);
|
|
}
|
|
dispatch($jobs)->delay(now()->addSeconds(5));
|
|
|
|
}
|
|
public function failed(Throwable $exception)
|
|
{
|
|
Event(new NotificationStatus("Notification: " . $exception->getMessage(),false));
|
|
|
|
$this->telegram->SendErrorMessage($exception->getMessage());
|
|
}
|
|
|
|
}
|