-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdocker-update.sh
More file actions
executable file
·211 lines (177 loc) · 8.1 KB
/
docker-update.sh
File metadata and controls
executable file
·211 lines (177 loc) · 8.1 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
#!/bin/bash
set -x
########## Print GNU GPLv3 LICENSE notices ##########
echo '
██████ ██████ ██████ ██ ██ ███████ ██████
███████ ██████ ████ ██████ ██████ ████ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
███ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ █████ █████ ██████
███ ██ ██ ██ ██ ███ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
███████ ██████ ██ ██████ ██████ ██ ████ ██████ ██████ ██████ ██ ██ ███████ ██ ██
Portable, Server independent, Docker-based code to get the zorg Websites and Services up, running, and hosted.
Copyright (C) 2024-2025 zorg Verein <https://github.com/zorgch>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
This program comes with ABSOLUTELY NO WARRANTY; for details read the README.
This is free software, and you are welcome to redistribute it
under certain conditions; see the LICENSE.'
# NOTE: Don't forget to make me executable: chmod +x docker-update.sh
############ Parse CLI arguments ##############
# Start immediately when `--force` is passed
SKIP_TIMER=false
[ "${1:-}" = "--force" ] && SKIP_TIMER=true
######## Check and Load dependencies ##########
if [ ! -f "./docker-compose.yml" ]; then
echo "Error: No ./docker-compose.yml file found"
exit 1
fi
if [ ! -f "./.env" ]; then
echo "Error: No ./.env file found"
exit 1
else
source "./.env"
fi
############### Assign Variables ##############
DOCKER_STACK="Docker"
DOCKER_STATUS_URL="the Status Dashboard"
[ -n "$COMPOSE_PROJECT_NAME" ] && DOCKER_STACK="Docker $COMPOSE_PROJECT_NAME"
[[ -n "$DASHBOARD_HOST" && -n "$DOMAINNAME" ]] && DOCKER_STATUS_URL="[Status Dashboard](https://$DASHBOARD_HOST.$DOMAINNAME)"
[ -n "$TELEGRAM_BOT_SERVICEALERTS_TOKEN" ] && BOT_TOKEN="$TELEGRAM_BOT_SERVICEALERTS_TOKEN"
[ -n "$TELEGRAM_BOT_SERVICEALERTS_CHATID" ] && CHAT_ID="$TELEGRAM_BOT_SERVICEALERTS_CHATID"
[ -n "$TELEGRAM_BOT_SERVICEALERTS_CHATTOPICID" ] && CHAT_TOPIC_ID="$TELEGRAM_BOT_SERVICEALERTS_CHATTOPICID"
TIME_TO_START="in T-15"
if [ "$SKIP_TIMER" = true ]; then
TIME_TO_START="start NOW (forced)"
fi
MESSAGE_ANNOUNCE="ANNOUNCEMENT 🔜 Scheduled updates for *$DOCKER_STACK* $TIME_TO_START!"
MESSAGE_TIMER_T5="⌛️ In 5 minutes: *$DOCKER_STACK* updating starts!"
#MESSAGE_TIMER_C10="🔟"
#MESSAGE_TIMER_C05="5️⃣"
#MESSAGE_TIMER_C04="4️⃣"
MESSAGE_TIMER_C03="3️⃣"
MESSAGE_TIMER_C02="2️⃣"
MESSAGE_TIMER_C01="1️⃣"
MESSAGE_START="⚙️ STARTING: *$DOCKER_STACK* updates..."
MESSAGE_CLEANUP="🗑️ Removing unused *__STATS__*..."
MESSAGE_UPDATE="📥 Updating *__DOCKERIMAGE__*"
MESSAGE_NOUPDATE="⏩ Skipping *__DOCKERIMAGE__* (no update)"
MESSAGE_ERRUPDATE="⚠️ Something went wrong checking *__DOCKERIMAGE__*, please investigate."
MESSAGE_RESTARTING="🔁 Restarting *${COMPOSE_PROJECT_NAME:+$COMPOSE_PROJECT_NAME-}__DOCKERSERVICE__*..."
MESSAGE_FINISH="✅ ALL DONE: *$DOCKER_STACK* is up-to-date!
> Check $DOCKER_STATUS_URL"
send_telegram_message() {
local STATUSMESSAGE="$1"
STATUSMESSAGE="${STATUSMESSAGE//:/\\:}" # Escape ":"
STATUSMESSAGE="${STATUSMESSAGE//-/\\-}" # Escape "-"
STATUSMESSAGE="${STATUSMESSAGE//,/\\,}" # Escape ","
STATUSMESSAGE="${STATUSMESSAGE//./\\.}" # Escape "."
STATUSMESSAGE="${STATUSMESSAGE//!/\\!}" # Escape "!"
# Only send if both token and chat_id are set and message is not empty
if [[ -n "$BOT_TOKEN" && -n "$CHAT_ID" && -n "$STATUSMESSAGE" ]]; then
curl -sS --fail -X POST -H "Content-Type:multipart/form-data" \
-F chat_id="$CHAT_ID" \
-F text="$STATUSMESSAGE" \
-F message_thread_id="$CHAT_TOPIC_ID" \
-F parse_mode=MarkdownV2 \
"https://api.telegram.org/bot$BOT_TOKEN/sendMessage" 2>&1 || true
else
# Alternatively print to stdout
echo "$1"
fi
}
########### Pre-flight Checks ##############
DOCKER_COMPOSE_CONFIG_CHECK=$(docker compose --profile all config 2>&1 > /dev/null)
if [ $? -ne 0 ]; then
ERROR=" :
\`docker compose --profile all config\`
> ❌ $DOCKER_COMPOSE_CONFIG_CHECK
"
MESSAGE="${MESSAGE_ERRUPDATE/__DOCKERIMAGE__/$ERROR}"
send_telegram_message "$MESSAGE"
exit 1
fi
############ Announcements ###############
send_telegram_message "$MESSAGE_ANNOUNCE"
if [ "$SKIP_TIMER" = true ]; then
sleep 600 # Wait now 10 minutes
send_telegram_message "$MESSAGE_TIMER_T5"
sleep 297 # Wait now 4 minutes 57 seconds
#send_telegram_message "$MESSAGE_TIMER_C10"
#sleep 5
# Countdown start
send_telegram_message "$MESSAGE_TIMER_C03"
sleep 1
send_telegram_message "$MESSAGE_TIMER_C02"
sleep 1
send_telegram_message "$MESSAGE_TIMER_C01"
sleep 1
send_telegram_message "$MESSAGE_START"
fi
################# Cleanup unused Docker images #################
UNUSED_IMAGES=$(docker image ls -f "dangling=true" --format '{{.Repository}} {{.Size}}' |
awk '
$1!="<none>"{
s=$2
if(s~/GB$/){sub(/GB/,"",s); gb=s+0}
else if(s~/MB$/){sub(/MB/,"",s); gb=(s+0)/1024}
else if(s~/kB$/){sub(/kB/,"",s); gb=(s+0)/1024/1024}
else if(s~/B$/){sub(/B/,"",s); gb=(s+0)/1024/1024/1024}
sum+=gb; n[$1]=1
}
END{
printf "%.1fGB: ",sum; sep="";
for(i in n){printf "%s%s",sep,i; sep=", "}
}')
# Only send message and prune if there are unused images
if [[ ! "$UNUSED_IMAGES" =~ ^0\.0 ]]; then
MESSAGE="${MESSAGE_CLEANUP/__STATS__/$UNUSED_IMAGES}"
send_telegram_message "$MESSAGE"
docker image prune -a -f
docker builder prune -f
fi
########## Update all Docker images ##########
# Source: https://stackoverflow.com/a/44953583
for image in $(docker compose --profile all config | awk '/image:/ { print $2 }')
do
# Checking image for update status
if ! DOCKER_PULL_STATUS=$(docker pull "$image" 2>&1); then
MESSAGE="${MESSAGE_ERRUPDATE/__DOCKERIMAGE__/$image}"
send_telegram_message "$MESSAGE"
continue
fi
# New Image available
if echo "$DOCKER_PULL_STATUS" | grep -q "Downloaded newer image"; then
MESSAGE="${MESSAGE_UPDATE/__DOCKERIMAGE__/$image}"
send_telegram_message "$MESSAGE"
# MESSAGE2="${MESSAGE_RESTARTING/__DOCKERIMAGE__/$image}"
# send_telegram_message "$MESSAGE2"
# docker compose up -d --build "$image" 2>&1
# No udpate needed
elif echo "$DOCKER_PULL_STATUS" | grep -q "Image is up to date"; then
MESSAGE="${MESSAGE_NOUPDATE/__DOCKERIMAGE__/$image}"
send_telegram_message "$MESSAGE"
# Unknown update status
else
MESSAGE="${MESSAGE_ERRUPDATE/__DOCKERIMAGE__/$image}"
send_telegram_message "$MESSAGE"
fi
done
################# Restart all running Docker Services #################
for container in $(docker compose ps --services --status=running)
do
MESSAGE="${MESSAGE_RESTARTING/__DOCKERSERVICE__/$container}"
send_telegram_message "$MESSAGE"
docker compose up -d --remove-orphans --build "$container" 2>&1
done
#### Remove old Docker images (that have been updated) ####
docker image prune -a -f
############# Finish ##################
send_telegram_message "$MESSAGE_FINISH"
exit 0