blob: 3e53d2134ad259d5c023b37e6930eacccb72fbf2 [file] [log] [blame]
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +01001#!/usr/bin/env bash
2
Matthias Andreas Benkard3c18a082022-12-27 17:32:58 +01003############## Begin Function Section ##############
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +01004
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +02005SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
6MAILCOW_CONF="${SCRIPT_DIR}/mailcow.conf"
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +01007
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +02008# Ensure the script is run from the directory that contains mailcow.conf
9if [ ! -f "${PWD}/mailcow.conf" ]; then
10 if [ -f "${SCRIPT_DIR}/mailcow.conf" ]; then
11 echo -e "\e[33mPlease run this script directly from the mailcow installation directory:\e[0m"
12 echo -e " \e[36mcd ${SCRIPT_DIR} && ./update.sh\e[0m"
13 exit 1
Matthias Andreas Benkard8f915942021-08-16 10:57:25 +020014 else
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +020015 echo -e "\e[31mmailcow.conf not found in current directory or script directory (\e[36m${SCRIPT_DIR}\e[31m).\e[0m"
16 echo -e "\e[33mRun this script directly from your mailcow installation directory.\e[0m"
Matthias Andreas Benkardd63fe562023-11-18 13:18:30 +010017 exit 1
18 fi
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +020019fi
20BRANCH="$(cd "${SCRIPT_DIR}" && git rev-parse --abbrev-ref HEAD)"
Matthias Andreas Benkardd63fe562023-11-18 13:18:30 +010021
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +020022# Check for --dev flag early to skip _modules update
23for arg in "$@"; do
24 if [[ "$arg" == "--dev" || "$arg" == "-d" ]]; then
25 echo -e "\e[32mRunning in Developer mode...\e[0m"
26 DEV=y
27 break
28 fi
29done
30
31MODULE_DIR="${SCRIPT_DIR}/_modules"
32
33if [ ! "$DEV" ]; then
34 # Calculate hash before fetch
35 if [[ -d "${MODULE_DIR}" && -n "$(ls -A "${MODULE_DIR}" 2>/dev/null)" ]]; then
36 MODULES_HASH_BEFORE=$(find "${MODULE_DIR}" -type f -exec sha256sum {} \; 2>/dev/null | sort | sha256sum | awk '{print $1}')
37 else
38 MODULES_HASH_BEFORE="EMPTY"
Matthias Andreas Benkardd63fe562023-11-18 13:18:30 +010039 fi
40
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +020041 echo -e "\e[33mFetching latest _modules from origin/${BRANCH}…\e[0m"
42 git fetch origin "${BRANCH}"
43 git checkout "origin/${BRANCH}" -- _modules
Matthias Andreas Benkardd63fe562023-11-18 13:18:30 +010044
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +020045 if [[ ! -d "${MODULE_DIR}" || -z "$(ls -A "${MODULE_DIR}")" ]]; then
46 echo -e "\e[31mError: _modules is still missing or empty after fetch!\e[0m"
47 exit 2
48 fi
49
50 # Calculate hash after fetch
51 MODULES_HASH_AFTER=$(find "${MODULE_DIR}" -type f -exec sha256sum {} \; 2>/dev/null | sort | sha256sum | awk '{print $1}')
52
53 # Check if modules changed
54 if [[ "${MODULES_HASH_BEFORE}" != "${MODULES_HASH_AFTER}" ]]; then
55 echo -e "\e[33m_modules have been updated. Please restart the update script.\e[0m"
56 exit 2
57 fi
58else
59 echo -e "\e[33mDeveloper mode: Skipping _modules update from git\e[0m"
60 if [[ ! -d "${MODULE_DIR}" || -z "$(ls -A "${MODULE_DIR}")" ]]; then
61 echo -e "\e[31mError: _modules directory is missing or empty!\e[0m"
62 exit 2
Matthias Andreas Benkardd63fe562023-11-18 13:18:30 +010063 fi
64fi
Matthias Andreas Benkardd63fe562023-11-18 13:18:30 +010065
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +020066source _modules/scripts/core.sh
67source _modules/scripts/ipv6_controller.sh
68source _modules/scripts/new_options.sh
69source _modules/scripts/migrate_options.sh
Matthias Andreas Benkardd63fe562023-11-18 13:18:30 +010070
71############## End Function Section ##############
72
73# Check permissions
74if [ "$(id -u)" -ne "0" ]; then
75 echo "You need to be root"
76 exit 1
77fi
78
Matthias Andreas Benkardd63fe562023-11-18 13:18:30 +010079# Run pre-update-hook
80if [ -f "${SCRIPT_DIR}/pre_update_hook.sh" ]; then
81 bash "${SCRIPT_DIR}/pre_update_hook.sh"
82fi
83
Matthias Andreas Benkardd63fe562023-11-18 13:18:30 +010084# Exit on error and pipefail
85set -o pipefail
86
87# Setting high dc timeout
88export COMPOSE_HTTP_TIMEOUT=600
89
90# Add /opt/bin to PATH
91PATH=$PATH:/opt/bin
92
93umask 0022
94
95# Unset COMPOSE_COMMAND and DOCKER_COMPOSE_VERSION Variable to be on the newest state.
96unset COMPOSE_COMMAND
97unset DOCKER_COMPOSE_VERSION
98
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +020099get_installed_tools
100
101get_docker_version
Matthias Andreas Benkardd63fe562023-11-18 13:18:30 +0100102
103export LC_ALL=C
104DATE=$(date +%Y-%m-%d_%H_%M_%S)
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +0200105BRANCH="$(cd "${SCRIPT_DIR}"; git rev-parse --abbrev-ref HEAD)"
Matthias Andreas Benkardd63fe562023-11-18 13:18:30 +0100106
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +0100107while (($#)); do
108 case "${1}" in
109 --check|-c)
110 echo "Checking remote code for updates..."
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +0200111 LATEST_REV=$(git ls-remote --exit-code --refs --quiet https://github.com/mailcow/mailcow-dockerized "${BRANCH}" | cut -f1)
112 if [ "$?" -ne 0 ]; then
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +0100113 echo "A problem occurred while trying to fetch the latest revision from github."
114 exit 99
115 fi
116 if [[ -z $(git log HEAD --pretty=format:"%H" | grep "${LATEST_REV}") ]]; then
Matthias Andreas Benkard8f915942021-08-16 10:57:25 +0200117 echo -e "Updated code is available.\nThe changes can be found here: https://github.com/mailcow/mailcow-dockerized/commits/master"
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +0200118 git log --date=short --pretty=format:"%ad - %s" "$(git rev-parse --short HEAD)"..origin/master
119 exit 0
120 else
121 echo "No updates available."
122 exit 3
123 fi
124 ;;
125 --check-tags)
126 echo "Checking remote tags for updates..."
127 LATEST_TAG_REV=$(git ls-remote --exit-code --quiet --tags origin | tail -1 | cut -f1)
128 if [ "$?" -ne 0 ]; then
129 echo "A problem occurred while trying to fetch the latest tag from github."
130 exit 99
131 fi
132 if [[ -z $(git log HEAD --pretty=format:"%H" | grep "${LATEST_TAG_REV}") ]]; then
133 echo -e "New tag is available.\nThe changes can be found here: https://github.com/mailcow/mailcow-dockerized/releases/latest"
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +0100134 exit 0
135 else
136 echo "No updates available."
137 exit 3
138 fi
139 ;;
140 --ours)
141 MERGE_STRATEGY=ours
142 ;;
143 --skip-start)
144 SKIP_START=y
145 ;;
Matthias Andreas Benkard3c18a082022-12-27 17:32:58 +0100146 --skip-ping-check)
147 SKIP_PING_CHECK=y
148 ;;
149 --stable)
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +0200150 CURRENT_BRANCH="$(cd "${SCRIPT_DIR}"; git rev-parse --abbrev-ref HEAD)"
Matthias Andreas Benkard3c18a082022-12-27 17:32:58 +0100151 NEW_BRANCH="master"
152 ;;
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +0100153 --gc)
154 echo -e "\e[32mCollecting garbage...\e[0m"
155 docker_garbage
156 exit 0
157 ;;
Matthias Andreas Benkard3c18a082022-12-27 17:32:58 +0100158 --nightly)
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +0200159 CURRENT_BRANCH="$(cd "${SCRIPT_DIR}"; git rev-parse --abbrev-ref HEAD)"
Matthias Andreas Benkard3c18a082022-12-27 17:32:58 +0100160 NEW_BRANCH="nightly"
161 ;;
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +0100162 --prefetch)
163 echo -e "\e[32mPrefetching images...\e[0m"
164 prefetch_images
165 exit 0
166 ;;
167 -f|--force)
Matthias Andreas Benkardb4b5cc12021-12-28 18:02:04 +0100168 echo -e "\e[32mRunning in forced mode...\e[0m"
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +0100169 FORCE=y
170 ;;
Matthias Andreas Benkardd63fe562023-11-18 13:18:30 +0100171 -d|--dev)
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +0200172 # Already handled at the top of the script before _modules update
173 ;;
174 --legacy)
175 CURRENT_BRANCH="$(cd "${SCRIPT_DIR}"; git rev-parse --abbrev-ref HEAD)"
176 NEW_BRANCH="legacy"
Matthias Andreas Benkardb7a13512023-11-18 16:44:25 +0100177 ;;
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +0100178 --help|-h)
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +0200179 echo './update.sh [-c|--check, --check-tags, --ours, --gc, --nightly, --prefetch, --skip-start, --skip-ping-check, --stable, --legacy, -f|--force, -d|--dev, -h|--help]
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +0100180
181 -c|--check - Check for updates and exit (exit codes => 0: update available, 3: no updates)
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +0200182 --check-tags - Check for newer tags and exit (exit codes => 0: newer tag available, 3: no newer tag)
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +0100183 --ours - Use merge strategy option "ours" to solve conflicts in favor of non-mailcow code (local changes over remote changes), not recommended!
184 --gc - Run garbage collector to delete old image tags
Matthias Andreas Benkard3c18a082022-12-27 17:32:58 +0100185 --nightly - Switch your mailcow updates to the unstable (nightly) branch. FOR TESTING PURPOSES ONLY!!!!
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +0100186 --prefetch - Only prefetch new images and exit (useful to prepare updates)
187 --skip-start - Do not start mailcow after update
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +0200188 --skip-ping-check - Skip ICMP Check to public DNS resolvers (Use it only if you'\''ve blocked any ICMP Connections to your mailcow machine)
189 --stable - Switch your mailcow updates to the stable (master) branch. Default unless you changed it with --nightly or --legacy.
190 --legacy - Switch your mailcow updates to the legacy branch. The legacy branch will only receive security updates until February 2026.
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +0100191 -f|--force - Force update, do not ask questions
Matthias Andreas Benkardd63fe562023-11-18 13:18:30 +0100192 -d|--dev - Enables Developer Mode (No Checkout of update.sh for tests)
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +0100193'
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +0200194 exit 0
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +0100195 esac
196 shift
197done
198
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +0200199[[ ! -f mailcow.conf ]] && { echo -e "\e[31mmailcow.conf is missing! Is mailcow installed?\e[0m"; exit 1;}
200
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +0100201chmod 600 mailcow.conf
202source mailcow.conf
Matthias Andreas Benkard3c18a082022-12-27 17:32:58 +0100203
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +0200204get_compose_type
Matthias Andreas Benkard3c18a082022-12-27 17:32:58 +0100205
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +0100206DOTS=${MAILCOW_HOSTNAME//[^.]};
Matthias Andreas Benkardd63fe562023-11-18 13:18:30 +0100207if [ ${#DOTS} -lt 1 ]; then
208 echo -e "\e[31mMAILCOW_HOSTNAME (${MAILCOW_HOSTNAME}) is not a FQDN!\e[0m"
209 sleep 1
210 echo "Please change it to a FQDN and redeploy the stack with $COMPOSE_COMMAND up -d"
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +0100211 exit 1
Matthias Andreas Benkardd63fe562023-11-18 13:18:30 +0100212elif [[ "${MAILCOW_HOSTNAME: -1}" == "." ]]; then
213 echo "MAILCOW_HOSTNAME (${MAILCOW_HOSTNAME}) is ending with a dot. This is not a valid FQDN!"
214 exit 1
215elif [ ${#DOTS} -eq 1 ]; then
216 echo -e "\e[33mMAILCOW_HOSTNAME (${MAILCOW_HOSTNAME}) does not contain a Subdomain. This is not fully tested and may cause issues.\e[0m"
217 echo "Find more information about why this message exists here: https://github.com/mailcow/mailcow-dockerized/issues/1572"
218 read -r -p "Do you want to proceed anyway? [y/N] " response
219 if [[ "$response" =~ ^([yY][eE][sS]|[yY])+$ ]]; then
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +0200220 echo "OK. Proceeding."
Matthias Andreas Benkardd63fe562023-11-18 13:18:30 +0100221 else
222 echo "OK. Exiting."
223 exit 1
224 fi
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +0100225fi
226
Matthias Andreas Benkardd63fe562023-11-18 13:18:30 +0100227detect_bad_asn
228
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +0200229if [[ ("${SKIP_PING_CHECK}" == "y") ]]; then
Matthias Andreas Benkard3c18a082022-12-27 17:32:58 +0100230echo -e "\e[32mSkipping Ping Check...\e[0m"
231
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +0100232else
Matthias Andreas Benkard3c18a082022-12-27 17:32:58 +0100233 echo -en "Checking internet connection... "
234 if ! check_online_status; then
235 echo -e "\e[31mfailed\e[0m"
236 exit 1
237 else
238 echo -e "\e[32mOK\e[0m"
239 fi
240fi
241
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +0200242if ! [ "$NEW_BRANCH" ]; then
Matthias Andreas Benkard3c18a082022-12-27 17:32:58 +0100243 echo -e "\e[33mDetecting which build your mailcow runs on...\e[0m"
244 sleep 1
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +0200245 if [ "${BRANCH}" == "master" ]; then
Matthias Andreas Benkard3c18a082022-12-27 17:32:58 +0100246 echo -e "\e[32mYou are receiving stable updates (master).\e[0m"
247 echo -e "\e[33mTo change that run the update.sh Script one time with the --nightly parameter to switch to nightly builds.\e[0m"
248
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +0200249 elif [ "${BRANCH}" == "nightly" ]; then
Matthias Andreas Benkard3c18a082022-12-27 17:32:58 +0100250 echo -e "\e[31mYou are receiving unstable updates (nightly). These are for testing purposes only!!!\e[0m"
251 sleep 1
252 echo -e "\e[33mTo change that run the update.sh Script one time with the --stable parameter to switch to stable builds.\e[0m"
253
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +0200254 elif [ "${BRANCH}" == "legacy" ]; then
255 echo -e "\e[31mYou are receiving legacy updates. The legacy branch will only receive security updates until February 2026.\e[0m"
256 sleep 1
257 echo -e "\e[33mTo change that run the update.sh Script one time with the --stable parameter to switch to stable builds.\e[0m"
258
Matthias Andreas Benkard3c18a082022-12-27 17:32:58 +0100259 else
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +0200260 echo -e "\e[33mYou are receiving updates from an unsupported branch.\e[0m"
Matthias Andreas Benkard3c18a082022-12-27 17:32:58 +0100261 sleep 1
262 echo -e "\e[33mThe mailcow stack might still work but it is recommended to switch to the master branch (stable builds).\e[0m"
263 echo -e "\e[33mTo change that run the update.sh Script one time with the --stable parameter to switch to stable builds.\e[0m"
264 fi
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +0200265elif [ "$FORCE" ]; then
Matthias Andreas Benkard3c18a082022-12-27 17:32:58 +0100266 echo -e "\e[31mYou are running in forced mode!\e[0m"
267 echo -e "\e[31mA Branch Switch can only be performed manually (monitored).\e[0m"
268 echo -e "\e[31mPlease rerun the update.sh Script without the --force/-f parameter.\e[0m"
269 sleep 1
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +0200270elif [ "$NEW_BRANCH" == "master" ] && [ "$CURRENT_BRANCH" != "master" ]; then
271 echo -e "\e[33mYou are about to switch your mailcow updates to the stable (master) branch.\e[0m"
Matthias Andreas Benkard3c18a082022-12-27 17:32:58 +0100272 sleep 1
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +0200273 echo -e "\e[33mBefore you do: Please take a backup of all components to ensure that no data is lost...\e[0m"
Matthias Andreas Benkard3c18a082022-12-27 17:32:58 +0100274 sleep 1
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +0200275 echo -e "\e[31mWARNING: Please see on GitHub or ask in the community if a switch to master is stable or not.
276 In some rear cases an update back to master can destroy your mailcow configuration such as database upgrade, etc.
277 Normally an upgrade back to master should be safe during each full release.
278 Check GitHub for Database changes and update only if there similar to the full release!\e[0m"
Matthias Andreas Benkard3c18a082022-12-27 17:32:58 +0100279 read -r -p "Are you sure you that want to continue upgrading to the stable (master) branch? [y/N] " response
280 if [[ ! "${response}" =~ ^([yY][eE][sS]|[yY])+$ ]]; then
281 echo "OK. If you prepared yourself for that please run the update.sh Script with the --stable parameter again to trigger this process here."
282 exit 0
283 fi
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +0200284 BRANCH="$NEW_BRANCH"
Matthias Andreas Benkard3c18a082022-12-27 17:32:58 +0100285 DIFF_DIRECTORY=update_diffs
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +0200286 DIFF_FILE="${DIFF_DIRECTORY}/diff_before_upgrade_to_master_$(date +"%Y-%m-%d-%H-%M-%S")"
287 mv diff_before_upgrade* "${DIFF_DIRECTORY}/" 2> /dev/null
Matthias Andreas Benkard3c18a082022-12-27 17:32:58 +0100288 if ! git diff-index --quiet HEAD; then
289 echo -e "\e[32mSaving diff to ${DIFF_FILE}...\e[0m"
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +0200290 mkdir -p "${DIFF_DIRECTORY}"
291 git diff "${BRANCH}" --stat > "${DIFF_FILE}"
292 git diff "${BRANCH}" >> "${DIFF_FILE}"
Matthias Andreas Benkard3c18a082022-12-27 17:32:58 +0100293 fi
294 echo -e "\e[32mSwitching Branch to ${BRANCH}...\e[0m"
295 git fetch origin
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +0200296 git checkout -f "${BRANCH}"
Matthias Andreas Benkard3c18a082022-12-27 17:32:58 +0100297
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +0200298elif [ "$NEW_BRANCH" == "nightly" ] && [ "$CURRENT_BRANCH" != "nightly" ]; then
Matthias Andreas Benkard3c18a082022-12-27 17:32:58 +0100299 echo -e "\e[33mYou are about to switch your mailcow Updates to the unstable (nightly) branch.\e[0m"
300 sleep 1
301 echo -e "\e[33mBefore you do: Please take a backup of all components to ensure that no Data is lost...\e[0m"
302 sleep 1
303 echo -e "\e[31mWARNING: A switch to nightly is possible any time. But a switch back (to master) isn't.\e[0m"
304 read -r -p "Are you sure you that want to continue upgrading to the unstable (nightly) branch? [y/N] " response
305 if [[ ! "${response}" =~ ^([yY][eE][sS]|[yY])+$ ]]; then
306 echo "OK. If you prepared yourself for that please run the update.sh Script with the --nightly parameter again to trigger this process here."
307 exit 0
308 fi
309 BRANCH=$NEW_BRANCH
310 DIFF_DIRECTORY=update_diffs
311 DIFF_FILE=${DIFF_DIRECTORY}/diff_before_upgrade_to_nightly_$(date +"%Y-%m-%d-%H-%M-%S")
312 mv diff_before_upgrade* ${DIFF_DIRECTORY}/ 2> /dev/null
313 if ! git diff-index --quiet HEAD; then
314 echo -e "\e[32mSaving diff to ${DIFF_FILE}...\e[0m"
315 mkdir -p ${DIFF_DIRECTORY}
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +0200316 git diff "${BRANCH}" --stat > "${DIFF_FILE}"
317 git diff "${BRANCH}" >> "${DIFF_FILE}"
Matthias Andreas Benkard3c18a082022-12-27 17:32:58 +0100318 fi
319 git fetch origin
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +0200320 git checkout -f "${BRANCH}"
321elif [ "$NEW_BRANCH" == "legacy" ] && [ "$CURRENT_BRANCH" != "legacy" ]; then
322 echo -e "\e[33mYou are about to switch your mailcow Updates to the legacy branch.\e[0m"
323 sleep 1
324 echo -e "\e[33mBefore you do: Please take a backup of all components to ensure that no Data is lost...\e[0m"
325 sleep 1
326 echo -e "\e[31mWARNING: A switch to stable or nightly is possible any time.\e[0m"
327 read -r -p "Are you sure you want to continue upgrading to the legacy branch? [y/N] " response
328 if [[ ! "${response}" =~ ^([yY][eE][sS]|[yY])+$ ]]; then
329 echo "OK. If you prepared yourself for that please run the update.sh Script with the --legacy parameter again to trigger this process here."
330 exit 0
331 fi
332 BRANCH=$NEW_BRANCH
333 DIFF_DIRECTORY=update_diffs
334 DIFF_FILE=${DIFF_DIRECTORY}/diff_before_upgrade_to_legacy_$(date +"%Y-%m-%d-%H-%M-%S")
335 mv diff_before_upgrade* ${DIFF_DIRECTORY}/ 2> /dev/null
336 if ! git diff-index --quiet HEAD; then
337 echo -e "\e[32mSaving diff to ${DIFF_FILE}...\e[0m"
338 mkdir -p ${DIFF_DIRECTORY}
339 git diff "${BRANCH}" --stat > "${DIFF_FILE}"
340 git diff "${BRANCH}" >> "${DIFF_FILE}"
341 fi
342 git fetch origin
343 git checkout -f "${BRANCH}"
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +0100344fi
345
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +0200346if [ ! "$DEV" ]; then
347 EXIT_COUNT=0
Matthias Andreas Benkardd63fe562023-11-18 13:18:30 +0100348 echo -e "\e[32mChecking for newer update script...\e[0m"
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +0200349 SHA1_1="$(sha1sum update.sh)"
350 git fetch origin
351 git checkout "origin/${BRANCH}" -- update.sh
352 SHA1_2="$(sha1sum update.sh)"
353 if [[ "${SHA1_1}" != "${SHA1_2}" ]]; then
Matthias Andreas Benkardd63fe562023-11-18 13:18:30 +0100354 chmod +x update.sh
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +0200355 EXIT_COUNT+=1
Matthias Andreas Benkardd63fe562023-11-18 13:18:30 +0100356 fi
Matthias Andreas Benkardb7a13512023-11-18 16:44:25 +0100357fi
358
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +0200359if [ ! "$FORCE" ]; then
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +0100360 read -r -p "Are you sure you want to update mailcow: dockerized? All containers will be stopped. [y/N] " response
Matthias Andreas Benkard8f915942021-08-16 10:57:25 +0200361 if [[ ! "${response}" =~ ^([yY][eE][sS]|[yY])+$ ]]; then
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +0100362 echo "OK, exiting."
363 exit 0
364 fi
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +0200365 detect_major_update
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +0100366fi
367
368echo -e "\e[32mValidating docker-compose stack configuration...\e[0m"
Matthias Andreas Benkard3c18a082022-12-27 17:32:58 +0100369sed -i 's/HTTPS_BIND:-:/HTTPS_BIND:-/g' docker-compose.yml
370sed -i 's/HTTP_BIND:-:/HTTP_BIND:-/g' docker-compose.yml
371if ! $COMPOSE_COMMAND config -q; then
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +0100372 echo -e "\e[31m\nOh no, something went wrong. Please check the error message above.\e[0m"
373 exit 1
374fi
375
376echo -e "\e[32mChecking for conflicting bridges...\e[0m"
Matthias Andreas Benkard3c18a082022-12-27 17:32:58 +0100377MAILCOW_BRIDGE=$($COMPOSE_COMMAND config | grep -i com.docker.network.bridge.name | cut -d':' -f2)
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +0100378while read NAT_ID; do
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +0200379 iptables -t nat -D POSTROUTING "$NAT_ID"
380done < <(iptables -L -vn -t nat --line-numbers | grep "$IPV4_NETWORK" | grep -E 'MASQUERADE.*all' | grep -v "${MAILCOW_BRIDGE}" | cut -d' ' -f1)
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +0100381
382DIFF_DIRECTORY=update_diffs
383DIFF_FILE=${DIFF_DIRECTORY}/diff_before_update_$(date +"%Y-%m-%d-%H-%M-%S")
384mv diff_before_update* ${DIFF_DIRECTORY}/ 2> /dev/null
385if ! git diff-index --quiet HEAD; then
386 echo -e "\e[32mSaving diff to ${DIFF_FILE}...\e[0m"
387 mkdir -p ${DIFF_DIRECTORY}
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +0200388 git diff --stat > "${DIFF_FILE}"
389 git diff >> "${DIFF_FILE}"
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +0100390fi
391
392echo -e "\e[32mPrefetching images...\e[0m"
393prefetch_images
394
395echo -e "\e[32mStopping mailcow...\e[0m"
396sleep 2
Matthias Andreas Benkard3c18a082022-12-27 17:32:58 +0100397MAILCOW_CONTAINERS=($($COMPOSE_COMMAND ps -q))
398$COMPOSE_COMMAND down
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +0100399echo -e "\e[32mChecking for remaining containers...\e[0m"
400sleep 2
401for container in "${MAILCOW_CONTAINERS[@]}"; do
402 docker rm -f "$container" 2> /dev/null
403done
404
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +0200405configure_ipv6
406
Matthias Andreas Benkard8f915942021-08-16 10:57:25 +0200407[[ -f data/conf/nginx/ZZZ-ejabberd.conf ]] && rm data/conf/nginx/ZZZ-ejabberd.conf
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +0200408migrate_config_options
409adapt_new_options
Matthias Andreas Benkard8f915942021-08-16 10:57:25 +0200410
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +0200411if [ ! "$DEV" ]; then
412 DEFAULT_REPO="https://github.com/mailcow/mailcow-dockerized"
413 CURRENT_REPO=$(git config --get remote.origin.url)
414 if [ "$CURRENT_REPO" != "$DEFAULT_REPO" ]; then
415 echo "The Repository currently used is not the default mailcow Repository."
416 echo "Currently Repository: $CURRENT_REPO"
417 echo "Default Repository: $DEFAULT_REPO"
418 if [ ! "$FORCE" ]; then
419 read -r -p "Should it be changed back to default? [y/N] " repo_response
420 if [[ "$repo_response" =~ ^([yY][eE][sS]|[yY])+$ ]]; then
421 git remote set-url origin $DEFAULT_REPO
422 fi
423 else
424 echo "Running in forced mode... setting Repo to default!"
425 git remote set-url origin $DEFAULT_REPO
Matthias Andreas Benkard9dabdab2024-03-06 13:12:09 +0100426 fi
Matthias Andreas Benkardd63fe562023-11-18 13:18:30 +0100427 fi
428fi
429
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +0200430if [ ! "$DEV" ]; then
Matthias Andreas Benkard9dabdab2024-03-06 13:12:09 +0100431 echo -e "\e[32mCommitting current status...\e[0m"
432 [[ -z "$(git config user.name)" ]] && git config user.name moo
433 [[ -z "$(git config user.email)" ]] && git config user.email moo@cow.moo
434 [[ ! -z $(git ls-files data/conf/rspamd/override.d/worker-controller-password.inc) ]] && git rm data/conf/rspamd/override.d/worker-controller-password.inc
435 git add -u
436 git commit -am "Before update on ${DATE}" > /dev/null
437 echo -e "\e[32mFetching updated code from remote...\e[0m"
438 git fetch origin #${BRANCH}
439 echo -e "\e[32mMerging local with remote code (recursive, strategy: \"${MERGE_STRATEGY:-theirs}\", options: \"patience\"...\e[0m"
440 git config merge.defaultToUpstream true
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +0200441 git merge -X"${MERGE_STRATEGY:-theirs}" -Xpatience -m "After update on ${DATE}"
Matthias Andreas Benkard9dabdab2024-03-06 13:12:09 +0100442 # Need to use a variable to not pass return codes of if checks
443 MERGE_RETURN=$?
444 if [[ ${MERGE_RETURN} == 128 ]]; then
445 echo -e "\e[31m\nOh no, what happened?\n=> You most likely added files to your local mailcow instance that were now added to the official mailcow repository. Please move them to another location before updating mailcow.\e[0m"
446 exit 1
447 elif [[ ${MERGE_RETURN} == 1 ]]; then
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +0200448 echo -e "\e[93mPotential conflict, trying to fix...\e[0m"
Matthias Andreas Benkard9dabdab2024-03-06 13:12:09 +0100449 git status --porcelain | grep -E "UD|DU" | awk '{print $2}' | xargs rm -v
450 git add -A
451 git commit -m "After update on ${DATE}" > /dev/null
452 git checkout .
453 echo -e "\e[32mRemoved and recreated files if necessary.\e[0m"
454 elif [[ ${MERGE_RETURN} != 0 ]]; then
455 echo -e "\e[31m\nOh no, something went wrong. Please check the error message above.\e[0m"
456 echo
457 echo "Run $COMPOSE_COMMAND up -d to restart your stack without updates or try again after fixing the mentioned errors."
458 exit 1
459 fi
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +0200460else
Matthias Andreas Benkard9dabdab2024-03-06 13:12:09 +0100461 echo -e "\e[33mDEVELOPER MODE: Not creating a git diff and commiting it to prevent development stuff within a backup diff...\e[0m"
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +0100462fi
463
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +0100464echo -e "\e[32mFetching new images, if any...\e[0m"
465sleep 2
Matthias Andreas Benkard3c18a082022-12-27 17:32:58 +0100466$COMPOSE_COMMAND pull
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +0100467
468# Fix missing SSL, does not overwrite existing files
469[[ ! -d data/assets/ssl ]] && mkdir -p data/assets/ssl
470cp -n -d data/assets/ssl-example/*.pem data/assets/ssl/
471
472echo -e "Checking IPv6 settings... "
473if grep -q 'SYSCTL_IPV6_DISABLED=1' mailcow.conf; then
474 echo
475 echo '!! IMPORTANT !!'
476 echo
477 echo 'SYSCTL_IPV6_DISABLED was removed due to complications. IPv6 can be disabled by editing "docker-compose.yml" and setting "enable_ipv6: true" to "enable_ipv6: false".'
Matthias Andreas Benkardd63fe562023-11-18 13:18:30 +0100478 echo "This setting will only be active after a complete shutdown of mailcow by running $COMPOSE_COMMAND down followed by $COMPOSE_COMMAND up -d."
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +0100479 echo
480 echo '!! IMPORTANT !!'
481 echo
482 read -p "Press any key to continue..." < /dev/tty
483fi
484
485# Checking for old project name bug
486sed -i --follow-symlinks 's#COMPOSEPROJECT_NAME#COMPOSE_PROJECT_NAME#g' mailcow.conf
487
488# Fix Rspamd maps
489if [ -f data/conf/rspamd/custom/global_from_blacklist.map ]; then
490 mv data/conf/rspamd/custom/global_from_blacklist.map data/conf/rspamd/custom/global_smtp_from_blacklist.map
491fi
492if [ -f data/conf/rspamd/custom/global_from_whitelist.map ]; then
493 mv data/conf/rspamd/custom/global_from_whitelist.map data/conf/rspamd/custom/global_smtp_from_whitelist.map
494fi
495
496# Fix deprecated metrics.conf
497if [ -f "data/conf/rspamd/local.d/metrics.conf" ]; then
498 if [ ! -z "$(git diff --name-only origin/master data/conf/rspamd/local.d/metrics.conf)" ]; then
499 echo -e "\e[33mWARNING\e[0m - Please migrate your customizations of data/conf/rspamd/local.d/metrics.conf to actions.conf and groups.conf after this update."
500 echo "The deprecated configuration file metrics.conf will be moved to metrics.conf_deprecated after updating mailcow."
501 fi
502 mv data/conf/rspamd/local.d/metrics.conf data/conf/rspamd/local.d/metrics.conf_deprecated
503fi
504
Matthias Andreas Benkard3c18a082022-12-27 17:32:58 +0100505# Set app_info.inc.php
506if [ ${BRANCH} == "master" ]; then
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +0200507 mailcow_git_version=$(git describe --tags $(git rev-list --tags --max-count=1))
Matthias Andreas Benkard3c18a082022-12-27 17:32:58 +0100508elif [ ${BRANCH} == "nightly" ]; then
509 mailcow_git_version=$(git rev-parse --short $(git rev-parse @{upstream}))
510 mailcow_last_git_version=""
511else
512 mailcow_git_version=$(git rev-parse --short HEAD)
513 mailcow_last_git_version=""
514fi
515
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +0200516mailcow_git_commit=$(git rev-parse "origin/${BRANCH}")
Matthias Andreas Benkard3c18a082022-12-27 17:32:58 +0100517mailcow_git_commit_date=$(git log -1 --format=%ci @{upstream} )
518
519if [ $? -eq 0 ]; then
520 echo '<?php' > data/web/inc/app_info.inc.php
521 echo ' $MAILCOW_GIT_VERSION="'$mailcow_git_version'";' >> data/web/inc/app_info.inc.php
522 echo ' $MAILCOW_LAST_GIT_VERSION="";' >> data/web/inc/app_info.inc.php
523 echo ' $MAILCOW_GIT_OWNER="mailcow";' >> data/web/inc/app_info.inc.php
524 echo ' $MAILCOW_GIT_REPO="mailcow-dockerized";' >> data/web/inc/app_info.inc.php
525 echo ' $MAILCOW_GIT_URL="https://github.com/mailcow/mailcow-dockerized";' >> data/web/inc/app_info.inc.php
526 echo ' $MAILCOW_GIT_COMMIT="'$mailcow_git_commit'";' >> data/web/inc/app_info.inc.php
527 echo ' $MAILCOW_GIT_COMMIT_DATE="'$mailcow_git_commit_date'";' >> data/web/inc/app_info.inc.php
528 echo ' $MAILCOW_BRANCH="'$BRANCH'";' >> data/web/inc/app_info.inc.php
529 echo ' $MAILCOW_UPDATEDAT='$(date +%s)';' >> data/web/inc/app_info.inc.php
530 echo '?>' >> data/web/inc/app_info.inc.php
531else
532 echo '<?php' > data/web/inc/app_info.inc.php
533 echo ' $MAILCOW_GIT_VERSION="'$mailcow_git_version'";' >> data/web/inc/app_info.inc.php
534 echo ' $MAILCOW_LAST_GIT_VERSION="";' >> data/web/inc/app_info.inc.php
535 echo ' $MAILCOW_GIT_OWNER="mailcow";' >> data/web/inc/app_info.inc.php
536 echo ' $MAILCOW_GIT_REPO="mailcow-dockerized";' >> data/web/inc/app_info.inc.php
537 echo ' $MAILCOW_GIT_URL="https://github.com/mailcow/mailcow-dockerized";' >> data/web/inc/app_info.inc.php
538 echo ' $MAILCOW_GIT_COMMIT="";' >> data/web/inc/app_info.inc.php
539 echo ' $MAILCOW_GIT_COMMIT_DATE="";' >> data/web/inc/app_info.inc.php
540 echo ' $MAILCOW_BRANCH="'$BRANCH'";' >> data/web/inc/app_info.inc.php
541 echo ' $MAILCOW_UPDATEDAT='$(date +%s)';' >> data/web/inc/app_info.inc.php
542 echo '?>' >> data/web/inc/app_info.inc.php
543 echo -e "\e[33mCannot determine current git repository version...\e[0m"
544fi
545
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +0100546if [[ ${SKIP_START} == "y" ]]; then
Matthias Andreas Benkard3c18a082022-12-27 17:32:58 +0100547 echo -e "\e[33mNot starting mailcow, please run \"$COMPOSE_COMMAND up -d --remove-orphans\" to start mailcow.\e[0m"
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +0100548else
549 echo -e "\e[32mStarting mailcow...\e[0m"
550 sleep 2
Matthias Andreas Benkard3c18a082022-12-27 17:32:58 +0100551 $COMPOSE_COMMAND up -d --remove-orphans
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +0100552fi
553
554echo -e "\e[32mCollecting garbage...\e[0m"
555docker_garbage
556
Matthias Andreas Benkard8f915942021-08-16 10:57:25 +0200557# Run post-update-hook
558if [ -f "${SCRIPT_DIR}/post_update_hook.sh" ]; then
559 bash "${SCRIPT_DIR}/post_update_hook.sh"
560fi
561
Matthias Andreas Benkard3c18a082022-12-27 17:32:58 +0100562# echo "In case you encounter any problem, hard-reset to a state before updating mailcow:"
563# echo
564# git reflog --color=always | grep "Before update on "
565# echo
Matthias Andreas Benkardb7a13512023-11-18 16:44:25 +0100566# echo "Use \"git reset --hard hash-on-the-left\" and run $COMPOSE_COMMAND up -d afterwards."