ssh-deploy/src/rsyncCli.js

85 lines
2.3 KiB
JavaScript
Raw Normal View History

const { execSync } = require('child_process');
const nodeRsync = require('rsyncwrapper');
2023-01-02 01:23:54 +01:00
// eslint-disable-next-line no-async-promise-executor
2023-01-02 01:57:24 +01:00
const validateRsync = new Promise(async (resolve, reject) => {
2023-01-02 02:16:05 +01:00
let rsyncCli;
2023-01-02 02:01:20 +01:00
try {
execSync('rsync --version', { stdio: 'inherit' });
rsyncCli = true;
} catch (e) {
2023-01-02 02:01:47 +01:00
rsyncCli = false;
2023-01-02 02:09:17 +01:00
console.log('⚠️ [CLI] Rsync doesn\'t exists', e);
2023-01-02 02:01:20 +01:00
}
2022-12-31 10:00:32 +01:00
if (rsyncCli) {
2022-12-31 10:14:04 +01:00
console.log('⚠️ [CLI] Rsync exists');
2023-01-02 01:23:54 +01:00
resolve();
2023-01-02 02:16:05 +01:00
return;
2022-12-31 10:00:32 +01:00
}
2022-12-30 14:29:31 +01:00
console.log('⚠️ [CLI] Rsync doesn\'t exists. Start 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 01:23:54 +01:00
resolve();
} catch (err) {
2023-01-02 01:23:54 +01:00
reject(Error(`⚠️ [CLI] Rsync installation failed. Aborting ... error: ${err.message}`));
}
2023-01-02 01:23:54 +01:00
});
const rsyncCli = ({
2023-01-02 02:13:02 +01:00
source, rsyncServer, exclude, remotePort,
2023-01-02 01:33:48 +01:00
privateKey, args, sshCmdArgs, callback
}) => {
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
};
try {
// RSYNC COMMAND
/* eslint-disable object-property-newline */
nodeRsync({
2023-01-02 01:33:48 +01:00
...defaultOptions,
2023-01-02 02:13:02 +01:00
src: source, dest: rsyncServer, excludeFirst: exclude, port: remotePort,
2023-01-02 01:33:48 +01:00
privateKey, 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) => {
validateRsync
.then(() => {
rsyncCli(params);
})
.catch((err) => {
throw err;
});
};
module.exports = {
sshDeploy
2020-09-18 23:26:31 +02:00
};