update.sh 24 KB

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