-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathexec.sh
More file actions
executable file
·184 lines (160 loc) · 4.31 KB
/
exec.sh
File metadata and controls
executable file
·184 lines (160 loc) · 4.31 KB
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
#!/bin/bash -eu
#title :exec
#description :This script helps you exec into a running Fargate container
#author :Alex Bragg, Unicon, Inc.
#date :FEB-2022
#version :0.1
export ARGS=$@
export PATH=$PATH:/usr/local/bin
export AWS_DEFAULT_OUTPUT="json"
export TRACING_ENABLED=false
set -o pipefail
main() {
getOptions ${ARGS[*]}
checkDependencies
login
execInto
}
die () {
echo -e >&2 "\n$@\n"
exit 1
}
printGreen() {
GREEN='\033[0;32m'
NC='\033[0m'
printf >&2 "${GREEN}$@${NC}\n"
}
getOptions() {
while [[ $# -gt 0 ]]; do
key="$1"
case $key in
-s|--stack-file)
export STACK_FILE=$2
shift
;;
-h|--help)
printUsage
exit 0
;;
-v|--verbose)
set -x
export VERBOSE="-v"
export TRACING_ENABLED=true
;;
*)
printUsage
die "Invalid option: $key"
;;
esac
shift
done
if [ "foo" = "${STACK_FILE:=foo}" ]; then
printUsage
fi
if [ ! -f "${STACK_FILE}.aws" ]; then
printUsage
die "Stacks and basic settings for the target environment ${STACK_FILE}.aws"
else
source "${STACK_FILE}.aws"
export AWS_REGION TEMPLATE_BUCKET STACKS STACK_ORDER PROJECT FARGATE_CLUSTER
export AWS_PROFILE=$SSO_ACCOUNT_NAME
export AWS_DEFAULT_REGION=$AWS_REGION
fi
}
printUsage() {
cat << EOF
This script helps you exec into a running Fargate container
usage: $0 -s stack-file -c container [-h][-v]
options:
-s, --stack-file The stack file name prefix
(ex. dev-networking or dev-services)
-v, --verbose Enable debug output
-h, --help prints this usage
EOF
}
checkDependencies() {
which aws 2>&1 > /dev/null || die "This script requires the AWS CLI tools installed"
which jq 2>&1 > /dev/null || die "This script requires the jq tool installed"
session-manager-plugin 2>&1 > /dev/null || die "This script requires the session-manager-plugin. See https://docs.aws.amazon.com/systems-manager/latest/userguide/session-manager-working-with-install-plugin.html"
}
login() {
printGreen "Login to AWS SSO"
source ./scripts/login.sh || die "You may need to edit $PWD/.aws/config and add the missing profile"
}
findService() {
declare -A serviceNames
services=( $(aws ecs list-services --cluster ${FARGATE_CLUSTER} | jq -r '.serviceArns[]') )
for service in "${services[@]}"; do
serviceNames[${service##*/}]=$service
done
PS3='Select the service: '
select ch in "${!serviceNames[@]}"; do
if [[ 1 -le $REPLY && $REPLY -le ${#serviceNames[@]} ]]; then
export SERVICE=$ch
export SERVICE_ARN=${serviceNames[$ch]}
break
else
echo "Invalid choice"
fi
done
}
findTask() {
declare -A taskIds
findService
tasks=( $(aws ecs list-tasks --cluster ${FARGATE_CLUSTER} --service-name ${SERVICE} | jq -r '.taskArns[]') )
for task in "${tasks[@]}"; do
taskIds[${task##*/}]=$task
done
if [[ ${#tasks[@]} -eq 0 ]]; then
echo "There are no tasks running in this service"
echo "Please choose another service"
findTask
return
fi
if [[ ${#tasks[@]} -gt 1 ]]; then
PS3='Select the task: '
select ch in "${!taskIds[@]}"; do
if [[ 1 -le $REPLY && $REPLY -le ${#tasks[@]} ]]; then
export TASK=$ch
export TASK_ARN=${taskIds[$ch]}
break
else
echo "Invalid choice"
fi
done
else
export TASK=${tasks[0]##*/}
export TASK_ARN=${tasks[0]}
fi
}
findContainer() {
declare -A containerNames
findTask
containers=( $(aws ecs describe-tasks --cluster ${FARGATE_CLUSTER} --tasks ${TASK_ARN} | jq -r '.tasks[].containers[].name') )
for container in "${containers[@]}"; do
containerNames[${container##*/}]=$container
done
if [[ ${#containers[@]} -gt 1 ]]; then
PS3='Select the container: '
select ch in "${!containerNames[@]}"; do
if [[ 1 -le $REPLY && $REPLY -le ${#containers[@]} ]]; then
export CONTAINER=$ch
break
else
echo "Invalid choice"
fi
done
else
export CONTAINER=${containers[0]}
fi
}
execInto() {
findContainer
aws ecs execute-command \
--cluster ${FARGATE_CLUSTER} \
--task ${TASK_ARN} \
--container ${CONTAINER} \
--command "/bin/sh" \
--interactive
}
main