diff --git a/web/app/Controllers/CloudAdminController.php b/web/app/Controllers/CloudAdminController.php index 6650bc2e..25e34e00 100644 --- a/web/app/Controllers/CloudAdminController.php +++ b/web/app/Controllers/CloudAdminController.php @@ -11,8 +11,10 @@ use App\Services\Cloud\CloudError; use App\Services\Cloud\CloudInput; use App\Services\Cloud\CloudNetworkSelector; use App\Services\Cloud\CloudAgentSettingsService; +use App\Services\Cloud\CloudRescueCatalogService; use App\Services\Cloud\CloudRescueTemplateDeletionService; use App\Services\Cloud\CloudRescueTemplateImportService; +use App\Services\Cloud\CloudRescueTemplateReplicaCoordinator; use App\Services\Cloud\CloudRescueTemplateRetirementService; use App\Services\Cloud\CloudTemplateCatalogService; use App\Services\Cloud\CloudTemplateReplicaCoordinator; @@ -235,6 +237,39 @@ final class CloudAdminController )->withHeader('Cache-Control', 'no-store'); } + public function rescueTemplateReplicaRepair( + $request, + $response, + $args = [] + ) { + $actor = $this->actor($request, true); + $body = CloudInput::object($request); + CloudInput::only($body, []); + $templateId = strtolower((string) ($args['id'] ?? '')); + if (preg_match('/^[a-f0-9]{32}$/D', $templateId) !== 1) { + throw new CloudError('救援 ISO 标识无效'); + } + $hostId = filter_var( + $args['host_id'] ?? null, + FILTER_VALIDATE_INT, + ['options' => ['min_range' => 1]] + ); + if ($hostId === false) { + throw new CloudError('宿主机标识无效'); + } + $this->host((int) $hostId); + $result = CloudRescueTemplateReplicaCoordinator::syncHost( + $templateId, + (int) $hostId, + $actor, + 'admin_repair' + ); + return $response->withJson( + $result, + ($result['scheduled'] ?? false) === true ? 202 : 200 + )->withHeader('Cache-Control', 'no-store'); + } + public function templates($request, $response, $args = []) { $method = $request->getMethod(); @@ -307,6 +342,41 @@ final class CloudAdminController )->withHeader('Cache-Control', 'no-store'); } + if ($kind === 'rescue' && !isset($args['id'])) { + if ($method === 'GET') { + return $response->withJson( + CloudRescueCatalogService::adminCatalog() + )->withHeader('Cache-Control', 'no-store'); + } + if ($method === 'POST') { + $result = CloudRescueCatalogService::importRemote( + $body, + $actor + ); + try { + $validation = + CloudRescueTemplateReplicaCoordinator::beginValidation( + (string) $result['template_id'], + (int) $result['validation_host_id'], + $actor + ); + $result = array_merge($result, $validation, [ + 'validation_scheduled' => + ($validation['scheduled'] ?? false) === true, + ]); + } catch (\Throwable $exception) { + error_log(sprintf( + 'rescue catalog validation scheduling deferred: %s: %s', + (string) $result['template_id'], + $exception->getMessage() + )); + $result['validation_scheduled'] = false; + } + return $response->withJson($result, 202) + ->withHeader('Cache-Control', 'no-store'); + } + } + $query = $request->getQueryParams(); $hostId = filter_var( $query['host_id'] ?? ($body['host_id'] ?? null), diff --git a/web/app/Services/Cloud/CloudRescueCatalogService.php b/web/app/Services/Cloud/CloudRescueCatalogService.php index f73909a5..d30ef685 100644 --- a/web/app/Services/Cloud/CloudRescueCatalogService.php +++ b/web/app/Services/Cloud/CloudRescueCatalogService.php @@ -2,11 +2,83 @@ namespace App\Services\Cloud; +use App\Models\CloudHost; use App\Models\CloudRescueTemplate; +use App\Models\CloudRescueTemplateArtifact; +use App\Models\CloudRescueTemplateReplica; /** PHP authority for global immutable rescue ISO generations. */ final class CloudRescueCatalogService { + /** Global administrator view, including every host replica position. */ + public static function adminCatalog(): array + { + $templates = CloudRescueTemplate::query() + ->where('state', '<>', 'retired') + ->orderByDesc('created_at')->get(); + $templateIds = $templates->map( + static fn (CloudRescueTemplate $template): string => + (string) $template->id + )->all(); + $replicas = $templateIds === [] ? collect() + : CloudRescueTemplateReplica::query()->whereIn( + 'template_id', + $templateIds + )->orderBy('template_id')->orderBy('host_id')->get(); + $artifacts = $templateIds === [] ? collect() + : CloudRescueTemplateArtifact::query()->whereIn( + 'template_id', + $templateIds + )->get()->keyBy('template_id'); + $eligible = array_fill_keys(array_map( + 'intval', + CloudAgentSettingsService::enabledHostIds() + ), true); + $hosts = []; + foreach (CloudHost::query()->orderBy('id')->get() as $host) { + $hostId = (int) $host->id; + $name = trim((string) ($host->name ?? '')); + $hosts[$hostId] = [ + 'id' => $hostId, + 'name' => $name !== '' ? $name : '宿主机 #' . $hostId, + 'region' => trim((string) ($host->region ?? '')), + 'enabled' => (bool) $host->enabled, + 'eligible' => isset($eligible[$hostId]), + 'record_present' => true, + ]; + } + foreach ($replicas as $replica) { + $hostId = (int) $replica->host_id; + if (!isset($hosts[$hostId])) { + $hosts[$hostId] = [ + 'id' => $hostId, + 'name' => '宿主机 #' . $hostId, + 'region' => '', + 'enabled' => false, + 'eligible' => false, + 'record_present' => false, + ]; + } + } + ksort($hosts, SORT_NUMERIC); + $replicasByTemplate = []; + foreach ($replicas as $replica) { + $replicasByTemplate[(string) $replica->template_id][] = $replica; + } + return [ + 'hosts' => array_values($hosts), + 'templates' => $templates->map( + static fn (CloudRescueTemplate $template): array => + self::adminDto( + $template, + $replicasByTemplate[(string) $template->id] ?? [], + $hosts, + $artifacts->get((string) $template->id) + ) + )->all(), + ]; + } + public static function importRemote( array $input, array $actor, @@ -154,4 +226,120 @@ final class CloudRescueCatalogService } return $value; } + + /** + * @param list $replicas + * @param array> $hosts + */ + private static function adminDto( + CloudRescueTemplate $template, + array $replicas, + array $hosts, + ?CloudRescueTemplateArtifact $artifact + ): array { + $byHost = []; + foreach ($replicas as $replica) { + $byHost[(int) $replica->host_id] = $replica; + } + $summary = [ + 'hosts' => count($hosts), + 'eligible' => 0, + 'ready' => 0, + 'attention' => 0, + 'cleanup_debt' => 0, + ]; + $matrix = []; + foreach ($hosts as $hostId => $host) { + $replica = $byHost[$hostId] ?? null; + $eligible = ($host['eligible'] ?? false) === true; + $exact = $replica !== null + && CloudRescueTemplateReplicaCoordinator::isExactReplica( + $template, + $replica + ); + $repair = CloudRescueTemplateReplicaCoordinator::repairPlan( + $template, + $replica, + $eligible + ); + $cleanupDebt = $replica !== null + && (string) $template->state === 'retiring' + && (string) $replica->state !== 'deleted'; + $summary['eligible'] += $eligible ? 1 : 0; + $summary['ready'] += $exact ? 1 : 0; + $summary['attention'] += $eligible && !$exact ? 1 : 0; + $summary['cleanup_debt'] += $cleanupDebt ? 1 : 0; + $matrix[] = [ + 'host_id' => $hostId, + 'host_name' => (string) $host['name'], + 'host_region' => (string) $host['region'], + 'host_enabled' => (bool) $host['enabled'], + 'host_eligible' => $eligible, + 'host_record_present' => (bool) $host['record_present'], + 'state' => $replica === null + ? 'missing' : (string) $replica->state, + 'revision' => $replica === null + ? 0 : (int) $replica->revision, + 'generation_token' => $replica === null + ? '' : (string) $replica->generation_token, + 'size_bytes' => $replica === null + ? 0 : (int) $replica->size_bytes, + 'sha256' => $replica === null + ? '' : (string) $replica->sha256, + 'error_code' => $replica === null + ? '' : (string) $replica->error_code, + 'operation_id' => $replica === null + ? '' : (string) $replica->operation_id, + 'job_id' => $replica === null || $replica->job_id === null + ? null : (int) $replica->job_id, + 'observed_state' => $replica === null + ? 'never' : (string) $replica->observed_state, + 'observed_revision' => $replica === null + ? 0 : (int) $replica->observed_revision, + 'observed_generation_token' => $replica === null + ? '' : (string) $replica->observed_generation_token, + 'observed_size_bytes' => $replica === null + ? 0 : (int) $replica->observed_size_bytes, + 'observed_sha256' => $replica === null + ? '' : (string) $replica->observed_sha256, + 'last_verified_at' => $replica === null + ? 0 : (int) $replica->last_verified_at, + 'cleanup_resolved_at' => $replica === null + || $replica->cleanup_resolved_at === null + ? null : (int) $replica->cleanup_resolved_at, + 'exact' => $exact, + 'cleanup_debt' => $cleanupDebt, + 'repair_action' => $repair['action'], + 'repair_reason' => $repair['reason'], + ]; + } + return [ + 'id' => (string) $template->id, + 'name' => (string) $template->name, + 'size_bytes' => (int) $template->size_bytes, + 'sha256' => (string) $template->sha256, + 'revision' => (int) $template->revision, + 'generation_token' => (string) $template->generation_token, + 'state' => (string) $template->state, + 'created_at' => (int) $template->created_at, + 'updated_at' => (int) $template->updated_at, + 'recovery_actions' => + CloudRescueCatalogStateMachine::recoveryActions( + 'catalog', + (string) $template->state + ), + 'replica_summary' => $summary, + 'replicas' => $matrix, + 'artifact' => $artifact === null ? null : [ + 'state' => (string) $artifact->state, + 'error_code' => (string) $artifact->error_code, + 'delete_generation' => (int) $artifact->delete_generation, + 'delete_next_check_at' => + (int) $artifact->delete_next_check_at, + 'delete_attention_at' => + $artifact->delete_attention_at === null + ? null : (int) $artifact->delete_attention_at, + ], + ]; + } } diff --git a/web/app/Services/Cloud/CloudRescueTemplateReplicaCoordinator.php b/web/app/Services/Cloud/CloudRescueTemplateReplicaCoordinator.php index 7fb1f88b..d241f3f5 100644 --- a/web/app/Services/Cloud/CloudRescueTemplateReplicaCoordinator.php +++ b/web/app/Services/Cloud/CloudRescueTemplateReplicaCoordinator.php @@ -392,6 +392,30 @@ final class CloudRescueTemplateReplicaCoordinator ); } + /** UI projection of the same retry contract enforced by syncHost(). */ + public static function repairPlan( + CloudRescueTemplate $template, + ?CloudRescueTemplateReplica $replica, + bool $eligible + ): array { + if (!$eligible || !in_array((string) $template->state, [ + 'active', 'degraded', 'error', 'distributing', + ], true)) { + return ['action' => null, 'reason' => 'host_or_catalog_unavailable']; + } + if ($replica !== null && self::isExactReplica($template, $replica)) { + return ['action' => null, 'reason' => 'exact']; + } + if ($replica === null || (string) $replica->state === 'missing') { + return ['action' => 'import', 'reason' => 'missing']; + } + if ((string) $replica->state === 'failed' + && self::retryableFailureEvidence($template, $replica, false)) { + return ['action' => 'import', 'reason' => 'proven_not_applied']; + } + return ['action' => null, 'reason' => (string) $replica->state]; + } + /** Publish availability after all relevant rows have been locked. */ public static function projectCatalogLocked( CloudRescueTemplate $template, @@ -583,33 +607,41 @@ final class CloudRescueTemplateReplicaCoordinator if ((string) $replica->state === 'missing') { return; } - if ((string) $replica->state !== 'failed') { - throw new CloudError('救援 ISO 副本当前不能安全重新同步', 409); + if (!self::retryableFailureEvidence($template, $replica, true)) { + throw new CloudError('救援 ISO 副本失败效果尚未证明可重试', 409); } - $job = $replica->job_id === null ? null - : CloudJob::query()->where('id', (int) $replica->job_id) - ->lockForUpdate()->first(); - if ($job === null - || !in_array((string) $job->kind, self::IMPORT_KINDS, true) - || (int) $job->host_id !== (int) $replica->host_id - || (string) $job->execution_mode !== 'agent' - || (string) $job->phase !== 'terminal' - || (string) $job->outcome !== 'failed' - || (string) $job->effect_resolution !== 'not_applied' - || (bool) $job->effect_may_have_applied - || !(bool) $job->safe_to_resubmit - || (bool) $job->finalization_incident - || CloudJobService::hasAgentRecoveryEnvelopeConflict($job) - || !hash_equals( + } + + private static function retryableFailureEvidence( + CloudRescueTemplate $template, + CloudRescueTemplateReplica $replica, + bool $lock + ): bool { + if ((string) $replica->state !== 'failed' + || $replica->job_id === null) { + return false; + } + $query = CloudJob::query()->where('id', (int) $replica->job_id); + $job = $lock ? $query->lockForUpdate()->first() : $query->first(); + return $job !== null + && in_array((string) $job->kind, self::IMPORT_KINDS, true) + && (int) $job->host_id === (int) $replica->host_id + && (string) $job->execution_mode === 'agent' + && (string) $job->phase === 'terminal' + && (string) $job->outcome === 'failed' + && (string) $job->effect_resolution === 'not_applied' + && !(bool) $job->effect_may_have_applied + && (bool) $job->safe_to_resubmit + && !(bool) $job->finalization_incident + && !CloudJobService::hasAgentRecoveryEnvelopeConflict($job) + && hash_equals( (string) $job->operation_id, (string) $replica->operation_id ) - || !hash_equals( + && hash_equals( (string) $template->generation_token, (string) $replica->generation_token - )) { - throw new CloudError('救援 ISO 副本失败效果尚未证明可重试', 409); - } + ); } private static function lockedReplica( diff --git a/web/config/routes_cloud.php b/web/config/routes_cloud.php index f09bb75d..0f855c8e 100644 --- a/web/config/routes_cloud.php +++ b/web/config/routes_cloud.php @@ -207,6 +207,10 @@ $app->group('/api/v1', function () { '/admin/cloud-templates/{id}/replicas/{host_id:[1-9][0-9]*}/repair', 'App\Controllers\CloudAdminController:templateReplicaRepair' ); + $this->post( + '/admin/rescue-templates/{id}/replicas/{host_id:[1-9][0-9]*}/repair', + 'App\Controllers\CloudAdminController:rescueTemplateReplicaRepair' + ); $this->post('/admin/cloud-templates/import', 'App\Controllers\CloudAdminController:templates')->setArgument('kind', 'cloud'); $this->post('/admin/cloud-templates/upload', 'App\Controllers\CloudAdminController:cloudTemplateUpload'); $this->post('/admin/rescue-templates/upload', 'App\Controllers\CloudAdminController:rescueUpload'); diff --git a/web/public/cloud/admin.css b/web/public/cloud/admin.css index 628ceae9..f92c7e38 100644 --- a/web/public/cloud/admin.css +++ b/web/public/cloud/admin.css @@ -265,6 +265,10 @@ align-items: stretch; } +.cloud-admin-rescue-global-layout > .cloud-admin-imports { + grid-template-columns: minmax(0, 1fr); +} + .cloud-admin-template-layout .cloud-admin-import-card, .cloud-admin-template-layout .cloud-admin-import-card .card-body { height: 100%; @@ -450,6 +454,27 @@ gap: 8px; } +.cloud-rescue-version-actions { + display: flex; + align-items: center; + justify-content: space-between; + gap: 16px; + padding: 0 18px 17px; +} + +.cloud-rescue-version-note { + margin: 0; + color: #718096; + font-size: 13px; + font-weight: 700; + line-height: 1.5; +} + +.cloud-rescue-version-actions .btn { + flex: 0 0 auto; + white-space: nowrap; +} + .cloud-template-matrix { border-top: 1px solid #e5ebf2; } @@ -1203,6 +1228,15 @@ justify-content: stretch; } + .cloud-rescue-version-actions { + align-items: stretch; + flex-direction: column; + } + + .cloud-rescue-version-actions .btn { + width: 100%; + } + .cloud-template-version-actions .btn { flex: 1 1 150px; } diff --git a/web/public/cloud/admin.js b/web/public/cloud/admin.js index 772daae1..0b83eef8 100644 --- a/web/public/cloud/admin.js +++ b/web/public/cloud/admin.js @@ -761,6 +761,9 @@ function catalogStateView(value) { return ({ active: ['已就绪', 'is-ready'], degraded: ['部分可用', 'is-warning'], error: ['不可用', 'is-error'], preparing: ['准备源文件', 'is-running'], + validating: ['首台校验', 'is-running'], distributing: ['分发中', 'is-running'], + source_failed: ['源文件失败', 'is-error'], source_unknown: ['源文件待核查', 'is-error'], + retiring: ['退役清理中', 'is-running'], retired: ['已退役', 'is-muted'], importing: ['分发中', 'is-running'], syncing: ['同步中', 'is-running'], deleting: ['删除中', 'is-running'], delete_failed: ['删除失败', 'is-error'], }[stateName] || ['状态未知', 'is-muted']); @@ -995,41 +998,153 @@ function renderCloudTemplates(list, entries) { section.append(head, versions); list.append(section); } } -function renderRescueTemplates(list, entries, hostId) { - for (const entry of entries) { - const row = element('div', 'cloud-admin-row'); - const name = element('input', 'form-control'); name.value = entry.name || ''; name.readOnly = true; name.setAttribute('aria-label', '模版名称'); - const remove = element('button', 'btn btn-outline-danger', '删除'); remove.type = 'button'; remove.disabled = !canManage; - remove.addEventListener('click', async () => { - if (state.busy || !confirm(`删除所选宿主机上的模版“${entry.name}”?正在使用的模版可能不能删除。`)) return; - state.busy = true; remove.disabled = true; - try { - const result = await api(`${catalogPath()}/${encodeURIComponent(entry.id)}?host_id=${encodeURIComponent(hostId)}`, { method: 'DELETE' }); - if (result?.deleted === true) { await loadCatalog(); notice('救援镜像已确认删除。'); } - else { importProgress(result, '删除救援镜像', async () => { await loadCatalog(); notice('救援镜像已删除。'); }); notice('删除任务已接收。'); } - } catch (error) { notice(error.message, true); remove.disabled = !canManage; } - finally { state.busy = false; } +function rescueReplicaStateView(entry, replica) { + const stateName = String(replica?.state || 'missing').toLowerCase(); + if (stateName === 'deleted') return ['已清理', 'is-muted']; + if (stateName === 'deleting') return ['正在清理', 'is-running']; + if (stateName === 'delete_unknown') return ['删除效果待核查', 'is-error']; + if (stateName === 'delete_failed') return ['删除失败', 'is-error']; + if (replica?.host_record_present === false) return ['宿主机记录缺失', 'is-error']; + if (replica?.host_eligible !== true) return ['宿主机未就绪', 'is-muted']; + if (replica?.exact === true) return ['精确副本', 'is-ready']; + return ({ + missing: ['缺少副本', 'is-warning'], stale: ['版本不一致', 'is-error'], + pending: ['等待分发', 'is-running'], importing: ['正在分发', 'is-running'], + import_unknown: ['导入效果待核查', 'is-error'], failed: ['导入失败', 'is-error'], + active: ['内容不一致', 'is-error'], + }[stateName] || [String(entry?.state || '') === 'retiring' ? '等待清理' : '状态待核查', 'is-error']); +} +async function repairRescueReplica(entry, replica, button) { + if (state.busy || replica?.repair_action !== 'import') return; + if (!confirm(`确定在 ${replica.host_name || `宿主机 #${replica.host_id}`} 补齐救援 ISO“${entry.name}”的精确副本?`)) return; + state.busy = true; button.disabled = true; + try { + const { payload, status } = await requestJson( + `/api/v1/admin/rescue-templates/${encodeURIComponent(entry.id)}/replicas/${encodeURIComponent(replica.host_id)}/repair`, + { method: 'POST', body: {} }, + ); + if (status === 200 && payload?.scheduled === false) { + await loadCatalog(); notice('宿主机已经具有精确副本,无需重复处理。'); return; + } + if (status !== 202 || !taskUrl(payload)) throw new Error('补齐请求未返回严格的任务回执,请勿重复提交并联系管理员核查。'); + const handle = importProgress(payload, '补齐救援 ISO 副本', async () => { + await loadCatalog(); notice('救援 ISO 副本已补齐。'); }); - row.append(name, remove, element('small', 'cloud-admin-meta', `${entry.size_bytes > 0 ? (Number(entry.size_bytes) / 1024 ** 3).toFixed(2) + ' GiB' : entry.id}`)); - list.append(row); + if (handle) notice(`副本补齐任务已接收(#${payload.task_id})。`); + } catch (error) { notice(error.message, true); button.disabled = !canManage; } + finally { state.busy = false; } +} +function rescueReplicaMatrix(entry) { + const details = element('details', 'cloud-template-matrix'); + const replicas = Array.isArray(entry.replicas) ? entry.replicas : []; + const totals = entry.replica_summary || {}; + const summary = element('summary', 'cloud-template-matrix-summary'); + const label = element('span'); + label.append( + element('strong', '', '宿主机副本'), + element('small', '', `${Number(totals.ready || 0)} / ${Number(totals.eligible || 0)} 台精确`), + ); + if (Number(totals.cleanup_debt || 0) > 0) label.append(element('small', 'text-danger', `${Number(totals.cleanup_debt)} 台待清理`)); + summary.append(label, element('span', 'cloud-template-matrix-arrow')); + details.append(summary); + if (!replicas.length) { + details.append(element('p', 'cloud-template-matrix-empty', '尚无宿主机记录。')); + return details; + } + const wrap = element('div', 'cloud-template-matrix-scroll'); + const table = element('table', 'cloud-template-matrix-table'); + const head = element('thead'); const headRow = element('tr'); + for (const title of ['宿主机', '目录 / 实际', '状态', '最近核对', '操作']) headRow.append(element('th', '', title)); + head.append(headRow); const body = element('tbody'); + for (const replica of replicas) { + const row = element('tr'); + const host = element('td', 'cloud-template-host-cell'); host.setAttribute('data-label', '宿主机'); + host.append(element('strong', '', replica.host_name || `宿主机 #${replica.host_id}`), element('small', '', replica.host_region || (replica.host_enabled ? '地区未设置' : '已停用'))); + const versions = element('td', 'cloud-template-version-cell'); versions.setAttribute('data-label', '目录 / 实际'); + versions.append( + element('strong', 'admin-mono', `r${Number(entry.revision || 0)} / r${Number(replica.revision || 0)}`), + element('small', 'admin-mono', `GEN ${compactIdentifier(replica.generation_token, 10) || '—'}`), + element('small', 'admin-mono', `OBS r${Number(replica.observed_revision || 0)} · ${compactIdentifier(replica.observed_generation_token, 10) || '—'}`), + ); + const status = element('td'); status.setAttribute('data-label', '状态'); + const statusContent = element('span', 'cloud-template-cell-content'); + const [statusLabel, statusClass] = rescueReplicaStateView(entry, replica); + statusContent.append(element('span', `cloud-template-state ${statusClass}`, statusLabel)); + const diagnostics = [replica.error_code, compactIdentifier(replica.operation_id)].filter(Boolean).join(' · '); + if (diagnostics) statusContent.append(element('small', 'cloud-template-diagnostic', diagnostics)); + status.append(statusContent); + const verified = element('td'); verified.setAttribute('data-label', '最近核对'); + verified.append(element('span', 'cloud-template-cell-content', catalogTime(replica.last_verified_at))); + const actions = element('td', 'cloud-template-replica-action'); actions.setAttribute('data-label', '操作'); + if (canManage && replica.repair_action === 'import') { + const repair = element('button', 'btn btn-sm btn-outline-primary', '补齐副本'); repair.type = 'button'; + repair.addEventListener('click', () => repairRescueReplica(entry, replica, repair)); actions.append(repair); + } else actions.append(element('span', 'text-muted', '—')); + row.append(host, versions, status, verified, actions); body.append(row); } + table.append(head, body); wrap.append(table); details.append(wrap); + return details; +} +function rescueTemplateCard(entry) { + const card = element('article', 'cloud-template-version-card'); + card.dataset.catalogState = String(entry.state || 'unknown').toLowerCase(); + const header = element('div', 'cloud-template-version-head'); + const identity = element('div', 'cloud-template-version-identity'); + const badges = element('div', 'cloud-template-version-badges'); + const [stateLabel, stateClass] = catalogStateView(entry.state); + badges.append(element('span', `cloud-template-state ${stateClass}`, stateLabel)); + identity.append(badges, element('strong', '', entry.name || entry.id)); + identity.append(element('small', '', `目录修订 r${Number(entry.revision || 0)} · ${catalogTime(entry.created_at)}`)); + const metadata = element('div', 'cloud-template-master-meta'); + const bytes = Number(entry.size_bytes || 0); + metadata.append( + element('span', '', bytes > 0 ? `${(bytes / 1024 ** 3).toFixed(2)} GiB` : '大小待确认'), + element('span', 'admin-mono', `SHA ${compactIdentifier(entry.sha256, 14) || '—'}`), + element('span', 'admin-mono', `GEN ${compactIdentifier(entry.generation_token, 10) || '—'}`), + element('span', 'admin-mono', `MASTER ${String(entry.artifact?.state || 'missing')}`), + ); + header.append(identity, metadata); + const actionBar = element('div', 'cloud-rescue-version-actions'); + const warning = element('p', 'cloud-rescue-version-note', String(entry.state || '') === 'retiring' + ? `正在清理:${Number(entry.replica_summary?.cleanup_debt || 0)} 台宿主机仍有清理债务。` + : '删除后会立即停止新挂载;已经挂载救援介质的实例必须先退出救援。'); + const remove = element('button', 'btn btn-outline-danger', String(entry.state || '') === 'retiring' ? '正在退役' : '退役并清理'); + remove.type = 'button'; remove.disabled = !canManage || String(entry.state || '') === 'retiring'; + remove.addEventListener('click', async () => { + if (state.busy || !confirm(`退役全局救援 ISO“${entry.name}”?系统会停止新挂载,并异步删除全部宿主机副本和对象存储精确版本。`)) return; + state.busy = true; remove.disabled = true; + try { + const result = await api(`${catalogPath()}/${encodeURIComponent(entry.id)}`, { + method: 'DELETE', body: { expected_revision: Number(entry.revision), generation_token: String(entry.generation_token || '') }, + }); + await loadCatalog(); + const scheduled = Array.isArray(result?.tasks) ? result.tasks.length : 0; + notice(scheduled > 0 ? `救援 ISO 已停止使用,并调度 ${scheduled} 个宿主机清理任务。` : '救援 ISO 已停止使用,后台将继续核对清理。'); + } catch (error) { notice(error.message, true); remove.disabled = !canManage; } + finally { state.busy = false; } + }); + actionBar.append(warning, remove); + card.append(header, actionBar, rescueReplicaMatrix(entry)); + return card; +} +function renderRescueTemplates(list, entries) { + for (const entry of entries) list.append(rescueTemplateCard(entry)); } async function loadCatalog() { const rescue = isRescueCatalog(); - const hostId = rescue ? byId('catalog-host')?.value || '' : ''; - state.selectedHost = hostId; const epoch = ++state.epoch; const list = byId('catalog-list'); list.replaceChildren(); - if (rescue && !hostId) return list.append(element('p', 'text-muted', '请先添加宿主机。')); - notice(rescue ? '正在读取所选宿主机的模版…' : '正在读取云镜像版本与副本矩阵…'); - const url = rescue ? `${catalogPath()}?host_id=${encodeURIComponent(hostId)}` : catalogPath(); + notice(rescue ? '正在读取救援 ISO 目录与副本矩阵…' : '正在读取云镜像版本与副本矩阵…'); try { - const payload = await api(url); if (epoch !== state.epoch) return; + const payload = await api(catalogPath()); if (epoch !== state.epoch) return; const entries = collection(payload, 'templates'); if (!entries.length) list.append(element('p', 'text-muted', '暂无模版。')); - else if (rescue) renderRescueTemplates(list, entries, hostId); + else if (rescue) renderRescueTemplates(list, entries); else renderCloudTemplates(list, entries); - if (rescue) notice(`已加载 ${entries.length} 个救援模版。`); + if (rescue) { + const available = entries.filter(entry => ['active', 'degraded'].includes(String(entry.state || ''))).length; + notice(`已加载 ${entries.length} 个救援 ISO:${available} 个可挂载,${entries.length - available} 个正在处理或需核查。`); + } else { const available = entries.filter(cloudTemplateAvailable).length; notice(`已加载 ${entries.length} 条云镜像记录:${available} 个可用,${entries.length - available} 个不可用。`); @@ -1120,11 +1235,35 @@ async function uploadCloudTemplate(event) { input.disabled = !canManage; } } -async function uploadRescue(event) { - event.preventDefault(); if (state.busy) return; const hostId = byId('catalog-host').value; const file = byId('rescue-file').files?.[0]; if (!hostId || !file) return notice('请选择宿主机和 ISO。', true); if (!file.name.toLowerCase().endsWith('.iso') || file.size > 8 * 1024 ** 3 || file.size < 32768) return notice('请选择小于 8 GiB 的有效 ISO 文件。', true); - const button = byId('rescue-upload-button'); state.busy = true; button.disabled = true; byId('catalog-host').disabled = true; notice('正在上传救援 ISO,请勿关闭页面…'); - try { const result = await api(`/api/v1/admin/rescue-templates/upload?host_id=${encodeURIComponent(hostId)}&name=${encodeURIComponent(byId('rescue-name').value.trim())}`, { method: 'POST', body: file, raw: true }); byId('rescue-file').value = ''; if (importProgress(result, '导入救援 ISO', async () => { await loadCatalog(); notice('救援 ISO 导入已完成。'); })) notice('任务已接收,正在后台导入救援 ISO。'); } - catch (error) { notice(error.message, true); } finally { state.busy = false; button.disabled = !canManage; byId('catalog-host').disabled = !canManage; } +async function importRescue(event) { + event.preventDefault(); + if (state.busy) return; + const name = byId('rescue-name').value.trim(); + const url = byId('rescue-url').value.trim(); + const sha256 = byId('rescue-sha256').value.trim().toLowerCase(); + if (!name || name.length > 64) return notice('请填写 1 至 64 个字符的模版名称。', true); + if (sha256 && !/^[a-f0-9]{64}$/.test(sha256)) return notice('SHA-256 可留空;如果填写,请输入完整的 64 位十六进制校验值。', true); + try { + const parsed = new URL(url); + if (parsed.protocol !== 'https:' || (parsed.port && parsed.port !== '443') || parsed.username || parsed.password || parsed.hash || !parsed.pathname.toLowerCase().endsWith('.iso')) throw new Error(); + } catch { return notice('请填写无需账号、使用 443 端口并以 .iso 结尾的 HTTPS 直链。', true); } + const body = { name, url }; if (sha256) body.sha256 = sha256; + const button = byId('rescue-import-button'); state.busy = true; button.disabled = true; + notice('正在隔离下载并保存救援 ISO,请勿重复提交…'); + try { + const result = await api(catalogPath(), { method: 'POST', body }); + byId('rescue-import').reset(); + if (result?.validation_scheduled === true && taskUrl(result)) { + const handle = importProgress(result, '首台宿主机校验', async () => { + await loadCatalog(); notice('首台宿主机校验完成,正在向其余宿主机分发。'); + }); + if (handle) notice(`救援 ISO 已进入隔离目录,首台校验任务已接收(#${result.task_id})。`); + } else { + await loadCatalog(); + notice('救援 ISO 已进入隔离目录;首台校验未立即调度,后台协调器会继续核对。'); + } + } catch (error) { notice(error.message, true); } + finally { state.busy = false; button.disabled = !canManage; } } async function initialize() { if (!root) return; @@ -1149,7 +1288,7 @@ async function initialize() { notice(canManage ? '请选择宿主机编辑,或添加新的宿主机。' : '当前管理员仅可查看配置。'); } else if (root.dataset.cloudAdmin === 'templates') { byId('template-import')?.addEventListener('submit', importTemplate); byId('template-upload')?.addEventListener('submit', uploadCloudTemplate); byId('template-catalog-refresh')?.addEventListener('click', loadCatalog); byId('template-startup-form').addEventListener('submit', saveStartupScript); byId('template-startup-close').addEventListener('click', closeStartupScript); byId('template-startup-script').addEventListener('input', updateStartupScriptControls); byId('template-startup-dialog').addEventListener('cancel', event => { event.preventDefault(); closeStartupScript(); }); await loadCatalog(); } - else if (root.dataset.cloudAdmin === 'rescue') { await loadHosts(); const select = byId('catalog-host'); select.replaceChildren(); for (const host of state.hosts) { const option = element('option', '', `${host.name} · ${host.region || '地区未设置'}`); option.value = String(host.id); select.append(option); } select.addEventListener('change', loadCatalog); byId('rescue-upload')?.addEventListener('submit', uploadRescue); await loadCatalog(); } + else if (root.dataset.cloudAdmin === 'rescue') { byId('rescue-import')?.addEventListener('submit', importRescue); byId('rescue-catalog-refresh')?.addEventListener('click', loadCatalog); await loadCatalog(); } } catch (error) { notice(error.message, true); } } void initialize(); diff --git a/web/resources/views/venix/admin/cloud/rescue.tpl b/web/resources/views/venix/admin/cloud/rescue.tpl index baa3db3b..dcb3184c 100644 --- a/web/resources/views/venix/admin/cloud/rescue.tpl +++ b/web/resources/views/venix/admin/cloud/rescue.tpl @@ -8,60 +8,55 @@

Recovery

救援模版

-

按宿主机维护独立的可启动救援 ISO。

+

维护全局救援 ISO 目录,并核对每台宿主机的精确副本。

正在加载…

-
-
-
-
-

Target host

-
选择宿主机
-

救援 ISO 只上传到当前选择的宿主机,不会混入普通安装 ISO。

-
-
- - - -
-
-
+
+ + ISO 会先进入对象存储隔离区,仅在首台宿主机完成哈希与启动校验后才发布并分发;删除会先停止新挂载,再异步清理全部宿主机副本和精确对象版本。
-
-
-
-
-
-

Upload

-
上传救援 ISO
-

救援系统自身决定登录方式。

+
+
+ +
+
+
+

Remote

+
从 HTTPS 直链导入
+

救援系统自身决定登录方式,面板不会注入实例密码或 SSH 公钥。

+
+
- -
-
-
-
只支持可启动 ISO,上传大小还受 PHP、Web 服务器及反代限制;完成前请勿关闭页面。
+
+
+
只接受无需账号、使用标准 443 端口并以 .iso 结尾的 HTTPS 地址,最大 8 GiB。
+
留空时记录下载内容的实际校验值;填写后会在上传对象存储前严格核对。
+
+
-
-
- + +
-
+

Catalog

-
救援 ISO 列表
-

这里仅显示当前宿主机已经保存的救援介质。

+
目录与宿主机副本
+

查看全局主副本、发布状态以及每台可用宿主机上的精确版本。

+
+
+ +
-
+

只有宿主机上修订号、生成标识、文件大小和 SHA-256 全部一致的副本才允许用户挂载;效果未知或内容分歧不会被自动覆盖。

diff --git a/web/tests/CloudRescueReplicaWorkflowTest.php b/web/tests/CloudRescueReplicaWorkflowTest.php index 6bc04126..617b7043 100644 --- a/web/tests/CloudRescueReplicaWorkflowTest.php +++ b/web/tests/CloudRescueReplicaWorkflowTest.php @@ -20,6 +20,7 @@ namespace { use App\Models\CloudRescueTemplateArtifact; use App\Models\CloudRescueTemplateReplica; use App\Services\Cloud\CloudObjectStorageS3Client; + use App\Services\Cloud\CloudRescueCatalogService; use App\Services\Cloud\CloudRescueTemplateReplicaCoordinator; use App\Services\Cloud\CloudRescueTemplateReconciliationService; use App\Services\Cloud\CloudRescueTemplateRetirementService; @@ -300,6 +301,25 @@ namespace { 'All exact replicas did not activate the catalog.'); $addHost(33); + $adminCatalog = CloudRescueCatalogService::adminCatalog(); + $adminEntry = $adminCatalog['templates'][0] ?? null; + $missingHost = is_array($adminEntry) + ? array_values(array_filter( + $adminEntry['replicas'], + static fn (array $replica): bool => + (int) $replica['host_id'] === 33 + ))[0] ?? null + : null; + $assert(count($adminCatalog['hosts']) === 3 + && is_array($adminEntry) + && (int) $adminEntry['replica_summary']['eligible'] === 3 + && (int) $adminEntry['replica_summary']['ready'] === 2 + && (int) $adminEntry['replica_summary']['attention'] === 1 + && is_array($missingHost) + && $missingHost['state'] === 'missing' + && $missingHost['repair_action'] === 'import' + && $missingHost['repair_reason'] === 'missing', + 'Administrator rescue catalog did not expose the exact host matrix.'); $remoteExact = $result; $clientFactory = static fn (int $hostId): object => new class( $hostId, diff --git a/web/tests/VirtualizationAssetsTest.php b/web/tests/VirtualizationAssetsTest.php index dc6eeaef..41b25736 100644 --- a/web/tests/VirtualizationAssetsTest.php +++ b/web/tests/VirtualizationAssetsTest.php @@ -652,22 +652,27 @@ $assert(str_contains($runtimeDeployScript, 'centosstream: "CentOS Stream"') && str_contains($runtimeDeployScript, '/^[0-9]+(?:\\.[0-9]+){0,2}$/'), 'Cloud image family icons or dotted Ubuntu versions are not mapped correctly.'); $importStart = strpos($adminScript, 'async function importTemplate('); -$importEnd = $importStart === false ? false : strpos($adminScript, 'async function uploadRescue(', $importStart); +$importEnd = $importStart === false ? false : strpos($adminScript, 'async function uploadCloudTemplate(', $importStart); $importScript = $importStart === false || $importEnd === false ? '' : substr($adminScript, $importStart, $importEnd - $importStart); $assert(!str_contains($templateAdmin, 'catalog-host') && str_contains($templateAdmin, '所有可售地区共用的 cloud-init 系统镜像目录') - && str_contains($rescueAdmin, 'id="catalog-host"') + && !str_contains($rescueAdmin, 'id="catalog-host"') + && !str_contains($rescueAdmin, 'type="file"') + && str_contains($rescueAdmin, 'id="rescue-import"') + && str_contains($rescueAdmin, 'id="rescue-url"') + && str_contains($rescueAdmin, 'id="rescue-sha256"') + && str_contains($rescueAdmin, '维护全局救援 ISO 目录') && str_contains($adminScript, "root.dataset.cloudAdmin === 'templates'") && str_contains($adminScript, "root.dataset.cloudAdmin === 'rescue'"), - 'Cloud images must be global while rescue templates remain host-scoped.'); + 'Cloud images and rescue ISOs must use global catalogs without a host selector.'); $assert(str_contains($importScript, 'if (sha256) body.sha256 = sha256;') && !str_contains($importScript, 'host_id') && !str_contains($importScript, 'catalog-host'), 'Global cloud image imports must omit blank SHA-256 and all host selectors.'); $uploadStart = strpos($adminScript, 'async function uploadCloudTemplate('); $uploadEnd = $uploadStart === false ? false - : strpos($adminScript, 'async function uploadRescue(', $uploadStart); + : strpos($adminScript, 'async function importRescue(', $uploadStart); $uploadScript = $uploadStart === false || $uploadEnd === false ? '' : substr($adminScript, $uploadStart, $uploadEnd - $uploadStart); $assert(str_contains($templateAdmin, 'id="template-upload"') @@ -685,6 +690,40 @@ $assert(str_contains($uploadScript, "file.name.toLowerCase().endsWith('.qcow2')" && str_contains($adminScript, "request.setRequestHeader('Content-Type', 'application/octet-stream')") && str_contains($adminScript, "request.upload.addEventListener('progress'"), 'Browser QCOW2 upload must stream raw bytes with explicit metadata and progress.'); +$rescueImportStart = strpos($adminScript, 'async function importRescue('); +$rescueImportEnd = $rescueImportStart === false ? false + : strpos($adminScript, 'async function initialize(', $rescueImportStart); +$rescueImportScript = $rescueImportStart === false || $rescueImportEnd === false + ? '' : substr( + $adminScript, + $rescueImportStart, + $rescueImportEnd - $rescueImportStart + ); +$assert(str_contains($rescueImportScript, 'if (sha256) body.sha256 = sha256;') + && str_contains($rescueImportScript, "parsed.protocol !== 'https:'") + && str_contains($rescueImportScript, "parsed.pathname.toLowerCase().endsWith('.iso')") + && str_contains($rescueImportScript, "api(catalogPath(), { method: 'POST', body })") + && str_contains($rescueImportScript, 'validation_scheduled === true') + && !str_contains($rescueImportScript, 'host_id') + && !str_contains($rescueImportScript, 'rescue-file'), + 'Global rescue ISO import must be HTTPS-only and independent of browser file uploads or host selection.'); +$assert(str_contains($cloudAdminController, + 'CloudRescueCatalogService::adminCatalog()') + && str_contains($cloudAdminController, + 'CloudRescueCatalogService::importRemote(') + && str_contains($cloudAdminController, + 'CloudRescueTemplateReplicaCoordinator::beginValidation(') + && str_contains($cloudAdminController, + 'CloudRescueTemplateReplicaCoordinator::syncHost(') + && str_contains($cloudRoutes, + '/admin/rescue-templates/{id}/replicas/{host_id:[1-9][0-9]*}/repair') + && str_contains($adminScript, + '/api/v1/admin/rescue-templates/${encodeURIComponent(entry.id)}/replicas/') + && str_contains($adminScript, + 'expected_revision: Number(entry.revision)') + && str_contains($adminScript, + "generation_token: String(entry.generation_token || '')"), + 'Global rescue catalog must expose exact host replicas, repair, and revisioned retirement.'); $assert(str_contains($cloudRoutes, "'/admin/cloud-templates/upload', 'App\\Controllers\\CloudAdminController:cloudTemplateUpload'") && str_contains($cloudAdminController, 'public function cloudTemplateUpload(')