-
Notifications
You must be signed in to change notification settings - Fork 0
/
prepareDeploy.js
122 lines (106 loc) · 3.52 KB
/
prepareDeploy.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
111
112
113
114
115
116
117
118
119
120
121
/*
* Script to prepare a deploy on AWS Lambda
*/
var fs = require('fs');
var path = require('path');
var zip = require('node-zip');
console.log("Prepare the AWS Lambda deploy package.")
var configPath = require('./package.json').alexa.configPath;
console.log("Using configuration path: " + configPath);
var certPath = './certs'
var configFile = configPath + '/deployConfig.js'
var cfg = require(configFile);
console.log("IOTEndpoint: " + cfg.IOTEndpoint);
console.log("certPath: " + cfg.certPath);
console.log("certID: " + cfg.certID);
if (!fs.existsSync(certPath)) {
fs.mkdirSync(certPath);
}
var count = 0;
var zipper = new zip();
var allfiles = [];
// -------------- Add secret files pointed to by the deployConfig.js
allfiles.push({source: configFile, target: path.join(certPath, 'deployConfig.js')});
var certFiles = ['root-CA.crt',
cfg.certID+'-certificate.pem.crt',
cfg.certID+'-public.pem.key',
cfg.certID+'-private.pem.key',
]
for (var ix = 0 ; ix < certFiles.length; ix++) {
allfiles.push({source: path.join(cfg.certPath, certFiles[ix]), target: path.join(certPath, certFiles[ix])});
}
// -------------- Add sourcefiles and node_modules
allfiles.push({source: 'index.js', target: 'index.js'});
allfiles.push({source: 'iotgateway.js', target: 'iotgateway.js'});
allfiles.push({source: 'package.json', target: 'package.json'});
allfiles.push({source: 'node_modules', target: 'node_modules'});
console.log(" Zipping " + allfiles.length + " files");
for (var ix = 0; ix < allfiles.length; ix++) {
var cc = ix;
copyAndZip(allfiles[ix], function(file, err) {
console.log("Done with "+file.target);
if (err) {
console.error("********** Problems with "+file.source + ": " + err.message);
return;
}
if (count == 0) {
var zipfileName = 'deploy.zip';
console.log("Zipping "+ zipfileName);
var data = zipper.generate({ base64:false, compression: 'DEFLATE' });
fs.writeFileSync(zipfileName, data, 'binary');
}
});
}
function copyAndZip(file, cb) {
count++;
console.log("CopyZip "+ file.source + " => " + file.target + " "+count);
if (!fs.existsSync(file.source)) {
cb(file, new Error("File "+ file.source + " does not exist."));
return;
}
copyFile(file.source, file.target, function(err) {
if (err) cb(file, err);
else {
addFile(file.target);
count--;
cb(file);
}
});
}
function addFile(filePath) {
if (fs.lstatSync(filePath).isDirectory()) {
console.log("Zipping folder "+ filePath);
zipper.folder(filePath);
var directory = fs.readdirSync(filePath);
directory.forEach(function(subfilepath) {
var filename = path.join(filePath,subfilepath);
console.log(" Zipping file "+ filename);
addFile(filename);
});
} else {
zipper.file(filePath, fs.readFileSync(filePath));
}
}
function copyFile(source, target, cb) {
var cbCalled = false;
if (source == target) {done(); return;}
console.log("Copying "+ source + " => " + target);
var rd = fs.createReadStream(source);
rd.on("error", function(err) {
done(err);
});
var wr = fs.createWriteStream(target);
wr.on("error", function(err) {
done(err);
});
wr.on("close", function(ex) {
done();
});
rd.pipe(wr);
function done(err) {
if (!cbCalled) {
cb(err);
cbCalled = true;
}
}
}