2023-01-02 23:06:33 +03:00
|
|
|
const { execSync } = require('child_process');
|
|
|
|
const nodeRsync = require('rsyncwrapper');
|
|
|
|
|
|
|
|
const nodeRsyncPromise = async (config) => new Promise((resolve, reject) => {
|
2023-01-03 00:59:19 +03:00
|
|
|
const logCMD = (cmd) => {
|
|
|
|
console.warn('================================================================');
|
|
|
|
console.log(cmd);
|
|
|
|
console.warn('================================================================');
|
|
|
|
};
|
|
|
|
|
2023-01-02 23:06:33 +03:00
|
|
|
try {
|
|
|
|
nodeRsync(config, (error, stdout, stderr, cmd) => {
|
|
|
|
if (error) {
|
|
|
|
console.error('❌ [Rsync] error: ');
|
|
|
|
console.error(error);
|
|
|
|
console.error('❌ [Rsync] stderr: ');
|
|
|
|
console.error(stderr);
|
|
|
|
console.error('❌️ [Rsync] stdout: ');
|
|
|
|
console.error(stdout);
|
2023-01-03 00:59:19 +03:00
|
|
|
console.error('❌ [Rsync] command: ');
|
|
|
|
logCMD(cmd);
|
2023-01-02 23:06:33 +03:00
|
|
|
reject(new Error(`${error.message}\n\n${stderr}`));
|
|
|
|
} else {
|
2023-01-03 00:59:19 +03:00
|
|
|
console.log('⭐ [Rsync] command finished: ');
|
|
|
|
logCMD(cmd);
|
2023-01-02 23:06:33 +03:00
|
|
|
resolve(stdout);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} catch (error) {
|
|
|
|
console.error('❌ [Rsync] command error: ', error.message, error.stack);
|
|
|
|
reject(error);
|
2022-12-31 12:00:32 +03:00
|
|
|
}
|
2023-01-02 23:06:33 +03:00
|
|
|
});
|
2022-12-31 12:00:32 +03:00
|
|
|
|
2023-01-02 23:06:33 +03:00
|
|
|
const validateRsync = async () => {
|
|
|
|
try {
|
|
|
|
execSync('rsync --version', { stdio: 'inherit' });
|
|
|
|
console.log('✅️ [CLI] Rsync exists');
|
|
|
|
return;
|
|
|
|
} catch (error) {
|
|
|
|
console.warn('⚠️ [CLI] Rsync doesn\'t exists', error.message);
|
|
|
|
}
|
2022-12-31 12:00:32 +03:00
|
|
|
|
2023-01-02 23:06:33 +03:00
|
|
|
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}`);
|
|
|
|
}
|
2020-04-11 17:29:20 +03:00
|
|
|
};
|
|
|
|
|
2023-01-02 23:06:33 +03:00
|
|
|
const rsyncCli = async ({
|
|
|
|
source, rsyncServer, exclude, remotePort,
|
|
|
|
privateKeyPath, args, sshCmdArgs
|
|
|
|
}) => {
|
|
|
|
console.log(`[Rsync] Starting Rsync Action: ${source} to ${rsyncServer}`);
|
2023-01-03 04:49:54 +03:00
|
|
|
if (exclude && exclude.length > 0) console.log(`[Rsync] excluding folders ${exclude}`);
|
2020-04-11 17:54:21 +03:00
|
|
|
|
2023-01-02 23:06:33 +03:00
|
|
|
const defaultOptions = {
|
|
|
|
ssh: true,
|
2023-01-03 00:30:57 +03:00
|
|
|
recursive: true,
|
|
|
|
onStdout: (data) => console.log(data.toString()),
|
|
|
|
onStderr: (data) => console.error(data.toString())
|
2023-01-02 23:06:33 +03:00
|
|
|
};
|
2020-04-11 17:29:20 +03:00
|
|
|
|
2023-01-02 23:06:33 +03:00
|
|
|
// RSYNC COMMAND
|
|
|
|
/* eslint-disable object-property-newline */
|
|
|
|
return nodeRsyncPromise({
|
|
|
|
...defaultOptions,
|
|
|
|
src: source, dest: rsyncServer, excludeFirst: exclude, port: remotePort,
|
2023-01-03 00:30:57 +03:00
|
|
|
privateKey: privateKeyPath, args, sshCmdArgs
|
2020-04-11 17:29:20 +03:00
|
|
|
});
|
2023-01-02 23:06:33 +03:00
|
|
|
};
|
2020-04-11 17:29:20 +03:00
|
|
|
|
2023-01-02 23:06:33 +03:00
|
|
|
const sshDeploy = async (params) => {
|
|
|
|
await validateRsync();
|
|
|
|
const stdout = await rsyncCli(params);
|
|
|
|
console.log('✅ [Rsync] finished.', stdout);
|
|
|
|
process.env.RSYNC_STDOUT = `${stdout}`;
|
|
|
|
return stdout;
|
2020-04-11 17:29:20 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = {
|
2023-01-02 23:06:33 +03:00
|
|
|
sshDeploy
|
2020-09-19 00:26:31 +03:00
|
|
|
};
|