update.sh 24 KB

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