68 lines
2.9 KiB
JavaScript
68 lines
2.9 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('=== 打包完成 ===');
|
|
|
|
exec(`tar -tzf h5.tar.gz | head -10`, (listErr, stdout) => {
|
|
if (listErr) { console.log('列目录失败:', listErr); return; }
|
|
console.log('tar包内容:', stdout);
|
|
|
|
conn.on('ready', () => {
|
|
console.log('\n=== 上传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=== 解压到宿主机build目录 ===');
|
|
conn.exec('rm -rf /www/huishou/dist/build/h5 && cd /www/huishou/dist/build && tar -xzf /tmp/h5.tar.gz && rm /tmp/h5.tar.gz', (err2, stream2) => {
|
|
if (err2) { console.log('ERR2:', err2); conn.end(); return; }
|
|
stream2.on('data', (d) => process.stdout.write(d.toString()));
|
|
stream2.on('close', () => {
|
|
console.log('\n=== 验证宿主机h5目录 ===');
|
|
conn.exec('ls -la /www/huishou/dist/build/h5/', (err3, stream3) => {
|
|
if (err3) { console.log('ERR3:', err3); conn.end(); return; }
|
|
stream3.on('data', (d) => console.log(d.toString()));
|
|
stream3.on('close', () => {
|
|
console.log('\n=== 重启Docker容器 ===');
|
|
conn.exec('cd /www/huishou && docker-compose restart nginx', (err4, stream4) => {
|
|
if (err4) { console.log('ERR4:', err4); conn.end(); return; }
|
|
stream4.on('data', (d) => process.stdout.write(d.toString()));
|
|
stream4.on('close', () => {
|
|
setTimeout(() => {
|
|
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();
|
|
});
|
|
});
|
|
}, 5000);
|
|
});
|
|
});
|
|
});
|
|
});
|
|
});
|
|
});
|
|
});
|
|
});
|
|
});
|
|
|
|
conn.connect({ host: 'hs.yifenbao.cn', port: 22, username: 'root', password: 'Torch1112' });
|
|
});
|
|
}); |