-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpull_and_recreate_stacks.sh
79 lines (73 loc) · 2.08 KB
/
pull_and_recreate_stacks.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
####################################################
# Docker auto update containers with latest image #
####################################################
#### /!\ You can add -y to avoid confirmations /!\
##### VARS #####
stacks_path=/YOURPATH
stacks_folders=($(ls $stacks_path))
##### VARS #####
##### FUNCTIONS #####
pulling_image () {
echo -e "\e[32m"
echo "Pulling lastest images if necessary"
echo -e "\e[37m"
for stack in "${stacks_folders[@]}"
do
cd $stacks_path/$stack
docker-compose pull
done
echo -e "\e[39m"
}
recreate_containers () {
echo -e "\e[32m"
echo "Recreating containers if necessary"
echo -e "\e[37m"
for stack in "${stacks_folders[@]}"
do
cd $stacks_path/$stack
docker-compose up -d
done
echo -e "\e[39m"
}
dangling_image () {
echo -e "\e[32m"
echo "Removing oldest images"
echo -e "\e[37m"
for stack in "${stacks_folders[@]}"
do
cd $stacks_path/$stack
docker image prune -f
done
echo -e "\e[39m"
}
ask_yes_or_no () {
read -p "$1 ([Y]es or [N]o): "
case $(echo $REPLY | tr '[A-Z]' '[a-z]') in
y|yes) echo "yes" ;;
*) echo "no" ;;
esac
}
##### FUNCTIONS #####
if [ "$1" == "-y" ]
then
pulling_image
recreate_containers
dangling_image
echo -e "\e[36mOperation completed"
echo -e "\e[39m"
else
echo -e "\e[91m"
if [[ "no" == $(ask_yes_or_no "Do you want to pull the latest images from all your stacks and then automatically recreate them if necessary?") || \
"no" == $(ask_yes_or_no "Are you *really* sure?!") ]]
then
echo "Skipped."
exit 0
else
pulling_image
recreate_containers
dangling_image
echo -e "\e[36mOperation completed"
echo -e "\e[39m"
fi
fi