Assign discovered host VMs to administrator

dev
admin 2 months ago
parent 52294f7c3f
commit 0c7c8a7022

@ -142,9 +142,23 @@ location = /engine-console {
## 7. 页面配置与验收
1. 管理员添加宿主机:现有 SSH/root/私钥口令、核验指纹,设置区域、网桥、目录、地址池/网关/DNS/网卡前缀,测试连接。
2. 导入云镜像与救援模板;无已有模板不能开通云镜像 VM。当前云镜像下载沿用 AlmaLinux 9/10 官方源校验规则,预存其他系统图标不代表已实现其云模板导入;其他系统可用合适 ISO 安装,云镜像适配后续扩展。
2. 导入云镜像与救援模板;无已有模板不能开通云镜像 VM。云镜像 SHA-256 可留空;留空时仍会在下载后计算并保存实际摘要,填写时则会严格比对。预存其他系统图标不代表已实现其云模板导入;其他系统可用合适 ISO 安装,云镜像适配后续扩展。
3. 套餐设置规格、价格、库存、已启用宿主机。本版价格是一次开通金额,不是自动月费。
4. 测试用户注册/充值/购买,验证任务、VM 控制台、SSH、重装、救援、删除与图表。
5. 日志库 `engine_vm_metric_samples` 应在所有用户退出后继续新增;首批图表需等待采样间隔。
先不要启用旧 `web/crontab`,付款扫描/备份任务按保留业务单独核对。真实 MariaDB/KVM/SMTP/支付未在开发环境联调,先验收测试环境。`uncertain` 状态先核对宿主机,不直接改库退款或重复毁盘任务。
## 8. 从旧版更新
拉取新代码后要重新执行幂等的业务库升级,再重启引擎和 worker:
```bash
cd /home/virt-cloud
git pull --ff-only origin venix
cd /home/virt-cloud/web
php xcat cloudInit
systemctl restart virt-cloud-engine.service virt-cloud-worker.service
```
已存在的宿主机需在管理页重新保存一次。面板会扫描宿主机,将没有面板元数据且尚未分配的存量虚拟机导入业务库,并归属给当前超级管理员。重复保存不会复制记录,也不会改写已有归属。

@ -80,6 +80,10 @@ class MemoryUsers:
def owns_vm(self, host_scope, vm_id, user_id):
return self.store.get("test_vm_owner", f"{host_scope}:{vm_id}") == str(user_id)
def owner_of_vm(self, host_scope, vm_id):
value = self.store.get("test_vm_owner", f"{host_scope}:{vm_id}")
return None if value is None else int(value)
def assign_vm(self, host_scope, vm_id, user_id):
self.store.set("test_vm_owner", f"{host_scope}:{vm_id}", str(user_id))

@ -177,6 +177,45 @@ def test_user_admin_super_admin_boundaries_and_cross_user_vm(boundary):
{"action": "reboot"}).status_code == 403
def test_existing_unmanaged_vms_are_claimed_once_for_configuring_admin(boundary):
client, engine, _, _, services = boundary
path = "/internal/v1/hosts/hk"
assert call(client, "GET", path + "/host", ADMIN).status_code == 200
manual_uuid = "22222222-2222-4222-8222-222222222222"
orphaned_managed_uuid = "33333333-3333-4333-8333-333333333333"
services["hk"].other_vms = [
{"uuid": manual_uuid, "name": "manual-vm", "state": "shutoff",
"vcpu": 2, "memory_mib": 2048, "disks": [], "managed": False},
{"uuid": orphaned_managed_uuid, "name": "unknown-panel-vm", "state": "shutoff",
"vcpu": 2, "memory_mib": 2048, "disks": [], "managed": True},
]
body = {"known_owners": {VM_ID: 41}}
assert call(client, "POST", path + "/host/discover-vms", ADMIN, body).status_code == 403
discovered = call(client, "POST", path + "/host/discover-vms", SUPER, body)
assert discovered.status_code == 200, discovered.text
rows = {row["uuid"]: row for row in discovered.json()["items"]}
assert rows[VM_ID]["owner_user_id"] == 41
assert rows[manual_uuid]["owner_user_id"] == SUPER["user_id"]
assert orphaned_managed_uuid not in rows
assert discovered.json()["claimed_count"] == 2
assert discovered.json()["untracked_managed_count"] == 1
component = engine.registry.components["hk"]
scope = host_scope(component.manager.profile)
assert component.state.users.owner_of_vm(scope, manual_uuid) == SUPER["user_id"]
repeated = call(client, "POST", path + "/host/discover-vms",
{**SUPER, "user_id": 2}, body)
assert repeated.status_code == 200
repeated_rows = {row["uuid"]: row for row in repeated.json()["items"]}
assert repeated_rows[manual_uuid]["owner_user_id"] == SUPER["user_id"]
assert repeated.json()["claimed_count"] == 0
assert component.state.users.owner_of_vm(scope, manual_uuid) == SUPER["user_id"]
assert {row["uuid"] for row in call(
client, "GET", path + "/vms", {**USER, "user_id": SUPER["user_id"]}
).json()["items"]} == {manual_uuid}
assert call(client, "GET", path + "/vms", {**USER, "user_id": 2}).json()["items"] == []
def test_stable_host_identity_and_namespaces():
store = MemoryStore()
first, second = HostStore(store, "a"), HostStore(store, "b")

