-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathec2-functions
390 lines (319 loc) · 11 KB
/
ec2-functions
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
: ${DEBUG:=1}
debug() {
[[ "$DEBUG" ]] && echo "[DEBUG] $@" 1>&2
}
_getQuery() {
if [[ "$ec2_fields" ]]; then
echo "$ec2_fields"
else
# you can choose fields from the output json:
# http://docs.aws.amazon.com/cli/latest/reference/ec2/describe-instances.html#output
local fields="id: InstanceId"
#fields="$fields, ami: ImageId"
fields="$fields, pubIp: PublicIpAddress"
fields="$fields, privIp: PrivateIpAddress"
#fields="$fields, state: State.Name"
fields="$fields, idx: AmiLaunchIndex"
fields="$fields"', name: join(``, Tags[?Key==`Name`].Value || `[]`)'
fields="$fields"', owner: join(``, Tags[?Key==`owner`].Value || `[]`)'
#fields="$fields"', tagz: join(`,`,Tags[].join(`=`,[Key,Value] || `[]`) || `[]`)'
# simple fields example
#fields="$fields"', xxx: yyy'
# field contains jmespath literal
#fields="$fields"', xxx: `literal`'
echo "Reservations[].Instances[].{${fields}}"
fi
}
ec2-find-img() {
declare ownerid=${1:? required: ownerid}
declare imglocstart=${2:?required: img location prefix}
aws ec2 describe-images \
--filter Name=owner-id,Values=$ownerid \
--out json \
| jq '.Images[]|select(.ImageLocation|startswith("'$imglocstart'"))|[.Name, .ImageId]' -c \
| sort | tail -1
}
ec2-find-img-regions() {
for r in $(ec2-regions); do
debug "-----> region $r"
AWS_DEFAULT_REGION=$r ec2-find-img "$@"
done
}
check-sigil() {
if sigil -v &> /dev/null;then
debug "sigil is already installed"
else
debug "install sigil"
curl https://dl.gliderlabs.com/sigil/latest/$(uname -sm|tr \ _).tgz \
| tar -zxC /usr/local/bin
debug "installed: $(sigil -v)"
fi
}
route53-seq() {
check-sigil
cat <<"EOF" > /tmp/dns.tmpl
{{ range $rec := (stdin | json | pointer "/ResourceRecordSets") \}}
{{ $rec.Type }} | {{$rec.Name}} | {{range $rr := $rec.ResourceRecords}}{{$rr.Value}} |{{end}}
{{end}}
EOF
aws route53 list-resource-record-sets --hosted-zone-id /hostedzone/ZM3N9EMI8XJSW \
| sigil -f /tmp/dns.tmpl \
| sort
}
ec2-list-centos() {
ec2-find-img-regions 679593333241 aws-marketplace/CentOS-6.5-GA
}
ec2-list-debian-wheezy() {
ec2-find-img-regions 379101102735 379101102735/debian-wheezy-amd64
}
ec2-list-debian-jessie() {
ec2-find-img-regions 379101102735 379101102735/debian-jessie-amd64
}
ec2-list-amazon-linux() {
# ec2-find-img-regions 137112412989 amzn-ami-eu-west-1/amzn-ami-h
aws ec2 describe-images \
--filter Name=owner-alias,Values=amazon \
--query 'reverse(sort_by(Images[?contains(ImageLocation,`amazon/amzn-ami-hvm`) && ends_with(ImageLocation, `gp2`)].{loc:ImageLocation,id:ImageId}, &loc))[0].id' --out text
}
ec2-list-fedora() {
ec2-find-img-regions 206029621532 amazon/fedora
}
ec2-list-redhat() {
ec2-find-img-regions 309956199498 309956199498/RHEL
}
ec2-list-ubuntu() {
ec2-find-img-regions 099720109477 099720109477/ubuntu/images/hvm-ssd/ubuntu-trusty-14.04-amd64-server-20150
}
ec2-list-all() {
local filter=$@
local query="$(_getQuery)"
[[ "$TRACE" ]] && set -x
aws ec2 describe-instances $filter --query "$query" --out table
set +x
}
ec2-ip-by-name() {
declare name=$1
: ${name:? filter value for the Name tag is required}
aws ec2 describe-instances \
--filters Name=tag:Name,Values=$name \
--query 'Reservations[].Instances[].{pubIp: PublicIpAddress}' \
--out text
}
ec2-sudo-ssh-by-name() {
declare name=$1
declare user=${2:-ubuntu}
: ${name:? filter value for the Name tag is required}
: ${user:? ssh username required}
local ip=$(ec2-ip-by-name $name)
debug ssh to $ip
ssh -t \
-o UserKnownHostsFile=/dev/null \
-o StrictHostKeyChecking=no \
-o ControlMaster=auto \
-o ControlPath=/tmp/%r@%h:%p \
-o ControlPersist=yes \
ubuntu@${ip} sudo su
}
ec2-full-access() {
declare name=$1
: ${name:? required}
local group=$( aws ec2 describe-instances \
--filter Name=tag:Name,Values=$name \
--query Reservations[0].Instances[0].SecurityGroups[0].GroupId \
--out text
)
echo group=$group
aws ec2 authorize-security-group-ingress \
--ip-protocol -1 \
--cidr-ip=$(curl http://ipecho.net/plain)/32 \
--group-id $group
}
ec2-list-filtered() {
local filterName=${1?:" first param REQUIRED: <filtername> example: instance-state-name"}
local filterValue=${2:?" second param RREQUIRED: <values> example: running"}
ec2-list-all --filters Name=$filterName,Values=$filterValue
}
ec2-list-running-us-eu() {
for r in $(ec2-regions-usa-eu); do
debug "-----> region $r"
AWS_DEFAULT_REGION=$r ec2-list-running "$@"
done
}
ec2-list-running() {
ec2-list-filtered instance-state-name running
}
ec2-list-by-name() {
ec2-list-filtered tag:Name ${1:?"first param REQUIRED: <name>"}
}
ec2-list-by-owner() {
ec2-list-filtered tag:owner ${1:?"first param REQUIRED: <owner>"}
}
ec2-list-by-tagvalue() {
ec2-list-filtered
}
ec2-regions() {
aws ec2 describe-regions --query "Regions[].RegionName" --out text
}
ec2-regions-usa-eu() {
aws ec2 describe-regions --query 'Regions[? starts_with(RegionName, `eu`) || starts_with(RegionName, `us`)].RegionName' --out text
}
ec2-list-self-images() {
: ${AWS_OWNER:=755047402263}
for reg in $(ec2-regions); do
aws ec2 describe-images \
--region $reg \
--filters Name=owner-id,Values=$AWS_OWNER \
--query "Images[].[ImageId,Name]" \
--out text | xargs -n 2 echo $reg
done
}
___ec2-delete-image-by-prefix() {
declare prefix=$1
: ${prefix:? required}
for reg in $(ec2-regions); do
echo === region: $reg
amis=$( aws ec2 describe-images --owners=self --region $reg \
| jq '.Images[]|select(.Name|startswith("'$prefix'"))|.ImageId ' -r
)
for ami in $amis; do
echo "---> delete: $ami"
ec2-delete-ami $reg $ami
done
done
}
ec2-delete-keys-by-fingerprint() {
declare fingerprint=$1
: ${fingerprint:? required}
for reg in $(aws ec2 describe-regions --query "Regions[].RegionName" --out text); do
echo === clean $reg
aws ec2 describe-key-pairs --region=$reg \
| jq '.KeyPairs[]|select(.KeyFingerprint=="'$fingerprint'")|.KeyName' -r \
| xargs -P 20 -t -n 1 --no-run-if-empty -I KEYS bash -c "aws ec2 delete-key-pair --key-name KEYS --region=$reg"
done
}
ec2-delete-keys-by-prefix() {
declare keynamePrefix=$1
: ${keynamePrefix:? required}
for reg in $(aws ec2 describe-regions --query "Regions[].RegionName" --out text); do
echo === clean $reg
aws ec2 describe-key-pairs --region=$reg \
| jq '.KeyPairs[]|.KeyName|select(startswith("'$keynamePrefix'"))' -r \
| xargs -P 20 -t -n 1 --no-run-if-empty -I @ bash -c "aws ec2 delete-key-pair --key-name '@' --region=$reg"
done
}
ec2-delete-sec-grp-by-prefix() {
declare secGroupPrefix=${1}
: ${secGroupPrefix:?required}
for reg in $(ec2-regions); do
debug "clean secGrps in: $reg"
aws ec2 describe-security-groups --region=$reg --query 'SecurityGroups[?starts_with(GroupName,`'${secGroupPrefix}'`)].GroupId' --out text \
| xargs -n1 -P20 --no-run-if-empty aws ec2 delete-security-group --region=$reg --group-id
done
}
ec2-clean-images() {
local amiWhiteList="ci-2014-06-25\|ci-sequenceiq\|ambari-docker-140716083934"
ec2-list-self-images | grep -v "$amiWhiteList"|xargs -n 3 echo ec2-delete-ami
}
ec2-delete-ami() {
local region=$1
local ami=$2
: ${region:? requeired 1. parameter}
: ${ami:? requeired 2. parameter}
local name=$(aws ec2 describe-images \
--region $region \
--image-ids $ami \
--query "Images[].Name" \
--out text
)
local snap=$(aws ec2 describe-images \
--region $region \
--image-ids $ami \
--query "Images[].BlockDeviceMappings[].Ebs.SnapshotId" \
--out text
)
echo [ec2-deregister-image] $name
aws ec2 deregister-image --region $region --image-id $ami --out text
echo [ec2-delete-snapshot] $snap
aws ec2 delete-snapshot --region $region --snapshot-id $snap --out text
}
ec2_check_all_users() {
: ${AWS_IAM_USERS:=$(aws iam list-users --query Users[].UserName --out text)}
for u in $AWS_IAM_USERS; do
if aws iam get-login-profile --user-name $u &>/dev/null; then
echo === set password-reset-required for $u
aws iam update-login-profile --user-name $u --password-reset-required
#aws iam get-login-profile --user-name $u --query LoginProfile.[CreateDate,UserName,PasswordResetRequired] --out text
fi
done
}
ec2_check_password_reset_required() {
: ${AWS_IAM_USERS:=$(aws iam list-users --query Users[].UserName --out text)}
echo "===> password reset required for:"
for u in $AWS_IAM_USERS; do
if aws iam get-login-profile --user-name $u &>/dev/null; then
#aws iam update-login-profile --user-name $u --password-reset-required
if [[ $(aws iam get-login-profile --user-name $u --query LoginProfile.PasswordResetRequired --out text) == "True" ]]; then
echo " - $u"
fi
fi
done
}
ec2_password_last_used() {
echo "===> last login date"
aws iam list-users --query Users[].[PasswordLastUsed,UserName] --out text|grep -v None|sort -r
}
ec2_check_passwords() {
ec2_password_last_used
ec2_check_password_reset_required
}
ec2-delete-images-by-name-prefix() {
declare prefix=${1:? required: prefix}
echo > /tmp/delete.sh
for reg in $(ec2-regions); do
local imagesToDelete=$(
aws ec2 describe-images \
--owners=self \
--query 'Images[?starts_with(Name,`'$prefix'`)].[ImageId]' \
--region $reg \
--out text | xargs echo
)
#echo "[WARNING] about to DELETE:"
aws ec2 describe-images \
--image-ids $imagesToDelete \
--query 'Images[].[ImageId,Name]' \
--region $reg \
--out text
#echo "press ENTER to continue or CTRL-C to exit"
#read x
xargs -n 1 echo ec2-delete-ami $reg <<< "$imagesToDelete" >> /tmp/delete.sh
done
echo -e '=== generated script: cat /tmp/delete.sh\n=== execute: . /tmp/delete.sh'
}
ec2-terminate-by-name-all-regs() {
local name=${1:? instance name is required}
for reg in $(ec2-regions); do
packerId=$(AWS_DEFAULT_REGION=$reg aws ec2 describe-instances \
--filters \
Name=tag:Name,Values="$name" \
Name=instance-state-name,Values=running \
--query 'Reservations[].Instances[].InstanceId' \
--out text)
if [[ "$packerId" ]];then
echo "=== $reg: terminating '$name' ..."
AWS_DEFAULT_REGION=$reg aws ec2 terminate-instances \
--instance-ids $packerId \
--query TerminatingInstances[].InstanceId
else
echo "--- No '$name' is: $reg"
fi
done
}
ec2-del-logs-by-group-name(){
declare logGroupPattern=${1:? logGroupPattern required}
logs=$(aws logs describe-log-groups --query 'logGroups[? contains(logGroupName,`'${logGroupPattern}'`)].logGroupName' --out text)
echo "about to delete LOGS: $logs"
echo "press ENTER to continue or CTRL-C to exit"
read x
echo $logs | xargs -r -n1 -P 30 aws logs delete-log-group --log-group-name
}
alias ec2-reload-functions="source $BASH_SOURCE"