139 lines
4.0 KiB
JavaScript
139 lines
4.0 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');
|
|
|
|
console.log('=== 1. 停止占用端口80的系统nginx ===');
|
|
conn.exec('systemctl stop nginx 2>/dev/null || service nginx stop 2>/dev/null || killall nginx 2>/dev/null; echo "系统nginx已停止"', (err, stream) => {
|
|
if (err) {
|
|
console.error('执行命令错误:', err.message);
|
|
conn.end();
|
|
return;
|
|
}
|
|
|
|
stream.on('close', () => {
|
|
console.log('系统nginx已停止\n');
|
|
|
|
console.log('=== 2. 更新docker-compose.yml ===');
|
|
const dockerCompose = `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`;
|
|
|
|
conn.sftp((err, sftp) => {
|
|
if (err) {
|
|
console.error('SFTP连接失败:', err.message);
|
|
conn.end();
|
|
return;
|
|
}
|
|
|
|
const writeStream = sftp.createWriteStream('/www/huishou/docker-compose.yml', { mode: 0o644 });
|
|
writeStream.on('close', () => {
|
|
console.log('docker-compose.yml已更新\n');
|
|
sftp.end();
|
|
|
|
console.log('=== 3. 重启Docker容器 ===');
|
|
conn.exec('cd /www/huishou && docker stop huishou huishou-backend 2>/dev/null; docker rm huishou huishou-backend 2>/dev/null; docker-compose up -d', (err, stream) => {
|
|
if (err) {
|
|
console.error('执行命令错误:', err.message);
|
|
conn.end();
|
|
return;
|
|
}
|
|
|
|
let output = '';
|
|
stream.stdout.on('data', (data) => {
|
|
output += data.toString();
|
|
});
|
|
|
|
stream.on('close', () => {
|
|
if (output) console.log(output);
|
|
console.log('Docker容器重启命令已执行\n');
|
|
|
|
setTimeout(() => {
|
|
console.log('=== 4. 检查容器状态 ===');
|
|
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', () => {
|
|
if (output2) {
|
|
console.log('容器状态:');
|
|
console.log(output2);
|
|
} else {
|
|
console.log('容器未启动');
|
|
}
|
|
conn.end();
|
|
});
|
|
});
|
|
}, 15000);
|
|
});
|
|
});
|
|
});
|
|
|
|
writeStream.write(dockerCompose);
|
|
writeStream.end();
|
|
});
|
|
});
|
|
});
|
|
});
|
|
|
|
conn.on('error', (err) => {
|
|
console.error('SSH连接错误:', err.message);
|
|
});
|
|
|
|
conn.on('end', () => {
|
|
console.log('\nSSH连接已关闭');
|
|
});
|
|
|
|
conn.connect(config); |