@ -222,6 +222,12 @@ class ImportTemplateBody(BaseModel):
name: str = Field(min_length=1, max_length=64)
class DiscoverVMsBody(BaseModel):
model_config = ConfigDict(extra="forbid")
known_owners: dict[str, int] = Field(default_factory=dict, max_length=10000)
class RenameTemplateBody(BaseModel):
model_config = ConfigDict(extra="forbid")
@ -1426,6 +1432,59 @@ def create_app(
"warnings": [resource_warning] if resource_warning else [],
}
@app.post("/api/v1/host/discover-vms")
def discover_vms(body: DiscoverVMsBody,
_session: Session = Depends(require_admin_write)):
if not database_mode:
raise VMError("虚拟机归属数据库不可用", 409)
known_owners: dict[str, int] = {}
for raw_uuid, owner_id in body.known_owners.items():
try:
canonical_uuid = str(uuid.UUID(raw_uuid))
except (TypeError, ValueError, AttributeError) as exc:
raise VMError("已有虚拟机归属中包含无效 UUID", 422) from exc
if isinstance(owner_id, bool) or owner_id < 1:
raise VMError("已有虚拟机归属中包含无效用户", 422)
known_owners[canonical_uuid] = owner_id
profile = current_profile()
scope = host_scope(profile)
default_owner = account_for_session(_session)["id"]
inventory = []
claimed = 0
untracked_managed = 0
for vm in new_service(profile).list_vms():
vm_uuid = str(uuid.UUID(vm["uuid"]))
owner_id = state.users.owner_of_vm(scope, vm_uuid)
desired_owner = known_owners.get(vm_uuid)
if owner_id is None and desired_owner is None and not vm.get("managed"):
desired_owner = default_owner
if owner_id is None and desired_owner is not None:
try:
state.users.assign_vm(scope, vm_uuid, desired_owner)
owner_id = desired_owner
claimed += 1
except (DatabaseError, UserError):
# Concurrent discovery never changes the winner. Read the
# immutable owner inserted by the other request.
owner_id = state.users.owner_of_vm(scope, vm_uuid)
if owner_id is None:
raise
if owner_id is None:
if vm.get("managed"):
untracked_managed += 1
continue
inventory.append({**vm, "owner_user_id": owner_id})
LOG.info(
"existing VMs discovered: host=%s claimed=%s visible=%s admin=%s",
profile.get("host"), claimed, len(inventory), _session.username,
)
return {
"items": inventory,
"claimed_count": claimed,
"untracked_managed_count": untracked_managed,
}
@app.get("/api/v1/resources")
def resources(_session: Session = Depends(require_approved)):
profile = current_profile()

@ -148,8 +148,11 @@ class ExternalOwnership:
return actor_account(actor_session()) if value["user_id"] == user_id else None
def owns_vm(self, scope: str, vm_id: str, user_id: int) -> bool:
return self.owner_of_vm(scope, vm_id) == user_id
def owner_of_vm(self, scope: str, vm_id: str) -> int | None:
raw = self.store.get("vm_owner", self._key(scope, vm_id))
return raw is not None and json.loads(raw)["user_id"] == user_id
return None if raw is None else int(json.loads(raw)["user_id"])
def assign_vm(self, scope: str, vm_id: str, user_id: int) -> None:
if isinstance(user_id, bool) or not isinstance(user_id, int) or user_id < 1:

