85 lines
1.9 KiB
JavaScript
85 lines
1.9 KiB
JavaScript
const { Client } = require('ssh2');
|
|
const fs = require('fs');
|
|
|
|
const config = {
|
|
host: 'hs.yifenbao.cn',
|
|
port: 22,
|
|
username: 'root',
|
|
password: 'Torch1112'
|
|
};
|
|
|
|
const conn = new Client();
|
|
|
|
conn.on('ready', () => {
|
|
console.log('SSH连接成功');
|
|
|
|
conn.sftp((err, sftp) => {
|
|
if (err) {
|
|
console.error('SFTP错误:', err.message);
|
|
conn.end();
|
|
return;
|
|
}
|
|
|
|
const content = fs.readFileSync('docker-compose.yml', 'utf8');
|
|
const ws = sftp.createWriteStream('/www/huishou/docker-compose.yml', { mode: 0o644 });
|
|
|
|
ws.on('close', () => {
|
|
console.log('docker-compose.yml已上传');
|
|
sftp.end();
|
|
|
|
const cmds = [
|
|
'docker stop huishou huishou-backend 2>/dev/null',
|
|
'docker rm huishou huishou-backend 2>/dev/null',
|
|
'cd /www/huishou && docker-compose up -d',
|
|
'sleep 15 && docker ps | grep huishou'
|
|
];
|
|
|
|
let i = 0;
|
|
|
|
function execNext() {
|
|
if (i >= cmds.length) {
|
|
conn.end();
|
|
console.log('\n部署完成');
|
|
return;
|
|
}
|
|
|
|
conn.exec(cmds[i], (err, stream) => {
|
|
if (err) {
|
|
console.error('错误:', err.message);
|
|
execNext();
|
|
return;
|
|
}
|
|
|
|
i++;
|
|
|
|
stream.stdout.on('data', (data) => {
|
|
console.log(data.toString().trim());
|
|
});
|
|
|
|
stream.stderr.on('data', (data) => {
|
|
console.log('stderr:', data.toString().trim());
|
|
});
|
|
|
|
stream.on('close', () => {
|
|
execNext();
|
|
});
|
|
});
|
|
}
|
|
|
|
execNext();
|
|
});
|
|
|
|
ws.write(content);
|
|
ws.end();
|
|
});
|
|
});
|
|
|
|
conn.on('error', (err) => {
|
|
console.error('SSH连接错误:', err.message);
|
|
});
|
|
|
|
conn.on('end', () => {
|
|
console.log('SSH连接已关闭');
|
|
});
|
|
|
|
conn.connect(config); |