1 | 'use strict';
|
2 |
|
3 | const AWS = require('aws-sdk');
|
4 | const get = require('lodash.get');
|
5 | const fs = require('fs-extra');
|
6 | const path = require('path');
|
7 | const utils = require('./utils');
|
8 |
|
9 | /**
|
10 | * Copy, zip and upload lambda functions to S3
|
11 | *
|
12 | * @param {Object} config the configuration object
|
13 | * @param {String} kesFolder the path to the `.kes` folder
|
14 | * @param {String} bucket the S3 bucket name
|
15 | * @param {String} key the main folder to store the data in the bucket (stack)
|
16 | */
|
17 | class Lambda {
|
18 | constructor(config) {
|
19 | this.config = config;
|
20 | this.cf_basename = path.basename(config.cfFile, '.template.yml');
|
21 | this.kesFolder = config.kesFolder;
|
22 | this.distFolder = path.join(this.kesFolder, 'dist');
|
23 | this.buildFolder = path.join(this.kesFolder, 'build', this.cf_basename);
|
24 | this.bucket = get(config, 'bucket');
|
25 | this.key = path.join(this.config.stack, 'lambdas');
|
26 | this.grouped = {};
|
27 | }
|
28 |
|
29 | |
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 | buildS3Path(lambda) {
|
38 | if (lambda.source) {
|
39 |
|
40 | lambda.hash = this.getHash(lambda.source).toString();
|
41 | lambda.bucket = this.bucket;
|
42 |
|
43 | if (!this.grouped.hasOwnProperty(lambda.hash)) {
|
44 | this.grouped[lambda.hash] = lambda.name;
|
45 | }
|
46 |
|
47 |
|
48 | const lambdaName = this.grouped[lambda.hash];
|
49 | const zipFile = `${lambda.hash}-${lambdaName}.zip`;
|
50 | lambda.local = path.join(this.buildFolder, zipFile);
|
51 |
|
52 |
|
53 | lambda.remote = path.join(this.key, zipFile);
|
54 | }
|
55 | else if (lambda.s3Source) {
|
56 | lambda.remote = lambda.s3Source.key;
|
57 | lambda.bucket = lambda.s3Source.bucket;
|
58 | }
|
59 | return lambda;
|
60 | }
|
61 |
|
62 | |
63 |
|
64 |
|
65 |
|
66 |
|
67 |
|
68 | getHash(folderName, method) {
|
69 | if (!method) {
|
70 | method = 'shasum';
|
71 | }
|
72 |
|
73 | const alternativeMethod = 'sha1sum';
|
74 | let hash = utils.exec(`find ${folderName} -type f | \
|
75 | xargs ${method} | ${method} | awk '{print $1}' ${''}`, false);
|
76 |
|
77 | hash = hash.toString().replace(/\n/, '');
|
78 |
|
79 | if (hash.length === 0) {
|
80 | if (method === alternativeMethod) {
|
81 | throw new Error('You must either have shasum or sha1sum');
|
82 | }
|
83 | console.log(`switching to ${alternativeMethod}`);
|
84 | return this.getHash(folderName, alternativeMethod);
|
85 | }
|
86 |
|
87 | return hash;
|
88 | }
|
89 |
|
90 | |
91 |
|
92 |
|
93 |
|
94 |
|
95 |
|
96 | zipLambda(lambda) {
|
97 | console.log(`Zipping ${lambda.local}`);
|
98 |
|
99 |
|
100 | if (fs.existsSync(lambda.local)) {
|
101 | const stats = fs.statSync(lambda.local);
|
102 | if (stats.isFile() && stats.size > 0) {
|
103 | return Promise.resolve(lambda);
|
104 | }
|
105 | }
|
106 |
|
107 | return utils.zip(lambda.local, [lambda.source]).then(() => {
|
108 | console.log(`Zipped ${lambda.local}`);
|
109 | return lambda;
|
110 | });
|
111 | }
|
112 |
|
113 | |
114 |
|
115 |
|
116 |
|
117 |
|
118 |
|
119 |
|
120 |
|
121 |
|
122 |
|
123 | uploadLambda(lambda) {
|
124 | const s3 = new AWS.S3();
|
125 |
|
126 | const params = {
|
127 | Bucket: this.bucket,
|
128 | Key: lambda.remote,
|
129 | Body: fs.readFileSync(lambda.local)
|
130 | };
|
131 |
|
132 | return new Promise((resolve, reject) => {
|
133 |
|
134 | s3.headObject({
|
135 | Bucket: this.bucket,
|
136 | Key: lambda.remote
|
137 | }).promise().then((data) => {
|
138 | if (data.ContentLength !== params.Body.byteLength) {
|
139 | throw new Error('File sizes don\'t match');
|
140 | }
|
141 | console.log(`Already Uploaded: s3://${this.bucket}/${lambda.remote}`);
|
142 | return resolve(lambda);
|
143 | }).catch(() => {
|
144 | s3.upload(params, (e, r) => {
|
145 | if (e) return reject(e);
|
146 | console.log(`Uploaded: s3://${this.bucket}/${lambda.remote}`);
|
147 | return resolve(lambda);
|
148 | });
|
149 | });
|
150 | });
|
151 | }
|
152 |
|
153 | |
154 |
|
155 |
|
156 |
|
157 |
|
158 |
|
159 |
|
160 |
|
161 | zipAndUploadLambda(lambda) {
|
162 |
|
163 | const match = lambda.source.match(/.\.zip/);
|
164 | if (match) {
|
165 | lambda.local = lambda.source;
|
166 | return this.uploadLambda(lambda);
|
167 | }
|
168 | return this.zipLambda(lambda)
|
169 | .then(l => this.uploadLambda(l));
|
170 | }
|
171 |
|
172 | |
173 |
|
174 |
|
175 |
|
176 |
|
177 |
|
178 |
|
179 |
|
180 |
|
181 |
|
182 |
|
183 | process() {
|
184 | if (this.config.lambdas) {
|
185 |
|
186 | fs.mkdirpSync(this.buildFolder);
|
187 |
|
188 | let lambdas = this.config.lambdas;
|
189 |
|
190 |
|
191 | if (!Array.isArray(this.config.lambdas)) {
|
192 | lambdas = Object.keys(this.config.lambdas).map(name => {
|
193 | const lambda = this.config.lambdas[name];
|
194 | lambda.name = name;
|
195 | return lambda;
|
196 | });
|
197 | }
|
198 |
|
199 |
|
200 | lambdas.filter(l => l.npmSource).forEach(l => utils.exec(`npm install ${l.npmSource.name}@${l.npmSource.version}`));
|
201 |
|
202 |
|
203 | lambdas = lambdas.map(l => this.buildS3Path(l));
|
204 |
|
205 |
|
206 | let uniqueHashes = {};
|
207 | lambdas.filter(l => l.source).forEach(l => {
|
208 | uniqueHashes[l.hash] = l;
|
209 | });
|
210 | const jobs = Object.keys(uniqueHashes).map(l => this.zipAndUploadLambda(uniqueHashes[l]));
|
211 |
|
212 | return Promise.all(jobs)
|
213 | .then(() => {
|
214 |
|
215 |
|
216 |
|
217 | if (Array.isArray(this.config.lambdas)) {
|
218 | this.config.lambdas = lambdas;
|
219 | return this.config;
|
220 | }
|
221 | const tmp = {};
|
222 | lambdas.forEach(l => (tmp[l.name] = l));
|
223 | this.config.lambdas = tmp;
|
224 | return this.config;
|
225 | })
|
226 | .catch((e) => {
|
227 |
|
228 |
|
229 |
|
230 | if (e.message.includes('ENOENT')) {
|
231 | fs.removeSync(this.buildFolder);
|
232 | }
|
233 | throw e;
|
234 | });
|
235 | }
|
236 | else return Promise.resolve(this.config);
|
237 | }
|
238 |
|
239 | |
240 |
|
241 |
|
242 |
|
243 |
|
244 |
|
245 | updateSingleLambda(name) {
|
246 | const l = new AWS.Lambda();
|
247 |
|
248 |
|
249 | fs.mkdirpSync(this.buildFolder);
|
250 |
|
251 | let lambda;
|
252 | Object.keys(this.config.lambdas).forEach(n => {
|
253 | if (n === name) {
|
254 | lambda = this.config.lambdas[n];
|
255 | }
|
256 | });
|
257 |
|
258 | if (!lambda) {
|
259 | throw new Error('Lambda function is not defined in config.yml');
|
260 | }
|
261 | const stack = this.config.stackName;
|
262 | lambda = this.buildS3Path(lambda);
|
263 |
|
264 | console.log(`Updating ${name}`);
|
265 | let promise;
|
266 | const match = lambda.source.match(/.\.zip/);
|
267 | if (match) {
|
268 | lambda.local = lambda.source;
|
269 | promise = l.updateFunctionCode({
|
270 | FunctionName: `${stack}-${name}`,
|
271 | ZipFile: fs.readFileSync(lambda.source)
|
272 | }).promise();
|
273 | }
|
274 | else {
|
275 | promise = this.zipLambda(lambda).then(lambda => l.updateFunctionCode({
|
276 | FunctionName: `${stack}-${name}`,
|
277 | ZipFile: fs.readFileSync(lambda.local)
|
278 | }).promise());
|
279 | }
|
280 | return promise.then((r) => console.log(`Lambda function ${name} has been updated`));
|
281 | }
|
282 | }
|
283 |
|
284 | module.exports = Lambda;
|