-
Notifications
You must be signed in to change notification settings - Fork 0
/
ecsconnect.sh
executable file
·129 lines (105 loc) · 3.47 KB
/
ecsconnect.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
128
129
#!/usr/bin/env bash
# W. van der Toorren - TechNative B>V
# version: 2024043001
#
#set -x
# Init variables
breadcrumbs=""
print_breadcrumbs() {
## Function to print breadcrumbs, to give some information about the choices
printf "\r%s\n" "$1"
}
checkPrerequisites() {
gumLocation=$(which gum)
if [[ -z ${gumLocation} ]]; then
echo "!! Tool 'gum' not found. Please install it from here: https://github.com/charmbracelet/gum"
exit 1
fi
if [[ -z ${AWS_PROFILE} ]]; then
echo "!! AWS_PROFILE not set"
exit 1
fi
}
# Function to update breadcrumbs
update_breadcrumbs() {
if [[ -z "$breadcrumbs" ]]; then
breadcrumbs="$1"
else
breadcrumbs="$breadcrumbs > $1"
fi
print_breadcrumbs "$breadcrumbs"
}
# #Menu to pass ssh-k
# while getopts 'i:' opt; do
# case "$opt" in
# i)
# sshKey="$OPTARG"
# if [[ -f ${sshKey} ]]; then
# echo "Using ssh-key: '${OPTARG}' "
# sshKey="-i ${OPTARG}"
# else
# echo "ssh-key not found: '${OPTARG}'. Not using it"
# unset sshKey
# fi
# ;;
# ?)
# echo -e "Invalid command option.\nUsage: $(basename $0) [-i path_to/ssh_key]"
# exit 1
# ;;
# esac
# done
# shift "$(($OPTIND - 1))"
# Check AWS credentials via AWS_PROFILE
# if [[ -z ${AWS_DEFAULT_REGION} ]]; then
# echo "!! AWS_DEFAULT_REGION not set"
# exit 1
# fi
# execute AWS CLI-command to retrieve info about clusters. Exit when command fails
checkPrerequisites
aws_output=$(aws ecs list-clusters 2>/dev/null)
if [[ $? -ne 0 ]]; then
echo "!! Error listing clusters. Check AWS credentials"
exit 1
fi
# Extract fields needed from JSON_output with the use of jq-command
echo "-- Select cluster"
clusters=$(echo "$aws_output" | jq -r '.clusterArns[] | split("/")[-1]')
if [[ -z ${clusters} ]]; then
echo "!! No clusters found in current region: $(aws configure get region)"
exit 1
fi
cluster=$(gum choose $clusters)
update_breadcrumbs "$cluster" # update breadcrumbs with clustername
# clustertype=$(aws ecs describe-clusters --cluster ${cluster} | jq -r '.clusters[0].capacityProviders[]')
# execute AWS CLI-command to retrieve info about services in the cluster. Exit when command fails
aws_output=$(aws ecs list-services --cluster ${cluster} 2>/dev/null)
if [[ $? -ne 0 ]]; then
echo "!! Error listing services"
exit 1
fi
echo "-- Select service"
services=$(echo "$aws_output" | jq -r '.serviceArns[] | split("/")[-1]')
service=$(gum choose $services)
update_breadcrumbs "$service" # update breadcrumbs with clustername
# execute AWS CLI-command to retrieve info about tasks in the service. Exit when command fails
aws_output=$(aws ecs list-tasks --cluster $cluster --service $service --desired-status RUNNING --query 'taskArns[]')
if [[ $? -ne 0 ]]; then
echo "!! Error listing tasks."
exit 1
fi
echo "-- Select task_id"
# Filter taskArns based on lastStatus
taskids=$(echo "$aws_output" |jq -r '.[] | split("/") | last')
taskid=$(gum choose $taskids)
update_breadcrumbs "$taskid"# update breadcrumbs with clustername
aws_output=$(aws ecs describe-tasks --cluster ${cluster} --tasks ${taskids})
if [[ $? -ne 0 ]]; then
echo "!! Error listing containers. Check AWS credentials"
exit 1
fi
containers=$(echo "$aws_output" | jq -r '.tasks[0].containers[].name')
echo "-- Select container"
container=$(gum choose $containers)
taskarn=$(echo "$aws_output" | jq -r '.tasks[0].taskArn')
update_breadcrumbs "$container"
aws ecs execute-command --cluster ${cluster} --interactive --container ${container} --command /bin/bash --task ${taskarn}