-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathssm-util.sh
50 lines (44 loc) · 1.17 KB
/
ssm-util.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
#!/usr/bin/env bash
function send_command {
aws --region $region --profile $profile ssm send-command \
--document-name "AWS-RunShellScript" \
--comment "Shell command by $(whoami)" \
--instance-ids $instance \
--parameters commands="$command" \
| jq -r '.Command.CommandId'
}
function read_command_output {
aws --region $region --profile $profile ssm list-command-invocations \
--command-id "$command_id" \
--details \
| jq -r '.CommandInvocations[].CommandPlugins[].Output'
}
function wait_for_command {
result=0
for this_instance in $instance; do
responseCode=-1
printf "$this_instance:"
while [[ $responseCode -eq -1 ]]; do
printf "."
sleep 1
responseCode=$(aws --region $region --profile $profile ssm list-command-invocations \
--command-id "$command_id" \
--instance-id "$this_instance" \
| jq -r '.ResponseCode')
if [[ $responseCode -ne -1 ]]; then
echo;
fi
if [[ $responseCode -gt -0 ]]; then
result=1
fi
done
done
return $result
}
function run {
command_id=$(send_command)
wait_for_command
echo "Return code was $?"
read_command_output
}
run