-
Notifications
You must be signed in to change notification settings - Fork 0
/
dcc
79 lines (68 loc) · 2.1 KB
/
dcc
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
path_dir="."
# Colors
RED='\033[0;31m'
GREEN='\033[0;32m'
NC='\033[0m' # No Color
# Check if up
check_docker_status() {
local file="$1"
local status_line=$(docker-compose -f "$file" ps --services --filter "status=running" 2>/dev/null)
if [ -z "$status_line" ]; then
echo -e "${RED}down${NC}"
else
echo -e "${GREEN}up${NC}"
fi
}
# Find docker-compose.yaml or docker-compose.yml in all immediate subdirectories
compose_files=($(find . -maxdepth 2 -type f \( -name "docker-compose.yaml" -o -name "docker-compose.yml" \)))
if [ ${#compose_files[@]} -eq 0 ]; then
echo -e "${RED}Can't find docker-compose.yaml or docker-compose.yml files in any subdirectories.${NC}"
exit 0
fi
# List status
echo "Found files:"
for i in "${!compose_files[@]}"; do
status=$(check_docker_status "${compose_files[i]}")
dirname=$(basename `dirname ${compose_files[i]}`)
printf "%-3s %-40s %-20s\n" "$((i+1))." "${dirname}" "[Stack is $status]"
done
echo "Enter Files(1 || 1,3 | a = all), q=quit"
read -r choice
# input
if [ "$choice" == "q" ]; then
exit 0
elif [ "$choice" == "a" ]; then
selected_files=("${compose_files[@]}")
else
IFS=',' read -ra selected_indices <<< "$choice"
for index in "${selected_indices[@]}"; do
adjusted_index=$((index-1))
if [ $adjusted_index -lt 0 ] || [ $adjusted_index -ge ${#compose_files[@]} ]; then
echo -e "${RED}Invalid choice${NC}"
exit 1
fi
selected_files+=("${compose_files[$adjusted_index]}")
done
fi
echo "Do What? (u = up || d = down)"
read action
# Validate and map to full commands
if [ "$action" == "u" ]; then
action="up"
elif [ "$action" == "d" ]; then
action="down"
else
echo -e "${RED}Invalid action${NC}"
exit 1
fi
# Do the thing
for file in "${selected_files[@]}"; do
if [ "$action" == "up" ]; then
echo -e "${GREEN}docker-compose -f $file up -d${NC}"
docker-compose -f "$file" up -d
elif [ "$action" == "down" ]; then
echo -e "${RED}docker-compose -f $file down${NC}"
docker-compose -f "$file" down
fi
done