forked from newrelic/newrelic-lambda-layers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
libBuild.sh
224 lines (197 loc) · 4.49 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
#!/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.6
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"
;;
"python3.7")
rt_part="Python37"
;;
"python3.8")
rt_part="Python38"
;;
"python3.9")
rt_part="Python39"
;;
"python3.10")
rt_part="Python310"
;;
"nodejs14.x")
rt_part="NodeJS14X"
;;
"nodejs16.x")
rt_part="NodeJS16X"
;;
"nodejs18.x")
rt_part="NodeJS18X"
;;
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"
;;
"nodejs14.x")
name="nr-nodejs14.x"
;;
"nodejs16.x")
name="nr-nodejs16.x"
;;
"nodejs18.x")
name="nr-nodejs18.x"
;;
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" "dotnetcore3.1" "dotnet6")
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}"
}