164 lines
5.2 KiB
PHP
164 lines
5.2 KiB
PHP
<?php
|
|
|
|
namespace App\Jobs\VirtualMachine\Upgrade;
|
|
|
|
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\Action\VMJobs;
|
|
use App\Models\InvoiceItem;
|
|
use App\Models\ModuleVmware;
|
|
use Illuminate\Bus\Batchable;
|
|
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 UpgradeVirtualMachine implements ShouldQueue
|
|
{
|
|
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels, Batchable;
|
|
|
|
protected int $order;
|
|
protected int $product;
|
|
protected array $addon;
|
|
protected $push;
|
|
protected $telegram;
|
|
/**
|
|
* Create a new job instance.
|
|
*
|
|
* @return void
|
|
*/
|
|
public function __construct($product = 0, $addon = [],$order = 0)
|
|
{
|
|
$this->addon = $addon;
|
|
$this->product = $product;
|
|
$this->push = new PusherNotification;
|
|
$this->telegram = new Telegram;
|
|
$this->order = $order;
|
|
}
|
|
|
|
/**
|
|
* Execute the job.
|
|
*
|
|
* @return void
|
|
*/
|
|
public function handle(ServiceVCenter $serviceVCenter)
|
|
{
|
|
$product = ModuleVmware::with([
|
|
'IpAddress.Host.Datacenter.vCenter'
|
|
])->find($this->product);
|
|
|
|
$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['data']['operations_ready'] === true)
|
|
{
|
|
$StatusVirtualMachine = $serviceVCenter->PowerOffVirtualMachine($product->vmware_id);
|
|
|
|
if ($StatusVirtualMachine['error'])
|
|
{
|
|
throw new \Exception(json_encode($StatusVirtualMachine['data']));
|
|
}
|
|
|
|
$VirtualMachine = $serviceVCenter->CheckVirtualMachine($product->vmware_id);
|
|
|
|
if ($VirtualMachine['error'] === true) {
|
|
throw new \Exception(json_encode($VirtualMachine['message']));
|
|
}
|
|
if ($VirtualMachine['data']['operations_ready'])
|
|
{
|
|
do {
|
|
sleep(10);
|
|
$VirtualMachine = $serviceVCenter->CheckVirtualMachine($product->vmware_id);
|
|
}
|
|
while ($VirtualMachine['data']['operations_ready'] === false);
|
|
}
|
|
}
|
|
|
|
$DetailVirtualMachine = $serviceVCenter->GetDetailVirtualMachine($product->vmware_id);
|
|
|
|
|
|
if ($DetailVirtualMachine['error'])
|
|
{
|
|
throw new \Exception(json_encode($DetailVirtualMachine['message']));
|
|
}
|
|
|
|
$ResourceConfig = [];
|
|
|
|
foreach ($this->addon as $key => $item)
|
|
{
|
|
switch ($key)
|
|
{
|
|
case "cpu":
|
|
$ResourceConfig[$key] = $item + $DetailVirtualMachine['data']['cpu']['count'];
|
|
break;
|
|
case "ram":
|
|
$ResourceConfig[$key] = $item + ($DetailVirtualMachine['data']['memory']['size_MiB']/1024);
|
|
break;
|
|
case "ssd":
|
|
$ResourceConfig[$key] = $item + ($DetailVirtualMachine['data']['disks'][0]['value']['capacity']/1073741824);
|
|
break;
|
|
}
|
|
}
|
|
$jobs = [];
|
|
|
|
foreach ($ResourceConfig as $key => $item)
|
|
{
|
|
switch (strtolower($key))
|
|
{
|
|
case "cpu":
|
|
UpgradeCPU::dispatchSync($product->id,$item);
|
|
|
|
break;
|
|
case "ram":
|
|
UpgradeMemory::dispatchSync($product->id,$item);
|
|
|
|
break;
|
|
case "ssd":
|
|
UpgradeDisk::dispatchSync($product->id,$item);
|
|
break;
|
|
}
|
|
}
|
|
|
|
$VirtualMachine = $serviceVCenter->CheckVirtualMachine($product->vmware_id);
|
|
|
|
if ($VirtualMachine['data']['operations_ready'] === false)
|
|
{
|
|
VMJobs::dispatchSync($product->id,"Start");
|
|
|
|
}
|
|
$order = $this->order;
|
|
|
|
$SumAddon = InvoiceItem::whereHas("OrderItem",function ($query) use ($order) {
|
|
$query->where('id',$order);
|
|
})->sum('amount');
|
|
|
|
$product->increment('first_payment',$SumAddon);
|
|
$product->increment('next_payment',$SumAddon);
|
|
|
|
Event(new NotificationStatus("Nâng cấp cấu hình thành công",true));
|
|
}
|
|
|
|
public function failed(Throwable $exception)
|
|
{
|
|
Event(new NotificationStatus("Notification: $exception->getMessages()",false));
|
|
|
|
$this->telegram->SendErrorMessage($exception->getMessage());
|
|
}
|
|
}
|