feat: split online update into explicit stages
Release / release (push) Canceled after 0s

This commit is contained in:
Qiufeng
2026-08-18 09:01:34 +08:00
parent 3c7847c8f5
commit 6d4ae00ae0
5 changed files with 246 additions and 84 deletions
+1 -1
View File
@@ -272,7 +272,7 @@
"pageType": "business",
"pageTemplate": "system-update",
"requiresAuth": true,
"description": "查看当前版本、获取签名更新包、立即重启并查询历史更新记录。",
"description": "查看当前版本、独立获取版本信息、显式下载签名更新包、下载完成后安装重启并查询历史更新记录。",
"breadcrumb": ["系统治理", "系统更新"],
"roles": ["SYSTEM_ADMIN"],
"permission": "admin:update:view"
+102 -21
View File
@@ -7,7 +7,7 @@ function envelope(data: unknown) {
return { data, requestId: '01M00000000000000000000090' };
}
function session() {
function session(canExecute = true) {
return {
authenticated: true,
user: {
@@ -19,12 +19,13 @@ function session() {
},
roles: [{ code: 'SYSTEM_ADMIN', name: '系统管理员', workbenchRoute: '/governance/settings' }],
activeRole: 'SYSTEM_ADMIN',
permissions,
permissions: canExecute ? permissions : permissions.filter((permission) => permission !== 'admin:update:execute'),
};
}
function updateView(state: string, checked: boolean, enabled = true, recoveryPending = false) {
const busy = [
'QUEUED',
'DOWNLOAD_QUEUED',
'INSTALL_QUEUED',
'VERIFYING',
@@ -69,6 +70,12 @@ function updateView(state: string, checked: boolean, enabled = true, recoveryPen
};
}
interface FixtureOptions {
canExecute?: boolean;
failDownloadResponse?: boolean;
failInstallResponse?: boolean;
}
async function installFixture(
page: Page,
downloadBodies: unknown[],
@@ -78,7 +85,9 @@ async function installFixture(
completeAfterInstall = false,
initialState?: string,
completeBusyAfterFirstRead = false,
options: FixtureOptions = {},
) {
const { canExecute = true, failDownloadResponse = false, failInstallResponse = false } = options;
let checked = recoveryPending || Boolean(initialState);
let state = recoveryPending ? 'FAILED' : initialState || 'IDLE';
const history: Array<Record<string, unknown>> = [];
@@ -93,7 +102,7 @@ async function installFixture(
objectType: 'SYSTEM_UPDATE',
objectPublicId: 'SYSTEM_UPDATE',
resultCode: 'SUCCESS',
reason: actionCode === 'SYSTEM_UPDATE_REQUEST' ? '管理员确认立即重启' : null,
reason: actionCode === 'SYSTEM_UPDATE_REQUEST' ? '管理员确认立即更新并重启' : null,
beforeJson: null,
afterJson: JSON.stringify({ targetVersion }),
occurredAt: '2026-08-16T00:02:00Z',
@@ -103,9 +112,9 @@ async function installFixture(
await page.route('**/api/v1/**', async (route: Route) => {
const request = route.request();
const pathname = new URL(request.url()).pathname;
if (pathname === '/api/v1/auth/session') return route.fulfill({ json: envelope(session()) });
if (pathname === '/api/v1/auth/profile') return route.fulfill({ json: envelope(session().user) });
if (pathname === '/api/v1/auth/roles') return route.fulfill({ json: envelope(session().roles) });
if (pathname === '/api/v1/auth/session') return route.fulfill({ json: envelope(session(canExecute)) });
if (pathname === '/api/v1/auth/profile') return route.fulfill({ json: envelope(session(canExecute).user) });
if (pathname === '/api/v1/auth/roles') return route.fulfill({ json: envelope(session(canExecute).roles) });
if (pathname === '/api/v1/auth/csrf') {
return route.fulfill({
headers: { 'set-cookie': 'XSRF-TOKEN=update-csrf; Path=/; SameSite=Lax' },
@@ -129,19 +138,23 @@ async function installFixture(
if (pathname === '/api/v1/admin/system-update/check' && request.method() === 'POST') {
checked = true;
recordHistory('SYSTEM_UPDATE_CHECK');
return route.fulfill({ json: envelope(updateView(state, checked, enabled, recoveryPending)) });
const response = updateView(state, checked, enabled, recoveryPending);
if (!canExecute) response.allowedActions = ['CHECK'];
return route.fulfill({ json: envelope(response) });
}
if (pathname === '/api/v1/admin/system-update/download' && request.method() === 'POST') {
downloadBodies.push(request.postDataJSON());
recordHistory('SYSTEM_UPDATE_DOWNLOAD_REQUEST');
state = 'READY';
if (failDownloadResponse) return route.abort('connectionreset');
return route.fulfill({ json: envelope(updateView('DOWNLOAD_QUEUED', true, enabled)) });
}
if (pathname === '/api/v1/admin/system-update/install' && request.method() === 'POST') {
installBodies.push(request.postDataJSON());
recordHistory('SYSTEM_UPDATE_REQUEST');
const queued = updateView('INSTALL_QUEUED', true, enabled);
state = completeAfterInstall ? 'SUCCEEDED' : 'INSTALL_QUEUED';
state = failInstallResponse || completeAfterInstall ? 'SUCCEEDED' : 'INSTALL_QUEUED';
if (failInstallResponse) return route.abort('connectionreset');
return route.fulfill({ json: envelope(queued) });
}
if (pathname === '/api/v1/audit/logs' && request.method() === 'GET') {
@@ -158,7 +171,7 @@ async function installFixture(
});
}
test('system administrator checks and queues a signed online update', async ({ page }) => {
test('system administrator checks, downloads, and explicitly installs a signed online update', async ({ page }) => {
const downloadBodies: unknown[] = [];
const installBodies: unknown[] = [];
await installFixture(page, downloadBodies, installBodies);
@@ -166,16 +179,77 @@ test('system administrator checks and queues a signed online update', async ({ p
await expect(page.locator('h1.page-title-sr-only')).toHaveText('系统更新');
await expect(page.locator('.version-item').filter({ hasText: '当前版本' })).toContainText('1.0.0-preview.1');
await page.getByRole('button', { name: '获取更新', exact: true }).click();
await page.getByRole('button', { name: '获取版本', exact: true }).click();
await expect(page.locator('.version-item').filter({ hasText: '最新版本' })).toContainText('1.0.0-preview.2');
await expect(page.getByRole('button', { name: '立即重启', exact: true })).toBeVisible({ timeout: 5_000 });
expect(downloadBodies).toEqual([{ version: '1.0.0-preview.2' }]);
await expect(page.getByText('下载更新包', { exact: true })).toBeVisible();
await expect(page.getByRole('button', { name: '立即更新', exact: true })).toBeVisible({ timeout: 5_000 });
await expect(page.getByRole('button', { name: '立即更新并重启', exact: true })).toHaveCount(0);
expect(downloadBodies).toEqual([]);
await page.getByRole('button', { name: '立即重启', exact: true }).click();
await page.getByRole('button', { name: '立即更新', exact: true }).click();
expect(downloadBodies).toEqual([{ version: '1.0.0-preview.2' }]);
const updateState = page.locator('.version-item').filter({ hasText: '更新状态' });
await expect(updateState.getByText('下载已排队', { exact: true })).toBeVisible();
await expect(updateState.getByText('下载完成', { exact: true })).toBeVisible({ timeout: 8_000 });
await expect(page.getByRole('button', { name: '立即更新并重启', exact: true })).toBeVisible();
await page.getByRole('button', { name: '立即更新并重启', exact: true }).click();
await expect(page.getByText('安装请求已排队', { exact: true })).toBeVisible();
expect(installBodies).toEqual([{ version: '1.0.0-preview.2', reason: '管理员确认立即重启' }]);
expect(installBodies).toEqual([{ version: '1.0.0-preview.2', reason: '管理员确认立即更新并重启' }]);
});
test('rechecking a downloaded package prompts installation instead of downloading again', async ({ page }) => {
await installFixture(page, [], [], true, false, false, 'READY');
await page.goto('/governance/update');
await page.getByRole('button', { name: '获取版本', exact: true }).click();
await expect(page.getByText('更新包已下载完成,请点击“立即更新并重启”安装新版本', { exact: true })).toBeVisible();
await expect(page.getByRole('button', { name: '立即更新', exact: true })).toHaveCount(0);
await expect(page.getByRole('button', { name: '立即更新并重启', exact: true })).toBeVisible();
});
test('view-only administrators see a download permission message', async ({ page }) => {
await installFixture(page, [], [], true, false, false, undefined, false, { canExecute: false });
await page.goto('/governance/update');
await page.getByRole('button', { name: '获取版本', exact: true }).click();
await expect(page.getByText('发现新版本,但当前账号没有下载权限', { exact: true })).toBeVisible();
await expect(page.getByRole('button', { name: '立即更新', exact: true })).toHaveCount(0);
await expect(page.getByRole('button', { name: '立即更新并重启', exact: true })).toHaveCount(0);
});
test('download and install continue polling after an accepted command loses its response', async ({ page }) => {
await installFixture(page, [], [], true, false, false, undefined, false, {
failDownloadResponse: true,
});
await page.goto('/governance/update');
await page.getByRole('button', { name: '获取版本', exact: true }).click();
await page.getByRole('button', { name: '立即更新', exact: true }).click();
await expect(
page.locator('.version-item').filter({ hasText: '更新状态' }).getByText('下载完成', { exact: true }),
).toBeVisible({ timeout: 8_000 });
await expect(page.getByRole('button', { name: '立即更新并重启', exact: true })).toBeVisible();
});
test('accepted install continues to the refresh countdown after its response is lost', async ({ page }) => {
await installFixture(page, [], [], true, false, false, undefined, false, {
failInstallResponse: true,
});
await page.goto('/governance/update');
await page.getByRole('button', { name: '获取版本', exact: true }).click();
await page.getByRole('button', { name: '立即更新', exact: true }).click();
await expect(
page.locator('.version-item').filter({ hasText: '更新状态' }).getByText('下载完成', { exact: true }),
).toBeVisible({ timeout: 8_000 });
await page.getByRole('button', { name: '立即更新并重启', exact: true }).click();
await expect(page.getByText('新版本已通过健康检查,页面将在 10 秒后自动刷新。', { exact: true })).toBeVisible({
timeout: 8_000,
});
});
test('brand logo returns the active identity to its workbench', async ({ page }) => {
@@ -193,8 +267,9 @@ test('disabled online updates expose status without an executable check action',
await page.goto('/governance/update');
await expect(page.getByText('在线更新未配置', { exact: true })).toBeVisible();
await expect(page.getByRole('button', { name: '获取更新', exact: true })).toBeDisabled();
await expect(page.getByRole('button', { name: '立即重启', exact: true })).toHaveCount(0);
await expect(page.getByRole('button', { name: '获取版本', exact: true })).toBeDisabled();
await expect(page.getByRole('button', { name: '立即更新', exact: true })).toHaveCount(0);
await expect(page.getByRole('button', { name: '立即更新并重启', exact: true })).toHaveCount(0);
});
test('rollback recovery keeps the failure visible without an install action', async ({ page }) => {
@@ -202,15 +277,19 @@ test('rollback recovery keeps the failure visible without an install action', as
await page.goto('/governance/update');
await expect(page.getByText('回滚未完成,更新服务将重试', { exact: true })).toBeVisible();
await expect(page.getByRole('button', { name: '立即重启', exact: true })).toHaveCount(0);
await expect(page.getByRole('button', { name: '立即更新并重启', exact: true })).toHaveCount(0);
});
test('successful installation starts the ten-second automatic refresh countdown', async ({ page }) => {
await installFixture(page, [], [], true, false, true);
await page.goto('/governance/update');
await page.getByRole('button', { name: '获取更新', exact: true }).click();
await page.getByRole('button', { name: '立即重启', exact: true }).click({ timeout: 5_000 });
await page.getByRole('button', { name: '获取版本', exact: true }).click();
await page.getByRole('button', { name: '立即更新', exact: true }).click({ timeout: 5_000 });
await expect(
page.locator('.version-item').filter({ hasText: '更新状态' }).getByText('下载完成', { exact: true }),
).toBeVisible({ timeout: 8_000 });
await page.getByRole('button', { name: '立即更新并重启', exact: true }).click();
await expect(page.getByText('新版本已通过健康检查,页面将在 10 秒后自动刷新。', { exact: true })).toBeVisible({
timeout: 5_000,
@@ -221,7 +300,9 @@ test('reloading during installation resumes polling and starts the refresh count
await installFixture(page, [], [], true, false, false, 'INSTALLING', true);
await page.goto('/governance/update');
await expect(page.getByText('安装中', { exact: true })).toBeVisible();
await expect(
page.locator('.version-item').filter({ hasText: '更新状态' }).getByText('安装中', { exact: true }),
).toBeVisible();
await expect(page.getByText('新版本已通过健康检查,页面将在 10 秒后自动刷新。', { exact: true })).toBeVisible({
timeout: 6_000,
});
@@ -29,30 +29,40 @@
<div class="update-actions">
<t-button
variant="outline"
:loading="fetchingUpdate"
:disabled="!canFetchUpdate || updateBusy || restarting"
@click="fetchUpdate"
:loading="checkingUpdate"
:disabled="!canCheckUpdate || checkingUpdate || downloadingUpdate || installingUpdate"
@click="checkLatest"
>
<template #icon><t-icon name="cloud-download" /></template>
获取更新
<template #icon><t-icon name="refresh" /></template>
获取版本
</t-button>
<t-button
v-if="canRestart"
v-if="canDownloadUpdate"
theme="primary"
:loading="restarting"
:disabled="updateBusy || fetchingUpdate"
@click="restartNow"
:loading="downloadingUpdate"
:disabled="checkingUpdate || downloadingUpdate || installingUpdate"
@click="downloadNow"
>
<template #icon><t-icon name="cloud-download" /></template>
立即更新
</t-button>
<t-button
v-if="canInstall"
theme="primary"
:loading="installingUpdate"
:disabled="checkingUpdate || downloadingUpdate || installingUpdate"
@click="installNow"
>
<template #icon><t-icon name="poweroff" /></template>
立即重启
立即更新并重启
</t-button>
</div>
</div>
<t-steps :current="updateStep" :layout="stepLayout" status="process" class="update-steps">
<t-step-item title="获取版本" content="检查最新发布" />
<t-step-item title="下载校验" content="下载并验证签名" />
<t-step-item title="立即重启" content="备份并切换版本" />
<t-step-item title="下载更新" content="下载并验证签名" />
<t-step-item title="安装重启" content="备份并切换版本" />
<t-step-item title="完成更新" content="健康检查后刷新" />
</t-steps>
@@ -66,7 +76,11 @@
新版本已通过健康检查,页面将在 {{ updateRefreshSeconds }} 秒后自动刷新。
</t-alert>
<t-alert v-else :theme="updateStatus?.enabled === false ? 'warning' : 'info'" :close-btn="false">
{{ updateStatus?.message || '正在读取系统版本状态' }}
<div class="update-status-message">
<strong>{{ updateStateLabel }}</strong>
<span>{{ updateStatus?.message || '正在读取系统版本状态' }}</span>
<small v-if="updateActionHint">{{ updateActionHint }}</small>
</div>
</t-alert>
<div v-if="updateStatus?.releaseNotes" class="release-notes">
@@ -133,8 +147,9 @@ type TagTheme = 'default' | 'primary' | 'success' | 'warning' | 'danger';
const updateStatus = ref<SystemUpdateView | null>(null);
const isNarrow = ref(false);
const statusLoading = ref(false);
const fetchingUpdate = ref(false);
const restarting = ref(false);
const checkingUpdate = ref(false);
const downloadingUpdate = ref(false);
const installingUpdate = ref(false);
const statusError = ref('');
const historyRows = ref<AuditLog[]>([]);
const historyLoading = ref(false);
@@ -167,14 +182,27 @@ const currentIsLatest = computed(
!updateBusy.value &&
!['FAILED', 'UNKNOWN'].includes(updateStatus.value.state),
);
const canFetchUpdate = computed(
const canCheckUpdate = computed(
() =>
updateStatus.value?.enabled !== false &&
updateStatus.value?.state !== 'READY' &&
(updateStatus.value?.allowedActions.includes('CHECK') === true ||
updateStatus.value?.allowedActions.includes('DOWNLOAD') === true),
!updateBusy.value &&
updateStatus.value?.allowedActions.includes('CHECK') === true,
);
const canDownloadUpdate = computed(
() =>
updateStatus.value?.enabled !== false &&
!updateBusy.value &&
Boolean(updateStatus.value?.latestVersion) &&
updateStatus.value?.updateAvailable === true &&
updateStatus.value?.allowedActions.includes('DOWNLOAD') === true,
);
const canInstall = computed(
() =>
updateStatus.value?.enabled !== false &&
!updateBusy.value &&
updateStatus.value?.state === 'READY' &&
updateStatus.value?.allowedActions.includes('INSTALL') === true,
);
const canRestart = computed(() => updateStatus.value?.allowedActions.includes('INSTALL') === true);
const updateStep = computed(() => {
const state = updateStatus.value?.state || 'IDLE';
if (state === 'SUCCEEDED' || state === 'CURRENT' || currentIsLatest.value) return 3;
@@ -190,11 +218,11 @@ const updateStateLabel = computed(() => {
DISABLED: '未启用',
CURRENT: '已是最新',
QUEUED: '已排队',
DOWNLOAD_QUEUED: '等待下载',
DOWNLOAD_QUEUED: '下载已排队',
INSTALL_QUEUED: '等待重启',
VERIFYING: '校验中',
DOWNLOADING: '下载中',
READY: '等待重启',
READY: '下载完成',
BACKING_UP: '备份中',
INSTALLING: '安装中',
RUNNING: '重启中',
@@ -206,6 +234,20 @@ const updateStateLabel = computed(() => {
'待获取'
);
});
const updateActionHint = computed(() => {
const state = updateStatus.value?.state || 'IDLE';
if (state === 'READY') {
return canInstall.value ? '下载完成,请点击“立即更新并重启”安装新版本。' : '下载完成,但当前账号没有安装权限。';
}
if (canDownloadUpdate.value) return '已发现新版本,请点击“立即更新”开始下载。';
if (updateStatus.value?.updateAvailable) return '已发现新版本,但当前账号没有下载权限。';
if (['DOWNLOAD_QUEUED', 'VERIFYING', 'DOWNLOADING'].includes(state)) return '更新包正在下载和校验,请稍候。';
if (['INSTALL_QUEUED', 'BACKING_UP', 'INSTALLING', 'RUNNING'].includes(state)) {
return '更新包已确认,系统正在备份、切换并重启。';
}
if (state === 'IDLE' && !updateStatus.value?.latestVersion) return '点击“获取版本”检查最新发布。';
return '';
});
const updateStateTheme = computed<TagTheme>(() => {
const state = updateStatus.value?.state || 'IDLE';
if (['SUCCEEDED', 'CURRENT', 'READY'].includes(state) || currentIsLatest.value) return 'success';
@@ -233,7 +275,7 @@ async function loadStatus(silent = false) {
updateStatus.value = status;
if (installStates.has(status.state)) awaitingRefresh.value = true;
handleCompletion(status);
if (busyStates.has(status.state)) startPolling();
if (busyStates.has(status.state) || downloadingUpdate.value || installingUpdate.value) startPolling();
else stopPolling();
} catch (error) {
if (!silent) statusError.value = friendlyError(error, '系统版本状态读取失败');
@@ -266,9 +308,9 @@ async function refreshPage() {
await Promise.all([loadStatus(), loadHistory()]);
}
async function fetchUpdate() {
if (fetchingUpdate.value || updateBusy.value || !canFetchUpdate.value) return;
fetchingUpdate.value = true;
async function checkLatest() {
if (checkingUpdate.value || updateBusy.value || !canCheckUpdate.value) return;
checkingUpdate.value = true;
statusError.value = '';
try {
const checked = await checkSystemUpdate();
@@ -278,36 +320,61 @@ async function fetchUpdate() {
MessagePlugin.info('当前已是最新版本');
return;
}
if (!checked.latestVersion || !checked.allowedActions.includes('DOWNLOAD')) {
throw new Error('发现新版本,但当前账号没有下载更新包的权限');
if (checked.state === 'READY') {
MessagePlugin.success(
checked.allowedActions.includes('INSTALL')
? '更新包已下载完成,请点击“立即更新并重启”安装新版本'
: '更新包已下载完成,但当前账号没有安装权限',
);
} else if (!checked.allowedActions.includes('DOWNLOAD')) {
MessagePlugin.info('发现新版本,但当前账号没有下载权限');
} else {
MessagePlugin.success(`发现新版本 ${checked.latestVersion},请点击“立即更新”开始下载`);
}
updateStatus.value = await downloadSystemUpdate(checked.latestVersion);
await loadHistory(true);
MessagePlugin.success('更新包已开始下载,下载和签名校验完成后可立即重启');
startPolling();
} catch (error) {
statusError.value = friendlyError(error, '获取更新失败');
statusError.value = friendlyError(error, '获取版本失败');
} finally {
fetchingUpdate.value = false;
checkingUpdate.value = false;
}
}
async function restartNow() {
async function downloadNow() {
const version = updateStatus.value?.latestVersion;
if (!version || restarting.value || !canRestart.value) return;
restarting.value = true;
if (!version || downloadingUpdate.value || !canDownloadUpdate.value) return;
downloadingUpdate.value = true;
statusError.value = '';
startPolling();
try {
updateStatus.value = await downloadSystemUpdate(version);
MessagePlugin.success('更新包已开始下载,完成后可点击“立即更新并重启”');
void loadHistory(true);
} catch (error) {
statusError.value = friendlyError(error, '下载更新包失败');
void loadStatus(true);
} finally {
downloadingUpdate.value = false;
startPolling();
}
}
async function installNow() {
const version = updateStatus.value?.latestVersion;
if (!version || installingUpdate.value || !canInstall.value) return;
installingUpdate.value = true;
statusError.value = '';
stopRefreshCountdown();
awaitingRefresh.value = true;
startPolling();
try {
updateStatus.value = await installSystemUpdate(version, '管理员确认立即重启');
awaitingRefresh.value = true;
MessagePlugin.success('重启任务已提交,系统恢复后页面将自动刷新');
await loadHistory(true);
startPolling();
updateStatus.value = await installSystemUpdate(version, '管理员确认立即更新并重启');
MessagePlugin.success('更新任务已提交,系统完成切换并重启后页面将自动刷新');
void loadHistory(true);
} catch (error) {
statusError.value = friendlyError(error, '立即重启失败');
statusError.value = friendlyError(error, '立即更新并重启失败');
void loadStatus(true);
} finally {
restarting.value = false;
installingUpdate.value = false;
startPolling();
}
}
@@ -324,6 +391,7 @@ function stopPolling() {
function handleCompletion(status: SystemUpdateView) {
if (!awaitingRefresh.value) return;
if (status.state === 'SUCCEEDED') {
statusError.value = '';
awaitingRefresh.value = false;
startRefreshCountdown();
void loadHistory(true);
@@ -374,7 +442,7 @@ function actionLabel(action: string) {
{
SYSTEM_UPDATE_CHECK: '获取版本',
SYSTEM_UPDATE_DOWNLOAD_REQUEST: '下载更新包',
SYSTEM_UPDATE_REQUEST: '立即重启',
SYSTEM_UPDATE_REQUEST: '立即更新并重启',
}[action] || action
);
}
@@ -503,6 +571,17 @@ onBeforeUnmount(() => {
border-top: 1px solid var(--td-component-border);
}
.update-status-message {
display: flex;
flex-wrap: wrap;
align-items: baseline;
gap: 4px 10px;
}
.update-status-message small {
color: var(--td-text-color-secondary);
}
.release-notes span,
.section-heading p,
.section-heading > span {