forked from easingthemes/ssh-deploy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathremoteCmd.js
43 lines (40 loc) · 1.71 KB
/
remoteCmd.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
const { exec } = require('child_process');
const { sshServer, githubWorkspace, remotePort } = require('./inputs');
const { writeToFile } = require('./helpers');
const handleError = (message, isRequired, callback) => {
if (isRequired) {
callback(new Error(message));
} else {
console.warn(message);
}
};
// eslint-disable-next-line max-len
const remoteCmd = async (content, privateKeyPath, isRequired, label) => new Promise((resolve, reject) => {
const filename = `local_ssh_script-${label}.sh`;
try {
writeToFile({ dir: githubWorkspace, filename, content });
const dataLimit = 10000;
const rsyncStdout = (process.env.RSYNC_STDOUT || '').substring(0, dataLimit);
console.log(`Executing remote script: ssh -i ${privateKeyPath} ${sshServer}`);
exec(
`DEBIAN_FRONTEND=noninteractive ssh -p ${(remotePort || 22)} -i ${privateKeyPath} -o StrictHostKeyChecking=no ${sshServer} 'RSYNC_STDOUT="${rsyncStdout}" bash -s' < ${filename}`,
(err, data = '', stderr = '') => {
if (err) {
const message = `⚠️ [CMD] Remote script failed: ${err.message}`;
console.warn(`${message} \n`, data, stderr);
handleError(message, isRequired, reject);
} else {
const limited = data.substring(0, dataLimit);
console.log('✅ [CMD] Remote script executed. \n', limited, stderr);
resolve(limited);
}
}
);
} catch (err) {
handleError(err.message, isRequired, reject);
}
});
module.exports = {
remoteCmdBefore: async (cmd, privateKeyPath, isRequired) => remoteCmd(cmd, privateKeyPath, isRequired, 'before'),
remoteCmdAfter: async (cmd, privateKeyPath, isRequired) => remoteCmd(cmd, privateKeyPath, isRequired, 'after')
};