52 lines
1.5 KiB
JavaScript
52 lines
1.5 KiB
JavaScript
|
|
const { Client } = require('ssh2');
|
||
|
|
const fs = require('fs');
|
||
|
|
const path = require('path');
|
||
|
|
|
||
|
|
const c = new Client();
|
||
|
|
const localFile = path.join(__dirname, '..', 'test2', 'server', 'services', 'toolExecutor.js');
|
||
|
|
const remotePath = '/www/wwwroot/demo.tensorgrove.com.cn/server/services/toolExecutor.js';
|
||
|
|
|
||
|
|
const content = fs.readFileSync(localFile, 'utf8');
|
||
|
|
|
||
|
|
c.on('ready', () => {
|
||
|
|
console.log('SSH connected, deploying toolExecutor.js...');
|
||
|
|
|
||
|
|
// Use SFTP to write the file
|
||
|
|
c.sftp((err, sftp) => {
|
||
|
|
if (err) {
|
||
|
|
console.error('SFTP error:', err.message);
|
||
|
|
c.end();
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
const writeStream = sftp.createWriteStream(remotePath);
|
||
|
|
writeStream.on('close', () => {
|
||
|
|
console.log('File uploaded successfully.');
|
||
|
|
|
||
|
|
// Restart PM2
|
||
|
|
c.exec('pm2 restart bigwo-server && sleep 2 && pm2 logs bigwo-server --nostream --lines 10', (err, s) => {
|
||
|
|
if (err) {
|
||
|
|
console.error('Restart error:', err.message);
|
||
|
|
c.end();
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
let o = '';
|
||
|
|
s.on('data', (d) => (o += d));
|
||
|
|
s.stderr.on('data', (d) => (o += d));
|
||
|
|
s.on('close', () => {
|
||
|
|
console.log('PM2 restart output:\n' + o);
|
||
|
|
c.end();
|
||
|
|
});
|
||
|
|
});
|
||
|
|
});
|
||
|
|
|
||
|
|
writeStream.on('error', (err) => {
|
||
|
|
console.error('Write error:', err.message);
|
||
|
|
c.end();
|
||
|
|
});
|
||
|
|
|
||
|
|
writeStream.write(content);
|
||
|
|
writeStream.end();
|
||
|
|
});
|
||
|
|
}).connect({ host: '119.45.10.34', port: 22, username: 'root', password: '#xyzh%CS#2512@28' });
|