Add dashboard backup catalog and health tools
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import { NextResponse } from "next/server"
|
||||
import { backupSandboxArchive } from "@/app/lib/sandboxFiles"
|
||||
import { recordActivity } from "@/app/lib/activityLog"
|
||||
|
||||
function contentDisposition(fileName: string) {
|
||||
const fallback = fileName.replace(/[^\w.-]/g, "_") || "sandbox-backup.tar.gz"
|
||||
@@ -15,6 +16,14 @@ export async function GET(
|
||||
const requestUrl = new URL(request.url)
|
||||
const sourcePath = requestUrl.searchParams.get("path") || "/sandbox"
|
||||
const backup = await backupSandboxArchive(sandboxId, sourcePath)
|
||||
await recordActivity({
|
||||
type: "backup.download",
|
||||
status: "success",
|
||||
sandboxId,
|
||||
sandboxName: backup.sandboxName,
|
||||
message: `Created downloadable backup ${backup.fileName}.`,
|
||||
metadata: { sourcePath: backup.sourcePath, size: backup.bytes.byteLength },
|
||||
}).catch(() => undefined)
|
||||
|
||||
return new NextResponse(new Uint8Array(backup.bytes), {
|
||||
status: 200,
|
||||
@@ -30,6 +39,11 @@ export async function GET(
|
||||
})
|
||||
} catch (error) {
|
||||
const message = error instanceof Error ? error.message : "Failed to create sandbox backup"
|
||||
await recordActivity({
|
||||
type: "backup.download",
|
||||
status: "error",
|
||||
message,
|
||||
}).catch(() => undefined)
|
||||
return NextResponse.json({ ok: false, error: message }, { status: /required|path|large|exist|directory/.test(message) ? 400 : 500 })
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,95 @@
|
||||
import { NextResponse } from "next/server"
|
||||
import { execOpenShell, normalizeSandboxPhase, resolveSandboxRef } from "@/app/lib/openshellHost"
|
||||
import { listBackupCatalog } from "@/app/lib/backupCatalog"
|
||||
|
||||
function parseField(output: string, label: string) {
|
||||
const normalizedLabel = label.toLowerCase()
|
||||
const line = output
|
||||
.split(/\r?\n/)
|
||||
.map((entry) => entry.trim())
|
||||
.find((entry) => entry.toLowerCase().startsWith(`${normalizedLabel}:`))
|
||||
|
||||
return line ? line.slice(label.length + 1).trim() : null
|
||||
}
|
||||
|
||||
export async function GET(
|
||||
_request: Request,
|
||||
{ params }: { params: Promise<{ sandboxId: string }> },
|
||||
) {
|
||||
const startedAt = Date.now()
|
||||
try {
|
||||
const { sandboxId } = await params
|
||||
const resolved = await resolveSandboxRef(sandboxId)
|
||||
const phase = normalizeSandboxPhase(parseField(resolved.details, "Phase"))
|
||||
const catalog = await listBackupCatalog()
|
||||
const relatedBackups = catalog.filter((backup) => backup.sandboxId === sandboxId || backup.sandboxName === resolved.name)
|
||||
|
||||
let sshConfig = ""
|
||||
let sshConfigError = ""
|
||||
try {
|
||||
const result = await execOpenShell(["sandbox", "ssh-config", resolved.name])
|
||||
sshConfig = result.stdout.trim()
|
||||
} catch (error) {
|
||||
sshConfigError = error instanceof Error ? error.message : "ssh-config unavailable"
|
||||
}
|
||||
|
||||
const checks = [
|
||||
{
|
||||
key: "resolve",
|
||||
label: "OpenShell resolution",
|
||||
ok: true,
|
||||
detail: `${resolved.name} resolved by ${resolved.resolvedBy}.`,
|
||||
},
|
||||
{
|
||||
key: "phase",
|
||||
label: "Sandbox phase",
|
||||
ok: /running|ready/i.test(phase),
|
||||
detail: phase,
|
||||
},
|
||||
{
|
||||
key: "ssh",
|
||||
label: "SSH config",
|
||||
ok: sshConfig.length > 0,
|
||||
detail: sshConfig ? "Host alias is available." : sshConfigError || "No SSH config returned.",
|
||||
},
|
||||
{
|
||||
key: "backup",
|
||||
label: "Catalog backup",
|
||||
ok: relatedBackups.length > 0,
|
||||
detail: relatedBackups.length > 0
|
||||
? `${relatedBackups.length} saved backup${relatedBackups.length === 1 ? "" : "s"} in catalog.`
|
||||
: "No saved catalog backups yet.",
|
||||
},
|
||||
]
|
||||
|
||||
return NextResponse.json({
|
||||
ok: true,
|
||||
sandbox: {
|
||||
requested: resolved.requested,
|
||||
id: resolved.id,
|
||||
name: resolved.name,
|
||||
phase,
|
||||
},
|
||||
checks,
|
||||
backupCount: relatedBackups.length,
|
||||
durationMs: Date.now() - startedAt,
|
||||
checkedAt: new Date().toISOString(),
|
||||
})
|
||||
} catch (error) {
|
||||
const message = error instanceof Error ? error.message : "Failed to check sandbox health"
|
||||
return NextResponse.json({
|
||||
ok: false,
|
||||
error: message,
|
||||
checks: [
|
||||
{
|
||||
key: "resolve",
|
||||
label: "OpenShell resolution",
|
||||
ok: false,
|
||||
detail: message,
|
||||
},
|
||||
],
|
||||
durationMs: Date.now() - startedAt,
|
||||
checkedAt: new Date().toISOString(),
|
||||
}, { status: 500 })
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
import { NextResponse } from "next/server"
|
||||
import { restoreSandboxArchive } from "@/app/lib/sandboxFiles"
|
||||
import { recordActivity } from "@/app/lib/activityLog"
|
||||
|
||||
export async function POST(
|
||||
request: Request,
|
||||
@@ -19,6 +20,14 @@ export async function POST(
|
||||
const replace = rawReplace === "true" || rawReplace === "1"
|
||||
const payload = Buffer.from(await file.arrayBuffer())
|
||||
const restored = await restoreSandboxArchive(sandboxId, targetPath, file.name, payload, replace)
|
||||
await recordActivity({
|
||||
type: "backup.upload.restore",
|
||||
status: "success",
|
||||
sandboxId,
|
||||
sandboxName: restored.sandboxName,
|
||||
message: `Restored uploaded archive ${file.name} into ${restored.targetPath}.`,
|
||||
metadata: { targetPath: restored.targetPath, mode: restored.mode, bytes: restored.bytes },
|
||||
}).catch(() => undefined)
|
||||
|
||||
return NextResponse.json({
|
||||
ok: true,
|
||||
@@ -27,6 +36,11 @@ export async function POST(
|
||||
})
|
||||
} catch (error) {
|
||||
const message = error instanceof Error ? error.message : "Failed to restore sandbox backup"
|
||||
await recordActivity({
|
||||
type: "backup.upload.restore",
|
||||
status: "error",
|
||||
message,
|
||||
}).catch(() => undefined)
|
||||
return NextResponse.json({ ok: false, error: message }, { status: /required|path|large|unsafe|archive/.test(message) ? 400 : 500 })
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user