Use promise

This commit is contained in:
Dragan Filipovic 2023-01-02 12:57:35 +01:00
parent 7306f6bca0
commit fdc1b9a24d
4 changed files with 83 additions and 74 deletions

2
dist/index.js vendored

File diff suppressed because one or more lines are too long

View File

@ -5,7 +5,7 @@ const { addSshKey, getPrivateKeyPath } = require('./sshKey');
const { validateRequiredInputs } = require('./helpers'); const { validateRequiredInputs } = require('./helpers');
const inputs = require('./inputs'); const inputs = require('./inputs');
const run = () => { const run = async () => {
const { const {
source, remoteUser, remoteHost, remotePort, source, remoteUser, remoteHost, remotePort,
deployKeyName, sshPrivateKey, deployKeyName, sshPrivateKey,
@ -20,20 +20,25 @@ const run = () => {
const { path: privateKeyPath } = getPrivateKeyPath(deployKeyName); const { path: privateKeyPath } = getPrivateKeyPath(deployKeyName);
// Check Script before // Check Script before
if (scriptBefore) { if (scriptBefore) {
remoteCmdBefore(scriptBefore); await remoteCmdBefore(scriptBefore);
}
// Check script after
let callback = () => {};
if (scriptAfter) {
callback = (...result) => {
remoteCmdAfter(scriptAfter, result);
};
} }
/* eslint-disable object-property-newline */ /* eslint-disable object-property-newline */
sshDeploy({ await sshDeploy({
source, rsyncServer, exclude, remotePort, source, rsyncServer, exclude, remotePort,
privateKeyPath, args, sshCmdArgs, callback privateKeyPath, args, sshCmdArgs
}); });
// Check script after
if (scriptAfter) {
await remoteCmdAfter(scriptAfter);
}
}; };
run(); run()
.then(() => {
console.log('DONE');
})
.catch(() => {
console.error('ERROR');
});

View File

@ -3,24 +3,34 @@ const { exec } = require('child_process');
const { privateKey, sshServer, githubWorkspace } = require('./inputs'); const { privateKey, sshServer, githubWorkspace } = require('./inputs');
const { writeToFile } = require('./helpers'); const { writeToFile } = require('./helpers');
const remoteCmd = (content, label) => { const handleError = (errorMessage, isRequired, callback) => {
const message = `⚠️ [CMD] Remote script failed: ${errorMessage}`;
if (isRequired) {
callback(new Error(message));
} else {
console.log(message);
}
};
const remoteCmd = async (content, label, isRequired) => new Promise((resolve, reject) => {
const filename = `local_ssh_script-${label}.sh`; const filename = `local_ssh_script-${label}.sh`;
try { try {
writeToFile({ dir: githubWorkspace, filename, content }); writeToFile({ dir: githubWorkspace, filename, content });
exec(`ssh -i ${privateKey} ${sshServer} 'bash -s' < ${filename}`, (err, data, stderr) => { exec(`ssh -i ${privateKey} ${sshServer} 'RSYNC_STDOUT=${process.env.RSYNC_STDOUT} bash -s' < ${filename}`, (err, data, stderr) => {
if (err) { if (err) {
console.log('⚠️ [CMD] Remote script failed. ', err.message); handleError(err.message, isRequired, reject);
} else { } else {
console.log('✅ [CMD] Remote script executed. \n', data, stderr); console.log('✅ [CMD] Remote script executed. \n', data, stderr);
resolve(data);
} }
}); });
} catch (err) { } catch (err) {
console.log('⚠️ [CMD] Starting Remote script execution failed. ', err.message); handleError(err.message, isRequired, reject);
} }
}; });
module.exports = { module.exports = {
remoteCmdBefore: (cmd) => remoteCmd(cmd, 'before'), remoteCmdBefore: async (cmd, isRequired) => remoteCmd(cmd, 'before', isRequired),
remoteCmdAfter: (cmd) => remoteCmd(cmd, 'after') remoteCmdAfter: async (cmd, isRequired) => remoteCmd(cmd, 'after', isRequired)
}; };

View File

@ -1,30 +1,46 @@
const { execSync } = require('child_process'); const { execSync } = require('child_process');
const nodeRsync = require('rsyncwrapper'); const nodeRsync = require('rsyncwrapper');
const { writeToFile } = require('./helpers');
// eslint-disable-next-line no-async-promise-executor const nodeRsyncPromise = async (config) => new Promise((resolve, reject) => {
const validateRsync = new Promise(async (resolve, reject) => {
try { try {
execSync('rsync --version', { stdio: 'inherit' }); nodeRsync(config, (error, stdout, stderr, cmd) => {
console.log('⚠️ [CLI] Rsync exists'); if (error) {
resolve(); console.log('⚠️ [Rsync] stderr: ', stderr);
return; console.log('⚠️ [Rsync] stdout: ', stdout);
} catch (e) { console.log('⚠️ [Rsync] cmd: ', cmd);
console.log('⚠️ [CLI] Rsync doesn\'t exists', e); reject(error);
} } else {
resolve(stdout);
console.log('⚠️ [CLI] Rsync doesn\'t exists. Start installation with "apt-get" \n'); }
try { });
execSync('sudo DEBIAN_FRONTEND=noninteractive apt-get -y update && sudo DEBIAN_FRONTEND=noninteractive apt-get --no-install-recommends -y install rsync', { stdio: 'inherit' }); } catch (error) {
console.log('✅ [CLI] Rsync installed. \n'); console.error('⚠️ [Rsync] command error: ', error.message, error.stack);
resolve(); reject(error);
} catch (err) {
reject(Error(`⚠️ [CLI] Rsync installation failed. Aborting ... error: ${err.message}`));
} }
}); });
const rsyncCli = ({ const validateRsync = async () => {
try {
execSync('rsync --version', { stdio: 'inherit' });
console.log('⚠️ [CLI] Rsync exists');
return;
} catch (error) {
console.log('⚠️ [CLI] Rsync doesn\'t exists', error);
}
console.log('⚠️ [CLI] Start rsync installation with "apt-get" \n');
try {
execSync('sudo DEBIAN_FRONTEND=noninteractive apt-get -y update && sudo DEBIAN_FRONTEND=noninteractive apt-get --no-install-recommends -y install rsync', { stdio: 'inherit' });
console.log('✅ [CLI] Rsync installed. \n');
} catch (error) {
throw new Error(`⚠️ [CLI] Rsync installation failed. Aborting ... error: ${error.message}`);
}
};
const rsyncCli = async ({
source, rsyncServer, exclude, remotePort, source, rsyncServer, exclude, remotePort,
privateKeyPath, args, sshCmdArgs, callback privateKeyPath, args, sshCmdArgs
}) => { }) => {
console.log(`[Rsync] Starting Rsync Action: ${source} to ${rsyncServer}`); console.log(`[Rsync] Starting Rsync Action: ${source} to ${rsyncServer}`);
if (exclude) console.log(`[Rsync] excluding folders ${exclude}`); if (exclude) console.log(`[Rsync] excluding folders ${exclude}`);
@ -34,43 +50,21 @@ const rsyncCli = ({
recursive: true recursive: true
}; };
try { // RSYNC COMMAND
// RSYNC COMMAND /* eslint-disable object-property-newline */
/* eslint-disable object-property-newline */ return nodeRsyncPromise({
nodeRsync({ ...defaultOptions,
...defaultOptions, src: source, dest: rsyncServer, excludeFirst: exclude, port: remotePort,
src: source, dest: rsyncServer, excludeFirst: exclude, port: remotePort, privateKey: privateKeyPath, args, sshCmdArgs
privateKey: privateKeyPath, args, sshCmdArgs });
}, (error, stdout, stderr, cmd) => {
if (error) {
console.error('⚠️ [Rsync] error: ', error.message);
console.log('⚠️ [Rsync] stderr: ', stderr);
console.log('⚠️ [Rsync] stdout: ', stdout);
console.log('⚠️ [Rsync] cmd: ', cmd);
} else {
console.log('✅ [Rsync] finished.', stdout);
}
callback(error, stdout, stderr, cmd);
if (error) {
process.abort();
}
});
} catch (err) {
console.error('⚠️ [Rsync] command error: ', err.message, err.stack);
process.abort();
}
}; };
const sshDeploy = (params) => { const sshDeploy = async (params) => {
validateRsync await validateRsync();
.then(() => { const stdout = await rsyncCli(params);
rsyncCli(params); console.log('✅ [Rsync] finished.', stdout);
}) process.env.RSYNC_STDOUT = stdout;
.catch((err) => { return stdout;
throw err;
});
}; };
module.exports = { module.exports = {