-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdocker-compose-manager.dialog.sh
More file actions
executable file
·194 lines (169 loc) · 4.93 KB
/
docker-compose-manager.dialog.sh
File metadata and controls
executable file
·194 lines (169 loc) · 4.93 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
185
186
187
188
189
190
191
192
193
194
#!/bin/bash
##########################################################
########## Docker Compose Manager, dialog #########
########## Tested on: macOS, Linux #########
##########################################################
########## Parsing arguments ##########
ROOT_DIR="$1"
# set default value for root directory
if [ -z "$ROOT_DIR" ]; then
ROOT_DIR=$(pwd)
fi
########## Parsing arguments ##########
########## Setup Vars ##########
CURRENT_DIR=$(pwd)
ROOT_DIR=$(realpath $ROOT_DIR)
########## Setup Vars ##########
########## Getting the platform ##########
PLATFORM=""
UNAME_OUT="$(uname -s)"
case "$UNAME_OUT" in
Linux*) PLATFORM=Linux;;
Darwin*) PLATFORM=Mac;;
CYGWIN*) PLATFORM=Cygwin;;
MINGW*) PLATFORM=MinGw;;
*) PLATFORM="UNKNOWN:${UNAME_OUT}"
esac
########## Getting the platform ##########
########## Getting stacks ##########
array=()
init_array() {
tmp_array=()
# get docker-compose.yml files
if [ "$PLATFORM" = "Mac" ]
then
while IFS= read -r -d $'\n'; do
tmp_array+=("$REPLY")
done < <(find -E $ROOT_DIR -type f -regex .*/docker-compose.ya?ml | sort -u)
elif [ "$PLATFORM" = "Linux" ]
then
while IFS= read -r -d $'\n'; do
tmp_array+=("$REPLY")
done < <(find $ROOT_DIR -type f -regex .*/docker-compose.ya?ml | sort -u)
fi
# map file to parent dir
for i in "${tmp_array[@]}"
do
parent=$(dirname $i)
array+=("$(realpath $parent)")
done
}
init_array
########## Getting stacks ##########
########## Getting stacks as string ##########
array_string=""
for i in "${!array[@]}"; do
# array_string+="$((i + 1)) ${array[$i]} off " # multiple selection
array_string+="$((i + 1)) ${array[$i]} " # single selection
done
########## Getting stacks as string ##########
########## Operations as string ##########
operations=("up" "down" "restart" "upgrade" "resync")
operations_string=""
for i in "${!operations[@]}"; do
operations_string+="$((i + 1)) ${operations[$i]} " # single selection
done
########## Operations as string ##########
########## Get stack status ##########
get_stack_status() {
second_line=$(cd "$1"; docker-compose ps --services)
if [[ "$second_line" =~ ^[[:space:]] || -z "$second_line" || $second_line == "" ]]
then
running_services=""
stack_status="down"
else
stack_status="up"
running_services=$(echo "$second_line" | tr '\n\r' ' ')
fi
}
########## Get stack status ##########
########## Get stack ##########
SELECTED_FOLDER=""
select_folder(){
# get stack
TMP_FILE=$(mktemp)
# check if array empty
if [ -z "$array" ]
then
clear
echo "No stacks detected"
exit 0
fi
dialog --ok-label "NEXT" --cancel-label "QUIT" --menu "Select the stack:" 0 0 0 $array_string 2>$TMP_FILE
stack_index=$(cat $TMP_FILE)
rm $TMP_FILE
# clear
if [ $stack_index ]
then
SELECTED_FOLDER=${array[$((stack_index - 1))]}
else # No stack selected
clear
exit 0
fi
}
select_folder
########## Get stack ##########
########## Get operation ##########
SELECTED_OPERATION=""
select_operation(){
# get 2nd header
MENU_HEADER2="Select the operation: "
MENU_HEADER2="$MENU_HEADER2 Stack '$(basename $1)'"
stack_status=""
running_services=""
get_stack_status "$1"
MENU_HEADER2="$MENU_HEADER2 | Status '$stack_status'"
if [ "$stack_status" == "up" ]
then
MENU_HEADER2="$MENU_HEADER2 | running services: $running_services"
fi
MENU_HEADER2="$MENU_HEADER2 | located at '$1'"
TMP_FILE=$(mktemp)
dialog --ok-label "RUN" --cancel-label "QUIT" --menu "$MENU_HEADER2" 0 0 0 $operations_string 2>$TMP_FILE
operation_index=$(cat $TMP_FILE)
rm $TMP_FILE
# clear
if [ $operation_index ]
then
SELECTED_OPERATION=${operations[$((operation_index - 1))]}
else # No operation selected
clear
exit 0
fi
}
select_operation $SELECTED_FOLDER
########## Get operation ##########
########## Commands ##########
run_docker_compose_operation () {
clear
echo "----- Stack '$(basename $1)' -----"
echo "- status: $stack_status"
if [ "$stack_status" == "up" ]
then
echo "- running services: $running_services"
fi
echo "- located at: $1"
echo "- running command '$2' at '$1'..."
case $2 in
"resync") cd $1; docker-compose down && git pull && docker-compose up -d ;cd $CURRENT_DIR;exit;;
"upgrade") cd $1; docker-compose down && docker-compose pull && docker-compose up -d ;cd $CURRENT_DIR;exit;;
"restart") cd $1 && docker-compose down && docker-compose up -d ;cd $CURRENT_DIR;exit;;
"up") cd $1; docker-compose up -d ;cd $CURRENT_DIR;exit;;
"down") cd $1; docker-compose down ;cd $CURRENT_DIR;exit;;
"Quit") exit;;
*) echo "Invalid options operation '$2' at folder '$1'.";exit;;
esac
}
########## Commands ##########
########## Main ##########
case $SELECTED_FOLDER in
"Quit")
clear
exit
;;
*)
run_docker_compose_operation $SELECTED_FOLDER $SELECTED_OPERATION
exit
;;
esac
########## Main ##########