-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_instance_types.sh
executable file
·405 lines (337 loc) · 10.5 KB
/
get_instance_types.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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
#!/bin/bash
types=()
types+=( "nano" )
types+=( "micro" )
types+=( "small" )
types+=( "medium" )
types+=( "large" )
types+=( "xlarge" )
types+=( "2xlarge" )
types+=( "3xlarge" )
types+=( "4xlarge" )
types+=( "5xlarge" )
types+=( "6xlarge" )
types+=( "7xlarge" )
types+=( "8xlarge" )
types+=( "9xlarge" )
types+=( "10xlarge" )
types+=( "11xlarge" )
types+=( "12xlarge" )
types+=( "16xlarge" )
types+=( "18xlarge" )
types+=( "24xlarge" )
types+=( "32xlarge" )
types+=( "64xlarge" )
types+=( "128xlarge" )
types+=( "256xlarge" )
types+=( "metal" )
results_path="./results"
regions_file="${results_path}/regions.txt"
function regions() {
local aki="${USGAKI}"
local sak="${USGSAK}"
AWS_ACCESS_KEY_ID="${aki}" AWS_SECRET_ACCESS_KEY="${sak}" AWS_DEFAULT_REGION="us-gov-west-1" \
aws ec2 describe-regions \
--query "Regions[] [RegionName]" \
--output text | sort -ur > "${regions_file}"
aki="${STAAKI}"
sak="${STASAK}"
AWS_ACCESS_KEY_ID="${aki}" AWS_SECRET_ACCESS_KEY="${sak}" AWS_DEFAULT_REGION="us-west-2" \
aws ec2 describe-regions \
--query "Regions[] [RegionName]" \
--output text | sort -ur >> "${regions_file}"
}
function azs() {
local aki="NONE"
local sak="NONE"
while read -r region; do
if [[ "${region}" == *"gov"* ]]; then
aki="${USGAKI}"
sak="${USGSAK}"
else
aki="${STAAKI}"
sak="${STASAK}"
fi
AWS_ACCESS_KEY_ID="${aki}" AWS_SECRET_ACCESS_KEY="${sak}" AWS_DEFAULT_REGION="${region}" \
aws ec2 describe-availability-zones \
--filters Name=state,Values="available" \
--filters Name=opt-in-status,Values="opt-in-not-required" \
--query "AvailabilityZones[] [ZoneId]" \
--output text | sort -u > "${results_path}/${region}.txt"
done <"${regions_file}"
}
# create <az_id>.txt
function raw_types() {
local az_id=$1
local region=$2
aki="${STAAKI}"
sak="${STASAK}"
if [ "${region}" = "us-gov-west-1" ] || [ "${region}" = "us-gov-east-1" ]; then
aki="${USGAKI}"
sak="${USGSAK}"
fi
echo "Region: $region"
echo "AZ ID: $az_id"
AWS_ACCESS_KEY_ID="${aki}" AWS_SECRET_ACCESS_KEY="${sak}" AWS_DEFAULT_REGION="${region}" \
aws ec2 describe-instance-type-offerings \
--location-type availability-zone-id \
--region "${region}" \
--filters Name=location,Values="${az_id}" \
--query "InstanceTypeOfferings[] [InstanceType]" \
--output text | sort -u > "${az_id}-raw.txt"
# echo "Raw instance types:"
# cat "${az_id}-raw.txt"
}
function list_classes() {
local az_id=$1
cp "${az_id}-raw.txt" "${az_id}-preclasses.txt"
perl -p -i -e 's/^([a-z0-9]+)\..*$/$1/g' "${az_id}-preclasses.txt"
cat "${az_id}-preclasses.txt" | sort -u > "${az_id}-classes.txt"
}
typ_enc1=()
typ_enc1+=( "nano" )
typ_enc1+=( "micro" )
typ_enc1+=( "small" )
typ_enc1+=( "medium" )
typ_enc1+=( "xlarge" )
typ_enc1+=( "large" )
typ_enc1+=( "metal" )
typ_enc2=()
typ_enc2+=( 1 )
typ_enc2+=( 2 )
typ_enc2+=( 3 )
typ_enc2+=( 4 )
typ_enc2+=( 1000 )
typ_enc2+=( 5 )
typ_enc2+=( 2000 )
function list_types() {
local az_id=$1
cp "${az_id}-raw.txt" "${az_id}-pretypes.txt"
perl -p -i -e 's/^[a-z0-9]+\.(.*)$/$1/g' "${az_id}-pretypes.txt"
cat "${az_id}-pretypes.txt" | sort -u > "${az_id}-types.txt"
for i in "${!typ_enc1[@]}"; do
perl -p -i -e 's/(\d+)?'"${typ_enc1[$i]}"'/sprintf("%04d", '"${typ_enc2[$i]}"'+$1)/ge' "${az_id}-types.txt"
done
cat "${az_id}-types.txt" | sort -u > "${az_id}-enc-types.txt"
}
function decode_type() {
local enc_type=$1
local typ_int=$(sed -e 's/^000//' <<<"${enc_type}")
local dec_type="Decode failed"
for i in "${!typ_enc2[@]}"; do
if [[ ${typ_int} -eq ${typ_enc2[$i]} ]]; then
echo "${typ_enc1[$i]}"
return
fi
done
for i in "${!typ_enc2[@]}"; do
local remain="$((${typ_int} - ${typ_enc2[$i]}))"
if [ ${remain} -lt 300 ]; then
echo "${remain}${typ_enc1[$i]}"
return
fi
done
echo "${dec_type}"
}
function region_flag() {
local region=$1
local flag=us
case "${region}" in
sa-east-1)
flag=brazil
;;
me-south-1)
flag=bahrain
;;
eu-west-3)
flag=fr
;;
eu-west-2)
flag=uk
;;
eu-west-1)
flag=ireland
;;
eu-south-1)
flag=it
;;
eu-north-1)
flag=sweden
;;
eu-central-1)
flag=de
;;
ca-central-1)
flag=canada
;;
ap-southeast-2)
flag=australia
;;
ap-southeast-1)
flag=singapore
;;
ap-south-1)
flag=india
;;
ap-northeast-2)
flag=kr
;;
ap-northeast-1)
flag=jp
;;
ap-east-1)
flag=hong_kong
;;
af-south-1)
flag=south_africa
;;
*)
flag=us
;;
esac
echo ":${flag}:"
}
function update_links() {
local readme_file="README.md"
local offering_header="offering_header.md"
rm "${readme_file}"
rm "${offering_header}"
cat "README_header.md" > "${readme_file}"
cat "offering_header_header.md" > "${offering_header}"
printf "\nRegions: " >> "${offering_header}"
printf "\nRegions: " >> "${readme_file}"
while read -r region; do
local flag=$(region_flag "${region}")
printf "%s [%s](%s.md) " "${flag}" "${region}" "${region}" >> "${offering_header}"
printf "%s [%s](%s/%s.md) " "${flag}" "${region}" "${results_path}" "${region}" >> "${readme_file}"
done <"${regions_file}"
printf "\n\n" >> "${readme_file}"
printf "\n\n" >> "${offering_header}"
}
function spot_prices() {
local region=$1
local prices_file="${results_path}/spot-prices-${region}.json"
if [[ "${region}" == *"gov"* ]]; then
aki="${USGAKI}"
sak="${USGSAK}"
else
aki="${STAAKI}"
sak="${STASAK}"
fi
local today=$(date +%F)
local yesterday=$(date -d "yesterday" +%F)
AWS_ACCESS_KEY_ID="${aki}" AWS_SECRET_ACCESS_KEY="${sak}" AWS_DEFAULT_REGION="${region}" \
aws ec2 describe-spot-price-history \
--product-description "Linux/UNIX (Amazon VPC)" \
--query "SpotPriceHistory[*].{type:InstanceType,price:SpotPrice}" \
--start-time ${yesterday} \
--end-time ${today} \
--output json > "${prices_file}"
}
function write_instance_info() {
regions
azs
update_links
local aki="NONE"
local sak="NONE"
while read -r region; do
while read -r az_id; do
raw_types "${az_id}" "${region}"
list_classes "${az_id}"
list_types "${az_id}"
cat "${az_id}-classes.txt" >> "${region}-all-classes.txt"
cat "${az_id}-types.txt" >> "${region}-all-enc-types.txt"
done <"${results_path}/${region}.txt"
spot_prices "${region}"
cat "${region}-all-classes.txt" | sort -u >> "${region}-unique-classes.txt"
cat "${region}-all-enc-types.txt" | sort -u >> "${region}-unique-enc-types.txt"
local output="${results_path}/${region}.md"
printf "# %s %s AWS EC2 Instance Types\n\n" "$(region_flag ${region})" "${region}" > "${output}"
cat "offering_header.md" >> "${output}"
printf "Jump to class: " >> "${output}"
while read -r class; do
printf "[:black_small_square:%s](#%s) " "${class}" "${class}" >> "${output}"
done <"${region}-unique-classes.txt"
printf "\n\n" >> "${output}"
printf "Jump to type: " >> "${output}"
while read -r enc_type; do
local type=$(decode_type "${enc_type}")
printf "[:small_blue_diamond:%s](#%s) " "${type}" "${type}" >> "${output}"
done <"${region}-unique-enc-types.txt"
printf "\n\n" >> "${output}"
local yes_or_no=""
while read -r class; do
printf "\n\n## %s\n\n" "${class}" >> "${output}"
local table_header_bar="| ------------- |"
printf "| Instance Type |" >> "${output}"
while read -r az_id; do
printf " %s |" "${az_id}" >> "${output}"
table_header_bar="${table_header_bar} :-------------: |"
done <"${results_path}/${region}.txt"
# for spot
printf " %s |" "Spot price" >> "${output}"
table_header_bar="${table_header_bar} -------------: |"
printf "\n%s\n" "${table_header_bar}" >> "${output}"
for type in "${types[@]}"; do
local row="| ${class}.${type} |"
local include_row=0
while read -r az_id; do
yes_or_no=$(grep -o "${class}.${type}" "${az_id}-raw.txt" | wc -l)
if [ "${yes_or_no}" = "0" ]; then
row="${row} :red_circle: |"
else
include_row=1
row="${row} :green_circle: |"
fi
done <"${results_path}/${region}.txt"
if [ "${include_row}" = "1" ]; then
price=$(jq '[ .[] | select(.type == "'"${class}.${type}"'").price ] | max' "${results_path}/spot-prices-${region}.json")
price=$(sed -e 's/^"//' -e 's/"$//' <<<"${price}")
if [ "${price}" = "null" ]; then
printf "%s %s |\n" "${row}" "None found" >> "${output}"
else
printf "%s %.4f |\n" "${row}" "${price}" >> "${output}"
fi
fi
done
done <"${region}-unique-classes.txt"
local yes_or_no=""
while read -r enc_type; do
local type=$(decode_type "${enc_type}")
printf "\n\n## %s\n\n" "${type}" >> "${output}"
local table_header_bar="| ------------- |"
printf "| Instance Class |" >> "${output}"
while read -r az_id; do
printf " %s |" "${az_id}" >> "${output}"
table_header_bar="${table_header_bar} :-------------: |"
done <"${results_path}/${region}.txt"
# for spot
printf " %s |" "Spot price" >> "${output}"
table_header_bar="${table_header_bar} -------------: |"
printf "\n%s\n" "${table_header_bar}" >> "${output}"
while read -r class; do
local row="| ${class}.${type} |"
local include_row=0
while read -r az_id; do
yes_or_no=$(grep -o "${class}.${type}" "${az_id}-raw.txt" | wc -l)
if [ "${yes_or_no}" = "0" ]; then
row="${row} :red_circle: |"
else
include_row=1
row="${row} :green_circle: |"
fi
done <"${results_path}/${region}.txt"
if [ "${include_row}" = "1" ]; then
price=$(jq '[ .[] | select(.type == "'"${class}.${type}"'").price ] | max' "${results_path}/spot-prices-${region}.json")
price=$(sed -e 's/^"//' -e 's/"$//' <<<"${price}")
if [ "${price}" = "null" ]; then
printf "%s %s |\n" "${row}" "None found" >> "${output}"
else
printf "%s %.4f |\n" "${row}" "${price}" >> "${output}"
fi
fi
done <"${region}-unique-classes.txt"
done <"${region}-unique-enc-types.txt"
printf "\n\n\n" >> "${output}"
done <"${regions_file}"
}