-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.js
236 lines (192 loc) · 7.6 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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
/* eslint-env node */
'use strict';
const DeployPluginBase = require('ember-cli-deploy-plugin');
function _list(opts) {
let AWS = require('aws-sdk');
let RSVP = require('rsvp');
let accessKeyId = opts.accessKeyId;
let secretAccessKey = opts.secretAccessKey;
let archivePrefix = opts.archivePrefix;
let bucket = opts.bucket;
let region = opts.region;
let profile = opts.profile;
let manifestKey = opts.manifestKey
let client = new AWS.S3({
accessKeyId,
secretAccessKey,
region
});
if (profile) {
AWS.config.credentials = new AWS.SharedIniFileCredentials({
profile: profile
});
}
let listObjects = RSVP.denodeify(client.listObjects.bind(client));
let getObject = RSVP.denodeify(client.getObject.bind(client));
let revisionsResults;
return listObjects({ Bucket: bucket, Prefix: archivePrefix })
.then((results) => {
revisionsResults = results;
return getObject({ Bucket: bucket, Key: manifestKey });
})
.then((current) => {
return { revisions: revisionsResults, current };
})
.catch(() => {
return { revisions: revisionsResults, current: { Body: '{}'} };
})
.then((result) => {
if (result.revisions.length < 1) {
return { revisions: [] };
}
let revisionsData = result.revisions;
let current = result.current;
let data = revisionsData.Contents;
let body = current.Body;
let manifestData = JSON.parse(body);
let revisions = data.sort(function(a, b) {
return new Date(b.LastModified) - new Date(a.LastModified);
})
.map((d) => {
let match = d.Key.match(new RegExp(archivePrefix+'(.*)\\.zip'));
if (!match) {
return; // ignore files that are no zipped app builds
}
let revision = match[1];
return {
revision,
timestamp: d.LastModified,
active: d.Key === manifestData.key
}
}).filter((d) => d); // filter out empty values
return { revisions };
});
}
module.exports = {
name: 'ember-cli-deploy-fastboot-app-server-aws',
createDeployPlugin: function(options) {
let DeployPlugin = DeployPluginBase.extend({
name: options.name,
defaultConfig: {
archivePrefix: function(context) {
return context.fastbootArchivePrefix;
},
revisionKey: function(context) {
let revisionKey = context.revisionData && context.revisionData.revisionKey;
return context.commandOptions.revision || revisionKey;
},
downloaderManifestContent: function(context) {
// setup via ember-cli-deploy-fastboot-app-server plugin
return context.fastbootDownloaderManifestContent;
},
manifestKey: 'fastboot-deploy-info.json',
awsPrefix: ''
},
requiredConfig: ['bucket', 'region'],
activate: function(/* context */) {
let revisionKey = this.readConfig('revisionKey');
let bucket = this.readConfig('bucket');
let archivePrefix = this.readConfig('archivePrefix');
let awsPrefix = this.readConfig('awsPrefix');
// update manifest-file to point to passed revision
let downloaderManifestContent = this.readConfig('downloaderManifestContent');
let buildKey = `${archivePrefix}${revisionKey}.zip`;
if (awsPrefix) {
buildKey = `${awsPrefix}/${buildKey}`;
}
this.log(`creating manifest for bucket: ${bucket} and buildKey: ${buildKey}`, {verbose: true});
let manifest = downloaderManifestContent(bucket, buildKey);
let AWS = require('aws-sdk');
let RSVP = require('rsvp');
let accessKeyId = this.readConfig('accessKeyId');
let secretAccessKey = this.readConfig('secretAccessKey');
let region = this.readConfig('region');
let manifestKey = this.readConfig('manifestKey');
manifestKey = awsPrefix ? `${awsPrefix}/${manifestKey}` : manifestKey;
let client = new AWS.S3({
accessKeyId,
secretAccessKey,
region
});
let putObject = RSVP.denodeify(client.putObject.bind(client));
this.log(`updating manifest at ${manifestKey}`, {verbose: true});
return putObject({
Bucket: bucket,
Key: manifestKey,
Body: manifest,
ACL: 'public-read'
});
},
upload: function(context) {
let AWS = require('aws-sdk');
let RSVP = require('rsvp');
let fs = require('fs');
let accessKeyId = this.readConfig('accessKeyId');
let secretAccessKey = this.readConfig('secretAccessKey');
let bucket = this.readConfig('bucket');
let region = this.readConfig('region');
let awsPrefix = this.readConfig('awsPrefix');
let client = new AWS.S3({
accessKeyId,
secretAccessKey,
region
});
let putObject = RSVP.denodeify(client.putObject.bind(client));
let data = fs.readFileSync(context.fastbootArchivePath);
let key = awsPrefix ? `${awsPrefix}/${context.fastbootArchiveName}` : context.fastbootArchiveName;
this.log(`uploading fastboot archive to ${bucket}/${key}`, {verbose: true});
return putObject({
Bucket: bucket,
Body: data,
Key: key
});
},
fetchRevisions: function() {
let accessKeyId = this.readConfig('accessKeyId');
let secretAccessKey = this.readConfig('secretAccessKey');
let archivePrefix = this.readConfig('archivePrefix');
let bucket = this.readConfig('bucket');
let region = this.readConfig('region');
let manifestKey = this.readConfig('manifestKey');
let awsPrefix = this.readConfig('awsPrefix');
archivePrefix = awsPrefix ? `${awsPrefix}/${archivePrefix}` : archivePrefix;
manifestKey = awsPrefix ? `${awsPrefix}/${manifestKey}` : manifestKey;
let opts = {
accessKeyId, secretAccessKey, archivePrefix, bucket, region, manifestKey
};
return _list(opts)
.then((data) => {
let revisions = data.revisions;
revisions.forEach(r => {
this.log(`${r.revision} | ${r.timestamp} | active: ${r.active}`, {verbose: true});
});
return { revisions };
});
},
fetchInitialRevisions: function() {
let accessKeyId = this.readConfig('accessKeyId');
let secretAccessKey = this.readConfig('secretAccessKey');
let archivePrefix = this.readConfig('archivePrefix');
let bucket = this.readConfig('bucket');
let region = this.readConfig('region');
let profile = this.readConfig('profile');
let manifestKey = this.readConfig('manifestKey');
let awsPrefix = this.readConfig('awsPrefix');
archivePrefix = awsPrefix ? `${awsPrefix}/${archivePrefix}` : archivePrefix;
manifestKey = awsPrefix ? `${awsPrefix}/${manifestKey}` : manifestKey;
let opts = {
accessKeyId, secretAccessKey, archivePrefix, bucket, region, profile, manifestKey
};
return _list(opts, this)
.then((data) => {
let revisions = data.revisions;
revisions.forEach(r => {
this.log(`${r.revision} | ${r.timestamp} | active: ${r.active}`, {verbose: true});
});
return { initialRevisions: revisions };
});
}
});
return new DeployPlugin();
}
};