@ -41,7 +41,7 @@ READ_PATHS = (
r"(?:reinstall-jobs|rescue-jobs|vm-templates/import-jobs|rescue-templates/import-jobs)/[A-Za-z0-9_-]+",
)
WRITE_PATHS = {
"POST": (r"host/(?:scan|unlock|test)", r"vm-templates/import", r"rescue-templates",
"POST": (r"host/(?:scan|unlock|test|discover-vms)", r"vm-templates/import", r"rescue-templates",
r"vms", rf"vms/{VM_ID}/(?:actions|credential|reinstall|rescue|console-ticket)",
rf"vms/{VM_ID}/rescue/(?:exit|force-start)", r"reinstall-jobs/[A-Za-z0-9_-]+/ack-password"),
"PUT": (r"host",),

@ -21,6 +21,16 @@ final class CloudSetup
throw new \RuntimeException('请先在全新业务数据库导入 web/sql/ss.sql');
}
$connection->unprepared(self::schema('cloud.sql'));
$column = $connection->selectOne(
"SELECT IS_NULLABLE AS nullable_state FROM information_schema.COLUMNS "
. "WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'cloud_instances' "
. "AND COLUMN_NAME = 'order_id'"
);
if ($column !== null && strtoupper((string) $column->nullable_state) !== 'YES') {
$connection->unprepared(
'ALTER TABLE cloud_instances MODIFY order_id BIGINT UNSIGNED NULL'
);
}
if (!$connection->getSchemaBuilder()->hasColumn('shop', 'host_id')) {
$connection->getSchemaBuilder()->table('shop', static function ($table) {
$table->unsignedBigInteger('host_id')->nullable();

@ -6,6 +6,7 @@ use App\Services\Cloud\CloudAccess;
use App\Services\Cloud\CloudEngineClient;
use App\Services\Cloud\CloudError;
use App\Services\Cloud\CloudInput;
use App\Services\Cloud\CloudInstanceDiscovery;
final class CloudAdminController
{
@ -80,7 +81,24 @@ final class CloudAdminController
$host->enabled = $enabled;
$host->updated_at = time();
$host->save();
return $response->withJson(array_merge($result, $host->toArray()), isset($args['id']) ? 200 : 201);
$warnings = is_array($result['warnings'] ?? null) ? $result['warnings'] : [];
$inventorySync = null;
try {
$inventory = $engine->host((int) $host->id, 'POST', 'host/discover-vms', $actor, [
'known_owners' => CloudInstanceDiscovery::knownOwners((int) $host->id),
]);
$inventorySync = CloudInstanceDiscovery::synchronize($host, $inventory);
if ($inventorySync['ownership_conflicts'] > 0) {
$warnings[] = '发现虚拟机在业务库与引擎中的归属不一致,已保留原归属,请检查日志';
}
} catch (\Throwable $error) {
error_log('Host VM discovery failed after host save: ' . get_class($error));
$warnings[] = '宿主机已保存,但存量虚拟机同步失败;请执行 php xcat cloudInit 后再次保存宿主机';
}
return $response->withJson(array_merge($result, $host->toArray(), [
'warnings' => array_values(array_filter($warnings)),
'inventory_sync' => $inventorySync,
]), isset($args['id']) ? 200 : 201);
}
public function test($request, $response, $args)

@ -112,10 +112,13 @@ final class CloudApiController
$vm = ['name' => $instance->name, 'state' => 'unknown', 'detail' => '宿主机暂时无法连接'];
}
$host = CloudHost::find($instance->host_id);
$createdAt = ($spec['record_source'] ?? '') === 'discovered'
? (is_string($vm['created_at'] ?? null) && trim($vm['created_at']) !== '' ? $vm['created_at'] : null)
: gmdate('Y-m-d\TH:i:s\Z', (int) $instance->created_at);
return array_merge(CloudInput::safeResult($spec), CloudInput::safeResult($vm), ['id' => (string) $instance->id, 'uuid' => $instance->uuid,
'user_id' => (int) $instance->user_id, 'owner_user_id' => (int) $instance->user_id,
'display_uuid' => $instance->uuid, 'host_id' => (int) $instance->host_id, 'region' => $host?->region ?? '',
'created_at' => gmdate('Y-m-d\TH:i:s\Z', (int) $instance->created_at),
'created_at' => $createdAt,
'has_initial_root_password' => $instance->credential_cipher !== null]);
}

@ -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),
];
}
}

