-
Notifications
You must be signed in to change notification settings - Fork 22
/
index.js
138 lines (114 loc) · 3.39 KB
/
index.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
"use strict";
const AWS = require('aws-sdk');
const fs = require('fs');
const path = require('path');
const fsp = require('fs-promise');
const exec = require('child_process').exec;
function AppNotFoundError(message) {
let error = new Error(message);
error.name = 'AppNotFoundError';
return error;
}
/*
* Downloader class that downloads the latest version of the deployed
* app from S3 and unzips it.
*/
class S3Downloader {
constructor(options) {
this.ui = options.ui;
this.configBucket = options.bucket;
this.configKey = options.key;
this.s3 = new AWS.S3({
apiVersion: '2006-03-01',
signatureVersion: 'v4',
accessKeyId: options.accessKeyId,
secretAccessKey: options.secretAccessKey,
region: options.region
});
}
download() {
if (!this.configBucket || !this.configKey) {
this.ui.writeError('no S3 bucket or key provided; not downloading app');
return Promise.reject(new AppNotFoundError());
}
return this.fetchCurrentVersion()
.then(() => this.removeOldApp())
.then(() => this.downloadAppZip())
.then(() => this.unzipApp())
.then(() => this.installNPMDependencies())
.then(() => this.outputPath);
}
removeOldApp() {
if (!this.outputPath) {
return Promise.resolve();
}
this.ui.writeLine('removing ' + this.outputPath);
return fsp.remove(this.outputPath);
}
fetchCurrentVersion() {
let bucket = this.configBucket;
let key = this.configKey;
this.ui.writeLine('fetching current app version from ' + bucket + '/' + key);
let params = {
Bucket: bucket,
Key: key
};
return this.s3.getObject(params).promise()
.then(data => {
let config = JSON.parse(data.Body);
this.ui.writeLine('got config', config);
this.appBucket = config.bucket;
this.appKey = config.key;
this.zipPath = path.basename(config.key);
this.outputPath = outputPathFor(this.zipPath);
});
}
downloadAppZip() {
return new Promise((res, rej) => {
let bucket = this.appBucket;
let key = this.appKey;
let params = {
Bucket: bucket,
Key: key
};
let zipPath = this.zipPath;
let file = fs.createWriteStream(zipPath);
let request = this.s3.getObject(params);
this.ui.writeLine("saving S3 object " + bucket + "/" + key + " to " + zipPath);
request.createReadStream().pipe(file)
.on('close', res)
.on('error', rej);
});
}
unzipApp() {
let zipPath = this.zipPath;
return this.exec('unzip -q ' + zipPath)
.then(() => {
this.ui.writeLine("unzipped " + zipPath);
});
}
installNPMDependencies() {
return this.exec(`cd ${this.outputPath} && npm install -s`)
.then(() => this.ui.writeLine('installed npm dependencies'))
.catch(() => this.ui.writeError('unable to install npm dependencies'));
}
exec(command) {
return new Promise((resolve, reject) => {
exec(command, (error, stdout, stderr) => {
if (error) {
this.ui.writeError(`error running command ${command}`);
this.ui.writeError(stderr);
reject(error);
} else {
resolve();
}
});
});
}
}
function outputPathFor(zipPath) {
let name = path.basename(zipPath, '.zip');
// Remove MD5 hash
return name.split('-').slice(0, -1).join('-');
}
module.exports = S3Downloader;