From 807f060d1a7bfd008bb1d5f9e1e14f7c04f11365 Mon Sep 17 00:00:00 2001 From: Valentin Lab Date: Wed, 4 Oct 2023 08:49:04 +0200 Subject: [PATCH] new: [vps] add ``mongo upgrade`` action --- README.org | 19 ++++++++ bin/vps | 139 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 158 insertions(+) diff --git a/README.org b/README.org index 4760997..6c290e3 100644 --- a/README.org +++ b/README.org @@ -957,6 +957,25 @@ un bon moyen de voir cela). #+begin_src sh vps nextcloud upgrade #+end_src +*** mongo +**** Mise à jour en dernière version + +Cette commande permet d'appliquer successivement les version de +mongo, elle modifie le =compose.yml=. Et installe la dernière +version disponible donnée par =docker-tags-fetch docker.0k.io/mongo=. + +Étant donné que la commande est un peu nouvelle et la tâche assez +fastidieuse et risquée, ne pas hésiter à la lancer dans un =tmux= pour +être prêt à demander de l'aide. Également, lancer un =myc-update= avant. + +Aussi, il est toujours bon de vérifier que le backup fonctionne et que +la version sur le serveur de backup est à jour (lancer =vps backup= est +un bon moyen de voir cela). + +#+begin_src sh + vps mongo upgrade +#+end_src + *** onlyoffice **** Troubleshooting diff --git a/bin/vps b/bin/vps index 39e2835..2b5b79e 100755 --- a/bin/vps +++ b/bin/vps @@ -38,6 +38,41 @@ help="" version_gt() { test "$(printf '%s\n' "$@" | sort -V | head -n 1)" != "$1"; } +read-0a-err() { + local ret="$1" eof="" idx=0 last= + read -r -- "${ret?}" <<<"0" + shift + while [ "$1" ]; do + last=$idx + read -r -- "$1" || { + ## Put this last value in ${!ret} + eof="$1" + read -r -- "$ret" <<<"${!eof}" + break + } + ((idx++)) + shift + done + [ -z "$eof" ] || { + if [ "$last" != 0 ]; then + echo "Error: read-0a-err couldn't fill all value" >&2 + read -r -- "$ret" <<<"127" + else + if [ -z "${!ret}" ]; then + echo "Error: last value is not a number, did you finish with an errorlevel ?" >&2 + read -r -- "$ret" <<<"126" + fi + fi + false + } +} + +p-0a-err() { + "$@" + echo -n "$?" +} + + docker:running-container-projects() { :cache: scope=session @@ -2238,4 +2273,108 @@ host:sys:load_avg() { printf "%s " "" "$(date +%s)" "$uptime" } + +cmdline.spec.gnu mongo + +cmdline.spec::cmd:mongo:run() { + : +} + +cmdline.spec.gnu upgrade +cmdline.spec:mongo:cmd:upgrade:run() { + + : :posarg: [TARGET_VERSION] "Target version to migrate to" + : :optval: --service,-s "The mongo service name (defaults to 'mongo')" + : :optfla: --debug,-d "Display debugging information" + + local URL + + mongo_service="${opt_service:-mongo}" + available_actions=$(compose --get-available-actions) || exit 1 + available_actionable_services=($(e "$available_actions" | yq 'keys().[]')) + if [[ " ${available_actionable_services[*]} " != *" $mongo_service "* ]]; then + err "Service '$mongo_service' was not found in current 'compose.yml'." + exit 1 + fi + opts_compose=() + if [ -n "$opt_debug" ]; then + opts_compose+=("--debug") + else + opts_compose+=("-q") + fi + + project_name=$(compose:project_name) || exit 1 + containers="$(compose:service:containers "${project_name}" "${mongo_service}")" || exit 1 + + ## XXXvlab: quick hack, to make more beautiful later + cron_container=$(compose:service:containers "${project_name}" "cron") + containers="$containers $cron_container" + docker stop "$cron_container" >/dev/null 2>&1 || true + + before_version= + uptodate= + upgraded= + msgerr=() + while read-0a-err errlvl line; do + echo "$line" + rline=$(printf "%s" "$line" | sed_compat "s/$__color_sequence_regex//g") + case "$rline" in + "II Current mongo version: "*) + before_version="${rline#II Current mongo version: }" + ;; + "II ${mongo_service} is already up-to-date.") + if [ -z "$before_version" ]; then + msgerr+=("expected a 'current version' line before the 'up-to-date' one.") + continue + fi + after_version="$before_version" + uptodate=1 + ;; + "II Successfully upgraded from ${before_version} to "*) + after_version="${rline#II Successfully upgraded from ${before_version} to }" + upgraded=1 + ;; + *) + : + ;; + esac + done < <( + ## -q to remove the display of ``compose`` related information + ## like relation resolution. + ## -c on the upgrade action to force color + ansi_color=yes p-0a-err compose -c "${opts_compose[@]}" upgrade "$mongo_service" --no-hint -c "$TARGET_VERSION" + ) + + if [ "$errlvl" != 0 ]; then + exit "$errlvl" + fi + if [ -n "$uptodate" ]; then + for container in "${containers[@]}"; do + [ -n "$container" ] || continue + Wrap -d "start ${DARKYELLOW}${mongo_service}${NORMAL}'s container" -- \ + docker start "$container" || { + err "Failed to start container '$container'." + exit 1 + } + done + exit 0 + fi + if [ -z "$upgraded" ]; then + err "Unexpected output of 'upgrade' action with errorlevel 0 and without success" + exit 1 + fi + + desc="update \`compose.yml\` to set ${DARKYELLOW}$mongo_service${NORMAL}'s " + desc+="docker image to actual code version ${WHITE}${after_version}${NORMAL}" + Wrap -d "$desc" -- \ + compose:file:value-change \ + "${mongo_service}.docker-compose.image" \ + "docker.0k.io/mongo:${after_version}-myc" || exit 1 + + echo "${WHITE}Launching final compose${NORMAL}" + compose up || exit 1 +} + + + cmdline::parse "$@"