From d59986cbc3f63035b2b37f5ef5041b8de6ab2d9f Mon Sep 17 00:00:00 2001 From: nicok1 <34523672+nicok1@users.noreply.github.com> Date: Fri, 23 Feb 2024 14:40:29 +0100 Subject: [PATCH] Offer rollback option for interactive upgrading (#11771) --- cmd/check_config.go | 29 +++++++++++++++++ packaging/scripts/postremove.sh | 56 +++++++++++++++++++++++++++++++++ packaging/scripts/preinstall.sh | 2 +- 3 files changed, 86 insertions(+), 1 deletion(-) create mode 100644 cmd/check_config.go diff --git a/cmd/check_config.go b/cmd/check_config.go new file mode 100644 index 000000000..ad0cc23e8 --- /dev/null +++ b/cmd/check_config.go @@ -0,0 +1,29 @@ +package cmd + +import ( + _ "embed" + "os" + + "github.com/spf13/cobra" +) + +var checkconfig = &cobra.Command{ + Use: "checkconfig", + Short: "Check config file for errors", + Run: runConfigCheck, +} + +func init() { + rootCmd.AddCommand(checkconfig) +} + +func runConfigCheck(cmd *cobra.Command, args []string) { + err := loadConfigFile(&conf) + + if err != nil { + log.FATAL.Println("config invalid:", err) + os.Exit(1) + } else { + log.INFO.Println("config valid") + } +} diff --git a/packaging/scripts/postremove.sh b/packaging/scripts/postremove.sh index 4de8d7878..5baac5cb4 100644 --- a/packaging/scripts/postremove.sh +++ b/packaging/scripts/postremove.sh @@ -1,3 +1,4 @@ +#!/bin/sh set -e if [ -d /run/systemd/system ]; then @@ -15,4 +16,59 @@ if [ "$1" = "purge" ]; then deb-systemd-helper purge evcc.service >/dev/null || true deb-systemd-helper unmask evcc.service >/dev/null || true fi +fi + + + + +# if interactive: Call /usr/bin/evcc checkconfig and capture the output (newer version) +# if output contains "config valid" then do nothing +# else: Ask user if he wants to keep the old version (working) or the new version (not working) +# Remember the choice with /tmp/.evccrollback and fail new-postrm failed-upgrade old-version new-version to initiate dpkg's rollback +if [ "$1" = "upgrade" ]; then + INTERACTIVE=0 + # is shell script interactive? + if [ -t 0 ]; then + INTERACTIVE=1 + else + INTERACTIVE=0 + fi + + if [ $INTERACTIVE -eq 1 ]; then + checkConfigOutput=$(/usr/bin/evcc checkconfig 2>&1 || true) + if ! echo "$checkConfigOutput" | grep -q "config valid"; then + echo "--------------------------------------------------------------------------------" + echo "ERROR: your evcc configuration is not compatible with the new version. Please consider reading the release notes: https://github.com/evcc-io/evcc/releases" + echo "checkconfig Output:" + echo "$checkConfigOutput" + echo "--------------------------------------------------------------------------------" + + while true; do + echo "Do you want to keep your old (working) evcc version? [Y/n]: " + read choice + case "$choice" in + n*|N*|"") + echo "We will keep the new version. Your evcc configuration stays untouched!" + break + ;; + y*|Y*) + echo "The old version will be restored. Your evcc configuration stays untouched! Following errors are intended:" + touch /tmp/.evccrollback + exit 1 + break + ;; + *) + ;; + esac + done + fi + fi +fi + +# if upgrade goal fails, new-postrm failed-upgrade old-version new-version is called. It should fail to initiate rollback +if [ "$1" = "failed-upgrade" ]; then + if [ -f "/tmp/.evccrollback" ]; then + rm "/tmp/.evccrollback" + exit 1 + fi fi \ No newline at end of file diff --git a/packaging/scripts/preinstall.sh b/packaging/scripts/preinstall.sh index 0c8f563eb..17ee90882 100644 --- a/packaging/scripts/preinstall.sh +++ b/packaging/scripts/preinstall.sh @@ -101,4 +101,4 @@ if [ "$1" = "upgrade" ]; then copyDbToUserDir fi -exit 0 +exit 0 \ No newline at end of file