- libraryagent:0700 目录内 0600 unix socket + SO_PEERCRED 同 uid, 固定 binary/归档/日志/项目/写入者声明,请求只能选范围与作业号 - API:POST/GET /api/v1/library/captures(入队 library.capture + 读队列状态) - worker:library.capture handler 走 socket;启动先 recover;不挂 Docker socket、 不持有来源凭据 - web:整库版本页显示真实作业状态并可发起封存(未接线/无权限给出原因) - scripts/library-agent-install.sh:构建、安装 systemd user 服务并读回权限
This commit is contained in:
+120
-4
@@ -1,11 +1,15 @@
|
||||
import { useCallback, useEffect, useMemo, useState } from 'react'
|
||||
import {
|
||||
enqueueLibraryCapture,
|
||||
fetchLibraryCaptures,
|
||||
fetchLibrarySnapshots,
|
||||
formatBytes,
|
||||
scopeLabel,
|
||||
type LibraryCaptureJob,
|
||||
type LibrarySnapshot,
|
||||
type LibrarySnapshotList,
|
||||
} from './librarySnapshots'
|
||||
import { useOptionalAuth } from './auth'
|
||||
import './libraryVersions.css'
|
||||
|
||||
// 「整库版本」页:只读展示已封存的整库候选产物(对内/对外各一份最新)。
|
||||
@@ -26,12 +30,24 @@ const stamp = (value: string) =>
|
||||
|
||||
const metric = (snapshot: LibrarySnapshot, key: string) => String(snapshot.totals[key] ?? 0)
|
||||
|
||||
const jobStateLabel: Record<string, string> = {
|
||||
pending: '待执行',
|
||||
running: '执行中',
|
||||
retry: '重试中',
|
||||
completed: '已完成',
|
||||
failed: '失败',
|
||||
}
|
||||
|
||||
export function LibraryVersions() {
|
||||
const auth = useOptionalAuth()
|
||||
const [page, setPage] = useState<LibrarySnapshotList | null>(null)
|
||||
const [loading, setLoading] = useState(true)
|
||||
const [error, setError] = useState<string | null>(null)
|
||||
const [expanded, setExpanded] = useState<string | null>(null)
|
||||
const [scopeFilter, setScopeFilter] = useState<'all' | 'internal' | 'external'>('all')
|
||||
const [jobs, setJobs] = useState<LibraryCaptureJob[]>([])
|
||||
const [pendingScope, setPendingScope] = useState<string | null>(null)
|
||||
const [notice, setNotice] = useState<string | null>(null)
|
||||
|
||||
const load = useCallback(async (signal?: AbortSignal) => {
|
||||
setLoading(true)
|
||||
@@ -53,6 +69,51 @@ export function LibraryVersions() {
|
||||
return () => controller.abort()
|
||||
}, [load])
|
||||
|
||||
const refreshJobs = useCallback(async (signal?: AbortSignal) => {
|
||||
const items = await fetchLibraryCaptures(signal)
|
||||
setJobs(items)
|
||||
return items
|
||||
}, [])
|
||||
|
||||
useEffect(() => {
|
||||
const controller = new AbortController()
|
||||
void refreshJobs(controller.signal).catch(() => undefined)
|
||||
return () => controller.abort()
|
||||
}, [refreshJobs])
|
||||
|
||||
// A capture runs in a maintenance window (seconds), so a short poll is enough
|
||||
// to show the real queue state and to pick up the new sealed version.
|
||||
const active = jobs.some((job) => job.state === 'pending' || job.state === 'running' || job.state === 'retry')
|
||||
useEffect(() => {
|
||||
if (!active) return
|
||||
const timer = window.setInterval(() => {
|
||||
void refreshJobs()
|
||||
.then((items) => {
|
||||
if (!items.some((job) => job.state === 'pending' || job.state === 'running' || job.state === 'retry')) {
|
||||
void load()
|
||||
}
|
||||
})
|
||||
.catch(() => undefined)
|
||||
}, 3000)
|
||||
return () => window.clearInterval(timer)
|
||||
}, [active, refreshJobs, load])
|
||||
|
||||
const canCapture = (auth?.identity?.role === 'engineer' || auth?.identity?.role === 'admin') && page?.capture_available === true
|
||||
|
||||
const startCapture = async (scope: string) => {
|
||||
setPendingScope(scope)
|
||||
setNotice(null)
|
||||
try {
|
||||
const accepted = await enqueueLibraryCapture(scope)
|
||||
setNotice(`已投递封存作业:${scopeLabel(scope)} · ${accepted.capture_job}(等待主机侧执行)`)
|
||||
await refreshJobs()
|
||||
} catch (captureError) {
|
||||
setNotice(captureError instanceof Error ? captureError.message : '发起封存失败')
|
||||
} finally {
|
||||
setPendingScope(null)
|
||||
}
|
||||
}
|
||||
|
||||
const snapshots = page?.snapshots ?? []
|
||||
const latestByScope = useMemo(() => {
|
||||
const map: Record<string, LibrarySnapshot | undefined> = {}
|
||||
@@ -144,11 +205,32 @@ export function LibraryVersions() {
|
||||
</p>
|
||||
</>
|
||||
) : (
|
||||
<p className="library-scope-hint">
|
||||
该库没有封存产物。封存在目标主机执行:
|
||||
<code>scripts/library-capture.sh capture -scope {scope}</code>
|
||||
</p>
|
||||
<p className="library-scope-hint">该库还没有封存产物。</p>
|
||||
)}
|
||||
<div className="library-scope-actions">
|
||||
{page?.capture_available ? (
|
||||
canCapture ? (
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => void startCapture(scope)}
|
||||
disabled={
|
||||
pendingScope === scope ||
|
||||
jobs.some(
|
||||
(job) =>
|
||||
job.scope === scope &&
|
||||
(job.state === 'pending' || job.state === 'running' || job.state === 'retry')
|
||||
)
|
||||
}
|
||||
>
|
||||
{pendingScope === scope ? '投递中…' : `发起${scopeLabel(scope)}封存`}
|
||||
</button>
|
||||
) : (
|
||||
<span>发起封存需要知识工程师及以上权限</span>
|
||||
)
|
||||
) : (
|
||||
<span>{page?.capture_reason}</span>
|
||||
)}
|
||||
</div>
|
||||
</article>
|
||||
)
|
||||
})}
|
||||
@@ -239,6 +321,40 @@ export function LibraryVersions() {
|
||||
</table>
|
||||
)}
|
||||
|
||||
{notice ? <p className="library-capture-notice">{notice}</p> : null}
|
||||
|
||||
{jobs.length > 0 ? (
|
||||
<div className="library-capture-jobs">
|
||||
<h3>封存作业</h3>
|
||||
<table className="library-versions-table">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>投递时间</th>
|
||||
<th>范围</th>
|
||||
<th>状态</th>
|
||||
<th>作业号</th>
|
||||
<th>发起人</th>
|
||||
<th>最后错误</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{jobs.map((job) => (
|
||||
<tr key={job.job_id}>
|
||||
<td>{stamp(job.created_at)}</td>
|
||||
<td>{scopeLabel(job.scope)}</td>
|
||||
<td>{jobStateLabel[job.state] ?? job.state}</td>
|
||||
<td>
|
||||
<code>{job.capture_job.slice(0, 24)}</code>
|
||||
</td>
|
||||
<td>{job.requested_by || '—'}</td>
|
||||
<td className="library-capture-error">{job.last_error || '—'}</td>
|
||||
</tr>
|
||||
))}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
) : null}
|
||||
|
||||
{visible
|
||||
.filter((snapshot) => snapshot.snapshot_id === expanded)
|
||||
.map((snapshot) => (
|
||||
|
||||
@@ -37,6 +37,25 @@ export interface LibrarySnapshot {
|
||||
totals: Record<string, number>
|
||||
}
|
||||
|
||||
export interface LibraryCaptureJob {
|
||||
job_id: string
|
||||
capture_job: string
|
||||
scope: string
|
||||
state: string
|
||||
attempt: number
|
||||
last_error: string
|
||||
requested_by: string
|
||||
created_at: string
|
||||
updated_at: string
|
||||
}
|
||||
|
||||
export interface LibraryCaptureAccepted {
|
||||
job_id: string
|
||||
capture_job: string
|
||||
scope: string
|
||||
state: string
|
||||
}
|
||||
|
||||
export interface LibrarySnapshotList {
|
||||
available: boolean
|
||||
reason: string
|
||||
@@ -53,6 +72,8 @@ export interface LibrarySnapshotList {
|
||||
manifest_file_bytes: number
|
||||
manifest_allocated_bytes: number
|
||||
capture_note: string
|
||||
capture_available: boolean
|
||||
capture_reason: string
|
||||
}
|
||||
|
||||
const record = (value: unknown): value is Record<string, unknown> =>
|
||||
@@ -131,9 +152,55 @@ export async function fetchLibrarySnapshots(signal?: AbortSignal): Promise<Libra
|
||||
manifest_file_bytes: num(value.manifest_file_bytes),
|
||||
manifest_allocated_bytes: num(value.manifest_allocated_bytes),
|
||||
capture_note: text(value.capture_note),
|
||||
capture_available: bool(value.capture_available),
|
||||
capture_reason: text(value.capture_reason),
|
||||
}
|
||||
}
|
||||
|
||||
// 页面发起的封存只投递作业;停写入者、导出与恢复都在主机侧 agent 执行。
|
||||
export async function enqueueLibraryCapture(scope: string): Promise<LibraryCaptureAccepted> {
|
||||
const response = await fetch('/api/v1/library/captures', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json', Accept: 'application/json' },
|
||||
body: JSON.stringify({ scope }),
|
||||
})
|
||||
const value = (await response.json().catch(() => null)) as unknown
|
||||
if (!response.ok) {
|
||||
if (response.status === 403) throw new Error('只有知识工程师及以上可以发起封存。')
|
||||
if (response.status === 503 && record(value)) {
|
||||
throw new Error(text(value.reason) || '主机侧封存 agent 未接线')
|
||||
}
|
||||
throw new Error(`发起封存失败(HTTP ${response.status})`)
|
||||
}
|
||||
if (!record(value)) throw new Error('封存作业响应无效')
|
||||
return {
|
||||
job_id: text(value.job_id),
|
||||
capture_job: text(value.capture_job),
|
||||
scope: text(value.scope),
|
||||
state: text(value.state),
|
||||
}
|
||||
}
|
||||
|
||||
export async function fetchLibraryCaptures(signal?: AbortSignal): Promise<LibraryCaptureJob[]> {
|
||||
const response = await fetch('/api/v1/library/captures', {
|
||||
headers: { Accept: 'application/json' },
|
||||
signal,
|
||||
})
|
||||
const value = (await response.json().catch(() => null)) as unknown
|
||||
if (!response.ok || !record(value) || !Array.isArray(value.jobs)) return []
|
||||
return value.jobs.filter(record).map((job) => ({
|
||||
job_id: text(job.job_id),
|
||||
capture_job: text(job.capture_job),
|
||||
scope: text(job.scope),
|
||||
state: text(job.state),
|
||||
attempt: num(job.attempt),
|
||||
last_error: text(job.last_error),
|
||||
requested_by: text(job.requested_by),
|
||||
created_at: text(job.created_at),
|
||||
updated_at: text(job.updated_at),
|
||||
}))
|
||||
}
|
||||
|
||||
export function formatBytes(bytes: number): string {
|
||||
if (!Number.isFinite(bytes) || bytes <= 0) return '0 B'
|
||||
const units = ['B', 'KB', 'MB', 'GB', 'TB']
|
||||
|
||||
@@ -165,6 +165,50 @@
|
||||
max-width: 70ch;
|
||||
line-height: 1.5;
|
||||
}
|
||||
.library-scope-actions {
|
||||
margin-top: 14px;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 10px;
|
||||
font-size: 12px;
|
||||
color: var(--ink-mute, #64748b);
|
||||
}
|
||||
.library-scope-actions button {
|
||||
padding: 7px 14px;
|
||||
border-radius: 8px;
|
||||
border: 1px solid #2563eb;
|
||||
background: #eff6ff;
|
||||
color: #1d4ed8;
|
||||
cursor: pointer;
|
||||
}
|
||||
.library-scope-actions button:disabled {
|
||||
cursor: progress;
|
||||
opacity: 0.6;
|
||||
}
|
||||
.library-capture-notice {
|
||||
margin: 0;
|
||||
padding: 10px 14px;
|
||||
border-radius: 8px;
|
||||
background: #eff6ff;
|
||||
border: 1px solid #bfdbfe;
|
||||
color: #1d4ed8;
|
||||
font-size: 13px;
|
||||
}
|
||||
.library-capture-jobs {
|
||||
background: var(--bg-panel, #fff);
|
||||
border: 1px solid var(--border-base, #e2e8f0);
|
||||
border-radius: 10px;
|
||||
padding: 16px 20px;
|
||||
}
|
||||
.library-capture-jobs h3 {
|
||||
margin: 0 0 10px;
|
||||
font-size: 15px;
|
||||
}
|
||||
.library-capture-error {
|
||||
max-width: 360px;
|
||||
overflow-wrap: anywhere;
|
||||
white-space: normal;
|
||||
}
|
||||
.library-versions-accounting {
|
||||
justify-content: flex-start;
|
||||
font-size: 12px;
|
||||
|
||||
Reference in New Issue
Block a user