-
-
Notifications
You must be signed in to change notification settings - Fork 71
/
Copy pathindex.js
494 lines (464 loc) · 17.5 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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
'use strict';
const BbPromise = require('bluebird');
const s3 = require('@auth0/s3');
const chalk = require('chalk');
const minimatch = require('minimatch');
const path = require('path');
const fs = require('fs');
const resolveStackOutput = require('./resolveStackOutput')
const messagePrefix = 'S3 Sync: ';
const mime = require('mime');
const child_process = require('child_process');
const toS3Path = (osPath) => osPath.replace(new RegExp(`\\${path.sep}`, 'g'), '/');
class ServerlessS3Sync {
constructor(serverless, options) {
this.serverless = serverless;
this.options = options || {};
this.servicePath = this.serverless.service.serverless.config.servicePath;
this.commands = {
s3sync: {
usage: 'Sync directories and S3 prefixes',
lifecycleEvents: [
'sync',
'metadata',
'tags'
],
commands: {
bucket: {
options: {
bucket: {
usage: 'Specify the bucket you want to deploy (e.g. "-b myBucket1")',
required: true,
shortcut: 'b',
type: 'string'
}
},
lifecycleEvents: [
'sync',
'metadata',
'tags'
]
}
}
}
};
this.hooks = {
'after:deploy:deploy': () => options.nos3sync ? undefined : BbPromise.bind(this).then(this.sync).then(this.syncMetadata).then(this.syncBucketTags),
'after:offline:start:init': () => options.nos3sync ? undefined : BbPromise.bind(this).then(this.sync).then(this.syncMetadata).then(this.syncBucketTags),
'after:offline:start': () => options.nos3sync ? undefined : BbPromise.bind(this).then(this.sync).then(this.syncMetadata).then(this.syncBucketTags),
'before:remove:remove': () => options.nos3sync ? undefined : BbPromise.bind(this).then(this.clear),
's3sync:sync': () => BbPromise.bind(this).then(this.sync),
's3sync:metadata': () => BbPromise.bind(this).then(this.syncMetadata),
's3sync:tags': () => BbPromise.bind(this).then(this.syncBucketTags),
's3sync:bucket:sync': () => BbPromise.bind(this).then(this.sync),
's3sync:bucket:metadata': () => BbPromise.bind(this).then(this.syncMetadata),
's3sync:bucket:tags': () => BbPromise.bind(this).then(this.syncBucketTags),
};
}
isOffline() {
return String(this.options.offline).toUpperCase() === 'TRUE' || process.env.IS_OFFLINE;
}
getEndpoint() {
return this.serverless.service.custom.s3Sync.hasOwnProperty('endpoint') ? this.serverless.service.custom.s3Sync.endpoint : null;
}
client() {
const provider = this.serverless.getProvider('aws');
let awsCredentials, region;
if (provider.cachedCredentials && typeof(provider.cachedCredentials.accessKeyId) != 'undefined'
&& typeof(provider.cachedCredentials.secretAccessKey) != 'undefined'
&& typeof(provider.cachedCredentials.sessionToken) != 'undefined') {
// Temporarily disabled the below below because Serverless framework is not interpolating ${env:foo}
// in provider.credentials.region or provider.cachedCredentials.region
// region = provider.cachedCredentials.region
region = provider.getRegion();
awsCredentials = {
accessKeyId: provider.cachedCredentials.accessKeyId,
secretAccessKey: provider.cachedCredentials.secretAccessKey,
sessionToken: provider.cachedCredentials.sessionToken,
}
} else {
region = provider.getRegion() || provider.getCredentials().region;
awsCredentials = provider.getCredentials().credentials;
}
let s3Options = {
region: region,
credentials: awsCredentials
};
if(this.getEndpoint() && this.isOffline()) {
s3Options.endpoint = new provider.sdk.Endpoint(this.serverless.service.custom.s3Sync.endpoint);
s3Options.s3ForcePathStyle = true;
}
const s3Client = new provider.sdk.S3(s3Options);
if(this.getEndpoint() && this.isOffline()) {
//see: https://github.com/aws/aws-sdk-js/issues/1157
s3Client.shouldDisableBodySigning = () => true
}
return s3.createClient({ s3Client });
}
sync() {
let s3Sync = this.serverless.service.custom.s3Sync;
if(s3Sync.hasOwnProperty('buckets')) {
s3Sync = s3Sync.buckets;
}
const cli = this.serverless.cli;
if (!Array.isArray(s3Sync)) {
cli.consoleLog(`${messagePrefix}${chalk.red('No configuration found')}`)
return Promise.resolve();
}
if (this.options.bucket) {
cli.consoleLog(`${messagePrefix}${chalk.yellow(`Syncing directory attached to S3 bucket ${this.options.bucket}...`)}`);
} else {
cli.consoleLog(`${messagePrefix}${chalk.yellow('Syncing directories and S3 prefixes...')}`);
}
const servicePath = this.servicePath;
const promises = s3Sync.map((s) => {
let bucketPrefix = '';
if (s.hasOwnProperty('bucketPrefix')) {
bucketPrefix = s.bucketPrefix;
}
let acl = 'private';
if (s.hasOwnProperty('acl')) {
acl = s.acl;
}
let followSymlinks = false;
if (s.hasOwnProperty('followSymlinks')) {
followSymlinks = s.followSymlinks;
}
let defaultContentType = undefined
if (s.hasOwnProperty('defaultContentType')) {
defaultContentType = s.defaultContentType;
}
if ((!s.bucketName && !s.bucketNameKey) || !s.localDir) {
throw 'Invalid custom.s3Sync';
}
let deleteRemoved = true;
if (s.hasOwnProperty('deleteRemoved')) {
deleteRemoved = s.deleteRemoved;
}
let preCommand = undefined
if (s.hasOwnProperty('preCommand')) {
preCommand = s.preCommand;
}
return this.getBucketName(s)
.then(bucketName => {
if (this.options.bucket && bucketName != this.options.bucket) {
// if the bucket option is given, that means we're in the subcommand where we're
// only syncing one bucket, so only continue if this bucket name matches
return null;
}
return new Promise((resolve) => {
const localDir = [servicePath, s.localDir].join('/');
if (typeof(preCommand) != 'undefined') {
cli.consoleLog(`${messagePrefix}${chalk.yellow('Running pre-command...')}`);
child_process.execSync(preCommand, { stdio: 'inherit' });
}
const params = {
maxAsyncS3: 5,
localDir,
deleteRemoved,
followSymlinks: followSymlinks,
getS3Params: (localFile, stat, cb) => {
const s3Params = {};
let onlyForEnv;
if(Array.isArray(s.params)) {
s.params.forEach((param) => {
const glob = Object.keys(param)[0];
if(minimatch(localFile, `${path.resolve(localDir)}/${glob}`)) {
Object.assign(s3Params, this.extractMetaParams(param) || {});
onlyForEnv = s3Params['OnlyForEnv'] || onlyForEnv;
}
});
// to avoid parameter validation error
delete s3Params['OnlyForEnv'];
}
if (onlyForEnv && onlyForEnv !== this.options.env) {
cb(null, null);
} else {
cb(null, s3Params);
}
},
s3Params: {
Bucket: bucketName,
Prefix: bucketPrefix,
ACL: acl
}
};
if (typeof(defaultContentType) != 'undefined') {
Object.assign(params, {defaultContentType: defaultContentType})
}
const uploader = this.client().uploadDir(params);
uploader.on('error', (err) => {
throw err;
});
let percent = 0;
uploader.on('progress', () => {
if (uploader.progressTotal === 0) {
return;
}
const current = Math.round((uploader.progressAmount / uploader.progressTotal) * 10) * 10;
if (current > percent) {
percent = current;
cli.printDot();
}
});
uploader.on('end', () => {
resolve('done');
});
});
});
});
return Promise.all(promises)
.then(() => {
cli.printDot();
cli.consoleLog('');
cli.consoleLog(`${messagePrefix}${chalk.yellow('Synced.')}`);
});
}
clear() {
let s3Sync = this.serverless.service.custom.s3Sync;
if(s3Sync.hasOwnProperty('buckets')) {
s3Sync = s3Sync.buckets;
}
if (!Array.isArray(s3Sync)) {
return Promise.resolve();
}
const cli = this.serverless.cli;
cli.consoleLog(`${messagePrefix}${chalk.yellow('Removing S3 objects...')}`);
const promises = s3Sync.map((s) => {
let bucketPrefix = '';
if (s.hasOwnProperty('bucketPrefix')) {
bucketPrefix = s.bucketPrefix;
}
return this.getBucketName(s)
.then(bucketName => {
return new Promise((resolve) => {
const params = {
Bucket: bucketName,
Prefix: bucketPrefix
};
const uploader = this.client().deleteDir(params);
uploader.on('error', (err) => {
throw err;
});
let percent = 0;
uploader.on('progress', () => {
if (uploader.progressTotal === 0) {
return;
}
const current = Math.round((uploader.progressAmount / uploader.progressTotal) * 10) * 10;
if (current > percent) {
percent = current;
cli.printDot();
}
});
uploader.on('end', () => {
resolve('done');
});
});
});
});
return Promise.all(promises)
.then(() => {
cli.printDot();
cli.consoleLog('');
cli.consoleLog(`${messagePrefix}${chalk.yellow('Removed.')}`);
});
}
syncMetadata() {
let s3Sync = this.serverless.service.custom.s3Sync;
if(s3Sync.hasOwnProperty('buckets')) {
s3Sync = s3Sync.buckets;
}
const cli = this.serverless.cli;
if (!Array.isArray(s3Sync)) {
cli.consoleLog(`${messagePrefix}${chalk.red('No configuration found')}`)
return Promise.resolve();
}
cli.consoleLog(`${messagePrefix}${chalk.yellow('Syncing metadata...')}`);
const servicePath = this.servicePath;
const promises = s3Sync.map( async (s) => {
let bucketPrefix = '';
if (s.hasOwnProperty('bucketPrefix') && s.bucketPrefix.length > 0) {
bucketPrefix = s.bucketPrefix.replace(/\/?$/, '').replace(/^\/?/, '/')
}
let acl = 'private';
if (s.hasOwnProperty('acl')) {
acl = s.acl;
}
if ((!s.bucketName && !s.bucketNameKey) || !s.localDir) {
throw 'Invalid custom.s3Sync';
}
const localDir = path.join(servicePath, s.localDir);
let filesToSync = [];
let ignoreFiles = [];
if(Array.isArray(s.params)) {
s.params.forEach((param) => {
const glob = Object.keys(param)[0];
let files = this.getLocalFiles(localDir, []);
minimatch.match(files, `${path.resolve(localDir)}${path.sep}${glob}`, {matchBase: true}).forEach((match) => {
const params = this.extractMetaParams(param);
if (ignoreFiles.includes(match)) return;
if (params['OnlyForEnv'] && params['OnlyForEnv'] !== this.options.env) {
ignoreFiles.push(match);
filesToSync = filesToSync.filter(e => e.name !== match);
return;
}
// to avoid Unexpected Parameter error
delete params['OnlyForEnv'];
filesToSync = filesToSync.filter(e => e.name !== match);
filesToSync.push({name: match, params});
});
});
}
return this.getBucketName(s)
.then(bucketName => {
if (this.options && this.options.bucket && bucketName != this.options.bucket) {
// if the bucket option is given, that means we're in the subcommand where we're
// only syncing one bucket, so only continue if this bucket name matches
return null;
}
return Promise.all(filesToSync.map((file) => {
return new Promise((resolve) => {
let contentTypeObject = {};
let detectedContentType = mime.getType(file.name)
if (detectedContentType !== null || s.hasOwnProperty('defaultContentType')) {
contentTypeObject.ContentType = detectedContentType ? detectedContentType : s.defaultContentType;
}
let params = {
...contentTypeObject,
...file.params,
...{
CopySource: toS3Path(file.name.replace(path.resolve(localDir) + path.sep, `${bucketName}${bucketPrefix == '' ? '' : bucketPrefix}/`)),
Key: toS3Path(file.name.replace(path.resolve(localDir) + path.sep, `${bucketPrefix ? bucketPrefix.replace(/^\//, '') + '/' : ''}`)),
Bucket: bucketName,
ACL: acl,
MetadataDirective: 'REPLACE'
}
};
const uploader = this.client().copyObject(params);
uploader.on('error', (err) => {
throw err;
});
uploader.on('end', () => {
resolve('done');
});
});
}));
});
});
return Promise.all((promises))
.then(() => {
cli.printDot();
cli.consoleLog('');
cli.consoleLog(`${messagePrefix}${chalk.yellow('Synced metadata.')}`);
});
}
syncBucketTags() {
let s3Sync = this.serverless.service.custom.s3Sync;
if(s3Sync.hasOwnProperty('buckets')) {
s3Sync = s3Sync.buckets;
}
const cli = this.serverless.cli;
if (!Array.isArray(s3Sync)) {
cli.consoleLog(`${messagePrefix}${chalk.red('No configuration found')}`)
return Promise.resolve();
}
cli.consoleLog(`${messagePrefix}${chalk.yellow('Updating bucket tags...')}`);
const promises = s3Sync.map( async (s) => {
if (!s.bucketName && !s.bucketNameKey) {
throw 'Invalid custom.s3Sync';
}
if (!s.bucketTags) {
// bucket tags not configured for this bucket, skip it
// so we don't require additional s3:getBucketTagging permissions
return null;
}
// convert the tag key/value pairs into a TagSet structure for the putBucketTagging command
const tagsToUpdate = Object.keys(s.bucketTags).map(tagKey => ({
Key: tagKey,
Value: s.bucketTags[tagKey]
}));
return this.getBucketName(s)
.then(bucketName => {
if (this.options && this.options.bucket && bucketName != this.options.bucket) {
// if the bucket option is given, that means we're in the subcommand where we're
// only syncing one bucket, so only continue if this bucket name matches
return null;
}
// AWS.S3 does not have an option to append tags to a bucket, it can only rewrite the whole set of tags
// To avoid removing system tags set by other tools, we read the existing tags, merge our tags in the list
// and then write them all back
return this.client().s3.getBucketTagging({ Bucket: bucketName }).promise()
.then(data => data.TagSet)
.then(existingTagSet => {
this.mergeTags(existingTagSet, tagsToUpdate);
const putParams = {
Bucket: bucketName,
Tagging: {
TagSet: existingTagSet
}
};
return this.client().s3.putBucketTagging(putParams).promise();
})
});
});
return Promise.all((promises))
.then(() => {
cli.printDot();
cli.consoleLog('');
cli.consoleLog(`${messagePrefix}${chalk.yellow('Updated bucket tags.')}`);
});
}
mergeTags(existingTagSet, tagsToMerge) {
tagsToMerge.forEach(tag => {
const existingTag = existingTagSet.find(et => et.Key === tag.Key);
if (existingTag) {
existingTag.Value = tag.Value;
} else {
existingTagSet.push(tag);
}
});
}
getLocalFiles(dir, files) {
const cli = this.serverless.cli;
try {
fs.accessSync(dir, fs.constants.R_OK);
} catch (e) {
cli.consoleLog(`${messagePrefix}${chalk.red(`The directory ${dir} does not exist.`)}`);
return files;
}
fs.readdirSync(dir).forEach(file => {
let fullPath = path.join(dir, file);
try {
fs.accessSync(fullPath, fs.constants.R_OK);
} catch (e) {
cli.consoleLog(`${messagePrefix}${chalk.red(`The file ${fullPath} doesn not exist.`)}`);
return;
}
if (fs.lstatSync(fullPath).isDirectory()) {
this.getLocalFiles(fullPath, files);
} else {
files.push(fullPath);
}
});
return files;
}
extractMetaParams(config) {
const validParams = {};
const keys = Object.keys(config);
for (let i = 0; i < keys.length; i++) {
Object.assign(validParams, config[keys[i]])
}
return validParams;
}
getBucketName(s) {
if (s.bucketName) {
return Promise.resolve(s.bucketName)
} else if (s.bucketNameKey) {
return resolveStackOutput(this, s.bucketNameKey)
} else {
return Promise.reject("Unable to find bucketName. Please provide a value for bucketName or bucketNameKey")
}
}
}
module.exports = ServerlessS3Sync;