ssh-deploy/src/remoteCmd.js
Dragan Filipović ec9347f8c6
Feature/ssh cmd (#94)
* feat: Add SSH remote script support -  before and after rsync

* fix: remove __dirname

* feat: add sshCmdArgs option

* Add promise instead of callback

* fix: improve logs

* fix: Add simple command exists instead of a plugin

* add non interactive install

* feat: add onStderr and onStdout logs

* Improve reject messages

* feat: Add RSYNC_STDOUT env variable

* emoji updates

* fix: update workflow actions
2023-01-02 21:06:33 +01:00

41 lines
1.5 KiB
JavaScript

const { exec } = require('child_process');
const { sshServer, githubWorkspace } = 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 });
console.log(`Executing remote script: ssh -i ${privateKeyPath} ${sshServer}`);
exec(
`DEBIAN_FRONTEND=noninteractive ssh -i ${privateKeyPath} ${sshServer} 'RSYNC_STDOUT="${process.env.RSYNC_STDOUT}" 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 {
console.log('✅ [CMD] Remote script executed. \n', data, stderr);
resolve(data);
}
}
);
} 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')
};