feat: 本地镜像支持导入 tar/tar.gz
This commit is contained in:
1 parent
1791e94028
commit
4ade1439a3
3 files changed
+64
No files matched your search
@@ -1135,6 +1135,30 @@ app.get('/api/images', async (req, res) => {
|
||||
} catch (e) { res.status(500).json({ error: e.message }); }
|
||||
});
|
||||
|
||||
app.post('/api/images/import', (req, res) => {
|
||||
const sock = process.env.DOCKER_SOCK || '/var/run/docker.sock';
|
||||
const up = http.request({
|
||||
socketPath: sock, path: '/images/load?quiet=0', method: 'POST',
|
||||
headers: { 'Content-Type': 'application/x-tar', 'Transfer-Encoding': 'chunked' },
|
||||
}, (r) => {
|
||||
let buf = '';
|
||||
r.on('data', (c) => { buf += c.toString('utf8'); });
|
||||
r.on('end', () => {
|
||||
const errLine = buf.split(/\r?\n/).map(l => { try { return JSON.parse(l); } catch (_) { return null; } }).find(o => o && (o.error || o.errorDetail));
|
||||
if (r.statusCode !== 200 || errLine) {
|
||||
const msg = errLine ? ((errLine.errorDetail && errLine.errorDetail.message) || errLine.error) : buf.slice(-300);
|
||||
return res.status(400).json({ error: /invalid tar header|unexpected EOF/i.test(String(msg)) ? '文件不是有效的镜像包(需要 docker save 导出的 tar 或 tar.gz)' : '导入失败:' + msg });
|
||||
}
|
||||
const loaded = [...buf.matchAll(/Loaded image:?\s*([^\s"\\]+)/g)].map(m => m[1]);
|
||||
pushEvent('info', null, `导入镜像 ${loaded.length} 个`, loaded.slice(0, 3).join('、'));
|
||||
res.json({ ok: true, loaded, count: loaded.length, raw: buf.slice(-800) });
|
||||
});
|
||||
});
|
||||
up.on('error', (e) => { if (!res.headersSent) res.status(500).json({ error: e.message }); else res.end(); });
|
||||
req.on('aborted', () => up.destroy());
|
||||
req.pipe(up);
|
||||
});
|
||||
|
||||
app.get('/api/images/export', async (req, res) => {
|
||||
try {
|
||||
const ids = String(req.query.ids || '').split(',').map(x => x.trim()).filter(Boolean).slice(0, 40);
|
||||
|
||||
Reference in new issue
Block a user