Files

151 lines
6.3 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\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 = 600;
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);
if (!$product || !$product->IpAddress?->Host?->Datacenter?->vCenter) {
throw new \Exception("VMJobs: product {$this->data} missing vCenter relationship");
}
$vcenter = $product->IpAddress->Host->Datacenter->vCenter;
$serviceVCenter->setConnection($vcenter->host, $vcenter->token);
$VirtualMachineStatus = null;
$status = 1;
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;
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);
}
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);
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);
$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);
$status = 0;
break;
}
if ($VirtualMachineStatus && $VirtualMachineStatus['error']) {
throw new \Exception(json_encode($VirtualMachineStatus['data']));
}
if ($this->update) {
$product->update(['status' => $status]);
}
}
public function failed(Throwable $exception)
{
Event(new NotificationStatus("Notification: " . $exception->getMessage(), false));
$this->telegram->SendErrorMessage($exception->getMessage());
}
}