160 lines
5.2 KiB
PHP
160 lines
5.2 KiB
PHP
<?php
|
|
|
|
namespace App\Jobs\VirtualMachine\Sync;
|
|
|
|
use App\Http\Services\Telegram;
|
|
use App\Http\Services\vCenter as ServiceVCenter;
|
|
use App\Models\ModuleDatacenter;
|
|
use App\Models\ModuleDatastore;
|
|
use App\Models\ModuleHost;
|
|
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 Illuminate\Support\Facades\Log;
|
|
|
|
class SyncDatastore implements ShouldQueue
|
|
{
|
|
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
|
|
|
protected $vCenter = null;
|
|
protected $data = null;
|
|
protected Telegram $telegram;
|
|
|
|
/**
|
|
* Create a new job instance.
|
|
*
|
|
* @return void
|
|
*/
|
|
public function __construct()
|
|
{
|
|
$this->data = vCenter::select('id', 'host', 'user', 'password', 'token')->get();
|
|
$this->vCenter = new ServiceVCenter;
|
|
$this->telegram = new Telegram();
|
|
}
|
|
|
|
|
|
private function jobLog($message, $context = [])
|
|
{
|
|
Log::info('[SyncDatastore] ' . $message, $context);
|
|
}
|
|
/**
|
|
* Execute the job.
|
|
*
|
|
* @return void
|
|
*/
|
|
public function handle()
|
|
{
|
|
try {
|
|
$this->jobLog('JOB START');
|
|
|
|
// Initialize services
|
|
$client = new \App\Http\Services\vCenter\Client();
|
|
$resourceService = new \App\Http\Services\vCenter\ResourceService($client);
|
|
|
|
foreach ($this->data as $item) {
|
|
|
|
$this->jobLog('Processing vCenter', [
|
|
'vcenter_id' => $item->id,
|
|
'host' => $item->host
|
|
]);
|
|
|
|
// Login to vCenter
|
|
// loginServer returns token string on success, false on failure.
|
|
// It also updates the model instance passed to it.
|
|
$token = $client->loginServer($item);
|
|
|
|
if (!$token) {
|
|
$this->jobLog('Login failed');
|
|
continue;
|
|
}
|
|
|
|
$this->jobLog('Login success');
|
|
|
|
// ================= DATACENTER =================
|
|
$dataCenter = ModuleDatacenter::where('vcenter_id', $item->id)
|
|
->select('id', 'datacenter_id')
|
|
->get();
|
|
|
|
$this->jobLog('Datacenter count', [
|
|
'count' => $dataCenter->count()
|
|
]);
|
|
|
|
foreach ($dataCenter as $_item) {
|
|
|
|
$this->jobLog('Processing Datacenter', [
|
|
'module_datacenter_id' => $_item->id
|
|
]);
|
|
|
|
$allHost = ModuleHost::where('module_datacenter_id', $_item->id)
|
|
->select('id', 'name', 'host_id')
|
|
->get();
|
|
|
|
foreach ($allHost as $hostItem) {
|
|
|
|
$this->jobLog('Processing Host', [
|
|
'host_id' => $hostItem->id,
|
|
'host_name' => $hostItem->name,
|
|
'vcenter_host_id' => $hostItem->host_id
|
|
]);
|
|
|
|
// Use ResourceService to get datastores
|
|
$datastore = $resourceService->loadDatastore($hostItem->host_id);
|
|
|
|
if ($datastore === "Login_Again") {
|
|
$this->jobLog('Token expired → relogin');
|
|
$token = $client->loginServer($item);
|
|
|
|
if ($token) {
|
|
// Retry once
|
|
$datastore = $resourceService->loadDatastore($hostItem->host_id);
|
|
} else {
|
|
$this->jobLog('Relogin failed');
|
|
continue;
|
|
}
|
|
}
|
|
|
|
if (!$datastore || !is_array($datastore)) {
|
|
$this->jobLog('No valid datastore returned', ['response' => $datastore]);
|
|
continue;
|
|
}
|
|
|
|
$this->jobLog('Datastore count from host', [
|
|
'count' => count($datastore)
|
|
]);
|
|
|
|
foreach ($datastore as $_subItem) {
|
|
ModuleDatastore::updateOrCreate(
|
|
[
|
|
'datastore' => $_subItem['datastore'],
|
|
'module_host_id' => $hostItem->id,
|
|
],
|
|
[
|
|
'name' => $_subItem['name'],
|
|
'type' => $_subItem['type'],
|
|
'space' => floor($_subItem['free_space'] / 1073741824),
|
|
]
|
|
);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
$this->jobLog('JOB END SUCCESS');
|
|
|
|
} catch (\Throwable $exception) {
|
|
|
|
Log::error('SyncDatastore FAILED', [
|
|
'message' => $exception->getMessage(),
|
|
'file' => $exception->getFile(),
|
|
'line' => $exception->getLine()
|
|
]);
|
|
|
|
throw $exception;
|
|
}
|
|
}
|
|
|
|
}
|