forked from easingthemes/ssh-deploy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrsyncCli.js
86 lines (77 loc) · 2.69 KB
/
rsyncCli.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
const { execSync } = require('child_process');
const nodeRsync = require('rsyncwrapper');
const nodeRsyncPromise = async (config) => new Promise((resolve, reject) => {
const logCMD = (cmd) => {
console.warn('================================================================');
console.log(cmd);
console.warn('================================================================');
};
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);
console.error('❌ [Rsync] command: ');
logCMD(cmd);
reject(new Error(`${error.message}\n\n${stderr}`));
} else {
console.log('⭐ [Rsync] command finished: ');
logCMD(cmd);
resolve(stdout);
}
});
} catch (error) {
console.error('❌ [Rsync] command error: ', error.message, error.stack);
reject(error);
}
});
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);
}
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
}) => {
console.log(`[Rsync] Starting Rsync Action: ${source} to ${rsyncServer}`);
if (exclude && exclude.length > 0) console.log(`[Rsync] excluding folders ${exclude}`);
const defaultOptions = {
ssh: true,
recursive: true,
onStdout: (data) => console.log(data.toString()),
onStderr: (data) => console.error(data.toString())
};
// RSYNC COMMAND
/* eslint-disable object-property-newline */
return nodeRsyncPromise({
...defaultOptions,
src: source, dest: rsyncServer, excludeFirst: exclude, port: remotePort,
privateKey: privateKeyPath, args, sshCmdArgs
});
};
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
};