feat: 镜像仓库页支持批量 Compose 部署(指定目录/项目名/ENV 参数)

This commit is contained in:
jinghui committed 2026-10-26 23:13:22 +08:00
1 parent 573faf4139
commit 610226ff4c
4 files changed
+163 -2

No files matched your search

+51 -1
View File
@@ -430,7 +430,7 @@ app.get('/api/host', async (req, res) => {
loadavg: os.loadavg(), memTotal, memFree, swapTotal, swapFree, disk,
containers: info.Containers, running: info.ContainersRunning,
paused: info.ContainersPaused, stopped: info.ContainersStopped, pending, build: BUILD_ID,
images: info.Images, driver: info.Driver, lastError,
images: info.Images, driver: info.Driver, lastError, composeRoot: COMPOSE_ROOT,
});
} catch (e) { res.status(500).json({ error: e.message }); }
});
@@ -1280,6 +1280,56 @@ app.post('/api/images/pull', async (req, res) => {
const BUILTIN_NETS = ['bridge', 'host', 'none'];
/* ---------------- Compose 批量部署 ---------------- */
function safeSubdir(dir) {
const d = String(dir || '').trim().replace(/^\/+|\/+$/g, '');
if (!d) return null;
if (d.includes('..') || d.includes('//')) return null; // 明确拒绝,不做静默改写
if (!/^[A-Za-z0-9._\-\/]+$/.test(d)) return null;
return d;
}
app.post('/api/compose/deploy', async (req, res) => {
try {
const items = Array.isArray(req.body && req.body.items) ? req.body.items.slice(0, 20) : [];
if (!items.length) return res.status(400).json({ error: '没有可部署的 Compose 项目' });
const results = [];
for (const it of items) {
const dir = safeSubdir(it.dir);
if (!dir) { results.push({ dir: it.dir, ok: false, error: '目录名不合法(只允许字母数字 . _ - 和子路径)' }); continue; }
const compose = String(it.compose || '');
if (!/^\s*(services|version)\s*:/m.test(compose)) { results.push({ dir, ok: false, error: '内容不像 Compose 文件(缺少 services:)' }); continue; }
const target = path.join(COMPOSE_ROOT, dir);
const projName = String(it.project || path.basename(dir)).toLowerCase().replace(/[^a-z0-9_-]/g, '');
try {
await fsp.mkdir(target, { recursive: true });
await fsp.writeFile(path.join(target, 'docker-compose.yml'), compose.endsWith('\n') ? compose : compose + '\n');
const envLines = Object.entries(it.env || {}).filter(([k]) => /^[A-Za-z_][A-Za-z0-9_]*$/.test(k)).map(([k, v]) => `${k}=${String(v)}`);
if (projName) envLines.push(`COMPOSE_PROJECT_NAME=${projName}`);
if (envLines.length) await fsp.writeFile(path.join(target, '.env'), envLines.join('\n') + '\n');
const env = { ...process.env };
if (/^[a-z0-9][a-z0-9_-]*$/.test(projName)) env.COMPOSE_PROJECT_NAME = projName;
const out = await new Promise((resolve) => {
execFile('docker', ['compose', '-f', path.join(target, 'docker-compose.yml'), 'up', '-d'], {
cwd: target, timeout: 300000, maxBuffer: 4 * 1024 * 1024, env,
}, (err, stdout, stderr) => resolve({
ok: !err,
output: `${stdout || ''}${stderr || ''}`.trim().split('\n').slice(-10).join('\n'),
}));
});
results.push({ dir, project: projName, ok: out.ok, output: out.output, error: out.ok ? '' : '启动失败,见输出' });
} catch (e) {
results.push({ dir, ok: false, error: e.message });
}
}
const ok = results.filter(r => r.ok).length;
pendingCache.at = 0;
inspectAll().catch(() => {});
pushEvent('info', null, `批量部署 Compose 项目 ${ok} 个`, `失败 ${results.length - ok} 个`);
res.json({ ok: true, deployed: ok, results });
} catch (e) { res.status(500).json({ error: e.message }); }
});
app.get('/api/networks', async (req, res) => {
try {
const nets = await docker.listNetworks();