128 lines
3.2 KiB
JavaScript
128 lines
3.2 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连接成功\n');
|
|
|
|
let step = 1;
|
|
|
|
function run(cmd, desc) {
|
|
console.log(`=== ${step}. ${desc} ===`);
|
|
step++;
|
|
return new Promise((resolve) => {
|
|
conn.exec(cmd, (err, stream) => {
|
|
if (err) {
|
|
console.error('错误:', err.message);
|
|
resolve(false);
|
|
return;
|
|
}
|
|
|
|
let output = '';
|
|
stream.stdout.on('data', (data) => { output += data.toString(); });
|
|
stream.stderr.on('data', (data) => { output += data.toString(); });
|
|
|
|
stream.on('close', (code) => {
|
|
if (output) console.log(output.trim());
|
|
console.log('退出码:', code, '\n');
|
|
resolve(code === 0);
|
|
});
|
|
});
|
|
});
|
|
}
|
|
|
|
async function execute() {
|
|
await run('killall nginx 2>/dev/null; pkill nginx 2>/dev/null; sleep 1', '停止系统nginx');
|
|
|
|
await run('netstat -tlnp | grep ":80 " || echo "端口80已释放"', '检查端口80');
|
|
|
|
await run('cd /www/huishou && docker stop huishou huishou-backend 2>/dev/null; docker rm huishou huishou-backend 2>/dev/null', '清理旧容器');
|
|
|
|
await new Promise((resolve) => {
|
|
console.log('=== 4. 更新docker-compose.yml ===');
|
|
conn.sftp((err, sftp) => {
|
|
if (err) {
|
|
console.error('SFTP错误:', err.message);
|
|
resolve(false);
|
|
return;
|
|
}
|
|
|
|
const content = `services:
|
|
backend:
|
|
build:
|
|
context: .
|
|
dockerfile: Dockerfile
|
|
container_name: huishou-backend
|
|
ports:
|
|
- "4000:4000"
|
|
volumes:
|
|
- ./server/data:/app/server/data
|
|
- ./server/icons:/app/server/icons
|
|
environment:
|
|
- NODE_ENV=production
|
|
- PORT=4000
|
|
restart: unless-stopped
|
|
healthcheck:
|
|
test: ["CMD", "node", "-e", "require(\\'http\\').get(\\'http://localhost:4000/api/prices\\', (r) => process.exit(r.statusCode === 200 ? 0 : 1)).on(\\'error\\', () => process.exit(1))"]
|
|
interval: 30s
|
|
timeout: 10s
|
|
retries: 3
|
|
start_period: 10s
|
|
|
|
nginx:
|
|
image: nginx:latest
|
|
container_name: huishou
|
|
ports:
|
|
- "80:80"
|
|
volumes:
|
|
- ./admin:/usr/share/nginx/html/admin
|
|
- ./dist/build/h5:/usr/share/nginx/html/h5
|
|
- ./admin/nginx.conf:/etc/nginx/conf.d/default.conf
|
|
depends_on:
|
|
- backend
|
|
restart: unless-stopped
|
|
|
|
networks:
|
|
default:
|
|
driver: bridge
|
|
`;
|
|
|
|
const ws = sftp.createWriteStream('/www/huishou/docker-compose.yml', { mode: 0o644 });
|
|
ws.on('close', () => {
|
|
console.log('docker-compose.yml已更新\n');
|
|
sftp.end();
|
|
resolve(true);
|
|
});
|
|
ws.write(content);
|
|
ws.end();
|
|
});
|
|
});
|
|
|
|
await run('cd /www/huishou && docker-compose up -d', '启动Docker容器');
|
|
|
|
await new Promise(r => setTimeout(r, 10000));
|
|
|
|
await run('docker ps | grep huishou', '检查容器状态');
|
|
|
|
conn.end();
|
|
}
|
|
|
|
execute();
|
|
});
|
|
|
|
conn.on('error', (err) => {
|
|
console.error('SSH连接错误:', err.message);
|
|
});
|
|
|
|
conn.on('end', () => {
|
|
console.log('\nSSH连接已关闭');
|
|
});
|
|
|
|
conn.connect(config); |