-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdocker-compose-recursive.sh
More file actions
383 lines (323 loc) · 11.3 KB
/
docker-compose-recursive.sh
File metadata and controls
383 lines (323 loc) · 11.3 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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
#!/bin/bash
# https://github.com/PiDroid-B/docker-compose-recursive
# MIT ©2022 PiDroid-B
############################################################
# Const #
############################################################
VERSION="v0.3.2"
RED='\033[0;31m'
GREEN='\033[0;32m'
ORANGE='\033[0;33m'
GRAY='\033[1;30m'
BLUE='\033[1;34m'
NC='\033[0m'
# Gets the command name without path
cmd="$(basename "$0")"
ACTION=""
FORCE=0
FILE=""
DIRECTORIES=""
DIRECTORIES_INV=""
VERBOSE=0
PRUNE=0
UPDATE=0
DOCKER=""
############################################################
# Help #
############################################################
Help(){
echo -e "\
docker-compose-recursive.sh - version ${VERSION}
manage a tree of docker-compose :
#default only dir with 'OK' file can be managed (or use the 'force' option)
#conf file ${cmd}.conf is used is exist to manage the tree (can be define too by -c)
${cmd}
├── stack1
│ ├── docker-compose.yml
│ ├── .env
│ └── OK
└── stack2
└── docker-compose.yml
${GREEN}Usage : ${cmd} [FOLDER] <ACTION> [OPTION]${NC}
FOLDER
# apply only to this folder (apply on all folder if missing)
ACTION
#-h## Help : show this help
#-u## Up : builds, (re)creates, starts, and attaches to containers for each docker-compose
#-d## Down : stops containers and removes containers, networks, volumes, and images created by up
#-r## Restart : Down and Up on each docker-compose
#-p## Prune : remove unsed images, volumes and networks
#-i## Install/Update : check if new versions of docker-compose and dcr exist (autoupdate for docker-compose if force)
#-c [<file>]# Conf file : get list of folder from file instead of generate it, return list when file is missing
OPTION
#-f## Force : no check of 'OK' file (udr), auto install/upgrade (i)
#-v## Verbose : show more information
#${RED}/!\ use force only if you are sure what you do${NC}
#use this script at your own risk
some usefull commands :
generate conf file from running docker-compose :
docker compose ls | cut -f1 -d\" \" | sed \"1d\" > myfile.conf
https://github.com/PiDroid-B/docker-compose-recursive MIT ©2022 PiDroid-B
" | tr "#" "\t"
#" | column -t -s ";"
}
############################################################
# Functions #
############################################################
Up(){
echo -e "${GREEN}####### ${1} > up #######${NC}"
docker compose up -d
}
Down(){
echo -e "${ORANGE}####### ${1} > down #######${NC}"
docker compose down
}
Prune(){
echo -e "${GREEN}####### Prune unused images/volumes/networks #######${NC}"
verbose "# Prune"
verbose " - Prune unused images"
docker image prune -fa
verbose " - Prune unused volumes"
docker volume prune -f
verbose " - Prune unused networks\n"
docker network prune -f
}
DC_Update(){
local dc_curr_version
local dc_last_version
local github_content
local filename
local files
echo -e "${GREEN}####### Install/Update docker-compose #######${NC}"
verbose "# docker-compose"
dc_curr_version="not installed"
if [[ -r /usr/libexec/docker/cli-plugins/docker-compose ]]; then
dc_curr_version="$( docker compose version | sed -r "s/^.*(v[0-9]\.[0-9]\.[0-9]).*/\1/" )"
fi
verbose " - current version : ${dc_curr_version}"
github_content="$( curl -s https://api.github.com/repos/docker/compose/releases/latest )"
dc_last_version="$( echo "${github_content}" | grep "tag_name" | sed -r "s/^.*(v[0-9].[0-9].[0-9]).*/\1/" )"
#dc_last_version="$( curl -s https://api.github.com/repos/docker/compose/releases/latest | grep "tag_name" | sed -r "s/^.*(v[0-9].[0-9].[0-9]).*/\1/" )"
verbose " - last version : ${dc_last_version}"
if [[ "${dc_curr_version}" == "${dc_last_version}" ]]; then
echo "docker-compose already up to date"
else
echo "docker-compose update avaiable : ${dc_curr_version} > ${dc_last_version}"
verbose " - get information from github"
#github_content="$( curl -s https://api.github.com/repos/docker/compose/releases/latest )"
filename="docker-compose-$(uname -s)-$(uname -m)"
files="$( echo "${github_content}" | grep -Ei "browser_download_url.*$filename" | cut -d"\"" -f4 )"
if [[ "${FORCE}" -eq 1 ]]; then
echo "force==1 > auto-update"
verbose " - change directory to /tmp/"
command pushd "/tmp/" > /dev/null || exit 1
for f in $files; do
verbose " - download ${f}"
curl -L -o "/tmp/$(basename "${f}")" "${f}"
[[ "${f}" == *".sha256"* ]] && f_sha="$(basename "${f}")" || f_dc="$(basename "${f}")"
done
verbose " - checksum of ${f_dc} with ${f_sha}"
if ! sha256sum -c "${f_sha}" > /dev/null ; then
echo -e "${RED}${f_dc} with ${f_sha} : checksum failed ${NC}"
exit 1
fi
verbose " - copy ${filename}"
cp "${f_dc}" /usr/libexec/docker/cli-plugins/docker-compose || exit 1
command popd > /dev/null || exit 1
verbose " - return to previous directory $(pwd)"
chmod +x /usr/libexec/docker/cli-plugins/docker-compose || exit 1
verbose " - chmod for docker-compose"
echo -e "${GREEN}####### docker-compose up to date ! #######${NC}"
else
echo
echo "##################################################################################################"
echo -e "${ORANGE}docker-compose new version available : ${NC}${dc_curr_version} > ${GREEN}${dc_last_version}${NC}"
echo -e "\t${GREEN}How to install/upgrade${NC}"
echo
for f in $files; do
echo "wget \"${f}\" -O \"/tmp/$(basename \""${f}"\")\""
[[ "${f}" == *".sha256"* ]] && f_sha="${f}" || f_dc="${f}"
done
echo
echo "sha256sum -c \"${f_sha}\""
echo
echo "mv \"${f_dc}\" /usr/libexec/docker/cli-plugins/docker-compose"
echo
echo "chmod +x /usr/libexec/docker/cli-plugins/docker-compose"
echo ""
echo -e "${GREEN}####### Exit to manage it #######${NC}"
echo
exit 0
fi
fi
}
# Run_for_item <action (Up|Down)> <directory>
Run_for_item(){
local action=${1}
local directory=${2}
verbose "\n - Action ${action} (force=$FORCE) on $directory"
if [[ -r "${directory}/docker-compose.yml" ]]; then
verbose " + change directory to ${directory}"
command pushd "${directory}" > /dev/null || exit 1
if [[ "${FORCE}" -eq 1 || -e "OK" ]]; then
verbose " + Force==1 or file OK found"
verbose " + Do action ${action}"
${action} "${directory}"
else
verbose " + Force($FORCE)==0 or file OK not found"
verbose " + Skip action ${action}"
echo -e "${GRAY}####### ${directory} > ${1} [SKIP] (OK && force == false) #######${NC}"
fi
command popd > /dev/null || exit 1
verbose " + return to previous directory $(pwd)"
else
verbose " + file ${directory}/docker-compose.yml not found"
echo -e "${RED}####### ${directory} > ERROR ${directory} not found #######${NC}"
fi
}
# Run_for_all <action (Up|Down|Restart)>
Run_for_all(){
local action=${1}
verbose "# Action ${action} (force=$FORCE) on each directory of $(Array_to_Str "${DIRECTORIES}")"
if [[ "Down Restart" == *"${action}"* ]]; then
verbose "\n - Action ${action}[Down] (force=$FORCE) on each directory of $(Array_to_Str "${DIRECTORIES_INV}")"
for dir in $DIRECTORIES_INV; do
Run_for_item "Down" "${dir}"
done
fi
if [[ "Up Restart" == *"${action}"* ]]; then
verbose "\n - Action ${action}[Up] (force=$FORCE) on each directory of $(Array_to_Str "${DIRECTORIES}")"
for dir in $DIRECTORIES; do
Run_for_item "Up" "${dir}"
done
fi
}
# Run_for_one <action (Up|Down|Restart)> <docker>
Run_for_one(){
local action=${1}
local docker=${2}
verbose "# Action ${action} (force=$FORCE) on ${docker}"
if [[ "Down Restart" == *"${action}"* ]]; then
verbose "\n - Action ${action}[Down] (force=$FORCE) on ${docker}"
Run_for_item "Down" "${docker}"
fi
if [[ "Up Restart" == *"${action}"* ]]; then
verbose "\n - Action ${action}[Up] (force=$FORCE) on ${docker}"
Run_for_item "Up" "${docker}"
fi
}
# Array_to_Str <array>
Array_to_Str(){
local result=""
for i in $1; do
result="$result $i"
done
echo "$result"
}
############################################################
# Main #
############################################################
# if first char of first arg is not '-' then it's the directory of a docker compose
if [[ -n "${1}" && "${1:0:1}" != "-" ]]; then
DOCKER="${1}"
shift
fi
last_arg=""
for option in "$@"; do
case $option in
-c) FILE="<GetList>";;
-*)
for (( i=1; i<${#option}; i++ )); do
case ${option:$i:1} in
h) Help; exit 0;;
u) ACTION="Up";;
d) ACTION="Down";;
r) ACTION="Restart";;
i) UPDATE=1;;
f) FORCE=1;;
p) PRUNE=1;;
v) VERBOSE=1;;
esac
done
;;
*)
case $last_arg in
-c) FILE="$option";;
:) echo -e "${RED}missing argument for $OPTARG ${NC}\n\n"; Help; exit 1;;
?) echo -e "${RED}Invalid option ${NC}\n\n" ; Help; exit 1;;
esac
;;
esac
last_arg=$option
done
# while getopts "hudrifcvp" option; do
# case $option in
# h) Help; exit 0;;
# u) ACTION="Up";;
# d) ACTION="Down";;
# r) ACTION="Restart";;
# i) UPDATE=1;;
# f) FORCE=1;;
# c) FILE="${OPTARG}";;
# p) PRUNE=1;;
# v) VERBOSE=1;;
# :) echo -e "${RED}missing argument for $OPTARG ${NC}\n\n"; Help; exit 1;;
# ?) echo -e "${RED}Invalid option ${NC}\n\n" ; Help; exit 1;;
# esac
# done
verbose(){
[[ "${VERBOSE}" -eq 1 ]] && printf "${BLUE}${1}${NC}\n"
}
verbose "\
# Command and options
- ACTION=${ACTION}
- PRUNE=${PRUNE}
- UPDATE=${UPDATE}
- FORCE=${FORCE}
- DOCKER=${DOCKER}
- FILE=${FILE:-"<GetList>"}
- VERBOSE=${VERBOSE}
"
if [[ -z "${ACTION}" && "${PRUNE}" -eq 0 && "${UPDATE}" -eq 0 && "${FILE}" != "<GetList>" ]]; then
echo -e "${RED}missing option ${NC}\n\n"
Help
exit 1
fi
verbose "# Directories to manage"
if [[ -n "${FILE}" && -r "${FILE}" ]]; then
verbose " - file ${FILE} found"
DIRECTORIES="$(cat "${FILE}")"
else
verbose " - check file directories.conf exists"
if [[ -r /usr/local/etc/docker-compose-recursive/directories.conf ]]; then
verbose " - /usr/local/etc/docker-compose-recursive/directories.conf found"
DIRECTORIES="$(cat "/usr/local/etc/docker-compose-recursive/directories.conf" )"
elif [[ -r /etc/docker-compose-recursive/directories.conf ]]; then
verbose " - /etc/docker-compose-recursive/directories.conf found"
DIRECTORIES="$(cat "/etc/docker-compose-recursive/directories.conf" )"
elif [[ -r /opt/docker-compose-recursive/directories.conf ]]; then
verbose " - /opt/docker-compose-recursive/directories.conf found"
DIRECTORIES="$(cat "/opt/docker-compose-recursive/directories.conf" )"
else
verbose " - no file, list auto-generated from current directory"
DIRECTORIES="$(echo */ | tr ' ' '\n' | sed 's-/$--' )"
# if contains no folder
[[ "${DIRECTORIES}" == "*" ]] && DIRECTORIES=""
fi
fi
DIRECTORIES_INV="$(echo "${DIRECTORIES}" | tac)"
verbose " - DIRECTORIES=$(Array_to_Str "${DIRECTORIES}")"
verbose " - DIRECTORIES_INV=$(Array_to_Str "${DIRECTORIES_INV}")\n"
if [[ "${FILE}" == "<GetList>" ]]; then
echo "$DIRECTORIES"
exit 0
fi
echo -e "${GREEN}####### START #######${NC}\n"
[[ "${PRUNE}" -eq 1 ]] && Prune
[[ "${UPDATE}" -eq 1 ]] && DC_Update
if [[ -n "${DOCKER}" ]]; then
[[ -n "${ACTION}" ]] && Run_for_one "${ACTION}" "${DOCKER}"
else
[[ -n "${ACTION}" ]] && Run_for_all "${ACTION}"
fi
echo -e "${GREEN}####### END #######${NC}\n"