From 2641cb4d764af8f29a818ec3c6779ed7c27b394c Mon Sep 17 00:00:00 2001 From: Valentin Lab Date: Mon, 20 Nov 2023 12:26:34 +0100 Subject: [PATCH] new: [vps] add ``upgrade postgres`` action --- bin/vps | 104 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 104 insertions(+) diff --git a/bin/vps b/bin/vps index e352b3d..bb22f2e 100755 --- a/bin/vps +++ b/bin/vps @@ -2380,6 +2380,110 @@ cmdline.spec:mongo:cmd:upgrade:run() { } +cmdline.spec.gnu postgres + +cmdline.spec::cmd:postgres:run() { + : +} + +cmdline.spec.gnu upgrade +cmdline.spec:postgres:cmd:upgrade:run() { + + : :posarg: [TARGET_VERSION] "Target version to migrate to" + : :optval: --service,-s "The postgre service name (defaults to 'postgres')" + : :optfla: --debug,-d "Display debugging information" + + local URL + + depends yq + + postgres_service="${opt_service:-postgres}" + available_actions=$(compose --get-available-actions) || exit 1 + available_actionable_services=($(e "$available_actions" | yq 'keys().[]')) + if [[ " ${available_actionable_services[*]} " != *" $postgres_service "* ]]; then + err "Service '$postgres_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}" "${postgres_service}")) || exit 1 + + ## XXXvlab: quick hack, to make more beautiful later + cron_container=$(compose:service:containers "${project_name}" "cron") + 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 postgres version: "*) + before_version="${rline#II Current postgres version: }" + ;; + "II ${postgres_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 -q -c "${opts_compose[@]}" upgrade "$postgres_service" --no-hint -c "$TARGET_VERSION" 2>&1 + ) + + if [ "$errlvl" != 0 ]; then + exit "$errlvl" + fi + if [ -n "$uptodate" ]; then + for container in "${containers[@]}"; do + [ -n "$container" ] || continue + Wrap -d "start ${DARKYELLOW}${postgres_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}$postgres_service${NORMAL}'s " + desc+="docker image to actual code version ${WHITE}${after_version}${NORMAL}" + Wrap -d "$desc" -- \ + compose:file:value-change \ + "${postgres_service}.docker-compose.image" \ + "docker.0k.io/postgres:${after_version}-myc" || exit 1 + + echo "${WHITE}Launching final compose${NORMAL}" + compose up || exit 1 +} + + cmdline.spec.gnu bench