-
${esc(p.name)}${p.services} 个服务
+
${esc(p.name)}${p.services} 个服务
${esc(p.file.replace((pend.root || '') + '/', ''))}
${p.images.missing.length
? `缺 ${p.images.missing.length} 个镜像 · 自动拉取`
@@ -668,6 +675,53 @@ async function renderContainers(silent, mode = 'running') {
if (el) { el.disabled = false; el.textContent = el.dataset.txt || '启动'; }
}
};
+ const pendNames = (pend && pend.items ? pend.items : []).map(x => x.name);
+ [...state.pendSel].forEach(n => { if (!pendNames.includes(n)) state.pendSel.delete(n); });
+ const syncPend = () => {
+ const n = $('#pendSelCount'); if (n) n.textContent = state.pendSel.size;
+ const s1 = $('#pendStartSel'); if (s1) { s1.disabled = !state.pendSel.size; s1.textContent = state.pendSel.size ? `批量启动 ${state.pendSel.size} 个` : '批量启动'; }
+ const d1 = $('#pendDelSel'); if (d1) { d1.disabled = !state.pendSel.size; d1.textContent = state.pendSel.size ? `批量删除 ${state.pendSel.size} 个` : '批量删除'; }
+ const a = $('#pendSelAll'); if (a) a.checked = state.pendSel.size > 0 && state.pendSel.size === pendNames.length;
+ };
+ $$('#view .pendsel').forEach(cb => {
+ cb.checked = state.pendSel.has(cb.dataset.name);
+ cb.onclick = (e) => { e.stopPropagation(); if (cb.checked) state.pendSel.add(cb.dataset.name); else state.pendSel.delete(cb.dataset.name); syncPend(); };
+ });
+ const psa = $('#pendSelAll');
+ if (psa) psa.onclick = () => {
+ if (psa.checked) pendNames.forEach(n => state.pendSel.add(n)); else state.pendSel.clear();
+ $$('#view .pendsel').forEach(cb => { cb.checked = psa.checked; });
+ syncPend();
+ };
+ const psb = $('#pendStartSel');
+ if (psb) psb.onclick = async () => {
+ const list = [...state.pendSel];
+ if (!list.length) return;
+ psb.disabled = true; psb.textContent = '启动中…';
+ try {
+ const r = await api('/api/pending-apps/batch-start', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ names: list }) });
+ const fail = r.results.filter(x => !x.ok);
+ toast(fail.length ? `已启动 ${r.started} 个,失败 ${fail.length} 个` : `已启动 ${r.started} 个应用`);
+ state.pendSel.clear();
+ setTimeout(render, 1200);
+ } catch (e) { toast(e.message, true); psb.disabled = false; psb.textContent = '批量启动'; }
+ };
+ const pdb = $('#pendDelSel');
+ if (pdb) pdb.onclick = async () => {
+ const list = [...state.pendSel];
+ if (!list.length) return;
+ if (!confirm(`确定删除选中的 ${list.length} 个未启动应用?\n\n目录会移到回收站 .trash(可恢复),不删除镜像。`)) return;
+ pdb.disabled = true; pdb.textContent = '删除中…';
+ try {
+ const r = await api('/api/pending-apps/delete', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ names: list }) });
+ const fail = r.results.filter(x => !x.ok);
+ toast(fail.length ? `已删除 ${r.deleted} 个,失败 ${fail.length} 个(${fail[0].error})` : `已删除 ${r.deleted} 个应用(已移入 .trash 回收站)`);
+ state.pendSel.clear();
+ setTimeout(render, 900);
+ } catch (e) { toast(e.message, true); pdb.disabled = false; pdb.textContent = '批量删除'; }
+ };
+ syncPend();
+
$$('#view [data-pend]').forEach(b => b.onclick = (e) => { e.stopPropagation(); startPend(b.dataset.pend, b); });
$$('#view [data-pend-card]').forEach(card => {
card.onclick = (e) => {
diff --git a/public/style.css b/public/style.css
index 01bbaba..65a2a4d 100644
--- a/public/style.css
+++ b/public/style.css
@@ -360,3 +360,11 @@ table.tb th.num{text-align:right}
.img-batch{background:#f4f8ff;border-color:#d6e4f7;color:#4a5a70}
.img-batch .bsel{color:#1a5fbf}
.img-batch b{color:#1a73e8}
+
+/* 未启动应用批量条 */
+.pend-batch{margin-bottom:10px;flex-wrap:wrap}
+.pend-batch .btn{margin-left:0}
+.pend-batch .btn.primary{margin-left:auto}
+.pend-chk{display:flex;align-items:center;margin-right:2px}
+.pend-chk input{width:14px;height:14px;cursor:pointer;accent-color:#1a73e8}
+.pend-top{gap:6px}
diff --git a/server/index.js b/server/index.js
index 4b9109b..0924a30 100644
--- a/server/index.js
+++ b/server/index.js
@@ -676,29 +676,104 @@ app.get('/api/pending-apps', async (req, res) => {
} catch (e) { res.status(500).json({ error: e.message }); }
});
+const TRASH_DIR = path.join(COMPOSE_ROOT, '.trash'); // 与 compose 目录同一文件系统,rename 即时完成
+
+async function startPendingProject(target) {
+ const projName = String(target.name).toLowerCase().replace(/[^a-z0-9_-]/g, '');
+ 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', target.file, 'up', '-d'], {
+ cwd: target.dir, timeout: 240000, maxBuffer: 2 * 1024 * 1024, env,
+ }, (err, stdout, stderr) => resolve({
+ ok: !err, code: err ? (err.code || 1) : 0,
+ output: `${stdout || ''}${stderr || ''}`.trim().split('\n').slice(-14).join('\n'),
+ }));
+ });
+ return out;
+}
+
app.post('/api/pending-apps/start', async (req, res) => {
const name = String((req.body && req.body.name) || '');
try {
const items = await scanPending(true);
const target = items.find(i => i.name === name);
if (!target) return res.status(404).json({ error: '未找到该项目(可能已经有容器在内)' });
- const projName = target.name.toLowerCase().replace(/[^a-z0-9_-]/g, '');
- 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', target.file, 'up', '-d'], {
- cwd: target.dir, timeout: 240000, maxBuffer: 2 * 1024 * 1024, env,
- }, (err, stdout, stderr) => resolve({
- ok: !err, code: err ? (err.code || 1) : 0,
- output: `${stdout || ''}${stderr || ''}`.trim().split('\n').slice(-14).join('\n'),
- }));
- });
+ const out = await startPendingProject(target);
pendingCache.at = 0;
inspectAll().catch(() => {});
res.json(out);
} catch (e) { res.status(500).json({ error: e.message }); }
});
+app.post('/api/pending-apps/batch-start', async (req, res) => {
+ try {
+ const names = Array.isArray(req.body && req.body.names) ? req.body.names.filter(n => typeof n === 'string').slice(0, 20) : [];
+ if (!names.length) return res.status(400).json({ error: '未选择应用' });
+ const items = await scanPending(true);
+ const results = [];
+ for (const name of names) {
+ const target = items.find(i => i.name === name);
+ if (!target) { results.push({ name, ok: false, error: '未找到该项目(可能已经有容器在内)' }); continue; }
+ try {
+ const out = await startPendingProject(target);
+ results.push({ name, ok: out.ok, output: out.output, error: out.ok ? '' : '启动失败,见输出' });
+ } catch (e) { results.push({ name, ok: false, error: e.message }); }
+ }
+ pendingCache.at = 0;
+ inspectAll().catch(() => {});
+ const ok = results.filter(r => r.ok).length;
+ pushEvent('info', null, `批量启动应用 ${ok} 个`, `失败 ${results.length - ok} 个`);
+ res.json({ ok: true, started: ok, results });
+ } catch (e) { res.status(500).json({ error: e.message }); }
+});
+
+app.post('/api/pending-apps/delete', async (req, res) => {
+ try {
+ const names = Array.isArray(req.body && req.body.names) ? req.body.names.filter(n => typeof n === 'string').slice(0, 20) : [];
+ const purge = !!(req.body && req.body.purge);
+ if (!names.length) return res.status(400).json({ error: '未选择应用' });
+ const items = await scanPending(true);
+ const results = [];
+ for (const name of names) {
+ const target = items.find(i => i.name === name);
+ if (!target) { results.push({ name, ok: false, error: '未找到该项目(可能已经有容器在内)' }); continue; }
+ try {
+ const conts = await docker.listContainers({ all: true });
+ const holders = conts.filter(c => (c.Labels || {})['com.docker.compose.project'] === String(name).toLowerCase()
+ || (c.Labels || {})['com.docker.compose.project'] === name);
+ if (holders.length) {
+ results.push({ name, ok: false, error: `该项目还有 ${holders.length} 个容器,请先删除容器` });
+ continue;
+ }
+ if (!target.dir.startsWith(COMPOSE_ROOT + path.sep)) {
+ results.push({ name, ok: false, error: '目录不在扫描根内,已跳过' });
+ continue;
+ }
+ if (purge) {
+ await fsp.rm(target.dir, { recursive: true, force: true });
+ results.push({ name, ok: true, moved: '' });
+ } else {
+ await fsp.mkdir(TRASH_DIR, { recursive: true });
+ const dest = path.join(TRASH_DIR, `${name}-${new Date().toISOString().replace(/[:.]/g, '-')}`);
+ try {
+ await fsp.rename(target.dir, dest);
+ } catch (err) {
+ if (err.code !== 'EXDEV') throw err;
+ await fsp.cp(target.dir, dest, { recursive: true });
+ await fsp.rm(target.dir, { recursive: true, force: true });
+ }
+ results.push({ name, ok: true, moved: dest });
+ }
+ } catch (e) { results.push({ name, ok: false, error: e.message }); }
+ }
+ pendingCache.at = 0;
+ const ok = results.filter(r => r.ok).length;
+ pushEvent('warn', null, `${purge ? '彻底删除' : '删除(移入回收站)'}应用 ${ok} 个`, '');
+ res.json({ ok: true, deleted: ok, results });
+ } catch (e) { res.status(500).json({ error: e.message }); }
+});
+
/* ---------------- 接口:优化建议 ---------------- */
async function getDf(force) {
if (!force && dfCache.data && Date.now() - dfCache.at < 60000) return dfCache.data;