fork 0k-charms
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.

274 lines
9.2 KiB

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