-
Notifications
You must be signed in to change notification settings - Fork 0
/
ecs-connect.sh
executable file
·79 lines (62 loc) · 2.32 KB
/
ecs-connect.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
#!/bin/bash
# List all ECS clusters alphabetically
echo "Listing ECS clusters..."
clusters_json=$(aws ecs list-clusters --output json)
clusters_tmp=($(echo "$clusters_json" | jq -r '.clusterArns | map(.[32:]) | sort[]'))
if [ ${#clusters_tmp[@]} -eq 0 ]; then
echo "No ECS clusters found."
exit 1
fi
# Create an empty array to store the trimmed values
clusters=()
# Loop through the original array and trim the leading "44:cluster/"
for element in "${clusters_tmp[@]}"; do
trimmed_element="${element#44:cluster/}"
clusters+=("$trimmed_element")
done
# Prompt the user to select a cluster
echo "Available ECS clusters:"
for i in "${!clusters[@]}"; do
echo "$i. ${clusters[$i]}"
done
read -p "Select a cluster (enter the corresponding number): " cluster_selection
if [[ ! $cluster_selection =~ ^[0-9]+$ ]] || [[ $cluster_selection -lt 0 ]] || [[ $cluster_selection -ge ${#clusters[@]} ]]; then
echo "Invalid selection. Please enter a valid number."
exit 1
fi
selected_cluster="${clusters[$cluster_selection]}"
echo "Selected cluster: $selected_cluster"
# List all tasks for the selected cluster
echo "Listing tasks for cluster $selected_cluster..."
tasks_json=$(aws ecs list-tasks --cluster "$selected_cluster" --output json)
tasks=($(echo "$tasks_json" | jq -r '.taskArns | sort[]'))
if [ ${#tasks[@]} -eq 0 ]; then
echo "No tasks found for cluster $selected_cluster."
exit 1
fi
# Check if there is only one task available
if [ ${#tasks[@]} -eq 1 ]; then
echo "Only one task available. Selecting it automatically."
selected_task="${tasks[0]}"
else
# Prompt the user to select a task
echo "Available tasks for cluster $selected_cluster:"
for i in "${!tasks[@]}"; do
echo "$i. ${tasks[$i]}"
done
read -p "Select a task (enter the corresponding number): " task_selection
if [[ ! $task_selection =~ ^[0-9]+$ ]] || [[ $task_selection -lt 0 ]] || [[ $task_selection -ge ${#tasks[@]} ]]; then
echo "Invalid selection. Please enter a valid number."
exit 1
fi
selected_task="${tasks[$task_selection]}"
fi
echo "Selected task: $selected_task"
# Execute the command to connect to the container
echo "Connecting to the container in task $selected_task..."
aws ecs execute-command \
--cluster "$selected_cluster" \
--task "$selected_task" \
--container "$selected_cluster" \
--interactive \
--command "/bin/sh"