update.sh 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566
  1. #!/usr/bin/env bash
  2. ############## Begin Function Section ##############
  3. SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
  4. MAILCOW_CONF="${SCRIPT_DIR}/mailcow.conf"
  5. # Ensure the script is run from the directory that contains mailcow.conf
  6. if [ ! -f "${PWD}/mailcow.conf" ]; then
  7. if [ -f "${SCRIPT_DIR}/mailcow.conf" ]; then
  8. echo -e "\e[33mPlease run this script directly from the mailcow installation directory:\e[0m"
  9. echo -e " \e[36mcd ${SCRIPT_DIR} && ./update.sh\e[0m"
  10. exit 1
  11. else
  12. echo -e "\e[31mmailcow.conf not found in current directory or script directory (\e[36m${SCRIPT_DIR}\e[31m).\e[0m"
  13. echo -e "\e[33mRun this script directly from your mailcow installation directory.\e[0m"
  14. exit 1
  15. fi
  16. fi
  17. BRANCH="$(cd "${SCRIPT_DIR}" && git rev-parse --abbrev-ref HEAD)"
  18. # Check for --dev flag early to skip _modules update
  19. for arg in "$@"; do
  20. if [[ "$arg" == "--dev" || "$arg" == "-d" ]]; then
  21. echo -e "\e[32mRunning in Developer mode...\e[0m"
  22. DEV=y
  23. break
  24. fi
  25. done
  26. MODULE_DIR="${SCRIPT_DIR}/_modules"
  27. if [ ! "$DEV" ]; then
  28. # Calculate hash before fetch
  29. if [[ -d "${MODULE_DIR}" && -n "$(ls -A "${MODULE_DIR}" 2>/dev/null)" ]]; then
  30. MODULES_HASH_BEFORE=$(find "${MODULE_DIR}" -type f -exec sha256sum {} \; 2>/dev/null | sort | sha256sum | awk '{print $1}')
  31. else
  32. MODULES_HASH_BEFORE="EMPTY"
  33. fi
  34. echo -e "\e[33mFetching latest _modules from origin/${BRANCH}…\e[0m"
  35. git fetch origin "${BRANCH}"
  36. git checkout "origin/${BRANCH}" -- _modules
  37. if [[ ! -d "${MODULE_DIR}" || -z "$(ls -A "${MODULE_DIR}")" ]]; then
  38. echo -e "\e[31mError: _modules is still missing or empty after fetch!\e[0m"
  39. exit 2
  40. fi
  41. # Calculate hash after fetch
  42. MODULES_HASH_AFTER=$(find "${MODULE_DIR}" -type f -exec sha256sum {} \; 2>/dev/null | sort | sha256sum | awk '{print $1}')
  43. # Check if modules changed
  44. if [[ "${MODULES_HASH_BEFORE}" != "${MODULES_HASH_AFTER}" ]]; then
  45. echo -e "\e[33m_modules have been updated. Please restart the update script.\e[0m"
  46. exit 2
  47. fi
  48. else
  49. echo -e "\e[33mDeveloper mode: Skipping _modules update from git\e[0m"
  50. if [[ ! -d "${MODULE_DIR}" || -z "$(ls -A "${MODULE_DIR}")" ]]; then
  51. echo -e "\e[31mError: _modules directory is missing or empty!\e[0m"
  52. exit 2
  53. fi
  54. fi
  55. source _modules/scripts/core.sh
  56. source _modules/scripts/ipv6_controller.sh
  57. source _modules/scripts/new_options.sh
  58. source _modules/scripts/migrate_options.sh
  59. ############## End Function Section ##############
  60. # Check permissions
  61. if [ "$(id -u)" -ne "0" ]; then
  62. echo "You need to be root"
  63. exit 1
  64. fi
  65. # Run pre-update-hook
  66. if [ -f "${SCRIPT_DIR}/pre_update_hook.sh" ]; then
  67. bash "${SCRIPT_DIR}/pre_update_hook.sh"
  68. fi
  69. # Exit on error and pipefail
  70. set -o pipefail
  71. # Setting high dc timeout
  72. export COMPOSE_HTTP_TIMEOUT=600
  73. # Add /opt/bin to PATH
  74. PATH=$PATH:/opt/bin
  75. umask 0022
  76. # Unset COMPOSE_COMMAND and DOCKER_COMPOSE_VERSION Variable to be on the newest state.
  77. unset COMPOSE_COMMAND
  78. unset DOCKER_COMPOSE_VERSION
  79. get_installed_tools
  80. get_docker_version
  81. export LC_ALL=C
  82. DATE=$(date +%Y-%m-%d_%H_%M_%S)
  83. BRANCH="$(cd "${SCRIPT_DIR}"; git rev-parse --abbrev-ref HEAD)"
  84. while (($#)); do
  85. case "${1}" in
  86. --check|-c)
  87. echo "Checking remote code for updates..."
  88. LATEST_REV=$(git ls-remote --exit-code --refs --quiet https://github.com/mailcow/mailcow-dockerized "${BRANCH}" | cut -f1)
  89. if [ "$?" -ne 0 ]; then
  90. echo "A problem occurred while trying to fetch the latest revision from github."
  91. exit 99
  92. fi
  93. if [[ -z $(git log HEAD --pretty=format:"%H" | grep "${LATEST_REV}") ]]; then
  94. echo -e "Updated code is available.\nThe changes can be found here: https://github.com/mailcow/mailcow-dockerized/commits/master"
  95. git log --date=short --pretty=format:"%ad - %s" "$(git rev-parse --short HEAD)"..origin/master
  96. exit 0
  97. else
  98. echo "No updates available."
  99. exit 3
  100. fi
  101. ;;
  102. --check-tags)
  103. echo "Checking remote tags for updates..."
  104. LATEST_TAG_REV=$(git ls-remote --exit-code --quiet --tags origin | tail -1 | cut -f1)
  105. if [ "$?" -ne 0 ]; then
  106. echo "A problem occurred while trying to fetch the latest tag from github."
  107. exit 99
  108. fi
  109. if [[ -z $(git log HEAD --pretty=format:"%H" | grep "${LATEST_TAG_REV}") ]]; then
  110. echo -e "New tag is available.\nThe changes can be found here: https://github.com/mailcow/mailcow-dockerized/releases/latest"
  111. exit 0
  112. else
  113. echo "No updates available."
  114. exit 3
  115. fi
  116. ;;
  117. --ours)
  118. MERGE_STRATEGY=ours
  119. ;;
  120. --skip-start)
  121. SKIP_START=y
  122. ;;
  123. --skip-ping-check)
  124. SKIP_PING_CHECK=y
  125. ;;
  126. --stable)
  127. CURRENT_BRANCH="$(cd "${SCRIPT_DIR}"; git rev-parse --abbrev-ref HEAD)"
  128. NEW_BRANCH="master"
  129. ;;
  130. --gc)
  131. echo -e "\e[32mCollecting garbage...\e[0m"
  132. docker_garbage
  133. exit 0
  134. ;;
  135. --nightly)
  136. CURRENT_BRANCH="$(cd "${SCRIPT_DIR}"; git rev-parse --abbrev-ref HEAD)"
  137. NEW_BRANCH="nightly"
  138. ;;
  139. --prefetch)
  140. echo -e "\e[32mPrefetching images...\e[0m"
  141. prefetch_images
  142. exit 0
  143. ;;
  144. -f|--force)
  145. echo -e "\e[32mRunning in forced mode...\e[0m"
  146. FORCE=y
  147. ;;
  148. -d|--dev)
  149. # Already handled at the top of the script before _modules update
  150. ;;
  151. --legacy)
  152. CURRENT_BRANCH="$(cd "${SCRIPT_DIR}"; git rev-parse --abbrev-ref HEAD)"
  153. NEW_BRANCH="legacy"
  154. ;;
  155. --help|-h)
  156. echo './update.sh [-c|--check, --check-tags, --ours, --gc, --nightly, --prefetch, --skip-start, --skip-ping-check, --stable, --legacy, -f|--force, -d|--dev, -h|--help]
  157. -c|--check - Check for updates and exit (exit codes => 0: update available, 3: no updates)
  158. --check-tags - Check for newer tags and exit (exit codes => 0: newer tag available, 3: no newer tag)
  159. --ours - Use merge strategy option "ours" to solve conflicts in favor of non-mailcow code (local changes over remote changes), not recommended!
  160. --gc - Run garbage collector to delete old image tags
  161. --nightly - Switch your mailcow updates to the unstable (nightly) branch. FOR TESTING PURPOSES ONLY!!!!
  162. --prefetch - Only prefetch new images and exit (useful to prepare updates)
  163. --skip-start - Do not start mailcow after update
  164. --skip-ping-check - Skip ICMP Check to public DNS resolvers (Use it only if you'\''ve blocked any ICMP Connections to your mailcow machine)
  165. --stable - Switch your mailcow updates to the stable (master) branch. Default unless you changed it with --nightly or --legacy.
  166. --legacy - Switch your mailcow updates to the legacy branch. The legacy branch will only receive security updates until February 2026.
  167. -f|--force - Force update, do not ask questions
  168. -d|--dev - Enables Developer Mode (No Checkout of update.sh for tests)
  169. '
  170. exit 0
  171. esac
  172. shift
  173. done
  174. [[ ! -f mailcow.conf ]] && { echo -e "\e[31mmailcow.conf is missing! Is mailcow installed?\e[0m"; exit 1;}
  175. chmod 600 mailcow.conf
  176. source mailcow.conf
  177. get_compose_type
  178. DOTS=${MAILCOW_HOSTNAME//[^.]};
  179. if [ ${#DOTS} -lt 1 ]; then
  180. echo -e "\e[31mMAILCOW_HOSTNAME (${MAILCOW_HOSTNAME}) is not a FQDN!\e[0m"
  181. sleep 1
  182. echo "Please change it to a FQDN and redeploy the stack with $COMPOSE_COMMAND up -d"
  183. exit 1
  184. elif [[ "${MAILCOW_HOSTNAME: -1}" == "." ]]; then
  185. echo "MAILCOW_HOSTNAME (${MAILCOW_HOSTNAME}) is ending with a dot. This is not a valid FQDN!"
  186. exit 1
  187. elif [ ${#DOTS} -eq 1 ]; then
  188. echo -e "\e[33mMAILCOW_HOSTNAME (${MAILCOW_HOSTNAME}) does not contain a Subdomain. This is not fully tested and may cause issues.\e[0m"
  189. echo "Find more information about why this message exists here: https://github.com/mailcow/mailcow-dockerized/issues/1572"
  190. read -r -p "Do you want to proceed anyway? [y/N] " response
  191. if [[ "$response" =~ ^([yY][eE][sS]|[yY])+$ ]]; then
  192. echo "OK. Proceeding."
  193. else
  194. echo "OK. Exiting."
  195. exit 1
  196. fi
  197. fi
  198. detect_bad_asn
  199. if [[ ("${SKIP_PING_CHECK}" == "y") ]]; then
  200. echo -e "\e[32mSkipping Ping Check...\e[0m"
  201. else
  202. echo -en "Checking internet connection... "
  203. if ! check_online_status; then
  204. echo -e "\e[31mfailed\e[0m"
  205. exit 1
  206. else
  207. echo -e "\e[32mOK\e[0m"
  208. fi
  209. fi
  210. if ! [ "$NEW_BRANCH" ]; then
  211. echo -e "\e[33mDetecting which build your mailcow runs on...\e[0m"
  212. sleep 1
  213. if [ "${BRANCH}" == "master" ]; then
  214. echo -e "\e[32mYou are receiving stable updates (master).\e[0m"
  215. echo -e "\e[33mTo change that run the update.sh Script one time with the --nightly parameter to switch to nightly builds.\e[0m"
  216. elif [ "${BRANCH}" == "nightly" ]; then
  217. echo -e "\e[31mYou are receiving unstable updates (nightly). These are for testing purposes only!!!\e[0m"
  218. sleep 1
  219. echo -e "\e[33mTo change that run the update.sh Script one time with the --stable parameter to switch to stable builds.\e[0m"
  220. elif [ "${BRANCH}" == "legacy" ]; then
  221. echo -e "\e[31mYou are receiving legacy updates. The legacy branch will only receive security updates until February 2026.\e[0m"
  222. sleep 1
  223. echo -e "\e[33mTo change that run the update.sh Script one time with the --stable parameter to switch to stable builds.\e[0m"
  224. else
  225. echo -e "\e[33mYou are receiving updates from an unsupported branch.\e[0m"
  226. sleep 1
  227. echo -e "\e[33mThe mailcow stack might still work but it is recommended to switch to the master branch (stable builds).\e[0m"
  228. echo -e "\e[33mTo change that run the update.sh Script one time with the --stable parameter to switch to stable builds.\e[0m"
  229. fi
  230. elif [ "$FORCE" ]; then
  231. echo -e "\e[31mYou are running in forced mode!\e[0m"
  232. echo -e "\e[31mA Branch Switch can only be performed manually (monitored).\e[0m"
  233. echo -e "\e[31mPlease rerun the update.sh Script without the --force/-f parameter.\e[0m"
  234. sleep 1
  235. elif [ "$NEW_BRANCH" == "master" ] && [ "$CURRENT_BRANCH" != "master" ]; then
  236. echo -e "\e[33mYou are about to switch your mailcow updates to the stable (master) branch.\e[0m"
  237. sleep 1
  238. echo -e "\e[33mBefore you do: Please take a backup of all components to ensure that no data is lost...\e[0m"
  239. sleep 1
  240. echo -e "\e[31mWARNING: Please see on GitHub or ask in the community if a switch to master is stable or not.
  241. In some rear cases an update back to master can destroy your mailcow configuration such as database upgrade, etc.
  242. Normally an upgrade back to master should be safe during each full release.
  243. Check GitHub for Database changes and update only if there similar to the full release!\e[0m"
  244. read -r -p "Are you sure you that want to continue upgrading to the stable (master) branch? [y/N] " response
  245. if [[ ! "${response}" =~ ^([yY][eE][sS]|[yY])+$ ]]; then
  246. echo "OK. If you prepared yourself for that please run the update.sh Script with the --stable parameter again to trigger this process here."
  247. exit 0
  248. fi
  249. BRANCH="$NEW_BRANCH"
  250. DIFF_DIRECTORY=update_diffs
  251. DIFF_FILE="${DIFF_DIRECTORY}/diff_before_upgrade_to_master_$(date +"%Y-%m-%d-%H-%M-%S")"
  252. mv diff_before_upgrade* "${DIFF_DIRECTORY}/" 2> /dev/null
  253. if ! git diff-index --quiet HEAD; then
  254. echo -e "\e[32mSaving diff to ${DIFF_FILE}...\e[0m"
  255. mkdir -p "${DIFF_DIRECTORY}"
  256. git diff "${BRANCH}" --stat > "${DIFF_FILE}"
  257. git diff "${BRANCH}" >> "${DIFF_FILE}"
  258. fi
  259. echo -e "\e[32mSwitching Branch to ${BRANCH}...\e[0m"
  260. git fetch origin
  261. git checkout -f "${BRANCH}"
  262. elif [ "$NEW_BRANCH" == "nightly" ] && [ "$CURRENT_BRANCH" != "nightly" ]; then
  263. echo -e "\e[33mYou are about to switch your mailcow Updates to the unstable (nightly) branch.\e[0m"
  264. sleep 1
  265. echo -e "\e[33mBefore you do: Please take a backup of all components to ensure that no Data is lost...\e[0m"
  266. sleep 1
  267. echo -e "\e[31mWARNING: A switch to nightly is possible any time. But a switch back (to master) isn't.\e[0m"
  268. read -r -p "Are you sure you that want to continue upgrading to the unstable (nightly) branch? [y/N] " response
  269. if [[ ! "${response}" =~ ^([yY][eE][sS]|[yY])+$ ]]; then
  270. echo "OK. If you prepared yourself for that please run the update.sh Script with the --nightly parameter again to trigger this process here."
  271. exit 0
  272. fi
  273. BRANCH=$NEW_BRANCH
  274. DIFF_DIRECTORY=update_diffs
  275. DIFF_FILE=${DIFF_DIRECTORY}/diff_before_upgrade_to_nightly_$(date +"%Y-%m-%d-%H-%M-%S")
  276. mv diff_before_upgrade* ${DIFF_DIRECTORY}/ 2> /dev/null
  277. if ! git diff-index --quiet HEAD; then
  278. echo -e "\e[32mSaving diff to ${DIFF_FILE}...\e[0m"
  279. mkdir -p ${DIFF_DIRECTORY}
  280. git diff "${BRANCH}" --stat > "${DIFF_FILE}"
  281. git diff "${BRANCH}" >> "${DIFF_FILE}"
  282. fi
  283. git fetch origin
  284. git checkout -f "${BRANCH}"
  285. elif [ "$NEW_BRANCH" == "legacy" ] && [ "$CURRENT_BRANCH" != "legacy" ]; then
  286. echo -e "\e[33mYou are about to switch your mailcow Updates to the legacy branch.\e[0m"
  287. sleep 1
  288. echo -e "\e[33mBefore you do: Please take a backup of all components to ensure that no Data is lost...\e[0m"
  289. sleep 1
  290. echo -e "\e[31mWARNING: A switch to stable or nightly is possible any time.\e[0m"
  291. read -r -p "Are you sure you want to continue upgrading to the legacy branch? [y/N] " response
  292. if [[ ! "${response}" =~ ^([yY][eE][sS]|[yY])+$ ]]; then
  293. echo "OK. If you prepared yourself for that please run the update.sh Script with the --legacy parameter again to trigger this process here."
  294. exit 0
  295. fi
  296. BRANCH=$NEW_BRANCH
  297. DIFF_DIRECTORY=update_diffs
  298. DIFF_FILE=${DIFF_DIRECTORY}/diff_before_upgrade_to_legacy_$(date +"%Y-%m-%d-%H-%M-%S")
  299. mv diff_before_upgrade* ${DIFF_DIRECTORY}/ 2> /dev/null
  300. if ! git diff-index --quiet HEAD; then
  301. echo -e "\e[32mSaving diff to ${DIFF_FILE}...\e[0m"
  302. mkdir -p ${DIFF_DIRECTORY}
  303. git diff "${BRANCH}" --stat > "${DIFF_FILE}"
  304. git diff "${BRANCH}" >> "${DIFF_FILE}"
  305. fi
  306. git fetch origin
  307. git checkout -f "${BRANCH}"
  308. fi
  309. if [ ! "$DEV" ]; then
  310. EXIT_COUNT=0
  311. echo -e "\e[32mChecking for newer update script...\e[0m"
  312. SHA1_1="$(sha1sum update.sh)"
  313. git fetch origin
  314. git checkout "origin/${BRANCH}" -- update.sh
  315. SHA1_2="$(sha1sum update.sh)"
  316. if [[ "${SHA1_1}" != "${SHA1_2}" ]]; then
  317. chmod +x update.sh
  318. EXIT_COUNT+=1
  319. fi
  320. fi
  321. if [ ! "$FORCE" ]; then
  322. read -r -p "Are you sure you want to update mailcow: dockerized? All containers will be stopped. [y/N] " response
  323. if [[ ! "${response}" =~ ^([yY][eE][sS]|[yY])+$ ]]; then
  324. echo "OK, exiting."
  325. exit 0
  326. fi
  327. detect_major_update
  328. fi
  329. echo -e "\e[32mValidating docker-compose stack configuration...\e[0m"
  330. sed -i 's/HTTPS_BIND:-:/HTTPS_BIND:-/g' docker-compose.yml
  331. sed -i 's/HTTP_BIND:-:/HTTP_BIND:-/g' docker-compose.yml
  332. if ! $COMPOSE_COMMAND config -q; then
  333. echo -e "\e[31m\nOh no, something went wrong. Please check the error message above.\e[0m"
  334. exit 1
  335. fi
  336. echo -e "\e[32mChecking for conflicting bridges...\e[0m"
  337. MAILCOW_BRIDGE=$($COMPOSE_COMMAND config | grep -i com.docker.network.bridge.name | cut -d':' -f2)
  338. while read NAT_ID; do
  339. iptables -t nat -D POSTROUTING "$NAT_ID"
  340. done < <(iptables -L -vn -t nat --line-numbers | grep "$IPV4_NETWORK" | grep -E 'MASQUERADE.*all' | grep -v "${MAILCOW_BRIDGE}" | cut -d' ' -f1)
  341. DIFF_DIRECTORY=update_diffs
  342. DIFF_FILE=${DIFF_DIRECTORY}/diff_before_update_$(date +"%Y-%m-%d-%H-%M-%S")
  343. mv diff_before_update* ${DIFF_DIRECTORY}/ 2> /dev/null
  344. if ! git diff-index --quiet HEAD; then
  345. echo -e "\e[32mSaving diff to ${DIFF_FILE}...\e[0m"
  346. mkdir -p ${DIFF_DIRECTORY}
  347. git diff --stat > "${DIFF_FILE}"
  348. git diff >> "${DIFF_FILE}"
  349. fi
  350. echo -e "\e[32mPrefetching images...\e[0m"
  351. prefetch_images
  352. echo -e "\e[32mStopping mailcow...\e[0m"
  353. sleep 2
  354. MAILCOW_CONTAINERS=($($COMPOSE_COMMAND ps -q))
  355. $COMPOSE_COMMAND down
  356. echo -e "\e[32mChecking for remaining containers...\e[0m"
  357. sleep 2
  358. for container in "${MAILCOW_CONTAINERS[@]}"; do
  359. docker rm -f "$container" 2> /dev/null
  360. done
  361. configure_ipv6
  362. [[ -f data/conf/nginx/ZZZ-ejabberd.conf ]] && rm data/conf/nginx/ZZZ-ejabberd.conf
  363. migrate_config_options
  364. adapt_new_options
  365. if [ ! "$DEV" ]; then
  366. DEFAULT_REPO="https://github.com/mailcow/mailcow-dockerized"
  367. CURRENT_REPO=$(git config --get remote.origin.url)
  368. if [ "$CURRENT_REPO" != "$DEFAULT_REPO" ]; then
  369. echo "The Repository currently used is not the default mailcow Repository."
  370. echo "Currently Repository: $CURRENT_REPO"
  371. echo "Default Repository: $DEFAULT_REPO"
  372. if [ ! "$FORCE" ]; then
  373. read -r -p "Should it be changed back to default? [y/N] " repo_response
  374. if [[ "$repo_response" =~ ^([yY][eE][sS]|[yY])+$ ]]; then
  375. git remote set-url origin $DEFAULT_REPO
  376. fi
  377. else
  378. echo "Running in forced mode... setting Repo to default!"
  379. git remote set-url origin $DEFAULT_REPO
  380. fi
  381. fi
  382. fi
  383. if [ ! "$DEV" ]; then
  384. echo -e "\e[32mCommitting current status...\e[0m"
  385. [[ -z "$(git config user.name)" ]] && git config user.name moo
  386. [[ -z "$(git config user.email)" ]] && git config user.email [email protected]
  387. [[ ! -z $(git ls-files data/conf/rspamd/override.d/worker-controller-password.inc) ]] && git rm data/conf/rspamd/override.d/worker-controller-password.inc
  388. git add -u
  389. git commit -am "Before update on ${DATE}" > /dev/null
  390. echo -e "\e[32mFetching updated code from remote...\e[0m"
  391. git fetch origin #${BRANCH}
  392. echo -e "\e[32mMerging local with remote code (recursive, strategy: \"${MERGE_STRATEGY:-theirs}\", options: \"patience\"...\e[0m"
  393. git config merge.defaultToUpstream true
  394. git merge -X"${MERGE_STRATEGY:-theirs}" -Xpatience -m "After update on ${DATE}"
  395. # Need to use a variable to not pass return codes of if checks
  396. MERGE_RETURN=$?
  397. if [[ ${MERGE_RETURN} == 128 ]]; then
  398. 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"
  399. exit 1
  400. elif [[ ${MERGE_RETURN} == 1 ]]; then
  401. echo -e "\e[93mPotential conflict, trying to fix...\e[0m"
  402. git status --porcelain | grep -E "UD|DU" | awk '{print $2}' | xargs rm -v
  403. git add -A
  404. git commit -m "After update on ${DATE}" > /dev/null
  405. git checkout .
  406. echo -e "\e[32mRemoved and recreated files if necessary.\e[0m"
  407. elif [[ ${MERGE_RETURN} != 0 ]]; then
  408. echo -e "\e[31m\nOh no, something went wrong. Please check the error message above.\e[0m"
  409. echo
  410. echo "Run $COMPOSE_COMMAND up -d to restart your stack without updates or try again after fixing the mentioned errors."
  411. exit 1
  412. fi
  413. else
  414. echo -e "\e[33mDEVELOPER MODE: Not creating a git diff and commiting it to prevent development stuff within a backup diff...\e[0m"
  415. fi
  416. echo -e "\e[32mFetching new images, if any...\e[0m"
  417. sleep 2
  418. $COMPOSE_COMMAND pull
  419. # Fix missing SSL, does not overwrite existing files
  420. [[ ! -d data/assets/ssl ]] && mkdir -p data/assets/ssl
  421. cp -n -d data/assets/ssl-example/*.pem data/assets/ssl/
  422. echo -e "Checking IPv6 settings... "
  423. if grep -q 'SYSCTL_IPV6_DISABLED=1' mailcow.conf; then
  424. echo
  425. echo '!! IMPORTANT !!'
  426. echo
  427. 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".'
  428. echo "This setting will only be active after a complete shutdown of mailcow by running $COMPOSE_COMMAND down followed by $COMPOSE_COMMAND up -d."
  429. echo
  430. echo '!! IMPORTANT !!'
  431. echo
  432. read -p "Press any key to continue..." < /dev/tty
  433. fi
  434. # Checking for old project name bug
  435. sed -i --follow-symlinks 's#COMPOSEPROJECT_NAME#COMPOSE_PROJECT_NAME#g' mailcow.conf
  436. # Fix Rspamd maps
  437. if [ -f data/conf/rspamd/custom/global_from_blacklist.map ]; then
  438. mv data/conf/rspamd/custom/global_from_blacklist.map data/conf/rspamd/custom/global_smtp_from_blacklist.map
  439. fi
  440. if [ -f data/conf/rspamd/custom/global_from_whitelist.map ]; then
  441. mv data/conf/rspamd/custom/global_from_whitelist.map data/conf/rspamd/custom/global_smtp_from_whitelist.map
  442. fi
  443. # Fix deprecated metrics.conf
  444. if [ -f "data/conf/rspamd/local.d/metrics.conf" ]; then
  445. if [ ! -z "$(git diff --name-only origin/master data/conf/rspamd/local.d/metrics.conf)" ]; then
  446. 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."
  447. echo "The deprecated configuration file metrics.conf will be moved to metrics.conf_deprecated after updating mailcow."
  448. fi
  449. mv data/conf/rspamd/local.d/metrics.conf data/conf/rspamd/local.d/metrics.conf_deprecated
  450. fi
  451. # Set app_info.inc.php
  452. if [ ${BRANCH} == "master" ]; then
  453. mailcow_git_version=$(git describe --tags $(git rev-list --tags --max-count=1))
  454. elif [ ${BRANCH} == "nightly" ]; then
  455. mailcow_git_version=$(git rev-parse --short $(git rev-parse @{upstream}))
  456. mailcow_last_git_version=""
  457. else
  458. mailcow_git_version=$(git rev-parse --short HEAD)
  459. mailcow_last_git_version=""
  460. fi
  461. mailcow_git_commit=$(git rev-parse "origin/${BRANCH}")
  462. mailcow_git_commit_date=$(git log -1 --format=%ci @{upstream} )
  463. if [ $? -eq 0 ]; then
  464. echo '<?php' > data/web/inc/app_info.inc.php
  465. echo ' $MAILCOW_GIT_VERSION="'$mailcow_git_version'";' >> data/web/inc/app_info.inc.php
  466. echo ' $MAILCOW_LAST_GIT_VERSION="";' >> data/web/inc/app_info.inc.php
  467. echo ' $MAILCOW_GIT_OWNER="mailcow";' >> data/web/inc/app_info.inc.php
  468. echo ' $MAILCOW_GIT_REPO="mailcow-dockerized";' >> data/web/inc/app_info.inc.php
  469. echo ' $MAILCOW_GIT_URL="https://github.com/mailcow/mailcow-dockerized";' >> data/web/inc/app_info.inc.php
  470. echo ' $MAILCOW_GIT_COMMIT="'$mailcow_git_commit'";' >> data/web/inc/app_info.inc.php
  471. echo ' $MAILCOW_GIT_COMMIT_DATE="'$mailcow_git_commit_date'";' >> data/web/inc/app_info.inc.php
  472. echo ' $MAILCOW_BRANCH="'$BRANCH'";' >> data/web/inc/app_info.inc.php
  473. echo ' $MAILCOW_UPDATEDAT='$(date +%s)';' >> data/web/inc/app_info.inc.php
  474. echo '?>' >> data/web/inc/app_info.inc.php
  475. else
  476. echo '<?php' > data/web/inc/app_info.inc.php
  477. echo ' $MAILCOW_GIT_VERSION="'$mailcow_git_version'";' >> data/web/inc/app_info.inc.php
  478. echo ' $MAILCOW_LAST_GIT_VERSION="";' >> data/web/inc/app_info.inc.php
  479. echo ' $MAILCOW_GIT_OWNER="mailcow";' >> data/web/inc/app_info.inc.php
  480. echo ' $MAILCOW_GIT_REPO="mailcow-dockerized";' >> data/web/inc/app_info.inc.php
  481. echo ' $MAILCOW_GIT_URL="https://github.com/mailcow/mailcow-dockerized";' >> data/web/inc/app_info.inc.php
  482. echo ' $MAILCOW_GIT_COMMIT="";' >> data/web/inc/app_info.inc.php
  483. echo ' $MAILCOW_GIT_COMMIT_DATE="";' >> data/web/inc/app_info.inc.php
  484. echo ' $MAILCOW_BRANCH="'$BRANCH'";' >> data/web/inc/app_info.inc.php
  485. echo ' $MAILCOW_UPDATEDAT='$(date +%s)';' >> data/web/inc/app_info.inc.php
  486. echo '?>' >> data/web/inc/app_info.inc.php
  487. echo -e "\e[33mCannot determine current git repository version...\e[0m"
  488. fi
  489. if [[ ${SKIP_START} == "y" ]]; then
  490. echo -e "\e[33mNot starting mailcow, please run \"$COMPOSE_COMMAND up -d --remove-orphans\" to start mailcow.\e[0m"
  491. else
  492. echo -e "\e[32mStarting mailcow...\e[0m"
  493. sleep 2
  494. $COMPOSE_COMMAND up -d --remove-orphans
  495. fi
  496. echo -e "\e[32mCollecting garbage...\e[0m"
  497. docker_garbage
  498. # Run post-update-hook
  499. if [ -f "${SCRIPT_DIR}/post_update_hook.sh" ]; then
  500. bash "${SCRIPT_DIR}/post_update_hook.sh"
  501. fi
  502. # echo "In case you encounter any problem, hard-reset to a state before updating mailcow:"
  503. # echo
  504. # git reflog --color=always | grep "Before update on "
  505. # echo
  506. # echo "Use \"git reset --hard hash-on-the-left\" and run $COMPOSE_COMMAND up -d afterwards."