user_id !== (int) ($actor['user_id'] ?? 0) && !($actor['is_admin'] ?? false)) { throw new CloudError('虚拟机不存在或无权访问', 404); } $rows = CloudInstanceSnapshot::where('instance_id', (int) $instance->id) ->where('host_id', (int) $instance->host_id) ->where('user_id', (int) $instance->user_id) ->whereIn('state', self::SLOT_STATES) ->orderByDesc('id')->get(); $operationIds = []; foreach ($rows as $row) { if (is_string($row->last_operation_id) && preg_match('/^[a-f0-9]{32}$/D', $row->last_operation_id) === 1) { $operationIds[] = $row->last_operation_id; } } $jobs = []; if ($operationIds !== []) { foreach (CloudJob::whereIn('operation_id', array_values(array_unique($operationIds))) ->get(['id', 'operation_id', 'user_id', 'kind', 'status']) as $job) { $jobs[(string) $job->operation_id] = $job; } } $snapshots = []; $canInspectAllJobs = (bool) ($actor['is_admin'] ?? false); foreach ($rows as $row) { $job = $jobs[(string) $row->last_operation_id] ?? null; // cloud_jobs records the audited actor. If a super administrator // acted for this tenant, the owner can see the catalog transition // but must not be told to poll an actor-scoped job endpoint that // will correctly return 404 to them. if ($job !== null && !$canInspectAllJobs && (int) $job->user_id !== (int) ($actor['user_id'] ?? 0)) { $job = null; } $snapshots[] = self::display($row, $job); } $active = CloudJob::where('instance_id', (int) $instance->id) ->whereIn('status', ['queued', 'running', 'uncertain']) ->orderBy('id')->first(['id', 'user_id', 'kind', 'status']); $global = ($actor['is_super_admin'] ?? false) && ($actor['can_manage'] ?? false); $writable = (int) $instance->user_id === (int) ($actor['user_id'] ?? 0) || $global; $used = count($snapshots); $canMutate = $writable && $active === null && (string) $instance->status === 'active' && ($global || $instance->serviceAvailable()); return [ 'instance_id' => (string) $instance->id, 'snapshots' => $snapshots, 'max_snapshots' => self::MAX_PER_INSTANCE, 'used_slots' => $used, 'can_create' => $canMutate && $used < self::MAX_PER_INSTANCE, 'can_mutate' => $canMutate, 'active_job' => $active === null ? null : self::jobSummary( $active, $canInspectAllJobs || (int) $active->user_id === (int) ($actor['user_id'] ?? 0) ), ]; } /** * Reinstall replaces the system-disk baseline underneath every retained * recovery point. Settings updates are different: they rotate the current * NoCloud instance ID, so a restored older vda reapplies the latest seed. * The caller holds cloud_instances, serializing this check with snapshot * create/delete admission for the same VM. */ public static function assertReinstallAllowedLocked( CloudInstance $instance): void { if (CloudInstanceSnapshot::where('instance_id', (int) $instance->id) ->where('host_id', (int) $instance->host_id) ->where('user_id', (int) $instance->user_id) ->whereIn('state', self::SLOT_STATES)->exists()) { throw new CloudError( '实例保留有快照,请先删除全部快照后再重装系统', 409 ); } } public static function create(CloudInstance $instance, array $actor, array $input): array { CloudInput::only($input, ['name', 'consistency_mode']); $name = CloudInput::snapshotName($input['name'] ?? null); $mode = CloudInput::snapshotConsistency($input['consistency_mode'] ?? null); $actor = CloudAccess::vmActor($instance, $actor); return $instance->getConnection()->transaction( function () use ($instance, $actor, $name, $mode): array { $locked = self::lockInstance($instance, $actor); self::assertNoActiveJob($locked); $used = CloudInstanceSnapshot::where('instance_id', (int) $locked->id) ->whereIn('state', self::SLOT_STATES)->count('id'); if ((int) $used >= self::MAX_PER_INSTANCE) { throw new CloudError('每台实例最多保留两个快照,请先删除旧快照', 409); } $now = time(); $snapshot = new CloudInstanceSnapshot(); $snapshot->snapshot_key = bin2hex(random_bytes(16)); $snapshot->instance_id = $locked->id; $snapshot->user_id = $locked->user_id; $snapshot->host_id = $locked->host_id; $snapshot->name = $name; $snapshot->state = 'creating'; $snapshot->consistency = $mode; $snapshot->size_bytes = null; $snapshot->last_operation_id = null; $snapshot->ready_at = null; $snapshot->last_restored_at = null; $snapshot->deleted_at = null; $snapshot->created_at = $snapshot->updated_at = $now; $snapshot->save(); $body = [ 'snapshot_key' => (string) $snapshot->snapshot_key, 'name' => $name, 'consistency_mode' => $mode, ]; $job = self::newJob($locked, $snapshot, $actor, 'snapshot_create', 'POST', '/snapshots', $body, 'creating'); return ['snapshot' => self::display($snapshot, $job), 'job' => $job]; }, 3 ); } public static function restore(CloudInstance $instance, array $actor, string $snapshotKey, array $input): array { $actor = CloudAccess::vmActor($instance, $actor); CloudInput::only($input, ['confirmation_name']); if (!is_string($input['confirmation_name'] ?? null) || !hash_equals((string) $instance->name, (string) $input['confirmation_name'])) { throw new CloudError('请填写实例名称确认恢复快照'); } return self::mutate($instance, $actor, $snapshotKey, 'snapshot_restore', 'POST', '/restore', 'restoring', (string) $input['confirmation_name']); } public static function delete(CloudInstance $instance, array $actor, string $snapshotKey, array $input): array { $actor = CloudAccess::vmActor($instance, $actor); CloudInput::only($input, ['confirmation_name']); if (!is_string($input['confirmation_name'] ?? null) || !hash_equals((string) $instance->name, (string) $input['confirmation_name'])) { throw new CloudError('请填写实例名称确认删除快照'); } return self::mutate($instance, $actor, $snapshotKey, 'snapshot_delete', 'DELETE', '', 'deleting', (string) $input['confirmation_name']); } /** Apply one terminal/running engine operation inside the worker transaction. */ public static function finishJobLocked(CloudJob $job, string $status, $result, array $payload): void { if (!in_array((string) $job->kind, ['snapshot_create', 'snapshot_restore', 'snapshot_delete'], true)) { return; } $snapshot = $job->snapshot_id === null ? null : CloudInstanceSnapshot::where('id', (int) $job->snapshot_id) ->lockForUpdate()->first(); if ($snapshot === null || (int) $snapshot->instance_id !== (int) $job->instance_id || (int) $snapshot->user_id !== (int) ($payload['owner_id'] ?? 0) || (int) $snapshot->host_id !== (int) $job->host_id || !hash_equals((string) $snapshot->last_operation_id, (string) $job->operation_id) || !hash_equals((string) $snapshot->snapshot_key, (string) ($payload['snapshot_key'] ?? ''))) { throw new CloudError('快照任务与面板记录不一致,必须人工核查', 409); } if ($status === 'running') { return; } $now = time(); if ($status === 'uncertain') { $snapshot->state = 'uncertain'; } elseif ($status === 'failed') { $snapshot->state = $job->kind === 'snapshot_create' ? 'failed' : 'ready'; } elseif ($status === 'succeeded') { if (!is_array($result) || !is_string($result['snapshot_key'] ?? null) || !hash_equals((string) $snapshot->snapshot_key, (string) $result['snapshot_key'])) { throw new CloudError('引擎成功结果缺少已确认的快照标识', 502); } if ($job->kind === 'snapshot_create') { $consistency = $result['consistency'] ?? null; if (($result['created'] ?? false) !== true || !is_string($consistency) || !hash_equals((string) $snapshot->consistency, $consistency) || !in_array($consistency, ['filesystem', 'crash'], true)) { throw new CloudError('引擎成功结果缺少已确认的快照一致性', 502); } $snapshot->state = 'ready'; $snapshot->ready_at = $now; $size = filter_var($result['size_bytes'] ?? null, FILTER_VALIDATE_INT, ['options' => ['min_range' => 0]]); $snapshot->size_bytes = $size === false ? null : (int) $size; } elseif ($job->kind === 'snapshot_restore') { if (($result['restored'] ?? false) !== true) { throw new CloudError('引擎成功结果缺少已确认的快照恢复状态', 502); } $snapshot->state = 'ready'; $snapshot->last_restored_at = $now; } else { if (($result['deleted'] ?? false) !== true) { throw new CloudError('引擎成功结果缺少已确认的快照删除状态', 502); } $snapshot->state = 'deleted'; $snapshot->deleted_at = $now; } } $snapshot->updated_at = $now; $snapshot->save(); } /** Preserve ambiguity after a dispatched request fails locally. */ public static function markJobUncertain(CloudJob $job): void { (new CloudInstanceSnapshot())->getConnection()->transaction( function () use ($job): void { if (in_array((string) $job->kind, ['snapshot_create', 'snapshot_restore', 'snapshot_delete'], true) && $job->snapshot_id !== null) { CloudInstanceSnapshot::where('id', (int) $job->snapshot_id) ->where('instance_id', (int) $job->instance_id) ->where('host_id', (int) $job->host_id) ->where('last_operation_id', (string) $job->operation_id) ->update(['state' => 'uncertain', 'updated_at' => time()]); } elseif (in_array((string) $job->kind, ['delete', 'expiry_delete'], true) && $job->instance_id !== null) { CloudInstanceSnapshot::where('instance_id', (int) $job->instance_id) ->whereIn('state', self::SLOT_STATES) ->update(['state' => 'uncertain', 'last_operation_id' => (string) $job->operation_id, 'updated_at' => time()]); } }, 3 ); } /** Cascade the catalog only after the VM deletion operation is known. */ public static function finishInstanceDeleteLocked(CloudJob $job, string $status): void { if (!in_array((string) $job->kind, ['delete', 'expiry_delete'], true) || $job->instance_id === null || !in_array($status, ['succeeded', 'failed', 'uncertain'], true)) { return; } $now = time(); if ($status === 'succeeded') { CloudInstanceSnapshot::where('instance_id', (int) $job->instance_id) ->whereIn('state', self::SLOT_STATES) ->update(['state' => 'deleted', 'deleted_at' => $now, 'last_operation_id' => (string) $job->operation_id, 'updated_at' => $now]); } elseif ($status === 'uncertain') { CloudInstanceSnapshot::where('instance_id', (int) $job->instance_id) ->whereIn('state', self::SLOT_STATES) ->update(['state' => 'uncertain', 'last_operation_id' => (string) $job->operation_id, 'updated_at' => $now]); } else { CloudInstanceSnapshot::where('instance_id', (int) $job->instance_id) ->where('state', 'uncertain') ->where('last_operation_id', (string) $job->operation_id) ->update(['state' => 'ready', 'updated_at' => $now]); } } private static function mutate(CloudInstance $instance, array $actor, string $snapshotKey, string $kind, string $method, string $suffix, string $nextState, string $confirmationName): array { if (preg_match('/^[a-f0-9]{32}$/D', $snapshotKey) !== 1) { throw new CloudError('快照标识无效', 404); } $actor = CloudAccess::vmActor($instance, $actor); return $instance->getConnection()->transaction( function () use ($instance, $actor, $snapshotKey, $kind, $method, $suffix, $nextState, $confirmationName): array { $locked = self::lockInstance($instance, $actor); if (!hash_equals((string) $locked->name, $confirmationName)) { throw new CloudError('实例名称已变化,请刷新后重新确认', 409); } self::assertNoActiveJob($locked); $snapshot = CloudInstanceSnapshot::where( 'snapshot_key', $snapshotKey )->where('instance_id', (int) $locked->id) ->where('host_id', (int) $locked->host_id) ->where('user_id', (int) $locked->user_id) ->lockForUpdate()->first(); if ($snapshot === null) { throw new CloudError('快照不存在或无权访问', 404); } if ((string) $snapshot->state !== 'ready') { throw new CloudError('快照正在执行其他操作,请等待完成', 409); } $job = self::newJob($locked, $snapshot, $actor, $kind, $method, '/snapshots/' . rawurlencode($snapshotKey) . $suffix, [], $nextState); return ['snapshot' => self::display($snapshot, $job), 'job' => $job]; }, 3 ); } private static function lockInstance(CloudInstance $instance, array $actor): CloudInstance { $locked = CloudInstance::where('id', (int) $instance->id) ->lockForUpdate()->first(); if ($locked === null || (string) $locked->status === 'deleted') { throw new CloudError('虚拟机不存在', 404); } CloudAccess::vmActor($locked, $actor); if ((string) $locked->status !== 'active') { throw new CloudError('实例正在执行到期销毁流程,不能提交快照操作', 409); } $global = ($actor['is_super_admin'] ?? false) && ($actor['can_manage'] ?? false); if (!$global && !$locked->serviceAvailable()) { throw new CloudError('实例服务已到期或正在恢复,不能执行快照操作', 409); } return $locked; } private static function assertNoActiveJob(CloudInstance $instance): void { if (CloudJob::where('instance_id', (int) $instance->id) ->whereIn('status', ['queued', 'running', 'uncertain'])->exists()) { throw new CloudError('虚拟机已有任务,请等待完成', 409); } } private static function newJob(CloudInstance $instance, CloudInstanceSnapshot $snapshot, array $actor, string $kind, string $method, string $suffix, array $body, string $nextState): CloudJob { $now = time(); $job = new CloudJob(); $job->operation_id = bin2hex(random_bytes(16)); $job->instance_id = $instance->id; $job->snapshot_id = $snapshot->id; // cloud_jobs.user_id is the audited actor, while the snapshot row and // encrypted owner_id retain the tenant that owns the instance. $job->user_id = (int) $actor['user_id']; $job->host_id = $instance->host_id; $job->kind = $kind; $job->status = 'queued'; $job->payload_cipher = CloudSecrets::encrypt([ 'method' => $method, 'resource' => 'vms/' . rawurlencode((string) $instance->uuid) . $suffix, 'body' => $body, 'owner_id' => (int) $instance->user_id, 'can_manage' => (bool) ($actor['can_manage'] ?? false), 'snapshot_key' => (string) $snapshot->snapshot_key, 'snapshot_previous_state' => (string) $snapshot->state, ]); $job->available_at = $job->created_at = $job->updated_at = $now; $job->save(); $snapshot->state = $nextState; $snapshot->last_operation_id = $job->operation_id; $snapshot->updated_at = $now; $snapshot->save(); return $job; } private static function display(CloudInstanceSnapshot $snapshot, ?CloudJob $job): array { return [ 'snapshot_key' => (string) $snapshot->snapshot_key, 'name' => (string) $snapshot->name, 'state' => in_array((string) $snapshot->state, ['creating', 'ready', 'restoring', 'deleting', 'uncertain'], true) ? (string) $snapshot->state : 'uncertain', 'consistency' => in_array((string) $snapshot->consistency, ['filesystem', 'crash'], true) ? (string) $snapshot->consistency : 'unknown', 'size_bytes' => $snapshot->size_bytes === null ? null : max(0, (int) $snapshot->size_bytes), 'created_at' => max(0, (int) $snapshot->created_at), 'ready_at' => $snapshot->ready_at === null ? null : max(0, (int) $snapshot->ready_at), 'last_restored_at' => $snapshot->last_restored_at === null ? null : max(0, (int) $snapshot->last_restored_at), 'updated_at' => max(0, (int) $snapshot->updated_at), 'job_id' => $job === null ? null : (string) $job->id, 'job_status' => $job === null ? null : (CloudOperationErrorCatalog::userStatus($job->status) ?? 'unknown'), ]; } private static function jobSummary(CloudJob $job, bool $includeId): array { $labels = CloudOperationCatalog::labels((string) $job->kind); $summary = [ 'kind' => (string) $job->kind, 'kind_label' => $labels['kind_label'], 'status' => CloudOperationErrorCatalog::userStatus($job->status) ?? 'unknown', ]; if ($includeId) { $summary['id'] = (string) $job->id; } return $summary; } }