feat: Implement comprehensive vCenter integration with new services for VM configuration, power management, templates, snapshots, and resource management, alongside infrastructure management and synchronization job updates.
This commit is contained in:
@@ -35,6 +35,11 @@ class SyncDatastore implements ShouldQueue
|
||||
$this->telegram = new Telegram();
|
||||
}
|
||||
|
||||
|
||||
private function jobLog($message, $context = [])
|
||||
{
|
||||
Log::info('[SyncDatastore] ' . $message, $context);
|
||||
}
|
||||
/**
|
||||
* Execute the job.
|
||||
*
|
||||
@@ -43,97 +48,112 @@ class SyncDatastore implements ShouldQueue
|
||||
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->vCenter->apiUri = $item->host;
|
||||
$this->jobLog('Processing vCenter', [
|
||||
'vcenter_id' => $item->id,
|
||||
'host' => $item->host
|
||||
]);
|
||||
|
||||
if (!$item->token) {
|
||||
$token = $this->vCenter->loginServer($item);
|
||||
if ($token) {
|
||||
vCenter::where('id', $item->id)->update(['token' => $token]);
|
||||
|
||||
$this->vCenter->token = $token;
|
||||
}
|
||||
}
|
||||
$this->vCenter->token = $item->token;
|
||||
|
||||
if (!$this->vCenter->token) {
|
||||
// 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;
|
||||
}
|
||||
|
||||
$dataCenter = ModuleDatacenter::where('vcenter_id', $item->id)->select('id', 'datacenter_id')->get();
|
||||
$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) {
|
||||
$allHost = ModuleHost::where('module_datacenter_id', $_item->id)->select('id', 'name')->get();
|
||||
if ($allHost) {
|
||||
foreach ($allHost as $hostItem) {
|
||||
$datastore = $this->vCenter->loadDatastore($item, $hostItem->name);
|
||||
|
||||
if ($datastore === "Login_Again") {
|
||||
$token = $this->vCenter->loginServer($item);
|
||||
if ($token) {
|
||||
vCenter::where('id', $item->id)->update(['token' => $token]);
|
||||
$this->vCenter->token = $token;
|
||||
}
|
||||
$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) {
|
||||
}
|
||||
|
||||
foreach ($datastore as $_subItem) {
|
||||
if (!$datastore || !is_array($datastore)) {
|
||||
$this->jobLog('No valid datastore returned', ['response' => $datastore]);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!ModuleDatastore::where('datastore', $_subItem['datastore'])->first()) {
|
||||
$this->jobLog('Datastore count from host', [
|
||||
'count' => count($datastore)
|
||||
]);
|
||||
|
||||
$template = $this->vCenter->getAllDataStore();
|
||||
|
||||
if ($template) {
|
||||
ModuleDatastore::create([
|
||||
'name' => $_subItem['name'],
|
||||
'datastore' => $_subItem['datastore'],
|
||||
'type' => $_subItem['type'],
|
||||
'space' => floor($_subItem['free_space'] / 1073741824),
|
||||
'module_host_id' => $hostItem->id
|
||||
]);
|
||||
ModuleHost::where('id', $hostItem->id)->update(['template' => $template]);
|
||||
} else {
|
||||
ModuleDatastore::create([
|
||||
'name' => $_subItem['name'],
|
||||
'datastore' => $_subItem['datastore'],
|
||||
'type' => $_subItem['type'],
|
||||
'space' => floor($_subItem['free_space'] / 1073741824),
|
||||
'module_host_id' => $hostItem->id
|
||||
]);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
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),
|
||||
]
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// Notify success via Telegram
|
||||
$this->telegram->SendJobRunInfo('SyncDatastore', 'Success', [
|
||||
'message' => 'Sync datastore completed successfully',
|
||||
'synced_vcenters' => $this->data->count(),
|
||||
]);
|
||||
|
||||
$this->jobLog('JOB END SUCCESS');
|
||||
|
||||
} catch (\Throwable $exception) {
|
||||
// Ghi log Laravel
|
||||
Log::error('SyncDatastore Job failed', [
|
||||
'error' => $exception->getMessage(),
|
||||
'trace' => $exception->getTraceAsString(),
|
||||
]);
|
||||
|
||||
// Gửi log lỗi qua Telegram
|
||||
$this->telegram->SendJobRunInfo('SyncDatastore', 'Failed', [
|
||||
'error' => $exception->getMessage(),
|
||||
Log::error('SyncDatastore FAILED', [
|
||||
'message' => $exception->getMessage(),
|
||||
'file' => $exception->getFile(),
|
||||
'line' => $exception->getLine(),
|
||||
'line' => $exception->getLine()
|
||||
]);
|
||||
|
||||
// Quăng lại lỗi để Laravel xử lý retry/failure
|
||||
throw $exception;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user