Updated updater and fixed bugs

Added basic testing implementations
    - WIP: only outputs the log file at end of a run.
    - added option '--dev-test' to be used when running and testing the
      program.
    - practice of feature will be within a docker container but can
      equally been used on a native system.

Created 'check_restoration' function for optimisation of repetition
    - simply ensures that the restoration at any point was successful.
    - if the restoration fails, the program logs and exits accordingly;
      otherwise, if it succeeded then at this point the program exits
      with a different exit code since something else went wrong to
      trigger a system restoration.
    - takes 2 arguments, the exit code of the restoration system and
      the exit code of the section which triggered the restoration.
    - refactored sections can be identified containing the
      'check_restoration' function call.

Removed trailing whitespaces.
This commit is contained in:
Ethan Smith-Coss 2021-06-21 14:35:48 +01:00
parent 9ec3bacc9d
commit da6922a337

66
updater
View File

@ -18,7 +18,7 @@
# Author: Ethan Smith-Coss # # Author: Ethan Smith-Coss #
# Version: 0.1.4 # # Version: 0.1.4 #
# Created: 2021-05-20T16:47+0100 # # Created: 2021-05-20T16:47+0100 #
# Last Modified: 2021-06-13T21:36+0100 # # Last Modified: 2021-06-21T14:29+0100 #
# # # #
# #################################### # # #################################### #
# # # #
@ -91,7 +91,21 @@ function clean_up {
echo >> "$runtime_dir/logs/updater.log" echo >> "$runtime_dir/logs/updater.log"
cat "$logs_dir/updater.log" >> "$logs_dir/updater.history" cat "$logs_dir/updater.log" >> "$logs_dir/updater.history"
[[ $test_run -eq 0 ]] && cat "$logs_dir/updater.log"
} }
function check_restoration {
[[ ! $1 -eq 0 ]] && { echo -e "failed.\nThere was an error trying to restore system files." \
"Either there no back was made or another issue occurred. Consult $log_file for more information." ; exit $1 ; }
log "DEBUG" "System restoration was successful, safe to use; however, the update was still failed. Exiting...($2+$1)" \
"$log_file"
echo -e "completed. However, there was still an issue trying to update Microsoft Edge (Beta) to the latest version." \
"Consult $log_file for more information. Exiting..."
[[ ! $nn_flag -eq 0 ]] && notify "Microsoft Edge (Beta) Update" "There was an issue installing the update to your system." \
"The previous version, v$current_version, has been restored."
}
## iterate over all user inputs ## iterate over all user inputs
while test $# -gt 0 ; do while test $# -gt 0 ; do
case $1 in case $1 in
@ -109,6 +123,9 @@ while test $# -gt 0 ; do
-nn | --no-notify) -nn | --no-notify)
nn_flag=0 ; shift nn_flag=0 ; shift
;; ;;
--dev-test)
test_run=0 ; shift
;;
*) *)
shift shift
;; ;;
@ -370,14 +387,37 @@ log "DEBUG" "Successfully created an archive gzip tarball of the system. Safe to
# install the new version to system # install the new version to system
log "DEBUG" "Copying files to there appropriate location..." "$log_file" log "DEBUG" "Copying files to there appropriate location..." "$log_file"
cp -r "$tmp_path/opt" "/" >>"$log_file" 2>&1 cp -ar "$tmp_path/opt" "/" >>"$log_file" 2>&1
exit_code=$? exit_code=$?
[[ ! $exit_code -eq 0 ]] && { echo -e "failed.\nThere was an issue installing files to the system. Reverting to previous version..." ; if [[ ! $exit_code -eq 0 ]] ; then
log "DEBUG" "There was an issue copying $tmp_path/opt/ file to system /opt/. Reverting system files and exiting...(9)" "$log_file" ; restore_files && exit 9 ; } echo -ne "failed.\nThere was an issue installing files to the system. Reverting to previous version..."
cp -r "$tmp_path/usr" "/" >>"$log_file" 2>&1 log "DEBUG" "There was an issue copying $tmp_path/opt/ file to system /opt/. Reverting system files and exiting...(9)" \
"$log_file"
## attempt restoration of system
restore_files "microsoft_msedge-beta"
return_code=$?
## check the restoration was successful
check_restoration $return_code 9
exit 9
fi
cp -ar "$tmp_path/usr" "/" >>"$log_file" 2>&1
exit_code=$? exit_code=$?
[[ ! $exit_code -eq 0 ]] && { echo -e "failed.\nThere was an issue installing files to the system. Reverting to previous version..." ; if [[ ! $exit_code -eq 0 ]] ; then
log "DEBUG" "There was an issue copying $tmp_path/usr/ file to system /usr/. Reverting system files and exiting...(9)" "$log_file" ; restore_files && exit 9 ; } echo -ne "failed.\nThere was an issue installing files to the system. Reverting to previous version..."
log "DEBUG" "There was an issue copying $tmp_path/usr/ file to system /usr/. Reverting system files and exiting...(9)" \
"$log_file"
## attempt restoration of system
restore_files "microsoft_msedge-beta"
return_code=$?
## check the restoration was successful
check_restoration $return_code 9
exit 9
fi
log "DEBUG" "Successfully installed files to their appropriate location." "$log_file" log "DEBUG" "Successfully installed files to their appropriate location." "$log_file"
echo "installation complete." echo "installation complete."
## add directories usr/ and opt/ to garbage ## add directories usr/ and opt/ to garbage
@ -385,19 +425,15 @@ echo "$tmp_path/usr" >> "$garbage"
echo "$tmp_path/opt" >> "$garbage" echo "$tmp_path/opt" >> "$garbage"
## confirm the update was successful - try to restore system otherwise ## confirm the update was successful - try to restore system otherwise
## :@TODO: validate new version has successfully installed properly - exit 12
if [[ "$(microsoft-edge-beta --version | cut -d' ' -f3)" != "$release_version" ]] ; then if [[ "$(microsoft-edge-beta --version | cut -d' ' -f3)" != "$release_version" ]] ; then
log "ERROR" "There was an issue with the update and installation. Restoring to previous version..." "$log_file" log "ERROR" "There was an issue with the update and installation. Restoring to previous version..." "$log_file"
printf "Update was unsuccessful. Restoring to previous version..." printf "Update was unsuccessful. Restoring to previous version..."
## attempt restoration of system ## attempt restoration of system
return_code=$(restore_files "microsoft/msedge-beta") restore_files "microsoft_msedge-beta"
[[ $return_code -eq 12 ]] && { echo -e "failed.\nThere was an error trying to restore system files." \ return_code=$?
"Either there no back was made or another issue occurred. Consult $log_file for more information." ; exit $return_code ; } ## check the restoration was successful
log "DEBUG" "System restoration was successful, safe to use; however, the update was still failed. Exiting...(11)" "$log_file" check_restoration $return_code 11
echo -e "completed. However, there was still an issue trying to update Microsoft Edge (Beta) to the latest version." \
"Consult $log_file for more information. Exiting..."
[[ ! $nn_flag -eq 0 ]] && notify "Microsoft Edge (Beta) Update" "There was an issue installing the update to your system. The previous version, v$current_version, has been restored."
exit 11 exit 11
fi fi