ssh-deploy/src/rsyncCli.js

73 lines
2.2 KiB
JavaScript
Raw Normal View History

const { execSync } = require('child_process');
const nodeRsync = require('rsyncwrapper');
2023-01-02 12:57:35 +01:00
const nodeRsyncPromise = async (config) => new Promise((resolve, reject) => {
try {
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 validateRsync = async () => {
2023-01-02 02:01:20 +01:00
try {
execSync('rsync --version', { stdio: 'inherit' });
2022-12-31 10:14:04 +01:00
console.log('⚠️ [CLI] Rsync exists');
2023-01-02 02:16:05 +01:00
return;
2023-01-02 12:57:35 +01:00
} catch (error) {
2023-01-02 18:19:19 +01:00
console.log('⚠️ [CLI] Rsync doesn\'t exists', error.message);
2022-12-31 10:00:32 +01:00
}
2023-01-02 12:57:35 +01:00
console.log('⚠️ [CLI] Start rsync installation with "apt-get" \n');
try {
2023-01-02 02:06:42 +01:00
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');
2023-01-02 12:57:35 +01:00
} catch (error) {
throw new Error(`⚠️ [CLI] Rsync installation failed. Aborting ... error: ${error.message}`);
}
2023-01-02 12:57:35 +01:00
};
2023-01-02 12:57:35 +01:00
const rsyncCli = async ({
2023-01-02 02:13:02 +01:00
source, rsyncServer, exclude, remotePort,
2023-01-02 12:57:35 +01:00
privateKeyPath, args, sshCmdArgs
}) => {
2023-01-02 02:13:02 +01:00
console.log(`[Rsync] Starting Rsync Action: ${source} to ${rsyncServer}`);
if (exclude) console.log(`[Rsync] excluding folders ${exclude}`);
const defaultOptions = {
ssh: true,
recursive: true
};
2023-01-02 12:57:35 +01:00
// RSYNC COMMAND
/* eslint-disable object-property-newline */
return nodeRsyncPromise({
...defaultOptions,
src: source, dest: rsyncServer, excludeFirst: exclude, port: remotePort,
2023-01-02 18:19:19 +01:00
privateKey: privateKeyPath, args, sshCmdArgs,
onStdout: (data) => console.log(data), onStderr: (data) => console.error(data)
2023-01-02 12:57:35 +01:00
});
};
2023-01-02 12:57:35 +01:00
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 = {
sshDeploy
2020-09-18 23:26:31 +02:00
};