-
Notifications
You must be signed in to change notification settings - Fork 42
/
libBuild.sh
373 lines (328 loc) · 9.17 KB
/
libBuild.sh
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
#!/usr/bin/env bash
set -Eeuo pipefail
# Regions that support arm64 architecture
REGIONS_ARM=(
af-south-1
ap-northeast-1
ap-northeast-2
ap-northeast-3
ap-south-1
ap-southeast-1
ap-southeast-2
ap-southeast-3
ca-central-1
eu-central-1
eu-north-1
eu-south-1
eu-west-1
eu-west-2
eu-west-3
me-south-1
sa-east-1
us-east-1
us-east-2
us-west-1
us-west-2
)
REGIONS_X86=(
af-south-1
ap-northeast-1
ap-northeast-2
ap-northeast-3
ap-south-1
ap-south-2
ap-southeast-1
ap-southeast-2
ap-southeast-3
ap-southeast-4
ca-central-1
eu-central-1
eu-central-2
eu-north-1
eu-south-1
eu-south-2
eu-west-1
eu-west-2
eu-west-3
me-central-1
me-south-1
sa-east-1
us-east-1
us-east-2
us-west-1
us-west-2
)
EXTENSION_DIST_DIR=extensions
EXTENSION_DIST_ZIP=extension.zip
EXTENSION_DIST_PREVIEW_FILE=preview-extensions-ggqizro707
EXTENSION_VERSION=2.3.15
function list_all_regions {
aws ec2 describe-regions \
--all-regions \
--query "Regions[].{Name:RegionName}" \
--output text | sort
}
function fetch_extension {
arch=$1
url="https://github.com/newrelic/newrelic-lambda-extension/releases/download/v${EXTENSION_VERSION}/newrelic-lambda-extension.${arch}.zip"
rm -rf $EXTENSION_DIST_DIR $EXTENSION_DIST_ZIP
curl -L $url -o $EXTENSION_DIST_ZIP
}
function download_extension {
fetch_extension $@
unzip $EXTENSION_DIST_ZIP -d .
rm -f $EXTENSION_DIST_ZIP
}
function layer_name_str() {
rt_part="LambdaExtension"
arch_part=""
case $1 in
"java8.al2")
rt_part="Java8"
;;
"java11")
rt_part="Java11"
;;
"java17")
rt_part="Java17"
;;
"java21")
rt_part="Java21"
;;
"python3.7")
rt_part="Python37"
;;
"python3.8")
rt_part="Python38"
;;
"python3.9")
rt_part="Python39"
;;
"python3.10")
rt_part="Python310"
;;
"python3.11")
rt_part="Python311"
;;
"python3.12")
rt_part="Python312"
;;
"python3.13")
rt_part="Python313"
;;
"nodejs18.x")
rt_part="NodeJS18X"
;;
"nodejs20.x")
rt_part="NodeJS20X"
;;
"nodejs22.x")
rt_part="NodeJS22X"
;;
"ruby3.2")
rt_part="Ruby32"
;;
"ruby3.3")
rt_part="Ruby33"
;;
"dotnet")
rt_part="Dotnet"
;;
esac
case $2 in
"arm64")
arch_part="ARM64"
;;
"x86_64")
arch_part=""
;;
esac
echo "NewRelic${rt_part}${arch_part}"
}
function s3_prefix() {
name="nr-extension"
case $1 in
"java8.al2")
name="java-8"
;;
"java11")
name="java-11"
;;
"python3.7")
name="nr-python3.7"
;;
"python3.8")
name="nr-python3.8"
;;
"python3.9")
name="nr-python3.9"
;;
"python3.10")
name="nr-python3.10"
;;
"python3.11")
name="nr-python3.11"
;;
"python3.12")
name="nr-python3.12"
;;
"python3.13")
name="nr-python3.13"
;;
"nodejs18.x")
name="nr-nodejs18.x"
;;
"nodejs20.x")
name="nr-nodejs20.x"
;;
"nodejs22.x")
name="nr-nodejs22.x"
;;
"ruby3.3")
name="nr-ruby3.3"
;;
"dotnet")
name="nr-dotnet"
;;
esac
echo $name
}
function hash_file() {
if command -v md5sum &> /dev/null ; then
md5sum $1 | awk '{ print $1 }'
else
md5 -q $1
fi
}
function publish_layer {
layer_archive=$1
region=$2
runtime_name=$3
arch=$4
layer_name=$( layer_name_str $runtime_name $arch )
hash=$( hash_file $layer_archive | awk '{ print $1 }' )
bucket_name="nr-layers-${region}"
s3_key="$( s3_prefix $runtime_name )/${hash}.${arch}.zip"
compat_list=( $runtime_name )
if [[ $runtime_name == "provided" ]]
then compat_list=("provided" "provided.al2" "provided.al2023" "dotnetcore3.1")
fi
if [[ $runtime_name == "dotnet" ]]
then compat_list=("dotnet6" "dotnet8")
fi
echo "Uploading ${layer_archive} to s3://${bucket_name}/${s3_key}"
aws --region "$region" s3 cp $layer_archive "s3://${bucket_name}/${s3_key}"
if [[ ${REGIONS_ARM[*]} =~ $region ]];
then arch_flag="--compatible-architectures $arch"
else arch_flag=""
fi
echo "Publishing ${runtime_name} layer to ${region}"
layer_version=$(aws lambda publish-layer-version \
--layer-name ${layer_name} \
--content "S3Bucket=${bucket_name},S3Key=${s3_key}" \
--description "New Relic Layer for ${runtime_name} (${arch})" \
--license-info "Apache-2.0" $arch_flag \
--compatible-runtimes ${compat_list[*]} \
--region "$region" \
--output text \
--query Version)
echo "Published ${runtime_name} layer version ${layer_version} to ${region}"
echo "Setting public permissions for ${runtime_name} layer version ${layer_version} in ${region}"
aws lambda add-layer-version-permission \
--layer-name ${layer_name} \
--version-number "$layer_version" \
--statement-id public \
--action lambda:GetLayerVersion \
--principal "*" \
--region "$region"
echo "Public permissions set for ${runtime_name} layer version ${layer_version} in region ${region}"
}
function publish_docker_ecr {
layer_archive=$1
runtime_name=$2
arch=$3
if [[ ${arch} =~ 'arm64' ]];
then
arch_flag="-arm64"
platform="linux/arm64"
else
arch_flag=""
platform="linux/amd64"
fi
version_flag=$(echo "$runtime_name" | sed 's/[^0-9]//g')
language_flag=$(echo "$runtime_name" | sed 's/[0-9].*//')
if [[ ${runtime_name} =~ 'extension' ]]; then
version_flag=$EXTENSION_VERSION
language_flag="lambdaextension"
fi
if [[ ${runtime_name} =~ 'dotnet' ]]; then
version_flag=""
arch_flag=${arch}
fi
# Remove 'dist/' prefix
if [[ $layer_archive == dist/* ]]; then
file_without_dist="${layer_archive#dist/}"
echo "File without 'dist/': $file_without_dist"
else
file_without_dist=$layer_archive
echo "File does not start with 'dist/': $file_without_dist"
fi
# public ecr repository name
# maintainer can use this("q6k3q1g1") repo name for testing
repository="x6n7b2o2"
# copy dockerfile
cp ../Dockerfile.ecrImage .
echo "Running : aws ecr-public get-login-password --region us-east-1 | docker login --username AWS --password-stdin public.ecr.aws/${repository}"
aws ecr-public get-login-password --region us-east-1 | docker login --username AWS --password-stdin public.ecr.aws/${repository}
echo "docker buildx build --platform ${platform} -t layer-nr-image-${language_flag}-${version_flag}${arch_flag}:latest \
-f Dockerfile.ecrImage \
--build-arg layer_zip=${layer_archive} \
--build-arg file_without_dist=${file_without_dist} \
."
docker buildx build --platform ${platform} -t layer-nr-image-${language_flag}-${version_flag}${arch_flag}:latest \
-f Dockerfile.ecrImage \
--build-arg layer_zip=${layer_archive} \
--build-arg file_without_dist=${file_without_dist} \
.
echo "docker tag layer-nr-image-${language_flag}-${version_flag}${arch_flag}:latest public.ecr.aws/${repository}/newrelic-lambda-layers-${language_flag}:${version_flag}${arch_flag}"
docker tag layer-nr-image-${language_flag}-${version_flag}${arch_flag}:latest public.ecr.aws/${repository}/newrelic-lambda-layers-${language_flag}:${version_flag}${arch_flag}
echo "docker push public.ecr.aws/${repository}/newrelic-lambda-layers-${language_flag}:${version_flag}${arch_flag}"
docker push public.ecr.aws/${repository}/newrelic-lambda-layers-${language_flag}:${version_flag}${arch_flag}
# delete dockerfile
rm -rf Dockerfile.ecrImage
}
function publish_docker_hub {
layer_archive=$1
runtime_name=$2
arch=$3
if [[ ${arch} =~ 'arm64' ]];
then arch_flag="-arm64"
else arch_flag=""
fi
version_flag=$(echo "$runtime_name" | sed 's/[^0-9]//g')
language_flag=$(echo "$runtime_name" | sed 's/[0-9].*//')
# Remove 'dist/' prefix
if [[ $layer_archive == dist/* ]]; then
file_without_dist="${layer_archive#dist/}"
echo "File without 'dist/': $file_without_dist"
else
file_without_dist=$layer_archive
echo "File does not start with 'dist/': $file_without_dist"
fi
# copy dockerfile
cp ../Dockerfile.ecrImage .
echo "docker build -t ${language_flag}-${version_flag}${arch_flag}:latest \
-f Dockerfile.ecrImage \
--build-arg layer_zip=${layer_archive} \
--build-arg file_without_dist=${file_without_dist} \
."
docker build -t ${language_flag}-${version_flag}${arch_flag}:latest \
-f Dockerfile.ecrImage \
--build-arg layer_zip=${layer_archive} \
--build-arg file_without_dist=${file_without_dist} \
.
echo "docker tag ${language_flag}-${version_flag}${arch_flag}:latest newrelic/newrelic-lambda-layers:${language_flag}-${version_flag}${arch_flag}"
docker tag ${language_flag}-${version_flag}${arch_flag}:latest newrelic/newrelic-lambda-layers:${language_flag}-${version_flag}${arch_flag}
echo "docker push newrelic/newrelic-lambda-layers:${language_flag}-${version_flag}${arch_flag}"
docker push newrelic/newrelic-lambda-layers:${language_flag}-${version_flag}${arch_flag}
}