From 82d76b18f006a4b9c48b07f17c64b54cf4176f6b Mon Sep 17 00:00:00 2001 From: th33xitus <> Date: Mon, 27 Jul 2020 10:29:28 +0200 Subject: [PATCH] new rollback feature --- kiauh.ini | 10 ++--- kiauh.sh | 22 ++++++++--- scripts/rollback.sh | 92 +++++++++++++++++++++++++++++++++++++++++++++ scripts/status.sh | 10 ++--- scripts/ui.sh | 34 +++++++++++++---- scripts/update.sh | 27 ------------- 6 files changed, 146 insertions(+), 49 deletions(-) create mode 100755 scripts/rollback.sh diff --git a/kiauh.ini b/kiauh.ini index b2f72f5..71c376d 100644 --- a/kiauh.ini +++ b/kiauh.ini @@ -1,7 +1,7 @@ #don't edit this file if you don't know what you are doing... backup_before_update=false -previous_origin_state=25a9a32e -previous_smoothing_state=feb966c1 -previous_shaping_state=e0b94b7f -previous_moonraker_state=9c29a4fc -previous_dev_moonraker_state=528f9f25 \ No newline at end of file +previous_origin_state=0 +previous_smoothing_state=0 +previous_shaping_state=0 +previous_moonraker_state=0 +previous_dev_moonraker_state=0 \ No newline at end of file diff --git a/kiauh.sh b/kiauh.sh index 4d99a5a..9e2fe4a 100755 --- a/kiauh.sh +++ b/kiauh.sh @@ -337,34 +337,40 @@ advanced_menu(){ 2) clear print_header - build_fw + load_klipper_state print_msg && clear_msg advanced_ui;; 3) clear print_header - flash_routine + build_fw print_msg && clear_msg advanced_ui;; 4) clear print_header - get_usb_id + flash_routine print_msg && clear_msg advanced_ui;; 5) clear print_header - get_usb_id && write_printer_id + get_usb_id print_msg && clear_msg advanced_ui;; 6) clear print_header - create_dwc2fk_cfg + get_usb_id && write_printer_id print_msg && clear_msg advanced_ui;; 7) + clear + print_header + create_dwc2fk_cfg + print_msg && clear_msg + advanced_ui;; + 8) clear print_header create_custom_hostname @@ -443,6 +449,12 @@ switch_menu(){ fi } +#rollback_menu(){ +# load_klipper_state +# print_msg && clear_msg +# advanced_menu +#} + backup_menu(){ print_header print_msg && clear_msg diff --git a/scripts/rollback.sh b/scripts/rollback.sh new file mode 100755 index 0000000..cd5f9ae --- /dev/null +++ b/scripts/rollback.sh @@ -0,0 +1,92 @@ +save_klipper_state(){ + source_ini + #read current klipper state + COMMIT_STATE=$(git rev-parse --short HEAD) + if [ $GET_BRANCH = origin/master ]; then + ORI_OLD=$previous_origin_state + ORI_NEW=$COMMIT_STATE + sed -i "/previous_origin_state=/s/$ORI_OLD/$ORI_NEW/" $INI_FILE + elif [ $GET_BRANCH = dmbutyugin/scurve-shaping ]; then + SHA_OLD=$previous_shaping_state + SHA_NEW=$COMMIT_STATE + sed -i "/previous_shaping_state=/s/$SHA_OLD/$SHA_NEW/" $INI_FILE + elif [ $GET_BRANCH = dmbutyugin/scurve-smoothing ]; then + SMO_OLD=$previous_smoothing_state + SMO_NEW=$COMMIT_STATE + sed -i "/previous_smoothing_state=/s/$SMO_OLD/$SMO_NEW/" $INI_FILE + elif [ $GET_BRANCH = Arksine/work-web_server-20200131 ]; then + WWS_OLD=$previous_moonraker_state + WWS_NEW=$COMMIT_STATE + sed -i "/previous_moonraker_state=/s/$WWS_OLD/$WWS_NEW/" $INI_FILE + elif [ $GET_BRANCH = Arksine/dev-moonraker-testing ]; then + DMT_OLD=$previous_dev_moonraker_state + DMT_NEW=$COMMIT_STATE + sed -i "/previous_dev_moonraker_state=/s/$DMT_OLD/$DMT_NEW/" $INI_FILE + fi +} + +load_klipper_state(){ + source_ini + print_branch + CURR_COMM=$(git rev-parse --short=8 HEAD) + if [ "$GET_BRANCH" == "origin/master" ]; then + PREV_COMM=$previous_origin_state + elif [ "$GET_BRANCH" == "dmbutyugin/scurve-shaping" ]; then + PREV_COMM=$previous_shaping_state + elif [ "$GET_BRANCH" == "dmbutyugin/scurve-smoothing" ]; then + PREV_COMM=$previous_smoothing_state + elif [ "$GET_BRANCH" == "Arksine/work-web_server-20200131" ]; then + PREV_COMM=$previous_moonraker_state + elif [ "$GET_BRANCH" == "Arksine/dev-moonraker-testing" ]; then + PREV_COMM=$previous_dev_moonraker_state + fi + PREV_COMM_DATE=$(git show -s --format=%cd --date=short $PREV_COMM) + CURR_COMM_DATE=$(git show -s --format=%cd --date=short $CURR_COMM) + if [ $CURR_COMM = $PREV_COMM ]; then + CURR_UI=$(echo -e "${green}$CURR_COMM from $CURR_COMM_DATE${default}") + PREV_UI=$(echo -e "${green}$PREV_COMM from $PREV_COMM_DATE${default}") + else + CURR_UI=$(echo -e "${yellow}$CURR_COMM from $CURR_COMM_DATE${default}") + PREV_UI=$(echo -e "${yellow}$PREV_COMM from $PREV_COMM_DATE${default}") + fi + rollback_ui + rollback_klipper +} + +rollback_klipper(){ + if [ "$CURR_COMM" != "$PREV_COMM" ]; then + while true; do + echo -e "${cyan}" + read -p "###### Do you want to rollback to $PREV_COMM? (Y/n): " yn + echo -e "${default}" + case "$yn" in + Y|y|Yes|yes|"") + clear + print_header + status_msg "Rolling back to $PREV_COMM ..." + git reset --hard $PREV_COMM -q + ok_msg "Rollback complete!"; echo + load_klipper_state + break;; + N|n|No|no) clear; advanced_menu; break;; + Q|q) clear; advanced_menu; break;; + esac + done + else + while true; do + echo -e "${cyan}" + read -p "Perform action: " action; echo + echo -e "${default}" + case "$action" in + Q|q) + clear; advanced_menu; break;; + *) + clear + print_header + print_unkown_cmd + print_msg && clear_msg + rollback_ui;; + esac + done + fi +} \ No newline at end of file diff --git a/scripts/status.sh b/scripts/status.sh index 45918ac..e17b2d9 100644 --- a/scripts/status.sh +++ b/scripts/status.sh @@ -128,15 +128,15 @@ read_branch(){ print_branch(){ read_branch if [ "$GET_BRANCH" == "origin/master" ]; then - PRINT_BRANCH="${cyan}$GET_BRANCH${default} " + PRINT_BRANCH="$GET_BRANCH " elif [ "$GET_BRANCH" == "dmbutyugin/scurve-shaping" ]; then - PRINT_BRANCH="${cyan}scurve-shaping${default} " + PRINT_BRANCH="scurve-shaping " elif [ "$GET_BRANCH" == "dmbutyugin/scurve-smoothing" ]; then - PRINT_BRANCH="${cyan}scurve-smoothing${default} " + PRINT_BRANCH="scurve-smoothing " elif [ "$GET_BRANCH" == "Arksine/work-web_server-20200131" ]; then - PRINT_BRANCH="${cyan}moonraker${default} " + PRINT_BRANCH="moonraker " elif [ "$GET_BRANCH" == "Arksine/dev-moonraker-testing" ]; then - PRINT_BRANCH="${cyan}dev-moonraker${default} " + PRINT_BRANCH="dev-moonraker " else PRINT_BRANCH="${red}----${default} " fi diff --git a/scripts/ui.sh b/scripts/ui.sh index 97fba1d..37fbe99 100644 --- a/scripts/ui.sh +++ b/scripts/ui.sh @@ -33,7 +33,7 @@ main_ui(){ hr echo -e "| 0) [System status] | |" echo -e "| | Klipper: $KLIPPER_STATUS|" - echo -e "| 1) [Install] | Branch: $PRINT_BRANCH|" + echo -e "| 1) [Install] | Branch: ${cyan}$PRINT_BRANCH${default}|" echo -e "| 2) [Update] | |" echo -e "| 3) [Remove] | DWC2: $DWC2_STATUS|" echo -e "| | Mainsail: $MAINSAIL_STATUS|" @@ -109,14 +109,15 @@ advanced_ui(){ hr echo -e "| | | " echo -e "| Klipper: | System: | " - echo -e "| 1) [Switch Version] | 7) [Change hostname] | " + echo -e "| 1) [Switch Version] | 8) [Change hostname] | " + echo -e "| 2) [Rollback] | | " echo -e "| | | " echo -e "| Firmware: | | " - echo -e "| 2) [Build Firmware] | | " - echo -e "| 3) [Flash MCU] | | " - echo -e "| 4) [Get Printer-ID] | | " - echo -e "| 5) [Write Printer-ID] | | " - echo -e "| 6) [Write DWC2 config] | | " + echo -e "| 3) [Build Firmware] | | " + echo -e "| 4) [Flash MCU] | | " + echo -e "| 5) [Get Printer-ID] | | " + echo -e "| 6) [Write Printer-ID] | | " + echo -e "| 7) [Write DWC2 config] | | " echo -e "| | | " quit_footer } @@ -161,4 +162,23 @@ kiauh_update_msg(){ echo -e "| ${yellow}There is a newer version of this script available!${default} | " echo -e "| ${yellow}Type 'update' if you want to update KIAUH now.${default} | " bottom_border +} + +rollback_ui(){ + top_border + echo -e "| $(title_msg "~~~~~~~~~~~~~ [ Rollback Menu ] ~~~~~~~~~~~~~") | " + hr + echo -e "| If serious errors occured after updating Klipper, | " + echo -e "| you can use this menu to return to the previously | " + echo -e "| used commit from which you have updated. | " + bottom_border + top_border + echo -e "| Active branch: ${green}$PRINT_BRANCH${default} | " + hr + echo -e "| Currently on commit: | " + echo -e "| $CURR_UI | " + hr + echo -e "| Commit last updated from: | " + echo -e "| $PREV_UI | " + quit_footer } \ No newline at end of file diff --git a/scripts/update.sh b/scripts/update.sh index e7aefed..16a4aa7 100644 --- a/scripts/update.sh +++ b/scripts/update.sh @@ -65,31 +65,4 @@ update_mainsail(){ status_msg "Updating Mainsail ..." install_mainsail start_klipper -} - -save_klipper_state(){ - source_ini - #read current klipper state - COMMIT_STATE=$(git rev-parse --short HEAD) - if [ $GET_BRANCH = origin/master ]; then - ORI_OLD=$previous_origin_state - ORI_NEW=$COMMIT_STATE - sed -i "/previous_origin_state=/s/$ORI_OLD/$ORI_NEW/" $INI_FILE - elif [ $GET_BRANCH = dmbutyugin/scurve-shaping ]; then - SHA_OLD=$previous_shaping_state - SHA_NEW=$COMMIT_STATE - sed -i "/previous_shaping_state=/s/$SHA_OLD/$SHA_NEW/" $INI_FILE - elif [ $GET_BRANCH = dmbutyugin/scurve-smoothing ]; then - SMO_OLD=$previous_smoothing_state - SMO_NEW=$COMMIT_STATE - sed -i "/previous_smoothing_state=/s/$SMO_OLD/$SMO_NEW/" $INI_FILE - elif [ $GET_BRANCH = Arksine/work-web_server-20200131 ]; then - WWS_OLD=$previous_moonraker_state - WWS_NEW=$COMMIT_STATE - sed -i "/previous_moonraker_state=/s/$WWS_OLD/$WWS_NEW/" $INI_FILE - elif [ $GET_BRANCH = Arksine/dev-moonraker-testing ]; then - DMT_OLD=$previous_dev_moonraker_state - DMT_NEW=$COMMIT_STATE - sed -i "/previous_dev_moonraker_state=/s/$DMT_OLD/$DMT_NEW/" $INI_FILE - fi } \ No newline at end of file