59 lines
2.5 KiB
JavaScript
59 lines
2.5 KiB
JavaScript
const { Client } = require('ssh2');
|
|
const fs = require('fs');
|
|
const { exec } = require('child_process');
|
|
|
|
const conn = new Client();
|
|
|
|
exec(`cd dist/build && tar -czf ../../h5.tar.gz h5`, (tarErr) => {
|
|
if (tarErr) { console.log('打包失败:', tarErr); return; }
|
|
console.log('=== 打包完成 ===');
|
|
|
|
conn.on('ready', () => {
|
|
console.log('=== 上传tar包 ===');
|
|
|
|
const content = fs.readFileSync('h5.tar.gz');
|
|
conn.sftp((err, sftp) => {
|
|
if (err) { console.log('SFTP ERR:', err); conn.end(); return; }
|
|
|
|
const writeStream = sftp.createWriteStream('/tmp/h5.tar.gz');
|
|
writeStream.write(content);
|
|
writeStream.end();
|
|
|
|
writeStream.on('close', () => {
|
|
sftp.end();
|
|
console.log('tar包上传成功');
|
|
|
|
console.log('\n=== 解压到容器内 ===');
|
|
conn.exec('docker cp /tmp/h5.tar.gz huishou:/tmp/h5.tar.gz', (err2, stream2) => {
|
|
if (err2) { console.log('ERR2:', err2); conn.end(); return; }
|
|
stream2.on('close', () => {
|
|
conn.exec('docker exec huishou sh -c "rm -rf /usr/share/nginx/html/h5/* && tar -xzf /tmp/h5.tar.gz -C /usr/share/nginx/html && rm /tmp/h5.tar.gz"', (err3, stream3) => {
|
|
if (err3) { console.log('ERR3:', err3); conn.end(); return; }
|
|
stream3.on('data', (d) => process.stdout.write(d.toString()));
|
|
stream3.on('close', () => {
|
|
console.log('\n=== 验证容器内h5目录 ===');
|
|
conn.exec('docker exec huishou ls -la /usr/share/nginx/html/h5/', (err4, stream4) => {
|
|
if (err4) { console.log('ERR4:', err4); conn.end(); return; }
|
|
stream4.on('data', (d) => console.log(d.toString()));
|
|
stream4.on('close', () => {
|
|
console.log('\n=== 测试网站 ===');
|
|
conn.exec('curl -s http://127.0.0.1:4001/ | head -3', (err5, stream5) => {
|
|
if (err5) { console.log('ERR5:', err5); conn.end(); return; }
|
|
stream5.on('data', (d) => console.log(d.toString()));
|
|
stream5.on('close', () => {
|
|
console.log('\n✅ 修复完成!');
|
|
conn.end();
|
|
});
|
|
});
|
|
});
|
|
});
|
|
});
|
|
});
|
|
});
|
|
});
|
|
});
|
|
});
|
|
});
|
|
|
|
conn.connect({ host: 'hs.yifenbao.cn', port: 22, username: 'root', password: 'Torch1112' });
|
|
}); |