@ -41,7 +41,7 @@ async function saveHost(event) {
if (!id && !byId('host-private-key').value.trim()) return notice('首次添加必须提供现有 SSH 私钥。', true);
const body = { name: byId('host-name').value.trim(), enabled: byId('host-enabled').checked, host, port, host_key: state.scanned.host_key, confirmed_fingerprint: state.scanned.fingerprint, private_key: byId('host-private-key').value.trim(), private_key_passphrase: byId('host-passphrase').value, region: byId('host-region').value.trim(), storage_pool: byId('host-storage-pool').value.trim(), iso_pool: byId('host-iso-pool').value.trim(), rescue_iso_pool: byId('host-rescue-pool').value.trim(), default_network: byId('host-network').value.trim(), public_pool_cidr: byId('host-public-pool').value.trim(), public_gateway: byId('host-gateway').value.trim(), public_dns_servers: formList(byId('host-dns').value), public_reserved_ips: formList(byId('host-reserved').value), public_vm_prefix: byId('host-vm-prefix').value ? Number(byId('host-vm-prefix').value) : null };
state.busy = true; byId('host-save').disabled = true; notice('正在测试连接并保存…');
try { const result = await api(`/api/v1/admin/hosts${id ? '/' + encodeURIComponent(id) : ''}`, { method: id ? 'PUT' : 'POST', body }); await loadHosts(); renderHosts(); fillHost(result?.id ? profile(result) : null); notice('宿主机配置已保存。套餐可在“商品管理”绑定此宿主机。'); }
try { const result = await api(`/api/v1/admin/hosts${id ? '/' + encodeURIComponent(id) : ''}`, { method: id ? 'PUT' : 'POST', body }); await loadHosts(); renderHosts(); fillHost(result?.id ? profile(result) : null); const imported = Number(result?.inventory_sync?.imported || 0); const warnings = Array.isArray(result?.warnings) ? result.warnings.filter(Boolean) : []; notice(`宿主机配置已保存${imported ? `,已将 ${imported} 台存量虚拟机归属给当前管理员` : ''}。套餐可在“商品管理”绑定此宿主机。${warnings.length ? ` 提示:${warnings.join(';')}` : ''}`, warnings.length > 0); }
catch (error) { byId('host-form-error').textContent = error.message; notice(error.message, true); }
finally { delete body.private_key; delete body.private_key_passphrase; byId('host-private-key').value = ''; byId('host-passphrase').value = ''; state.busy = false; byId('host-save').disabled = !canManage; }
}

