Assign discovered host VMs to administrator
parent
52294f7c3f
commit
0c7c8a7022
@ -0,0 +1,96 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\Cloud;
|
||||
|
||||
use App\Models\CloudHost;
|
||||
use App\Models\CloudInstance;
|
||||
|
||||
final class CloudInstanceDiscovery
|
||||
{
|
||||
public static function knownOwners(int $hostId): array
|
||||
{
|
||||
$owners = [];
|
||||
foreach (CloudInstance::where('host_id', $hostId)->get(['uuid', 'user_id']) as $instance) {
|
||||
$owners[(string) $instance->uuid] = (int) $instance->user_id;
|
||||
}
|
||||
return $owners;
|
||||
}
|
||||
|
||||
public static function synchronize(CloudHost $host, array $inventory): array
|
||||
{
|
||||
$items = $inventory['items'] ?? null;
|
||||
if (!is_array($items) || ($items !== [] && !array_is_list($items))) {
|
||||
throw new CloudError('宿主机虚拟机清单格式无效', 502);
|
||||
}
|
||||
$imported = 0;
|
||||
$existing = 0;
|
||||
$conflicts = 0;
|
||||
foreach ($items as $vm) {
|
||||
if (!is_array($vm)) {
|
||||
throw new CloudError('宿主机虚拟机记录格式无效', 502);
|
||||
}
|
||||
$uuid = strtolower((string) ($vm['uuid'] ?? ''));
|
||||
$ownerId = filter_var($vm['owner_user_id'] ?? null, FILTER_VALIDATE_INT);
|
||||
if (preg_match('/^[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$/D', $uuid) !== 1
|
||||
|| $ownerId === false || $ownerId < 1) {
|
||||
throw new CloudError('宿主机虚拟机归属记录无效', 502);
|
||||
}
|
||||
$current = CloudInstance::where('host_id', $host->id)->where('uuid', $uuid)->first();
|
||||
if ($current !== null) {
|
||||
$existing++;
|
||||
if ((int) $current->user_id !== $ownerId) {
|
||||
$conflicts++;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
$name = is_string($vm['name'] ?? null) ? trim($vm['name']) : '';
|
||||
$name = preg_replace('/[\x00-\x1F\x7F]/u', '', $name) ?? '';
|
||||
$name = mb_substr($name !== '' ? $name : 'vm-' . substr($uuid, 0, 8), 0, 63);
|
||||
$spec = ['record_source' => 'discovered'];
|
||||
foreach (['vcpu', 'memory_mib', 'disks', 'autostart', 'managed', 'public_ip_cidr',
|
||||
'os_name', 'os_source', 'install_iso'] as $field) {
|
||||
if (array_key_exists($field, $vm)) {
|
||||
$spec[$field] = $vm[$field];
|
||||
}
|
||||
}
|
||||
|
||||
$instance = new CloudInstance();
|
||||
$instance->uuid = $uuid;
|
||||
$instance->host_id = $host->id;
|
||||
$instance->user_id = $ownerId;
|
||||
$instance->order_id = null;
|
||||
$instance->name = $name;
|
||||
$instance->status = 'active';
|
||||
$instance->spec_json = json_encode(
|
||||
CloudInput::safeResult($spec),
|
||||
JSON_THROW_ON_ERROR | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE
|
||||
);
|
||||
$instance->credential_cipher = null;
|
||||
$instance->created_at = $instance->updated_at = time();
|
||||
try {
|
||||
$instance->save();
|
||||
$imported++;
|
||||
} catch (\Throwable $error) {
|
||||
// A concurrent retry may have inserted the same stable
|
||||
// host/UUID pair. Preserve its owner and never overwrite it.
|
||||
$current = CloudInstance::where('host_id', $host->id)
|
||||
->where('uuid', $uuid)->first();
|
||||
if ($current === null) {
|
||||
throw $error;
|
||||
}
|
||||
$existing++;
|
||||
if ((int) $current->user_id !== $ownerId) {
|
||||
$conflicts++;
|
||||
}
|
||||
}
|
||||
}
|
||||
return [
|
||||
'imported' => $imported,
|
||||
'existing' => $existing,
|
||||
'ownership_conflicts' => $conflicts,
|
||||
'engine_claimed' => (int) ($inventory['claimed_count'] ?? 0),
|
||||
'untracked_managed' => (int) ($inventory['untracked_managed_count'] ?? 0),
|
||||
];
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue