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.

269 lines
8.9 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 [[ "* $target *" != " ${last_available_versions[*]} " ]]; 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. container_stopped+=("$container")
  126. done
  127. fi
  128. ## XXXvlab: taking first container is probably not a good idea
  129. container="$(echo "$containers" | head -n 1)"
  130. settmpdir MIGRATION_TMPDIR
  131. set -o pipefail
  132. for image_version in "${upgrade_path[@]}"; do
  133. while true; do
  134. patched=()
  135. current_code_version=$(nextcloud:code:version)
  136. current_config_version=$(nextcloud:config:version)
  137. if [ "$current_config_version" == "$image_version" ]; then
  138. err "Unexpected step where config version ${WHITE}$current_config_version${NORMAL} is same than image version"
  139. exit 1
  140. fi
  141. if ! version_gt "$image_version" "$current_config_version"; then
  142. err "Unexpected step where config version ${WHITE}$current_config_version${NORMAL} is greater than image version ${WHITE}$image_version${NORMAL}"
  143. exit 1
  144. fi
  145. if (( ${image_version%%.*} - ${current_config_version%%.*} > 1 )); then
  146. 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}"
  147. exit 1
  148. fi
  149. if [ "$current_code_version" == "$image_version" ]; then
  150. ## Code already setup, we need to call ``occ upgrade`` by our selves
  151. info "Upgrading ${WHITE}$current_config_version${NORMAL} => ${WHITE}$image_version${NORMAL} (db)"
  152. compose --no-hooks \
  153. --add-compose-content="$SERVICE_NAME:
  154. docker-compose:
  155. image: docker.0k.io/nextcloud:${image_version}-myc" \
  156. run --rm \
  157. -u www-data --entrypoint /var/www/html/occ "$SERVICE_NAME" \
  158. upgrade 2>&1 |
  159. tee "$MIGRATION_TMPDIR/migration.log"
  160. errlvl="$?"
  161. else
  162. ## Code will be upgraded
  163. info "Upgrading ${WHITE}$current_config_version${NORMAL} => ${WHITE}$image_version${NORMAL} (code, db)"
  164. compose --no-hooks \
  165. --add-compose-content="$SERVICE_NAME:
  166. docker-compose:
  167. image: docker.0k.io/nextcloud:${image_version}-myc" \
  168. run --rm \
  169. -v "$CHARM_PATH"/src/fake-apache:/usr/bin/apache \
  170. --entrypoint /entrypoint.sh "$SERVICE_NAME" apache 2>&1 |
  171. tee "$MIGRATION_TMPDIR/migration.log"
  172. errlvl="$?"
  173. fi
  174. [ "$errlvl" == 0 ] && continue 2
  175. ##
  176. ## Damage control, there are some things we can solve
  177. ##
  178. if grep "^Update failed" "$MIGRATION_TMPDIR/migration.log" >/dev/null 2>&1; then
  179. ## XXXvlab: this comes from and should move to onlyoffice charm in
  180. ## some way.
  181. if grep "Update app onlyoffice" "$MIGRATION_TMPDIR/migration.log" >/dev/null 2>&1 &&
  182. grep "SQLSTATE" "$MIGRATION_TMPDIR/migration.log" >/dev/null 2>&1; then
  183. if [ -e "$DATASTORE"/"$SERVICE_NAME/var/www/html/custom_apps/onlyoffice/lib/Migration/Version070400Date20220607111111.php" ]; then
  184. patch="$CHARM_PATH/../onlyoffice/src/patch/00-onlyoffice-nextcloud.patch"
  185. if ! [[ " ${patched[*]} " == *" $patch "* ]]; then
  186. if (
  187. cd "$DATASTORE"/"$SERVICE_NAME/var/www/html/custom_apps/onlyoffice/";
  188. patch -Np1 --dry-run < "$patch" ); then
  189. info "Found OnlyOffice issue, correcting it, and retrying."
  190. (
  191. cd "$DATASTORE"/"$SERVICE_NAME/var/www/html/custom_apps/onlyoffice/";
  192. patch -Np1 < "$patch"
  193. ) || exit 1
  194. patched+=("$patch")
  195. continue
  196. fi
  197. fi
  198. fi
  199. fi
  200. fi
  201. err "Upgrade to ${WHITE}$image_version${NORMAL} ${DARKRED}failed${NORMAL}. Aborting."
  202. exit 1
  203. done
  204. done
  205. if grep "^Nextcloud is in maintenance mode" "$MIGRATION_TMPDIR/migration.log" >/dev/null 2>&1; then
  206. info "Forcing maintenance mode off"
  207. compose --no-hooks \
  208. --add-compose-content="$SERVICE_NAME:
  209. docker-compose:
  210. image: docker.0k.io/nextcloud:${target}-myc" \
  211. run --rm \
  212. -u www-data --entrypoint /var/www/html/occ "$SERVICE_NAME" \
  213. maintenance:mode --off
  214. fi
  215. info "Successfully upgraded from ${WHITE}$current_config_version${NORMAL} to ${WHITE}$target${NORMAL}"
  216. if [ -z "no_hint" ]; then
  217. cat <<EOF >&2
  218. Don't forget to force the version in your \`\`compose.yml\`\`. For instance:
  219. ${DARKYELLOW}$SERVICE_NAME${NORMAL}:
  220. ${DARKGRAY}# ...${NORMAL}
  221. ${WHITE}docker-compose${NORMAL}:
  222. ${WHITE}image${NORMAL}: docker.0k.io/nextcloud:${target}-myc
  223. ${DARKGRAY}# ...${NORMAL}
  224. EOF
  225. fi