-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdocker-compose-manager.local.sh
More file actions
executable file
·208 lines (185 loc) · 5.37 KB
/
Copy pathdocker-compose-manager.local.sh
File metadata and controls
executable file
·208 lines (185 loc) · 5.37 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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#!/bin/bash
##########################################################
########## Docker Compose Manager, dialog #########
########## run command on multi local stacks #########
########## docker-compose.local.ya?ml #########
########## 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=()
local_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.local.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.local.ya?ml | sort -u)
fi
# map file to parent dir
for i in "${tmp_array[@]}"
do
parent=$(dirname $i)
array+=("$(realpath $parent)")
local_array+=("$(realpath $i)")
done
}
init_array
########## Getting stacks ##########
########## Get array stack status ##########
array_stack_status=()
array_running_services=()
get_array_stack_status() {
for st in "${local_array[@]}" ; do
second_line=$(docker-compose -f "$st" ps --services) # add 2>/dev/null to disable WARNING
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
array_stack_status+=("$stack_status")
array_running_services+=("$running_services")
done
}
get_array_stack_status
########## Get array stack status ##########
########## Getting stacks as string ##########
array_string=""
for i in "${!array[@]}"; do
array_string+="$((i + 1)) (${array_stack_status[$i]})->${array[$i]} off " # multiple 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 operation ##########
SELECTED_OPERATION=""
select_operation(){
TMP_FILE=$(mktemp)
dialog --ok-label "NEXT" --cancel-label "QUIT" --menu "Select the operation: " 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
########## Get operation ##########
########## Get stacks ##########
SELECTED_FOLDER_INDEXES=()
SELECTED_FOLDERS=()
select_folder(){
# get stack
TMP_FILE=$(mktemp)
if [ -z "$array" ]
then
clear
echo "No stacks detected"
exit 0
fi
dialog --ok-label "RUN" --cancel-label "QUIT" --checklist "Select stacks (selected: '$SELECTED_OPERATION'):" 0 0 0 $array_string 2>$TMP_FILE
stack_indexes=$(cat $TMP_FILE)
rm $TMP_FILE
# clear
if [[ -z $stack_indexes ]]
then
clear
exit 0
else
stack_indexes=( "$stack_indexes" ) # convert string to array of indexes
for i in $stack_indexes; do
stack=${array[$((i - 1))]}
SELECTED_FOLDERS+=("$stack")
SELECTED_FOLDER_INDEXES+=($((i - 1)))
done
fi
}
select_folder
########## Get stacks ##########
########## Commands ##########
run_docker_compose_operation () {
echo "----- Stack '$(basename $1)' -----"
echo "- status: $3"
if [ "$3" == "up" ]
then
echo "- running services: $4"
fi
echo "- located at: $1"
echo "- running command '$2' at '$1'..."
case $2 in
"resync")
cd $1; docker-compose -f $5 down && git pull && docker-compose -f $5 up -d ;cd $CURRENT_DIR;
;;
"upgrade")
cd $1; docker-compose -f $5 down && docker-compose -f $5 pull && docker-compose -f $5 up -d ;cd $CURRENT_DIR;
;;
"restart")
cd $1 && docker-compose -f $5 down && docker-compose -f $5 up -d ;cd $CURRENT_DIR;
;;
"up")
cd $1; docker-compose -f $5 up -d ;cd $CURRENT_DIR;
;;
"down")
cd $1; docker-compose -f $5 down ;cd $CURRENT_DIR;
;;
"Quit")
exit;
;;
*)
echo "ERROR: Invalid options operation '$2' at folder '$1'.";
;;
esac
}
########## Commands ##########
########## Main ##########
if (( ${#SELECTED_FOLDERS[@]} != 0 )); then
clear
for i in ${SELECTED_FOLDER_INDEXES[*]} ; do
run_docker_compose_operation "${array[$i]}" "$SELECTED_OPERATION" "${array_stack_status[$i]}" "${array_running_services[$i]}" "${local_array[$i]}"
done
exit
else # No stacks selected.
clear
exit 0
fi
########## Main ##########