import { Client } from 'ssh2'; const conn = new Client(); conn.on('ready', () => { console.log('=== 检查并创建Nginx配置 ===\n'); conn.exec('ls -la /www/huishou/', (err, stream) => { if (err) { console.log('检查目录失败:', err.message); conn.end(); return; } stream.on('data', (data) => { console.log('目录内容:', data.toString()); }); stream.on('close', () => { conn.exec('ls -la /www/huishou/nginx/conf.d/ 2>/dev/null || echo "配置目录不存在"', (err, stream2) => { if (err) { console.log('检查配置目录失败:', err.message); conn.end(); return; } stream2.on('data', (data) => { console.log('配置目录:', data.toString()); }); stream2.on('close', () => { const nginxConfig = `server { listen 80; server_name hs.yifenbao.cn; location / { proxy_pass http://huishou-backend:4000; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; } location /api { proxy_pass http://huishou-backend:4000; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; } } `; conn.exec(`mkdir -p /www/huishou/nginx/conf.d && echo "${nginxConfig.replace(/"/g, '\\"')}" > /www/huishou/nginx/conf.d/default.conf`, (err, stream3) => { if (err) { console.log('创建配置失败:', err.message); conn.end(); return; } stream3.on('close', () => { console.log('配置文件已创建'); conn.exec('docker run -d --name huishou --network bridge -p 80:80 -v /www/huishou/nginx/conf.d:/etc/nginx/conf.d nginx:latest', (err, stream4) => { if (err) { console.log('启动容器失败:', err.message); conn.end(); return; } stream4.on('close', () => { console.log('容器启动中...'); setTimeout(() => { conn.exec('docker ps | grep huishou', (err, stream5) => { if (err) { console.log('检查容器失败:', err.message); conn.end(); return; } stream5.on('data', (data) => { console.log('\n容器状态:', data.toString()); }); stream5.on('close', () => { conn.end(); }); }); }, 3000); }); }); }); }); }); }); }); }); }); conn.on('error', (err) => { console.log('连接错误:', err.message); }); conn.connect({ host: 'hs.yifenbao.cn', port: 22, username: 'root', password: 'Torch1112' });