update.sh 24 KB

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