@ -13,7 +13,7 @@ CREATE TABLE IF NOT EXISTS cloud_instances (
uuid CHAR(36) NOT NULL,
host_id BIGINT UNSIGNED NOT NULL,
user_id BIGINT UNSIGNED NOT NULL,
order_id BIGINT UNSIGNED NOT NULL,
order_id BIGINT UNSIGNED DEFAULT NULL,
name VARCHAR(63) NOT NULL,
status VARCHAR(32) NOT NULL DEFAULT 'active',
spec_json LONGTEXT NOT NULL,

@ -30,6 +30,7 @@ namespace {
use App\Services\Cloud\CloudEngineClient;
use App\Services\Cloud\CloudAccess;
use App\Services\Cloud\CloudJobService;
use App\Services\Cloud\CloudInstanceDiscovery;
use App\Services\Cloud\CloudError;
use GuzzleHttp\Client;
use GuzzleHttp\Handler\MockHandler;
@ -48,7 +49,7 @@ namespace {
$schema->create('cloud_hosts', function (Blueprint $t) { $t->increments('id'); $t->string('name'); $t->string('region')->default(''); $t->integer('enabled'); $t->string('default_network'); });
$schema->create('cloud_orders', function (Blueprint $t) { $t->increments('id'); foreach (['user_id', 'plan_id', 'host_id'] as $v) { $t->integer($v); } $t->string('idempotency_key'); $t->string('request_hash'); $t->string('amount'); $t->string('status'); $t->text('spec_json'); $t->integer('instance_id')->nullable(); $t->integer('refunded_at')->nullable(); $t->integer('created_at'); $t->integer('updated_at'); $t->unique(['user_id', 'idempotency_key']); });
$schema->create('cloud_jobs', function (Blueprint $t) { $t->increments('id'); $t->string('operation_id')->unique(); foreach (['order_id', 'instance_id'] as $v) { $t->integer($v)->nullable(); } foreach (['user_id', 'host_id'] as $v) { $t->integer($v); } $t->string('kind'); $t->string('status'); $t->text('payload_cipher'); $t->integer('attempts')->default(0); $t->integer('dispatched_at')->nullable(); $t->string('lease_token')->nullable(); $t->integer('lease_until')->default(0); $t->integer('available_at'); $t->string('error_code')->nullable(); $t->text('result_json')->nullable(); $t->integer('created_at'); $t->integer('updated_at'); $t->unique('order_id'); });
$schema->create('cloud_instances', function (Blueprint $t) { $t->increments('id'); foreach (['host_id', 'user_id', 'order_id'] as $v) { $t->integer($v); } $t->string('uuid'); $t->string('name'); $t->string('status'); $t->text('spec_json'); $t->text('credential_cipher')->nullable(); $t->integer('created_at'); $t->integer('updated_at'); $t->unique('order_id'); $t->unique(['host_id', 'uuid']); });
$schema->create('cloud_instances', function (Blueprint $t) { $t->increments('id'); foreach (['host_id', 'user_id'] as $v) { $t->integer($v); } $t->integer('order_id')->nullable(); $t->string('uuid'); $t->string('name'); $t->string('status'); $t->text('spec_json'); $t->text('credential_cipher')->nullable(); $t->integer('created_at'); $t->integer('updated_at'); $t->unique('order_id'); $t->unique(['host_id', 'uuid']); });
$schema->create('cloud_ssh_keys', function (Blueprint $t) { $t->increments('id'); $t->integer('user_id'); $t->string('name'); $t->text('public_key'); $t->string('fingerprint'); $t->integer('created_at'); });
$user = new User(); $user->id = 1; $user->money = '100.00'; $user->enable = 1; $user->is_admin = false; $user->save(); $user->isLogin = true;
\App\Services\Auth::$user = $user;
@ -117,5 +118,19 @@ namespace {
$mock->append(new Response(200, [], '{"status":"succeeded","uuid":"11111111-1111-4111-8111-111111111111"}')); $worker->tick();
$last = end($history)['request']; $assert((string) $last->getBody() === '{}' && $last->getUri()->getQuery() === 'delete_disks=true'
&& $instance->fresh()->status === 'deleted' && $instance->fresh()->credential_cipher === null, 'Delete payload/query or credential removal failed.');
$manualUuid = '44444444-4444-4444-8444-444444444444';
$inventory = ['claimed_count' => 1, 'untracked_managed_count' => 0, 'items' => [[
'uuid' => $manualUuid, 'owner_user_id' => 99, 'name' => 'manual-vm', 'state' => 'running',
'vcpu' => 4, 'memory_mib' => 4096, 'disks' => [['capacity_gib' => 40]], 'managed' => false,
]]];
$sync = CloudInstanceDiscovery::synchronize($host, $inventory);
$imported = CloudInstance::where('uuid', $manualUuid)->first();
$assert($sync['imported'] === 1 && $imported !== null && (int) $imported->user_id === 99
&& $imported->order_id === null && $imported->credential_cipher === null,
'Discovered VM was not assigned to the configuring administrator without a fake order.');
$repeat = CloudInstanceDiscovery::synchronize($host, $inventory);
$assert($repeat['imported'] === 0 && $repeat['existing'] === 1
&& CloudInstance::where('uuid', $manualUuid)->count() === 1,
'Repeated VM discovery duplicated or rewrote the imported instance.');
echo "Cloud SQLite purchase, provisioning, refund, ambiguity, quota and tenant checks passed (crypto double only).\n";
}

Loading…
Cancel
Save