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