2023-01-02 01:02:44 +01:00
|
|
|
const { execSync } = require('child_process');
|
|
|
|
const which = require('which');
|
|
|
|
const nodeRsync = require('rsyncwrapper');
|
2020-04-11 16:29:20 +02:00
|
|
|
|
2023-01-02 01:23:54 +01:00
|
|
|
// eslint-disable-next-line no-async-promise-executor
|
|
|
|
const validateRsync = () => new Promise(async (resolve, reject) => {
|
2023-01-02 01:02:44 +01:00
|
|
|
const rsyncCli = await which('rsync', { nothrow: true });
|
|
|
|
execSync('rsync --version', { stdio: 'inherit' });
|
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:02:44 +01:00
|
|
|
execSync('rsync --version', { stdio: 'inherit' });
|
2023-01-02 01:23:54 +01:00
|
|
|
resolve();
|
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');
|
2022-12-31 10:00:32 +01:00
|
|
|
|
2023-01-02 01:02:44 +01:00
|
|
|
try {
|
|
|
|
execSync('sudo apt-get update && sudo apt-get --no-install-recommends install rsync', { stdio: 'inherit' });
|
|
|
|
console.log('✅ [CLI] Rsync installed. \n');
|
2023-01-02 01:23:54 +01:00
|
|
|
resolve();
|
2023-01-02 01:02:44 +01:00
|
|
|
} catch (err) {
|
2023-01-02 01:23:54 +01:00
|
|
|
reject(Error(`⚠️ [CLI] Rsync installation failed. Aborting ... error: ${err.message}`));
|
2023-01-02 01:02:44 +01:00
|
|
|
}
|
2023-01-02 01:23:54 +01:00
|
|
|
});
|
2020-04-11 16:29:20 +02:00
|
|
|
|
2023-01-02 01:02:44 +01:00
|
|
|
const rsyncCli = ({
|
|
|
|
source, sshServer, exclude, remotePort,
|
2023-01-02 01:33:48 +01:00
|
|
|
privateKey, args, sshCmdArgs, callback
|
2023-01-02 01:02:44 +01:00
|
|
|
}) => {
|
|
|
|
console.log(`[Rsync] Starting Rsync Action: ${source} to ${sshServer}`);
|
|
|
|
if (exclude) console.log(`[Rsync] excluding folders ${exclude}`);
|
|
|
|
|
|
|
|
const defaultOptions = {
|
|
|
|
ssh: true,
|
|
|
|
recursive: true
|
|
|
|
};
|
2020-04-11 16:54:21 +02:00
|
|
|
|
2023-01-02 01:02:44 +01:00
|
|
|
try {
|
|
|
|
// RSYNC COMMAND
|
|
|
|
/* eslint-disable object-property-newline */
|
|
|
|
nodeRsync({
|
2023-01-02 01:33:48 +01:00
|
|
|
...defaultOptions,
|
2023-01-02 01:02:44 +01:00
|
|
|
src: source, dest: sshServer, excludeFirst: exclude, port: remotePort,
|
2023-01-02 01:33:48 +01:00
|
|
|
privateKey, args, sshCmdArgs,
|
2023-01-02 01:02:44 +01:00
|
|
|
}, (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);
|
|
|
|
}
|
2020-04-11 16:29:20 +02:00
|
|
|
|
2023-01-02 01:02:44 +01:00
|
|
|
callback(error, stdout, stderr, cmd);
|
2020-04-11 16:29:20 +02:00
|
|
|
|
2023-01-02 01:02:44 +01:00
|
|
|
if (error) {
|
|
|
|
process.abort();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} catch (err) {
|
|
|
|
console.error('⚠️ [Rsync] command error: ', err.message, err.stack);
|
2020-04-11 16:29:20 +02:00
|
|
|
process.abort();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-01-02 01:02:44 +01:00
|
|
|
const sshDeploy = (params) => {
|
|
|
|
validateRsync
|
|
|
|
.then(() => {
|
|
|
|
rsyncCli(params);
|
|
|
|
})
|
|
|
|
.catch((err) => {
|
|
|
|
throw err;
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2020-04-11 16:29:20 +02:00
|
|
|
module.exports = {
|
2023-01-02 01:02:44 +01:00
|
|
|
sshDeploy
|
2020-09-18 23:26:31 +02:00
|
|
|
};
|