-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmultissm.sh
executable file
·127 lines (99 loc) · 3.3 KB
/
multissm.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
#!/usr/bin/env bash
set -o errexit
set -o nounset
set -o pipefail
#set -x
declare -r BASENAME=${0##*/}
main () {
local ids k v args=()
while (( $# )); do
case $1 in
-t|--tag)
(( $# >= 3 )) || die "$(usage)"
shift; k="$1"
;;
-a|--asg|-v|--value)
(( $# >= 3 )) || die "$(usage)"
k="${k:-aws:autoscaling:groupName}"
shift; v="$1"
;;
-h|--help) help; exit ;;
--) shift; break ;;
-*) die "$(usage)" ;;
*) args+=( "$1" ) ;;
esac
shift
done
set -- ${args[@]:+"${args[@]}"} "$@"
(( $# >= 1 )) || die "$(usage)"
for x in aws tmux session-manager-plugin; do
has "$x"
done
ids="$(get_instances "${k}" "${v}")"
tmux_ssm_session "${ids}" "sudo $*"
}
tmux_ssm_session () {
local session instances="$1" ssm_cmd="$2"
session="${BASENAME%%.*}-$(mktemp -u XXXXXX)"
tmux new-session -d -s "${session}" >/dev/null
trap 'tmux kill-session -t "${session}" 2>/dev/null' EXIT
for i in ${instances}; do
tmux select-layout -t "${session}:0" tiled
tmux split-window -t "${session}:0" \
"aws ssm start-session \
--document-name AWS-StartInteractiveCommand \
--parameters command='[\"${ssm_cmd}\"]' \
--target ${i}"
done
tmux kill-pane -t "${session}:0.0"
tmux select-layout -t "${session}:0" tiled
tmux select-window -t "${session}:0"
tmux set-window-option synchronize-panes on 2>/dev/null
exec tmux attach-session -t "${session}:0"
}
get_instances () {
local instances k="$1" v="$2"
instances="$(aws ec2 describe-instances \
--query 'Reservations[*].Instances[*].InstanceId' \
--filters Name=tag:"${k}",Values="${v}" \
Name=instance-state-code,Values=16 \
--output text)" || die "[${BASENAME}] error: aws: returned status $?"
if [[ -z ${instances} ]]; then
die "[${BASENAME}] error: aws: returned no EC2 instances"
fi
printf '%s' "${instances}"
}
help () {
cat <<EOF
Usage: ${BASENAME} [--tag KEY] --value VALUE | --asg NAME [--] COMMAND [ARG ...]
Run an interactive AWS SSM command in synchronised tmux panes on one or more EC2
instances by specifying the autoscaling group name or tag key and value.
Optional parameters:
-t, --tag KEY EC2 instance tag key
Required parameter:
-a, --asg NAME EC2 autoscaling group name; or
-v, --value VALUE EC2 instance tag value (if --tag provided)
Help options:
-h, --help Provide this help output
This script uses EC2 describe-instances to query for the autoscaling group or
tag key and value provided. If successful a local tmux session is created.
COMMAND is run on each returned instance in synchronised tmux panes using AWS
Session Manager plugin. Use -- if COMMAND requires its own arguments.
Examples:
${BASENAME} --tag Name --value prod-internal-cms02 -- bash
${BASENAME} --asg wordpress-prod htop
${BASENAME} --asg drupal-web-asg -- journalctl -f -u httpd
${BASENAME} --tag webgroup --value drupal-frontend -- watch -t -n1 date +%s
EOF
}
usage () {
cat <<EOF
Usage: ${BASENAME} [-h] [-t <key>] -v <value> | -a <asg name> [--] <command>
Try '${BASENAME} --help' for more information.
EOF
}
has () {
command -v "$1" &>/dev/null || die "error: executable '${x}' not found"
}
die () { printf '%s\n' "$*" >&2; exit 1; }
main "$@"