-
Notifications
You must be signed in to change notification settings - Fork 0
/
aws-s3-synch.js
110 lines (82 loc) · 2.8 KB
/
aws-s3-synch.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
var Joi = require('joi');
var schemas = require("./schemas");
const exec = require('child_process').execSync;
function execute ( command, cwd, out ) {
if ( out )
return cwd ? exec(command, {cwd: cwd}).toString() : exec(command).toString();
else if ( cwd )
return exec(command, {stdio: 'inherit', cwd: cwd})
else
exec(command, {stdio: 'inherit'})
}
function synchToS3(params, cb) {
var {source, destination, test, deleteRemoved} = params;
var syncCommand = "s3cmd sync -r --no-mime-magic --guess-mime-type";
if (params.deleteRemoved) {
syncCommand += " --delete-removed";
}
if ( test ) {
syncCommand += " --dry-run";
}
if (params.excludeFile) {
syncCommand += " --exclude-from " + params.excludeFile;
}
syncCommand += " " + source + " " + destination;
console.log("syncCommand : ", syncCommand);
console.log("\n");
console.log("--> Syncing on S3\n");
try {
execute(syncCommand);
console.log("\n====== Successfully synched to [", destination, "] ======\n");
if (cb && typeof cb === "function") return cb();
} catch (e) {
console.log("Error In Syncing Removed Files from : ", destination);
console.log("Error Command : ", syncCommand);
console.log("Errors : ", JSON.stringify(e));
if (cb && typeof cb === "function") return cb();
}
}
function synchFromS3(params, cb) {
var force = params.force ? "--force": "";
var skipExisting = params.skipExisting ? "--skip-existing" : "";
var dryRun = params.test ? "--dry-run" : "";
var Continue = params.continue ? "--continue" : "";
var syncCommand = `s3cmd ${force} ${skipExisting} ${dryRun} ${Continue} --recursive get ${params.source} ${params.destination}`;
console.log("syncCommand : ", syncCommand);
console.log("\n");
console.log("--> Syncing From S3\n");
try {
execute(syncCommand);
console.log("\n====== Successfully synched [", params.destination, "] ======\n");
if (cb && typeof cb === "function") return cb();
} catch (e) {
// console.log("Error In Syncing Removed Files from : ", destination);
console.log("Error Command : ", syncCommand);
console.log("Errors : ", JSON.stringify(e));
if (cb && typeof cb === "function") return cb(e);
}
}
module.exports = {
synchToS3(params, cb) {
// console.log("Params => ", params);
Joi.validate(params, schemas.synchToS3, (err, details) => {
if (err) {
// console.log("Error===> ", (JSON.stringify(err))) ;
if (cb && typeof cb === "function") return cb(err);
}
// console.log(params);
synchToS3(params, cb);
});
},
synchFromS3(params, cb) {
// console.log("Params => ", params);
Joi.validate(params, schemas.synchFromS3, (err, details) => {
if (err) {
// console.log("Error===> ", (JSON.stringify(err))) ;
if (cb && typeof cb === "function") return cb(err);
}
console.log(params);
synchFromS3(params, cb);
});
},
}