150 lines
4.1 KiB
JavaScript
150 lines
4.1 KiB
JavaScript
const { Client } = require('ssh2');
|
|
const fs = require('fs');
|
|
|
|
const config = {
|
|
host: 'hs.yifenbao.cn',
|
|
port: 22,
|
|
username: 'root',
|
|
password: 'Torch1112'
|
|
};
|
|
|
|
let step = 1;
|
|
|
|
function runCommand(conn, cmd, description, callback) {
|
|
console.log(`\n=== Step ${step}: ${description} ===`);
|
|
console.log('Command:', cmd);
|
|
step++;
|
|
|
|
conn.exec(cmd, (err, stream) => {
|
|
if (err) {
|
|
console.error('Error:', err.message);
|
|
callback(false);
|
|
return;
|
|
}
|
|
|
|
let output = '';
|
|
let errorOutput = '';
|
|
|
|
stream.stdout.on('data', (data) => {
|
|
output += data.toString();
|
|
});
|
|
|
|
stream.stderr.on('data', (data) => {
|
|
errorOutput += data.toString();
|
|
});
|
|
|
|
stream.on('close', (code, signal) => {
|
|
if (output) console.log('Output:', output.trim());
|
|
if (errorOutput) console.log('Error:', errorOutput.trim());
|
|
console.log('Exit code:', code);
|
|
callback(code === 0);
|
|
});
|
|
|
|
stream.on('error', (err) => {
|
|
console.error('Stream error:', err.message);
|
|
callback(false);
|
|
});
|
|
});
|
|
}
|
|
|
|
const conn = new Client();
|
|
|
|
conn.on('ready', () => {
|
|
console.log('SSH连接成功!');
|
|
|
|
runCommand(conn,
|
|
'mkdir -p /www/huishou && rm -rf /www/huishou/admin /www/huishou/dist /www/huishou/server /www/huishou/*.yml /www/huishou/*.dockerignore /www/huishou/Dockerfile /www/huishou/deploy.zip 2>/dev/null',
|
|
'清理服务器目录',
|
|
(success) => {
|
|
if (!success) {
|
|
conn.end();
|
|
return;
|
|
}
|
|
|
|
console.log('\n=== Step 2: 上传部署文件 ===');
|
|
conn.sftp((err, sftp) => {
|
|
if (err) {
|
|
console.error('SFTP连接失败:', err.message);
|
|
conn.end();
|
|
return;
|
|
}
|
|
|
|
const localFile = 'D:/Trae/huishou/deploy.zip';
|
|
const remoteFile = '/www/huishou/deploy.zip';
|
|
|
|
const readStream = fs.createReadStream(localFile);
|
|
const writeStream = sftp.createWriteStream(remoteFile, { mode: 0o644 });
|
|
|
|
writeStream.on('close', () => {
|
|
console.log('文件上传成功');
|
|
sftp.end();
|
|
|
|
runCommand(conn,
|
|
'cd /www/huishou && unzip -o deploy.zip && rm deploy.zip',
|
|
'解压文件',
|
|
(success) => {
|
|
if (!success) {
|
|
conn.end();
|
|
return;
|
|
}
|
|
|
|
runCommand(conn,
|
|
'cd /www/huishou && docker stop huishou huishou-backend 2>/dev/null; docker rm huishou huishou-backend 2>/dev/null',
|
|
'清理旧容器',
|
|
() => {
|
|
runCommand(conn,
|
|
'cd /www/huishou && docker-compose up -d',
|
|
'启动Docker容器',
|
|
(success) => {
|
|
if (!success) {
|
|
conn.end();
|
|
return;
|
|
}
|
|
|
|
console.log('\n=== Step 6: 等待容器启动 (15秒) ===');
|
|
setTimeout(() => {
|
|
runCommand(conn,
|
|
'docker ps | grep huishou',
|
|
'检查容器状态',
|
|
() => {
|
|
conn.end();
|
|
console.log('\n=== 部署完成 ===');
|
|
}
|
|
);
|
|
}, 15000);
|
|
}
|
|
);
|
|
}
|
|
);
|
|
}
|
|
);
|
|
});
|
|
|
|
writeStream.on('error', (err) => {
|
|
console.error('文件上传失败:', err.message);
|
|
sftp.end();
|
|
conn.end();
|
|
});
|
|
|
|
readStream.on('error', (err) => {
|
|
console.error('文件读取失败:', err.message);
|
|
sftp.end();
|
|
conn.end();
|
|
});
|
|
|
|
readStream.pipe(writeStream);
|
|
});
|
|
}
|
|
);
|
|
});
|
|
|
|
conn.on('error', (err) => {
|
|
console.error('SSH连接错误:', err.message);
|
|
});
|
|
|
|
conn.on('end', () => {
|
|
console.log('SSH连接已关闭');
|
|
});
|
|
|
|
console.log('开始部署...');
|
|
conn.connect(config); |