-
Notifications
You must be signed in to change notification settings - Fork 819
Expand file tree
/
Copy pathruntime.ts
More file actions
234 lines (196 loc) · 7.51 KB
/
runtime.ts
File metadata and controls
234 lines (196 loc) · 7.51 KB
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
import {
CheckDependenciesResult,
PackageRequest,
PackageResult,
BuildRequest,
BuildResult,
BuildType,
} from '@aws-amplify/amplify-function-plugin-interface';
import * as which from 'which';
import execa from 'execa';
import archiver from 'archiver';
import fs from 'fs-extra';
import * as glob from 'glob';
import path from 'path';
import { SemVer, coerce, gte, lt } from 'semver';
import { BIN_LOCAL, BIN, SRC, MAIN_BINARY, DIST, MAIN_BINARY_WIN } from './constants';
import { AmplifyError } from '@aws-amplify/amplify-cli-core';
const executableName = 'go';
const minimumVersion = <SemVer>coerce('1.0');
const maximumVersion = <SemVer>coerce('2.0');
let executablePath: string | null;
export const executeCommand = (
args: string[],
streamStdio: boolean,
env: Record<string, string> = {},
cwd: string | undefined = undefined,
stdioInput: string | undefined = undefined,
): string => {
try {
const output = execa.sync(executableName, args, {
stdio: streamStdio === true ? 'inherit' : 'pipe',
env,
cwd,
input: stdioInput,
});
if (output.exitCode !== 0) {
throw new AmplifyError('PackagingLambdaFunctionError', { message: `${executableName} failed, exit code was ${output.exitCode}` });
}
return output.stdout;
} catch (err) {
throw new AmplifyError('PackagingLambdaFunctionError', { message: `${executableName} failed, error message was ${err.message}` }, err);
}
};
const isBuildStale = (resourceDir: string, lastBuildTimeStamp: Date, outDir: string) => {
// If output directory does not exists or empty, rebuild required
if (!fs.existsSync(outDir) || glob.sync(`${outDir}/**`).length == 0) {
return true;
}
// If the timestamp of the src directory is newer than last build, rebuild required
const srcDir = path.join(resourceDir, SRC);
const dirTime = new Date(fs.statSync(srcDir).mtime);
if (dirTime > lastBuildTimeStamp) {
return true;
}
const fileUpdatedAfterLastBuild = glob
.sync(`${resourceDir}/${SRC}/**`)
.find((file) => new Date(fs.statSync(file).mtime) > lastBuildTimeStamp);
return !!fileUpdatedAfterLastBuild;
};
export const buildResource = async ({ buildType, srcRoot, lastBuildTimeStamp }: BuildRequest): Promise<BuildResult> => {
let rebuilt = false;
const buildDir = buildType === BuildType.DEV ? BIN_LOCAL : BIN;
const outDir = path.join(srcRoot, buildDir);
const isWindows = process.platform.startsWith('win');
if (!lastBuildTimeStamp || isBuildStale(srcRoot, lastBuildTimeStamp, outDir)) {
const srcDir = path.join(srcRoot, SRC);
// Clean and/or create the output directory
if (fs.existsSync(outDir)) {
fs.emptyDirSync(outDir);
} else {
fs.mkdirSync(outDir);
}
const envVars: any = { GOPROXY: 'direct' };
if (buildType === BuildType.PROD) {
envVars.GOOS = 'linux';
envVars.GOARCH = 'amd64';
}
if (isWindows) {
envVars.CGO_ENABLED = 0;
executeCommand(['install', 'github.com/aws/aws-lambda-go/cmd/build-lambda-zip@latest'], true, envVars, srcDir);
}
// for go@1.16, dependencies must be manually installed
executeCommand(['mod', 'tidy', '-v'], true, envVars, srcDir);
// Execute the build command, cwd must be the source file directory (Windows requires it)
// Details: https://github.com/aws/aws-lambda-go
executeCommand(['build', '-o', '../bin/bootstrap', '.'], true, envVars, srcDir);
rebuilt = true;
}
return {
rebuilt,
};
};
export const getGoVersion = (): SemVer => {
// Validate go version
const versionOutput = executeCommand(['version'], false);
if (versionOutput) {
const parts = versionOutput.split(' ');
// Output: go version go1.14 darwin/amd64
if (parts.length !== 4 || !parts[2].startsWith('go') || coerce(parts[2].slice(2)) === null) {
throw new Error(`Invalid version string: ${versionOutput}`);
}
const goVersion = <SemVer>coerce(parts[2].slice(2));
return goVersion;
}
throw new Error(`Invalid version string: ${versionOutput}`);
};
export const checkDependencies = async (): Promise<CheckDependenciesResult> => {
// Check if go is in the path
executablePath = which.sync(executableName, {
nothrow: true,
});
if (executablePath === null) {
return {
hasRequiredDependencies: false,
errorMessage: `${executableName} executable was not found in PATH, make sure it's available. It can be installed from https://golang.org/doc/install`,
};
}
const version = getGoVersion();
if (lt(version, minimumVersion) || gte(version, maximumVersion)) {
return {
hasRequiredDependencies: false,
errorMessage: `${executableName} version found was: ${version.format()}, but must be between ${minimumVersion.format()} and ${maximumVersion.format()}`,
};
}
return {
hasRequiredDependencies: true,
};
};
export const packageResource = async (request: PackageRequest, context: any): Promise<PackageResult> => {
// check if repackaging is needed
if (!request.lastPackageTimeStamp || request.lastBuildTimeStamp > request.lastPackageTimeStamp) {
const packageHash = await context.amplify.hashDir(request.srcRoot, [DIST]);
const zipFn = process.platform.startsWith('win') ? winZip : nixZip;
try {
await zipFn(request.srcRoot, request.dstFilename, context.print);
} catch (err) {
throw new AmplifyError(
'PackagingLambdaFunctionError',
{ message: `Packaging go function failed, error message was ${err.message}` },
err,
);
}
return { packageHash };
}
return {};
};
const winZip = async (src: string, dest: string, print: any) => {
// get lambda zip tool with the fix of https://go.dev/doc/go-get-install-deprecation
const version = getGoVersion();
try {
if (gte(version, <SemVer>coerce('1.17'))) {
await execa(executableName, ['install', 'github.com/aws/aws-lambda-go/cmd/build-lambda-zip@latest']);
} else {
await execa(executableName, ['get', '-u', 'github.com/aws/aws-lambda-go/cmd/build-lambda-zip']);
}
} catch (error: unknown) {
throw new Error(`Error installing build-lambda-zip: ${error}`);
}
const goPath = process.env.GOPATH;
if (!goPath) {
throw new Error('Could not determine GOPATH. Make sure it is set.');
}
await execa(path.join(goPath, 'bin', 'build-lambda-zip.exe'), ['-o', dest, path.join(src, BIN, MAIN_BINARY)]);
const resourceName = src.split(path.sep).pop();
print.warning(
`If the function ${resourceName} depends on assets outside of the go binary, you'll need to manually zip the binary along with the assets using WSL or another shell that generates a *nix-like zip file.`,
);
print.warning('See https://github.com/aws/aws-lambda-go/issues/13#issuecomment-358729411.');
};
const nixZip = async (src: string, dest: string): Promise<void> => {
const outDir = path.join(src, BIN);
const mainFile = path.join(outDir, MAIN_BINARY);
// zip source and dependencies and write to specified file
const file = fs.createWriteStream(dest);
return new Promise<void>((resolve, reject) => {
file.on('close', () => {
resolve();
});
file.on('error', (err) => {
reject(new Error(`Failed to zip with error: [${err}]`));
});
const zip = archiver.create('zip', {});
zip.pipe(file);
// Add the main file and make sure to set 755 as mode so it will be runnable by Lambda
zip.file(mainFile, {
name: MAIN_BINARY,
mode: 755,
});
// Add every other files in the out directory
zip.glob('**/*', {
cwd: outDir,
ignore: [mainFile],
});
void zip.finalize();
});
};