Updated updater to v0.1.3

Implemented notification system.
    - Send a notification to the user's system using the `notify` function in common.
    - Notifications are send on most important outcomes of the update
      session meaning program can have STDOUT piped to another location
      and still receive a message after operation.
    - Added the `--no-notify` flag to prevent notifications from being
      sent to the user. By default, notifications will be sent without
      this flag.

- Program now checks to ensure the script is being ran with root
  privilages. Program will exit otherwise without notification.

- Program now keeps track of garbage data to ensure a proper clean up
  after operation of a session.
    - Unnecessary large volumes of data sat in /tmp/ can be removed
      instantly to prevent taking up space which isn't required. File
      sizes significantly less can remain and cleared out on next
      restart of the system.

- Updated usage message.
- Changed some of the output messages.
- Fixed bug with relative paths.
This commit is contained in:
Ethan Smith-Coss 2021-05-28 18:09:13 +01:00
parent df462701f1
commit 2add5afc7c

70
updater
View File

@ -8,6 +8,7 @@
# gunzip # # gunzip #
# tar # # tar #
# sort (coreutils >= 8.32) # # sort (coreutils >= 8.32) #
# notify-send #
# # # #
# # # #
# Disclaimer: This version only works for MS Edge Beta. # # Disclaimer: This version only works for MS Edge Beta. #
@ -15,9 +16,9 @@
# and potentially both. # # and potentially both. #
# # # #
# Author: Ethan Smith-Coss # # Author: Ethan Smith-Coss #
# Version: 0.1.2 # # Version: 0.1.3 #
# Created: 2021-05-20T16:47+0100 # # Created: 2021-05-20T16:47+0100 #
# Last Modified: 2021-05-23T21:18+0100 # # Last Modified: 2021-05-28T12:54+0100 #
# # # #
# #################################### # # #################################### #
# # # #
@ -41,7 +42,7 @@
## program name ## program name
prog_name="Microsoft Edge Updater" prog_name="Microsoft Edge Updater"
## version ## version
version="0.1.2" version="0.1.3"
## usage statement ## usage statement
usage="Usage: $(basename $0) [OPTION] usage="Usage: $(basename $0) [OPTION]
$prog_name ($version) - An updater program which can install the latest available version of Microsoft Edge to non-Debian-based Linux distros. $prog_name ($version) - An updater program which can install the latest available version of Microsoft Edge to non-Debian-based Linux distros.
@ -49,16 +50,19 @@ $prog_name ($version) - An updater program which can install the latest availabl
DISCLAIMER: Currently this program can only update the Beta release of Microsoft Edge for amd64 architectures. DISCLAIMER: Currently this program can only update the Beta release of Microsoft Edge for amd64 architectures.
Options: Options:
-y, --yes-all Assume yes to all yes/no decisions. -nn, --no-notify Prevent notifications from being set after a session.
-v, --version Print out the version of the script and exit. -y, --yes-all Assume yes to all yes/no decisions.
--help Print this help message and exit -v, --version Print out the version of the script and exit.
-h, --help Print this help message and exit
" "
runtime_dir=$(realpath "${BASH_SOURCE[0]}" | xargs -r dirname) runtime_dir=$(realpath "${BASH_SOURCE[0]}" | xargs -r dirname)
## runtime locations
log_file="$runtime_dir/.log.1" log_file="$runtime_dir/.log.1"
logs_dir="$runtime_dir/logs" logs_dir="$runtime_dir/logs"
tmp_path="/tmp/microsoft-edge-autoupdater" tmp_path="/tmp/microsoft-edge-autoupdater"
garbage="$tmp_path/garbage.tmp" garbage="$tmp_path/garbage.tmp"
## program timer
SECONDS=0 SECONDS=0
[[ ! -d "$logs_dir" ]] && mkdir "$logs_dir" [[ ! -d "$logs_dir" ]] && mkdir "$logs_dir"
@ -68,9 +72,15 @@ SECONDS=0
function clean_up { function clean_up {
log "DEBUG" "EXIT signal was raised, cleaning up system after session before exiting..." "$log_file" log "DEBUG" "EXIT signal was raised, cleaning up system after session before exiting..." "$log_file"
printf "Cleaning up system after session..." printf "Cleaning up system after session..."
cp "$log_file" "$logs_dir/updater.log" && rm "$log_file" cp "$log_file" "$logs_dir/updater.log" # && rm "$log_file"
[[ -d "$tmp_path/opt" ]] && rm -r "$tmp_path/opt" # [[ -d "$tmp_path/opt" ]] && rm -r "$tmp_path/opt"
[[ -d "$tmp_path/usr" ]] && rm -r "$tmp_path/usr" # [[ -d "$tmp_path/usr" ]] && rm -r "$tmp_path/usr"
while read -r garbage_collection ; do
[[ -f "$garbage_collection" || -d "$garbage_collection" ]] && rm -r "$garbage_collection"
done < <(cat "$garbage")
[[ -f "$garbage" ]] && rm "$garbage"
log "CLEANER" "Finished cleaning up system after session. Nice and clean :D Goodbye." "$runtime_dir/logs/updater.log" log "CLEANER" "Finished cleaning up system after session. Nice and clean :D Goodbye." "$runtime_dir/logs/updater.log"
echo "done. Goodbye." echo "done. Goodbye."
@ -88,7 +98,7 @@ while test $# -gt 0 ; do
-y | --yes-all) -y | --yes-all)
yes_flag=0 ; shift yes_flag=0 ; shift
;; ;;
--help) -h | --help)
echo "$usage" echo "$usage"
exit 0 exit 0
;; ;;
@ -96,26 +106,36 @@ while test $# -gt 0 ; do
echo "$prog_name ($version)" echo "$prog_name ($version)"
exit 0 exit 0
;; ;;
-nn | --no-notify)
nn_flag=0 ; shift
;;
*) *)
shift shift
;; ;;
esac esac
done done
source "$runtime_dir/utils/common" ## check if the script is being ran as root - exit otherwise
log "PREINIT" "----[New instance of script has been started: $(date -Iseconds)]----" "$log_file" [[ $(id -u) -ne 0 ]] && { echo "Please run script with root privilages." ; exit 1 ; }
## setup the yes flag to automatically accept all yes/no inputs
yes_flag="${yes_flag:-1}"
log "DEBUG" "The yes flag has been set to: $yes_flag." "$log_file"
# set trap to trigger clean up function on any exit # set trap to trigger clean up function on any exit
trap clean_up EXIT trap clean_up EXIT
source "$runtime_dir/utils/common"
log "PREINIT" "----[New instance of script has been started: $(date -Iseconds)]----" "$log_file"
echo "$log_file" >> "$garbage"
## setup the yes flag to automatically accept all yes/no inputs
yes_flag=${yes_flag:-1}
log "DEBUG" "The yes flag has been set to: $yes_flag." "$log_file"
## setup the no-notify (nn) flag if notify-send isn't installed
command -v notify-send >/dev/null 2>&1 || nn_flag=0
nn_flag=${nn_flag:-1}
## check if Microsoft Edge (Beta) is already installed ## check if Microsoft Edge (Beta) is already installed
exit_code=$(command -v microsoft-edge-beta &>/dev/null) exit_code=$(command -v microsoft-edge-beta &>/dev/null)
[[ ! $exit_code -eq 0 ]] && { echo "Microsoft Edge (Beta) is not installed. Exiting updater..." ; [[ ! $exit_code -eq 0 ]] && { echo "Microsoft Edge (Beta) is not installed. Exiting updater..." ;
log "DEBUG" "Microsoft Edge (Beta) not recognised as an installed program. Exiting...(1)" ; exit 1 ; } log "DEBUG" "Microsoft Edge (Beta) not recognised as an installed program. Exiting...(1)" ; exit 1 ; }
log "DEBUG" "Microsoft Edge (Beta) is installed to system. Proceeding with update..." "$log_file" log "DEBUG" "Microsoft Edge (Beta) is installed to system. Proceeding with update..." "$log_file"
## ensure source.list exists ## ensure source.list exists
@ -135,6 +155,8 @@ echo "Checking to see if the host is known and reachable..."
if ! wget --spider "$host" >/dev/null 2>&1 ; then if ! wget --spider "$host" >/dev/null 2>&1 ; then
echo "Cannot reach end-point for distribution information. Check your Internet connection and try again. Exiting..." echo "Cannot reach end-point for distribution information. Check your Internet connection and try again. Exiting..."
log "DEBUG" "Upstream end-point is cannot be contacted. Potential Internet connection issue or end-point address ($dist_upstream). Exiting...(3)" "$log_file" log "DEBUG" "Upstream end-point is cannot be contacted. Potential Internet connection issue or end-point address ($dist_upstream). Exiting...(3)" "$log_file"
[[ ! $nn_flag -eq 0 ]] && notify "Microsoft Edge (Beta) Update" "There seems to be an issue connecting to the end-point of Microsoft. Check your Internet connection and try again."
exit 3 exit 3
fi fi
log "DEBUG" "Host is known and reachable. Continuing update to fetch data..." "$log_file" log "DEBUG" "Host is known and reachable. Continuing update to fetch data..." "$log_file"
@ -210,6 +232,7 @@ if [[ $upgradeable -eq 1 ]] ; then
script_time="$(($elapsed_time / 60))m $(($elapsed_time % 60))s" script_time="$(($elapsed_time / 60))m $(($elapsed_time % 60))s"
echo "Most recent version of Microsoft Edge (Beta) [v$current_version] is already installed on this system, no need to update. Finished in $script_time" echo "Most recent version of Microsoft Edge (Beta) [v$current_version] is already installed on this system, no need to update. Finished in $script_time"
log "DEBUG" "Most recent version of Microsoft Edge (Beta) is already installed to the system (v$current_version). Completed in $script_time. Exiting...(1)" "$log_file" log "DEBUG" "Most recent version of Microsoft Edge (Beta) is already installed to the system (v$current_version). Completed in $script_time. Exiting...(1)" "$log_file"
[[ ! $nn_flag -eq 0 ]] && notify "Microsoft Edge (Beta) Update" "There is no newer version of Microsoft Edge (Beta) to install. Newest version is already installed: v$current_version"
exit 1 exit 1
fi fi
@ -251,6 +274,7 @@ log "DEBUG" "Checking the following release version file end-point is accessible
if ! wget --spider "$url" >/dev/null 2>&1 ; then if ! wget --spider "$url" >/dev/null 2>&1 ; then
echo "Cannot reach end-point for latest release. Check your Internet connection and try again." echo "Cannot reach end-point for latest release. Check your Internet connection and try again."
log "DEBUG" "Pool end-point is cannot be contacted. Potential Internet connection issue or end-point address ($url). Exiting...(5)" "$log_file" log "DEBUG" "Pool end-point is cannot be contacted. Potential Internet connection issue or end-point address ($url). Exiting...(5)" "$log_file"
[[ ! $nn_flag -eq 0 ]] && notify "Microsoft Edge (Beta) Update" "There seems to be an issue connecting to the end-point of Microsoft. Check your Internet connection and try again."
exit 5 exit 5
fi fi
log "DEBUG" "Pool is known and reachable. Beginning download..." "$log_file" log "DEBUG" "Pool is known and reachable. Beginning download..." "$log_file"
@ -286,6 +310,8 @@ else
log "DEBUG" "File already downloaded to system. Skipped download and verifying checksum..." "$log_file" log "DEBUG" "File already downloaded to system. Skipped download and verifying checksum..." "$log_file"
printf "Latest Debian release file is downloaded to the system, skipping download. Validating checksums..." printf "Latest Debian release file is downloaded to the system, skipping download. Validating checksums..."
fi fi
## add the .deb file to garbage list
echo "$tmp_path/$filename" >> "$garbage"
## verify the downloaded file ## verify the downloaded file
if [[ "$(sha256sum "$tmp_path/$filename" | cut -d' ' -f1)" != "$(grep 'SHA256:.*' "$tmp_path/Release" | cut -d' ' -f2)" ]] ; then if [[ "$(sha256sum "$tmp_path/$filename" | cut -d' ' -f1)" != "$(grep 'SHA256:.*' "$tmp_path/Release" | cut -d' ' -f2)" ]] ; then
@ -293,6 +319,7 @@ if [[ "$(sha256sum "$tmp_path/$filename" | cut -d' ' -f1)" != "$(grep 'SHA256:.*
rm "$tmp_path/$filename" rm "$tmp_path/$filename"
log "DEBUG" "Checksum (SHA256) failed and integrity of file lost. File has been removed as either corrupt or hazardous/dangerous. Exiting...(7)" "$log_file" log "DEBUG" "Checksum (SHA256) failed and integrity of file lost. File has been removed as either corrupt or hazardous/dangerous. Exiting...(7)" "$log_file"
echo -e "failed.\nThe checksum (SHA256) failed for some reason and removed either because it was corrupt or dangerous. Exiting updater...\n" echo -e "failed.\nThe checksum (SHA256) failed for some reason and removed either because it was corrupt or dangerous. Exiting updater...\n"
[[ ! $nn_flag -eq 0 ]] && notify "Microsoft Edge (Beta) Update" "There was an issue with the checksum to verify the latest release. The Debian file has been automatically removed to prevent potential danger to the system."
exit 7 exit 7
fi fi
echo "complete." echo "complete."
@ -314,6 +341,8 @@ log "DEBUG" "Successfully extracted files from data.tar.gz." "$log_file"
echo "completed." echo "completed."
## etc/ directory only contains a cron daily directory, remove it ## etc/ directory only contains a cron daily directory, remove it
rm -r "$tmp_path/etc/" rm -r "$tmp_path/etc/"
## add data.tar.xz to the garbage list
echo "$tmp_path/data.tar.xz" >> "$garbage"
# compress archive the currently installed version of Edge (Beta) for restoration on failure # compress archive the currently installed version of Edge (Beta) for restoration on failure
## :@TODO: compress everything on the system for a backup. Remove it if installation of new version was successful ## :@TODO: compress everything on the system for a backup. Remove it if installation of new version was successful
@ -341,6 +370,9 @@ exit_code=$(cp -r "$tmp_path/usr" "/" >>"$log_file" 2>&1)
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 ; } 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 ; }
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
echo "$tmp_path/usr" >> "$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 ## :@TODO: validate new version has successfully installed properly - exit 12
@ -355,6 +387,7 @@ if [[ "$(microsoft-edge-beta --version | cut -d' ' -f3)" != "$release_version" ]
log "DEBUG" "System restoration was successful, safe to use; however, the update was still failed. Exiting...(11)" "$log_file" log "DEBUG" "System restoration was successful, safe to use; however, the update was still failed. Exiting...(11)" "$log_file"
echo -e "completed. However, there was still an issue trying to update Microsoft Edge (Beta) to the latest version." \ 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..." "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
@ -362,4 +395,5 @@ fi
elapsed_time=$SECONDS elapsed_time=$SECONDS
script_time="$(($elapsed_time / 60))m $(($elapsed_time % 60))s" script_time="$(($elapsed_time / 60))m $(($elapsed_time % 60))s"
log "DEBUG" "Installation and update was successful (completed in: $script_time) and system can be cleaned up to remove any waste." "$log_file" log "DEBUG" "Installation and update was successful (completed in: $script_time) and system can be cleaned up to remove any waste." "$log_file"
echo "Microsoft Edge (Beta) has been successfully updated to the latest version: v$release_version, in $script_time." echo "Microsoft Edge (Beta) has been successfully updated to the latest version: v$current_version -> v$release_version, in $script_time."
[[ ! $nn_flag -eq 0 ]] && notify "Microsoft Edge (Beta) Update" "Successfully updated Microsoft Edge (Beta) to the latest version: v$release_version"