1 | 'use strict';
|
2 |
|
3 | function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { step("next", value); }, function (err) { step("throw", err); }); } } return step("next"); }); }; }
|
4 |
|
5 | const fs = require('fs-extra');
|
6 | const path = require('path');
|
7 | const util = require('util');
|
8 | const utils = require('kes').utils;
|
9 | const yauzl = require('yauzl');
|
10 |
|
11 | const { Lambda } = require('kes');
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 | class UpdatedLambda extends Lambda {
|
22 | |
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 | constructor(config) {
|
30 | super(config);
|
31 | this.config = config;
|
32 | }
|
33 |
|
34 | |
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 | buildAllLambdaConfiguration(configKey) {
|
46 | if (this.config[configKey]) {
|
47 | let lambdas = this.config[configKey];
|
48 |
|
49 | if (!Array.isArray(this.config[configKey])) {
|
50 | lambdas = Object.keys(this.config[configKey]).map(name => {
|
51 | const lambda = this.config[configKey][name];
|
52 | lambda.name = name;
|
53 | return lambda;
|
54 | });
|
55 | }
|
56 | lambdas.forEach(lambda => this.buildS3Path(lambda));
|
57 | }
|
58 | }
|
59 |
|
60 | |
61 |
|
62 |
|
63 |
|
64 |
|
65 |
|
66 | addWorkflowLambdaHashes() {
|
67 | Object.keys(this.config.lambdas).forEach(key => {
|
68 | if (key in this.config.workflowLambdas && 'hash' in this.config.lambdas[key]) {
|
69 | this.config.workflowLambdas[key].hash = this.config.lambdas[key].hash;
|
70 | }
|
71 | });
|
72 | }
|
73 |
|
74 | |
75 |
|
76 |
|
77 |
|
78 |
|
79 |
|
80 |
|
81 |
|
82 | zipLambda(lambda) {
|
83 | var _this = this;
|
84 |
|
85 | return _asyncToGenerator(function* () {
|
86 |
|
87 |
|
88 | if (yield fs.pathExists(lambda.local)) {
|
89 | try {
|
90 | yield util.promisify(yauzl.open)(lambda.local);
|
91 | return Promise.resolve(lambda);
|
92 | } catch (e) {
|
93 | console.log(`${lambda.local} is invalid and will be rebuilt`);
|
94 | }
|
95 | }
|
96 |
|
97 | let msg = `Zipping ${lambda.local}`;
|
98 | const fileList = [lambda.source];
|
99 | if (lambda.useMessageAdapter) {
|
100 | const kesFolder = path.join(_this.config.kesFolder, 'build', 'adapter');
|
101 | fileList.push(kesFolder);
|
102 | msg += ' and injecting message adapter';
|
103 | }
|
104 |
|
105 | console.log(`${msg} for ${lambda.name}`);
|
106 |
|
107 | try {
|
108 | yield utils.zip(lambda.local, fileList);
|
109 | } catch (e) {
|
110 | console.log(`Error zipping ${e}`);
|
111 | throw e;
|
112 | }
|
113 |
|
114 | return lambda;
|
115 | })();
|
116 | }
|
117 |
|
118 | getLambdaVersionFromPackageFile(sourceDir) {
|
119 | let packageJson = '{}';
|
120 | const JsonFilePath = `${sourceDir}/../package.json`;
|
121 |
|
122 | try {
|
123 | if (fs.existsSync(JsonFilePath)) {
|
124 | packageJson = fs.readFileSync(`${JsonFilePath}`);
|
125 | }
|
126 | } catch (e) {
|
127 | console.log(`Error reading package.json from ${JsonFilePath}`);
|
128 | throw e;
|
129 | }
|
130 | const packageData = JSON.parse(packageJson);
|
131 |
|
132 | if (!packageData || !packageData.version) {
|
133 | return null;
|
134 | }
|
135 | return packageData.version;
|
136 | }
|
137 |
|
138 | |
139 |
|
140 |
|
141 |
|
142 |
|
143 |
|
144 |
|
145 |
|
146 |
|
147 |
|
148 |
|
149 |
|
150 | buildS3Path(lambdaArg) {
|
151 | const lambda = super.buildS3Path(lambdaArg);
|
152 |
|
153 | if (lambda.s3Source && lambda.s3Source.uniqueIdentifier) {
|
154 | const uniqueIdentifier = lambda.s3Source.uniqueIdentifier;
|
155 | if (!uniqueIdentifier.match(/^[a-z0-9]+$/)) {
|
156 | throw new Error(`Invalid uniqueIdentifier ${uniqueIdentifier} provided for lambda`);
|
157 | }
|
158 | lambda.hash = uniqueIdentifier;
|
159 | lambda.humanReadableIdentifier = uniqueIdentifier;
|
160 | } else {
|
161 | const lambdaVersion = this.getLambdaVersionFromPackageFile(lambda.source);
|
162 | lambda.humanReadableIdentifier = lambdaVersion || lambda.hash;
|
163 | }
|
164 |
|
165 |
|
166 | if (lambda.useMessageAdapter && UpdatedLambda.messageAdapterZipFileHash) {
|
167 | lambda.local = path.join(path.dirname(lambda.local), `${UpdatedLambda.messageAdapterZipFileHash}-${path.basename(lambda.local)}`);
|
168 | lambda.remote = path.join(path.dirname(lambda.remote), `${UpdatedLambda.messageAdapterZipFileHash}-${path.basename(lambda.remote)}`);
|
169 | }
|
170 |
|
171 | return lambda;
|
172 | }
|
173 | }
|
174 |
|
175 | module.exports = UpdatedLambda;
|
176 |
|
177 | UpdatedLambda.messageAdapterZipFileHash = undefined; |
\ | No newline at end of file |