You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

292 lines
9.8 KiB

1 month ago
  1. #!/bin/bash
  2. ## compose: no-hooks
  3. if [ -z "$SERVICE_DATASTORE" ]; then
  4. echo "This script is meant to be run through 'compose' to work properly." >&2
  5. exit 1
  6. fi
  7. version=0.1
  8. usage="$exname [-h|--help] [--force|-f] [TARGET_VERSION]"
  9. help="
  10. USAGE:
  11. $usage
  12. DESCRIPTION:
  13. Migrate the current nextcloud service to given target version. Don't
  14. forget to change your =compose.yml= accordingly afterwards.
  15. EXAMPLES:
  16. $exname 21.0.0
  17. "
  18. no_hint=
  19. force=
  20. target=
  21. while [ "$1" ]; do
  22. case "$1" in
  23. "--help"|"-h")
  24. print_help >&2
  25. exit 0
  26. ;;
  27. "--force"|"-f")
  28. force=yes
  29. ;;
  30. "--no-hint")
  31. no_hint=yes
  32. ;;
  33. --*|-*)
  34. err "Unexpected optional argument '$1'"
  35. print_usage >&2
  36. exit 1
  37. ;;
  38. *)
  39. [ -z "$target" ] && { target=$1 ; shift ; continue ; }
  40. err "Unexpected positional argument '$1'"
  41. print_usage >&2
  42. exit 1
  43. ;;
  44. esac
  45. shift
  46. done
  47. . lib/common
  48. nextcloud:code:version() {
  49. cat "$SERVICE_DATASTORE/var/www/html/version.php" |
  50. grep 'VersionString =' |
  51. cut -f 3 -d ' ' |
  52. cut -f 2 -d \'
  53. }
  54. current_image_version="${DOCKER_BASE_IMAGE#*:}"
  55. current_image_version="${current_image_version%-myc}"
  56. if ! [[ "$current_image_version" =~ ^[0-9]+.[0-9]+.[0-9]+$ ]]; then
  57. err "Current nextcloud version '$current_image_version' is unsupported yet."
  58. exit 1
  59. fi
  60. if ! [ -e "$SERVICE_DATASTORE/var/www/html/version.php" ]; then
  61. err "No code seem to have been deployed yet in datastore." \
  62. "This is not supported yet."
  63. exit 1
  64. fi
  65. current_code_version=$(nextcloud:code:version)
  66. if [ "$current_code_version" != "$current_image_version" ]; then
  67. err "Current code version ${WHITE}$current_code_version${NORMAL}" \
  68. "mismatch with current image version" \
  69. "${WHITE}$current_image_version${NORMAL}"
  70. exit 1
  71. fi
  72. current_config_version=$(nextcloud:config:version)
  73. info "Current config version: ${WHITE}$current_config_version${NORMAL}"
  74. if [ "$current_config_version" != "$current_code_version" ]; then
  75. warn "Current config version ${WHITE}$current_config_version${NORMAL}" \
  76. "mismatch with current code version" \
  77. "${WHITE}$current_code_version${NORMAL}"
  78. echo " Will use the config version as reference for upgrade." >&2
  79. fi
  80. last_available_versions=(
  81. $(DEBUG= docker:tags:fetch docker.0k.io/nextcloud 30 '[0-9]+\.[0-9+]\.[0-9]+-myc$' |
  82. sed -r 's/-myc$//g' |
  83. sort -rV)
  84. )
  85. ## XXXvlab: put this in kal-shlib-common
  86. version_gt() { test "$(printf '%s\n' "$@" | sort -V | head -n 1)" != "$1"; }
  87. last_upgradable_versions=()
  88. for v in "${last_available_versions[@]}"; do
  89. if version_gt "$v" "$current_config_version"; then
  90. last_upgradable_versions+=("$v")
  91. fi
  92. done
  93. docker_version=$(docker info --format '{{.ServerVersion}}')
  94. last_compatible_versions=()
  95. for v in "${last_upgradable_versions[@]}"; do
  96. if version_gt "$docker_version" 20.10.0; then
  97. last_compatible_versions+=("$v")
  98. continue
  99. fi
  100. if version_gt "26.99.99" "$v"; then
  101. last_compatible_versions+=("$v")
  102. fi
  103. done
  104. if [ "${#last_upgradable_versions[@]}" == 0 ]; then
  105. info "${DARKYELLOW}nextcloud${NORMAL} is already ${GREEN}up-to-date${NORMAL}."
  106. exit 0
  107. fi
  108. if [ -z "$target" ]; then
  109. info "Target latest version: ${WHITE}${last_available_versions[0]}${NORMAL}"
  110. target=${last_upgradable_versions[0]}
  111. else
  112. if [[ " ${last_available_versions[*]} " != *" $target "* ]]; then
  113. err "Invalid version ${WHITE}$target${NORMAL} selected, please specify one of:"
  114. for v in "${last_upgradable_versions[@]}"; do
  115. echo " - $v"
  116. done >&2
  117. exit 1
  118. fi
  119. info "Target version ${WHITE}$target${NORMAL}"
  120. fi
  121. if [[ " ${last_compatible_versions[*]} " != *" $target "* ]]; then
  122. err "Sorry, ${DARKYELLOW}$SERVICE_NAME${NORMAL} ${WHITE}$target${NORMAL}" \
  123. "require docker version >= ${WHITE}20.10${NORMAL} (current: $docker_version)"
  124. echo " Nextcloud versions <= ${WHITE}27${NORMAL} are still supported." >&2
  125. echo " You can specify one of:" >&2
  126. for v in "${last_compatible_versions[@]}"; do
  127. echo " - $v" >&2
  128. done
  129. exit 1
  130. fi
  131. upgrade_path=($(echo "${last_upgradable_versions[*]}" | tr ' ' '\n' | uniq -w 3 | sort -V))
  132. containers="$(get_running_containers_for_service "$SERVICE_NAME")"
  133. container_stopped=()
  134. if [ -n "$containers" ]; then
  135. #err "Running container(s) for $DARKYELLOW$SERVICE_NAME$NORMAL are still running:"
  136. for container in $containers; do
  137. docker stop "$container" >/dev/null || {
  138. err "Failed to stop container '$container'."
  139. exit 1
  140. }
  141. docker rm "$container" > /dev/null || {
  142. err "Couldn't delete container '$container'."
  143. }
  144. container_stopped+=("$container")
  145. done
  146. fi
  147. ## XXXvlab: taking first container is probably not a good idea
  148. container="$(echo "$containers" | head -n 1)"
  149. settmpdir MIGRATION_TMPDIR
  150. set -o pipefail
  151. for image_version in "${upgrade_path[@]}"; do
  152. while true; do
  153. patched=()
  154. current_code_version=$(nextcloud:code:version)
  155. current_config_version=$(nextcloud:config:version)
  156. if [ "$current_config_version" == "$image_version" ]; then
  157. err "Unexpected step where config version ${WHITE}$current_config_version${NORMAL} is same than image version"
  158. exit 1
  159. fi
  160. if ! version_gt "$image_version" "$current_config_version"; then
  161. err "Unexpected step where config version ${WHITE}$current_config_version${NORMAL} is greater than image version ${WHITE}$image_version${NORMAL}"
  162. exit 1
  163. fi
  164. if (( ${image_version%%.*} - ${current_config_version%%.*} > 1 )); then
  165. err "Unexpected step where config version ${WHITE}$current_config_version${NORMAL} is more than one major version less than image version ${WHITE}$image_version${NORMAL}"
  166. exit 1
  167. fi
  168. docker rmi "docker.0k.io/nextcloud:${current_config_version}-myc" >/dev/null 2>&1 || true
  169. if [ "$current_code_version" == "$image_version" ]; then
  170. ## Code already setup, we need to call ``occ upgrade`` by our selves
  171. info "Upgrading ${WHITE}$current_config_version${NORMAL} => ${WHITE}$image_version${NORMAL} (db)"
  172. compose --no-hooks \
  173. --add-compose-content="$SERVICE_NAME:
  174. docker-compose:
  175. image: docker.0k.io/nextcloud:${image_version}-myc" \
  176. run --rm \
  177. -u www-data --entrypoint /var/www/html/occ "$SERVICE_NAME" \
  178. upgrade 2>&1 |
  179. tee "$MIGRATION_TMPDIR/migration.log"
  180. errlvl="$?"
  181. else
  182. ## Code will be upgraded
  183. info "Upgrading ${WHITE}$current_config_version${NORMAL} => ${WHITE}$image_version${NORMAL} (code, db)"
  184. compose --no-hooks \
  185. --add-compose-content="$SERVICE_NAME:
  186. docker-compose:
  187. image: docker.0k.io/nextcloud:${image_version}-myc" \
  188. run --rm \
  189. -v "$CHARM_PATH"/src/fake-apache:/usr/bin/apache \
  190. --entrypoint /entrypoint.sh "$SERVICE_NAME" apache 2>&1 |
  191. tee "$MIGRATION_TMPDIR/migration.log"
  192. errlvl="$?"
  193. fi
  194. [ "$errlvl" == 0 ] && continue 2
  195. ##
  196. ## Damage control, there are some things we can solve
  197. ##
  198. if grep "^Update failed" "$MIGRATION_TMPDIR/migration.log" >/dev/null 2>&1; then
  199. ## XXXvlab: this comes from and should move to onlyoffice charm in
  200. ## some way.
  201. if grep "Update app onlyoffice" "$MIGRATION_TMPDIR/migration.log" >/dev/null 2>&1 && (
  202. grep "SQLSTATE" "$MIGRATION_TMPDIR/migration.log" >/dev/null 2>&1 ||
  203. grep "^Database error when running migration latest for app onlyoffice" "$MIGRATION_TMPDIR/migration.log" >/dev/null 2>&1 ); then
  204. if [ -e "$DATASTORE"/"$SERVICE_NAME/var/www/html/custom_apps/onlyoffice/lib/Migration/Version070400Date20220607111111.php" ]; then
  205. patch="$CHARM_PATH/../onlyoffice/src/patch/00-onlyoffice-nextcloud.patch"
  206. if ! [[ " ${patched[*]} " == *" $patch "* ]]; then
  207. if (
  208. cd "$DATASTORE"/"$SERVICE_NAME/var/www/html/custom_apps/onlyoffice/";
  209. patch -Np1 --dry-run < "$patch" ); then
  210. info "Found OnlyOffice issue, correcting it, and retrying."
  211. (
  212. cd "$DATASTORE"/"$SERVICE_NAME/var/www/html/custom_apps/onlyoffice/";
  213. patch -Np1 < "$patch"
  214. ) || exit 1
  215. patched+=("$patch")
  216. continue
  217. fi
  218. fi
  219. fi
  220. fi
  221. fi
  222. err "Upgrade to ${WHITE}$image_version${NORMAL} ${DARKRED}failed${NORMAL}. Aborting."
  223. exit 1
  224. done
  225. done
  226. if grep "^Nextcloud is in maintenance mode" "$MIGRATION_TMPDIR/migration.log" >/dev/null 2>&1; then
  227. info "Forcing maintenance mode off"
  228. compose --no-hooks \
  229. --add-compose-content="$SERVICE_NAME:
  230. docker-compose:
  231. image: docker.0k.io/nextcloud:${target}-myc" \
  232. run --rm \
  233. -u www-data --entrypoint /var/www/html/occ "$SERVICE_NAME" \
  234. maintenance:mode --off
  235. fi
  236. info "Successfully upgraded from ${WHITE}$current_config_version${NORMAL} to ${WHITE}$target${NORMAL}"
  237. if [ -z "$no_hint" ]; then
  238. cat <<EOF >&2
  239. Don't forget to force the version in your \`\`compose.yml\`\`. For instance:
  240. ${DARKYELLOW}$SERVICE_NAME${NORMAL}:
  241. ${DARKGRAY}# ...${NORMAL}
  242. ${WHITE}docker-compose${NORMAL}:
  243. ${WHITE}image${NORMAL}: docker.0k.io/nextcloud:${target}-myc
  244. ${DARKGRAY}# ...${NORMAL}
  245. EOF
  246. fi