forked from lucasteligioridis/bam
-
Notifications
You must be signed in to change notification settings - Fork 0
/
am
executable file
·71 lines (61 loc) · 2.63 KB
/
am
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
#!/bin/bash
[ ${#} -eq 0 ] && echo "usage: am <hostname> [command]" && exit 1
RED='\033[0;31m'
NC='\033[0m' # No Color
# filter list based on first few characters of search: name, instance-id, ami, private IP or instance-type
field="Name=tag:Name,Values=*${1}*"
[ "${AM_EXACT}" ] && field="Name=tag:Name,Values=${1}"
[ "${1:0:2}" == "i-" ] && field="Name=instance-id,Values=${1}"
[ "${1:0:4}" == "ami-" ] && field="Name=image-id,Values=${1}"
[ $(grep -E '^[0-9]+' <<< "${1:0:1}") ] && field="Name=private-ip-address,Values=*${1}*"
[ $(grep -E '^[a-z][0-9]' <<< "${1:0:2}") ] && field="Name=instance-type,Values=${1}*"
temp=$(mktemp)
trap 'rm -f ${temp}' EXIT
for region in ${AWS_REGIONS:-$(aws configure get region)}; do
aws ec2 describe-instances --region ${region} \
--filters \
${field} \
"Name=instance-state-name,Values=running" \
--query \
"sort_by(Reservations[*].Instances[].{name:Tags[?Key=='Name'] | [0].Value, \
id:InstanceId, ip:PrivateIpAddress, az:Placement.AvailabilityZone, type:InstanceType, \
up:LaunchTime, ami:ImageId, role:IamInstanceProfile.Arn}, &name)" \
--cli-read-timeout 0 >> ${temp} &
done
wait
# no matches? perhaps it was a hostname
if [ -z "$(jq '.[]' < ${temp})" ] && [ "$(which getent)" ]; then
ip=$(getent hosts ${1} | cut -d' ' -f1)
tpl='[{"name":"%s","ip":"%s","id":"unknown","az":"unknown","type":"unknown","ami":"unknown","role":"unknown","up":"unknown"}]'
[ "${ip}" ] && printf "${tpl}" "${1}" "${ip}" > ${temp}
fi
# no hosts found, then exit 1
[ -z "$(jq '.[]' < ${temp})" ] && echo -e "${RED}Error no host found: ${1}${NC}" && exit 1
# get command to run
if [ ${#} -gt 1 ]; then
shift # drop hostname
cmd="${@}"
# read from stdin
elif [ ! -t 0 ]; then
cmd="$(cat)"
fi
# run a command remotely on matching hosts
if [ "${cmd}" ]; then
if grep -q "sudo " <<< "${cmd}"; then
[ "${AM_SUDO_PASSWORD}" ] || read -p "[am] sudo password: " -s AM_SUDO_PASSWORD </dev/tty; echo
cmd=${cmd//sudo/"echo \"${AM_SUDO_PASSWORD}\" | sudo -p' ' -S"}
fi
for i in $(jq -r '.[] | [.ip, .name] | join(":")' < ${temp}); do
ip=${i%:*}
name=${i#*:}
echo "* ${name} ${ip}: ${@}" > /dev/stderr
ssh ${AM_FAST:+-f} ${AM_FAST:+-n} -t -q -o ConnectTimeout=10 -o StrictHostKeyChecking=no ${ip} -- "${cmd}"
ex=$?
[ "${ex}" != 0 ] && failed=${ex} && echo -e "${RED}Exited: ${ex}${NC}"
done
[ "${failed}" ] && exit ${failed}
# else print out the results
else
jq -r '.[] | [.name, .id, .ip, .az, .type, .ami, (.role|split("/")?|last), (.up|split("T")?|first) ] | join(" ")' < ${temp} | column -t
fi
exit 0