mirror of
https://github.com/easingthemes/ssh-deploy
synced 2025-04-20 07:03:14 +00:00
Use promise
This commit is contained in:
parent
7306f6bca0
commit
fdc1b9a24d
2
dist/index.js
vendored
2
dist/index.js
vendored
File diff suppressed because one or more lines are too long
29
src/index.js
29
src/index.js
@ -5,7 +5,7 @@ const { addSshKey, getPrivateKeyPath } = require('./sshKey');
|
||||
const { validateRequiredInputs } = require('./helpers');
|
||||
const inputs = require('./inputs');
|
||||
|
||||
const run = () => {
|
||||
const run = async () => {
|
||||
const {
|
||||
source, remoteUser, remoteHost, remotePort,
|
||||
deployKeyName, sshPrivateKey,
|
||||
@ -20,20 +20,25 @@ const run = () => {
|
||||
const { path: privateKeyPath } = getPrivateKeyPath(deployKeyName);
|
||||
// Check Script before
|
||||
if (scriptBefore) {
|
||||
remoteCmdBefore(scriptBefore);
|
||||
}
|
||||
// Check script after
|
||||
let callback = () => {};
|
||||
if (scriptAfter) {
|
||||
callback = (...result) => {
|
||||
remoteCmdAfter(scriptAfter, result);
|
||||
};
|
||||
await remoteCmdBefore(scriptBefore);
|
||||
}
|
||||
|
||||
/* eslint-disable object-property-newline */
|
||||
sshDeploy({
|
||||
await sshDeploy({
|
||||
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');
|
||||
});
|
||||
|
@ -3,24 +3,34 @@ const { exec } = require('child_process');
|
||||
const { privateKey, sshServer, githubWorkspace } = require('./inputs');
|
||||
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`;
|
||||
try {
|
||||
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) {
|
||||
console.log('⚠️ [CMD] Remote script failed. ', err.message);
|
||||
handleError(err.message, isRequired, reject);
|
||||
} else {
|
||||
console.log('✅ [CMD] Remote script executed. \n', data, stderr);
|
||||
resolve(data);
|
||||
}
|
||||
});
|
||||
} catch (err) {
|
||||
console.log('⚠️ [CMD] Starting Remote script execution failed. ', err.message);
|
||||
handleError(err.message, isRequired, reject);
|
||||
}
|
||||
};
|
||||
});
|
||||
|
||||
module.exports = {
|
||||
remoteCmdBefore: (cmd) => remoteCmd(cmd, 'before'),
|
||||
remoteCmdAfter: (cmd) => remoteCmd(cmd, 'after')
|
||||
remoteCmdBefore: async (cmd, isRequired) => remoteCmd(cmd, 'before', isRequired),
|
||||
remoteCmdAfter: async (cmd, isRequired) => remoteCmd(cmd, 'after', isRequired)
|
||||
};
|
||||
|
102
src/rsyncCli.js
102
src/rsyncCli.js
@ -1,30 +1,46 @@
|
||||
const { execSync } = require('child_process');
|
||||
const nodeRsync = require('rsyncwrapper');
|
||||
const { writeToFile } = require('./helpers');
|
||||
|
||||
// eslint-disable-next-line no-async-promise-executor
|
||||
const validateRsync = new Promise(async (resolve, reject) => {
|
||||
const nodeRsyncPromise = async (config) => new Promise((resolve, reject) => {
|
||||
try {
|
||||
execSync('rsync --version', { stdio: 'inherit' });
|
||||
console.log('⚠️ [CLI] Rsync exists');
|
||||
resolve();
|
||||
return;
|
||||
} catch (e) {
|
||||
console.log('⚠️ [CLI] Rsync doesn\'t exists', e);
|
||||
}
|
||||
|
||||
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' });
|
||||
console.log('✅ [CLI] Rsync installed. \n');
|
||||
resolve();
|
||||
} catch (err) {
|
||||
reject(Error(`⚠️ [CLI] Rsync installation failed. Aborting ... error: ${err.message}`));
|
||||
nodeRsync(config, (error, stdout, stderr, cmd) => {
|
||||
if (error) {
|
||||
console.log('⚠️ [Rsync] stderr: ', stderr);
|
||||
console.log('⚠️ [Rsync] stdout: ', stdout);
|
||||
console.log('⚠️ [Rsync] cmd: ', cmd);
|
||||
reject(error);
|
||||
} else {
|
||||
resolve(stdout);
|
||||
}
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('⚠️ [Rsync] command error: ', error.message, error.stack);
|
||||
reject(error);
|
||||
}
|
||||
});
|
||||
|
||||
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,
|
||||
privateKeyPath, args, sshCmdArgs, callback
|
||||
privateKeyPath, args, sshCmdArgs
|
||||
}) => {
|
||||
console.log(`[Rsync] Starting Rsync Action: ${source} to ${rsyncServer}`);
|
||||
if (exclude) console.log(`[Rsync] excluding folders ${exclude}`);
|
||||
@ -34,43 +50,21 @@ const rsyncCli = ({
|
||||
recursive: true
|
||||
};
|
||||
|
||||
try {
|
||||
// RSYNC COMMAND
|
||||
/* eslint-disable object-property-newline */
|
||||
nodeRsync({
|
||||
...defaultOptions,
|
||||
src: source, dest: rsyncServer, excludeFirst: exclude, port: remotePort,
|
||||
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();
|
||||
}
|
||||
// RSYNC COMMAND
|
||||
/* eslint-disable object-property-newline */
|
||||
return nodeRsyncPromise({
|
||||
...defaultOptions,
|
||||
src: source, dest: rsyncServer, excludeFirst: exclude, port: remotePort,
|
||||
privateKey: privateKeyPath, args, sshCmdArgs
|
||||
});
|
||||
};
|
||||
|
||||
const sshDeploy = (params) => {
|
||||
validateRsync
|
||||
.then(() => {
|
||||
rsyncCli(params);
|
||||
})
|
||||
.catch((err) => {
|
||||
throw err;
|
||||
});
|
||||
const sshDeploy = async (params) => {
|
||||
await validateRsync();
|
||||
const stdout = await rsyncCli(params);
|
||||
console.log('✅ [Rsync] finished.', stdout);
|
||||
process.env.RSYNC_STDOUT = stdout;
|
||||
return stdout;
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
|
Loading…
Reference in New Issue
Block a user