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.

297 lines
10 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. docker_version=$(docker info --format '{{.ServerVersion}}')
  99. last_compatible_versions=()
  100. for v in "${last_upgradable_versions[@]}"; do
  101. if version_gt "$docker_version" 20.10.0; then
  102. last_compatible_versions+=("$v")
  103. continue
  104. fi
  105. if version_gt "26.99.99" "$v"; then
  106. last_compatible_versions+=("$v")
  107. fi
  108. done
  109. if [ "${#last_upgradable_versions[@]}" == 0 ]; then
  110. info "${DARKYELLOW}nextcloud${NORMAL} is already ${GREEN}up-to-date${NORMAL}."
  111. exit 0
  112. fi
  113. if [ -z "$target" ]; then
  114. info "Target latest version: ${WHITE}${last_available_versions[0]}${NORMAL}"
  115. target=${last_upgradable_versions[0]}
  116. else
  117. if [[ " ${last_available_versions[*]} " != *" $target "* ]]; then
  118. err "Invalid version ${WHITE}$target${NORMAL} selected, please specify one of:"
  119. for v in "${last_upgradable_versions[@]}"; do
  120. echo " - $v"
  121. done >&2
  122. exit 1
  123. fi
  124. info "Target version ${WHITE}$target${NORMAL}"
  125. fi
  126. if [[ " ${last_compatible_versions[*]} " != *" $target "* ]]; then
  127. err "Sorry, ${DARKYELLOW}$SERVICE_NAME${NORMAL} ${WHITE}$target${NORMAL}" \
  128. "require docker version >= ${WHITE}20.10${NORMAL} (current: $docker_version)"
  129. echo " Nextcloud versions <= ${WHITE}27${NORMAL} are still supported." >&2
  130. echo " You can specify one of:" >&2
  131. for v in "${last_compatible_versions[@]}"; do
  132. echo " - $v" >&2
  133. done
  134. exit 1
  135. fi
  136. upgrade_path=($(echo "${last_upgradable_versions[*]}" | tr ' ' '\n' | uniq -w 3 | sort -V))
  137. containers="$(get_running_containers_for_service "$SERVICE_NAME")"
  138. container_stopped=()
  139. if [ -n "$containers" ]; then
  140. #err "Running container(s) for $DARKYELLOW$SERVICE_NAME$NORMAL are still running:"
  141. for container in $containers; do
  142. docker stop "$container" >/dev/null || {
  143. err "Failed to stop container '$container'."
  144. exit 1
  145. }
  146. docker rm "$container" > /dev/null || {
  147. err "Couldn't delete container '$container'."
  148. }
  149. container_stopped+=("$container")
  150. done
  151. fi
  152. ## XXXvlab: taking first container is probably not a good idea
  153. container="$(echo "$containers" | head -n 1)"
  154. settmpdir MIGRATION_TMPDIR
  155. set -o pipefail
  156. for image_version in "${upgrade_path[@]}"; do
  157. while true; do
  158. patched=()
  159. current_code_version=$(nextcloud:code:version)
  160. current_config_version=$(nextcloud:config:version)
  161. if [ "$current_config_version" == "$image_version" ]; then
  162. err "Unexpected step where config version ${WHITE}$current_config_version${NORMAL} is same than image version"
  163. exit 1
  164. fi
  165. if ! version_gt "$image_version" "$current_config_version"; then
  166. err "Unexpected step where config version ${WHITE}$current_config_version${NORMAL} is greater than image version ${WHITE}$image_version${NORMAL}"
  167. exit 1
  168. fi
  169. if (( ${image_version%%.*} - ${current_config_version%%.*} > 1 )); then
  170. 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}"
  171. exit 1
  172. fi
  173. docker rmi "docker.0k.io/nextcloud:${current_config_version}-myc" >/dev/null 2>&1 || true
  174. if [ "$current_code_version" == "$image_version" ]; then
  175. ## Code already setup, we need to call ``occ upgrade`` by our selves
  176. info "Upgrading ${WHITE}$current_config_version${NORMAL} => ${WHITE}$image_version${NORMAL} (db)"
  177. compose --no-hooks \
  178. --add-compose-content="$SERVICE_NAME:
  179. docker-compose:
  180. image: docker.0k.io/nextcloud:${image_version}-myc" \
  181. run --rm \
  182. -u www-data --entrypoint /var/www/html/occ "$SERVICE_NAME" \
  183. upgrade 2>&1 |
  184. tee "$MIGRATION_TMPDIR/migration.log"
  185. errlvl="$?"
  186. else
  187. ## Code will be upgraded
  188. info "Upgrading ${WHITE}$current_config_version${NORMAL} => ${WHITE}$image_version${NORMAL} (code, db)"
  189. compose --no-hooks \
  190. --add-compose-content="$SERVICE_NAME:
  191. docker-compose:
  192. image: docker.0k.io/nextcloud:${image_version}-myc" \
  193. run --rm \
  194. -v "$CHARM_PATH"/src/fake-apache:/usr/bin/apache \
  195. --entrypoint /entrypoint.sh "$SERVICE_NAME" apache 2>&1 |
  196. tee "$MIGRATION_TMPDIR/migration.log"
  197. errlvl="$?"
  198. fi
  199. [ "$errlvl" == 0 ] && continue 2
  200. ##
  201. ## Damage control, there are some things we can solve
  202. ##
  203. if grep "^Update failed" "$MIGRATION_TMPDIR/migration.log" >/dev/null 2>&1; then
  204. ## XXXvlab: this comes from and should move to onlyoffice charm in
  205. ## some way.
  206. if grep "Update app onlyoffice" "$MIGRATION_TMPDIR/migration.log" >/dev/null 2>&1 && (
  207. grep "SQLSTATE" "$MIGRATION_TMPDIR/migration.log" >/dev/null 2>&1 ||
  208. grep "^Database error when running migration latest for app onlyoffice" "$MIGRATION_TMPDIR/migration.log" >/dev/null 2>&1 ); then
  209. if [ -e "$DATASTORE"/"$SERVICE_NAME/var/www/html/custom_apps/onlyoffice/lib/Migration/Version070400Date20220607111111.php" ]; then
  210. patch="$CHARM_PATH/../onlyoffice/src/patch/00-onlyoffice-nextcloud.patch"
  211. if ! [[ " ${patched[*]} " == *" $patch "* ]]; then
  212. if (
  213. cd "$DATASTORE"/"$SERVICE_NAME/var/www/html/custom_apps/onlyoffice/";
  214. patch -Np1 --dry-run < "$patch" ); then
  215. info "Found OnlyOffice issue, correcting it, and retrying."
  216. (
  217. cd "$DATASTORE"/"$SERVICE_NAME/var/www/html/custom_apps/onlyoffice/";
  218. patch -Np1 < "$patch"
  219. ) || exit 1
  220. patched+=("$patch")
  221. continue
  222. fi
  223. fi
  224. fi
  225. fi
  226. fi
  227. err "Upgrade to ${WHITE}$image_version${NORMAL} ${DARKRED}failed${NORMAL}. Aborting."
  228. exit 1
  229. done
  230. done
  231. if grep "^Nextcloud is in maintenance mode" "$MIGRATION_TMPDIR/migration.log" >/dev/null 2>&1; then
  232. info "Forcing maintenance mode off"
  233. compose --no-hooks \
  234. --add-compose-content="$SERVICE_NAME:
  235. docker-compose:
  236. image: docker.0k.io/nextcloud:${target}-myc" \
  237. run --rm \
  238. -u www-data --entrypoint /var/www/html/occ "$SERVICE_NAME" \
  239. maintenance:mode --off
  240. fi
  241. info "Successfully upgraded from ${WHITE}$current_config_version${NORMAL} to ${WHITE}$target${NORMAL}"
  242. if [ -z "$no_hint" ]; then
  243. cat <<EOF >&2
  244. Don't forget to force the version in your \`\`compose.yml\`\`. For instance:
  245. ${DARKYELLOW}$SERVICE_NAME${NORMAL}:
  246. ${DARKGRAY}# ...${NORMAL}
  247. ${WHITE}docker-compose${NORMAL}:
  248. ${WHITE}image${NORMAL}: docker.0k.io/nextcloud:${target}-myc
  249. ${DARKGRAY}# ...${NORMAL}
  250. EOF
  251. fi