Files
huishou/start-containers.js
T
2026-07-27 14:07:26 +08:00

86 lines
2.1 KiB
JavaScript

const { Client } = require('ssh2');
const config = {
host: 'hs.yifenbao.cn',
port: 22,
username: 'root',
password: 'Torch1112'
};
const conn = new Client();
conn.on('ready', () => {
console.log('SSH连接成功');
conn.exec('cd /www/huishou && docker-compose up -d --build', (err, stream) => {
if (err) {
console.error('执行命令错误:', err.message);
conn.end();
return;
}
let output = '';
stream.stdout.on('data', (data) => {
output += data.toString();
process.stdout.write(data);
});
stream.stderr.on('data', (data) => {
output += data.toString();
process.stdout.write(data);
});
stream.on('close', () => {
console.log('容器构建并启动完成');
setTimeout(() => {
conn.exec('docker ps | grep huishou', (err, stream) => {
if (err) {
console.error('执行命令错误:', err.message);
conn.end();
return;
}
let output2 = '';
stream.stdout.on('data', (data) => {
output2 += data.toString();
});
stream.on('close', () => {
console.log('容器状态:');
console.log(output2);
conn.exec('curl -s http://localhost:4000/api/prices', (err, stream) => {
if (err) {
console.error('执行命令错误:', err.message);
conn.end();
return;
}
let output3 = '';
stream.stdout.on('data', (data) => {
output3 += data.toString();
});
stream.on('close', () => {
console.log('后端API测试:');
console.log(output3);
conn.end();
});
});
});
});
}, 10000);
});
});
});
conn.on('error', (err) => {
console.error('SSH连接错误:', err.message);
});
conn.on('end', () => {
console.log('SSH连接已关闭');
});
conn.connect(config);