forked from binoculars/aws-lambda-ffmpeg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
204 lines (182 loc) · 4.73 KB
/
gulpfile.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
var http = require('http');
var fs = require('fs');
var gulp = require('gulp');
var gutil = require('gulp-util');
var shell = require('gulp-shell');
var flatten = require('gulp-flatten');
var rename = require('gulp-rename');
var del = require('del');
var install = require('gulp-install');
var zip = require('gulp-zip');
var AWS = require('aws-sdk');
var runSequence = require('run-sequence');
var async = require('async');
var s3 = new AWS.S3();
var lambda = new AWS.Lambda();
var config;
try {
config = require('./config.json');
} catch (ex) {
config = {};
}
var filename = './build/ffmpeg-git-64bit-static.tar.xz';
var fileURL = 'http://johnvansickle.com/ffmpeg/builds/ffmpeg-git-64bit-static.tar.xz';
gulp.task('postinstall', function(cb) {
async.reject(
['config.json', 'test_event.json'],
fs.exists,
function(files) {
async.map(files, function(file, cb) {
return cb(null, gulp.src(file.replace(/\.json/, '_sample.json'))
.pipe(rename(file))
.pipe(gulp.dest('.'))
);
}, cb);
}
);
});
gulp.task('download-ffmpeg', function(cb) {
if(!fs.existsSync('./build')) {
fs.mkdirSync('./build');
}
var file = fs.createWriteStream(filename);
http.get(fileURL, function(response) {
response.pipe(file);
file.on('finish', function() {
file.close();
cb();
})
});
});
// Resorting to using a shell task. Tried a number of other things including
// LZMA-native, node-xz, decompress-tarxz. None of them work very well with this.
// This will probably work well for OS X and Linux, but maybe not Windows without Cygwin.
gulp.task('untar-ffmpeg', shell.task([
'tar -xvf ' + filename + ' -C ./build'
]));
gulp.task('copy-ffmpeg', function() {
return gulp.src(['build/ffmpeg-*/ffmpeg', 'build/ffmpeg-*/ffprobe'])
.pipe(flatten())
.pipe(gulp.dest('./dist'));
});
/*
From: https://medium.com/@AdamRNeary/a-gulp-workflow-for-amazon-lambda-61c2afd723b6
*/
// First we need to clean out the dist folder and remove the compiled zip file.
gulp.task('clean', function() {
return del([
'./build/*',
'./dist/*',
'./dist.zip'
]);
});
// The js task could be replaced with gulp-coffee as desired.
gulp.task('js', function() {
return gulp.src(['index.js', 'config.json'])
.pipe(gulp.dest('./dist'))
});
// Here we want to install npm packages to dist, ignoring devDependencies.
gulp.task('npm', function() {
return gulp.src('./package.json')
.pipe(gulp.dest('./dist'))
.pipe(install({production: true}));
});
// Now the dist directory is ready to go. Zip it.
gulp.task('zip', function() {
return gulp.src(['dist/**/*', '!dist/package.json', 'dist/.*'])
.pipe(zip('dist.zip'))
.pipe(gulp.dest('./'));
});
var cloudFormation = new AWS.CloudFormation();
var packageInfo = require('./package.json');
var bucket = config.functionBucket;
var functionName = packageInfo.name;
var key = functionName + '.zip';
// Upload the function code to S3
gulp.task('upload', function(cb) {
s3.upload({
Bucket: bucket,
Key: key,
Body: fs.createReadStream('./dist.zip')
}, cb);
});
var stackName = packageInfo.name;
// Deploy the CloudFormation Stack
gulp.task('deployStack', function(cb) {
cloudFormation.describeStacks({
StackName: stackName
}, function(err) {
var operation = err ? 'createStack' : 'updateStack';
cloudFormation[operation]({
StackName: stackName,
Capabilities: [
'CAPABILITY_IAM'
],
Parameters: [
{
ParameterKey: 'SourceBucketName',
ParameterValue: config.sourceBucket
},
{
ParameterKey: 'DestinationBucketName',
ParameterValue: config.destinationBucket
},
{
ParameterKey: 'LambdaS3Bucket',
ParameterValue: bucket
},
{
ParameterKey: 'LambdaS3Key',
ParameterValue: key
}
],
TemplateBody: fs.readFileSync('./cloudformation.json', {encoding: 'utf8'})
}, cb);
});
});
// Once the stack is deployed, this will update the function if the code is changed without recreating the stack
gulp.task('updateCode', function(cb) {
async.waterfall([
function(cb) {
cloudFormation.describeStackResource({
StackName: stackName,
LogicalResourceId: 'Lambda'
}, cb);
},
function(data, cb) {
lambda.updateFunctionCode({
FunctionName: data.StackResourceDetail.PhysicalResourceId,
S3Bucket: bucket,
S3Key: key
}, cb);
}
], cb);
});
// Builds the function and uploads
gulp.task('build-upload', function(cb) {
return runSequence(
'clean',
'download-ffmpeg',
'untar-ffmpeg',
['copy-ffmpeg', 'js', 'npm'],
'zip',
'upload',
cb
);
});
// For an already created stack
gulp.task('update', function(cb) {
return runSequence(
'build-upload',
'updateCode',
cb
);
});
// For a new stack (or you change cloudformation.json)
gulp.task('default', function(cb) {
return runSequence(
'build-upload',
'deployStack',
cb
);
});