Implement VM provision locking mechanism and enhance VM job handling
This commit is contained in:
@@ -6,7 +6,6 @@ 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;
|
||||
@@ -29,9 +28,8 @@ class VMJobs implements ShouldQueue
|
||||
protected $update = true;
|
||||
|
||||
|
||||
public function __construct($product = 0, $action = '',$update = true)
|
||||
public function __construct($product = 0, $action = '', $update = true)
|
||||
{
|
||||
|
||||
$this->data = $product;
|
||||
$this->push = new PusherNotification;
|
||||
$this->telegram = new Telegram;
|
||||
@@ -42,137 +40,111 @@ class VMJobs implements ShouldQueue
|
||||
|
||||
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;
|
||||
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;
|
||||
$jobs = null;
|
||||
switch ($this->action)
|
||||
{
|
||||
|
||||
switch ($this->action) {
|
||||
case "Suspend":
|
||||
$VirtualMachineStatus = $serviceVCenter->SuspendVirtualMachine($product->vmware_id);
|
||||
if ($VirtualMachineStatus === "Login_Again")
|
||||
{
|
||||
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]);
|
||||
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")
|
||||
{
|
||||
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]);
|
||||
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")
|
||||
{
|
||||
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]);
|
||||
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)
|
||||
{
|
||||
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")
|
||||
{
|
||||
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]);
|
||||
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)
|
||||
{
|
||||
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")
|
||||
{
|
||||
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]);
|
||||
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)
|
||||
{
|
||||
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 && $VirtualMachineStatus['error'])
|
||||
{
|
||||
if ($VirtualMachineStatus && $VirtualMachineStatus['error']) {
|
||||
throw new \Exception(json_encode($VirtualMachineStatus['data']));
|
||||
}
|
||||
|
||||
if ($this->update)
|
||||
{
|
||||
$product->update(
|
||||
['status' => $status]
|
||||
);
|
||||
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));
|
||||
|
||||
Event(new NotificationStatus("Notification: " . $exception->getMessage(), false));
|
||||
$this->telegram->SendErrorMessage($exception->getMessage());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user