Updated updater

Added quiet operation
    - this will prevent STDOUT and STDERR from being displayed to the
      terminal, unless a decision is encountered - will not assume yes.
    - notifications are still sent since '--no-notify' can be used.

All echo statements responsible for outputting have been changed to
initial check if script is running in quiet mode.

'printf' statements which do not serve a specific purpose have been
changed to 'echo' commands.

'echo' statements that are responsible for displaying error messages are
now sent over STDERR instead of STDOUT.

Added '--full-clean' argument which will remove any possibly generated
files, by updater, from the system, including logs.
This commit is contained in:
Ethan Smith-Coss 2021-06-24 15:51:05 +01:00
parent 0f397879d7
commit 21889a7362

114
updater
View File

@ -22,7 +22,7 @@
# Author: Ethan Smith-Coss #
# Version: 0.2.0 #
# Created: 2021-05-20T16:47+0100 #
# Last Modified: 2021-06-23T23:50+0100 #
# Last Modified: 2021-06-24T15:49+0100 #
# #
# #################################### #
# #
@ -57,9 +57,11 @@ Options:
-nn, --no-notify Prevent notifications from being set after a session.
-c, --set-channel= Change the channel to be updated for Microsoft Edge. Will default to the value defined in souce.list if flag is not present.
* beta - update Beta channel release.
* dev - update Dev channel release.
* dev - update Dev channel release.
-q, --quiet Operate strictly with no output on STDOUT or STDERR, prompts will NOT assume yes however. Logging still occurs in background.
--mode= Set the mode of the program to be ran in. Default mode occurs without this flag.
* debug - run program in debug mode.
--full-clean Remove all content generated by updater after a session, this includes all logs.
-y, --yes-all Assume yes to all yes/no decisions.
-v, --version Print out the version of the script and exit.
-h, --help Print this help message and exit
@ -81,7 +83,7 @@ SECONDS=0
# function responsible for cleaning up after a session execution
function clean_up {
log "DEBUG" "EXIT signal was raised, cleaning up system after session before exiting..." "$log_file"
printf "Cleaning up system after session..."
[[ ! $quiet -eq 0 ]] && echo -n "Cleaning up system after session..."
cp "$log_file" "$logs_dir/updater.log"
while read -r garbage_collection ; do
@ -91,7 +93,7 @@ function clean_up {
log "CLEANER" "Finished cleaning up system after session. Nice and clean :D Goodbye." "$runtime_dir/logs/updater.log"
echo "done. Goodbye."
[[ ! $quiet -eq 0 ]] && echo "done. Goodbye."
elapsed_time=$SECONDS
script_time="$(($elapsed_time / 60))m $(($elapsed_time % 60))s"
@ -105,12 +107,12 @@ function clean_up {
# ensure that the restoration was sucessful in reverting the system back
function check_restoration {
[[ ! $1 -eq 0 ]] && { echo -e "failed.\nThere was an error trying to restore system files." \
[[ ! $1 -eq 0 ]] && { [[ ! $quiet -eq 0 ]] && echo "failed." && >&2 echo "There 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 ($edge_channel) to the latest version." \
"Consult $log_file for more information. Exiting..."
[[ ! $quiet -eq 0 ]] && echo "completed." && >&2 echo "However, there was still an issue trying to update Microsoft Edge" \
"($edge_channel) to the latest version. Consult $log_file for more information. Exiting..."
[[ ! $nn_flag -eq 0 ]] && notify "Microsoft Edge ($edge_channel) Update" "There was an issue installing the update to your system." \
"The previous version, v$current_version, has been restored."
echo "$backup_file" >>"$garbage"
@ -122,10 +124,6 @@ while test $# -gt 0 ; do
-nn | --no-notify)
nn_flag=0 ; shift
;;
--mode=*)
[[ "$1" == "--mode=debug" ]] && debug_run=0
shift
;;
-c | --set-channel=*)
## if long flag, check if value is equal to beta or dev, set flag respectfully if so
[[ "$1" =~ "beta" || "$1" =~ "dev" ]] && edge_channel=$(echo "$1" | cut -d= -f2)
@ -133,6 +131,20 @@ while test $# -gt 0 ; do
[[ "$1" == "-c" && "$2" == "beta" || "$2" == "dev" ]] && edge_channel="$2"
shift
;;
-q | --quiet)
quiet=0 ; shift
;;
--full-clean)
echo -n "Fully cleaning system of generated content..."
[[ -d "$tmp_path" ]] && rm -rf "$tmp_path"
[[ -d "$runtime_dir/logs" ]] && rm -rf "$runtime_dir/logs"
echo "done."
exit 0
;;
--mode=*)
[[ "$1" == "--mode=debug" ]] && debug_run=0
shift
;;
-y | --yes-all)
yes_flag=0 ; shift
;;
@ -151,7 +163,7 @@ while test $# -gt 0 ; do
done
## check if the script is being ran as root - exit otherwise
[[ $(id -u) -ne 0 ]] && { echo "Please run script with root privilages." ; exit 1 ; }
[[ $(id -u) -ne 0 ]] && { >&2 echo "Please run script with root privilages." ; exit 1 ; }
# set trap to trigger clean up function on any exit
trap clean_up EXIT
@ -160,6 +172,7 @@ source "$runtime_dir/utils/common"
log "PREINIT" "----[New instance of script has been started: $(date -Iseconds)]----" "$log_file"
echo "$log_file" >> "$garbage"
# initialise all flags which require a value
## 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"
@ -169,43 +182,46 @@ nn_flag=${nn_flag:-1}
## set the debugging flag if not already
debug_run=${debug_run:-1}
[[ $debug_run -eq 0 ]] && log "DEBUG" "The mode flag has been set so script will operate in debug mode." "$log_file"
## setup the quiet flag to determine if program should be slient
quiet=${quiet:-1}
log "DEBUG" "The quiet flag has been set to: $quiet." "$log_file"
## ensure source.list exists
source_list="$runtime_dir/source.list"
log "DEBUG" "Checking for a source.list in current runtime directory..." "$log_file"
printf "Checking for source.list..."
[[ ! -f "$source_list" ]] && { echo -e "\nUpdater encountered an issue: source.list not found. Exiting..." ;
[[ ! $quiet -eq 0 ]] && echo -n "Checking for source.list..."
[[ ! -f "$source_list" ]] && { [[ ! $quiet -eq 0 ]] && >&2 echo -e "\nUpdater encountered an issue: source.list not found. Exiting..." ;
log "ERROR" "Cannot find source.list in runtime directory ($source_list). Exiting...(2)" "$log_file" ; exit 2 ; }
log "DEBUG" "Found a souce.list in $runtime_dir." "$log_file"
echo "found."
[[ ! $quiet -eq 0 ]] && echo "found."
## set the channel flag
log "DEBUG" "Determining the channel of Edge and checking if it is installed to the system..."
edge_channel="${edge_channel:-$(grep -o "\[dists.*\]" "$source_list" | sed -E 's/\[dists=(.*)\]/\1/' | cut -d, -f2)}"
## if for some reason there is no value, exit 254
[[ "$edge_channel" == "" ]] && { echo "There is no channel specified for Microsoft Edge. Please check the source.list file has a correct value, or use --set-channel to set the channel for that session." ;
[[ "$edge_channel" == "" ]] && { [[ ! $quiet -eq 0 ]] && >&2 echo "There is no channel specified for Microsoft Edge. Please check the source.list file has a correct value, or use --set-channel to set the channel for that session." ;
log "ERROR" "Encountered an issue trying to determine the channel of Microsoft Edge to update on the system. source.list is potentially corrupt since no default value was found. Exiting...(254)" ; exit 254 ; }
## check if Microsoft Edge is already installed - :@Ethan: this is some Tom Foolery, Bashism Fuckery
command -v microsoft-edge-$edge_channel &>/dev/null
exit_code=$?
[[ ! $exit_code -eq 0 ]] && { echo "Microsoft Edge ($edge_channel) is not installed. Exiting updater..." ;
[[ ! $exit_code -eq 0 ]] && { [[ ! $quiet -eq 0 ]] && >&2 echo "Microsoft Edge ($edge_channel) is not installed. Exiting updater..." ;
log "DEBUG" "Microsoft Edge ($edge_channel) not recognised as an installed program. Exiting...(1)" ; exit 1 ; }
log "DEBUG" "Microsoft Edge ($edge_channel) is installed to system. Proceeding with update..." "$log_file"
## ensure host is reachable
host=$(grep "\[host\]" "$source_list" | cut -d' ' -f 2)
log "DEBUG" "Checking if host is available according to defined value of [host]: $host..." "$log_file"
echo "Checking to see if the host is known and reachable..."
[[ ! $quiet -eq 0 ]] && echo -n "Checking to see if the host is known and reachable..."
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..."
>&2 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"
[[ ! $nn_flag -eq 0 ]] && notify "Microsoft Edge ($edge_channel) Update" "There seems to be an issue connecting to the end-point of Microsoft. Check your Internet connection and try again."
exit 3
fi
log "DEBUG" "Host is known and reachable. Continuing update to fetch data..." "$log_file"
echo "Host known and reachable: (host) $host"
[[ ! $quiet -eq 0 ]] && echo "host known and reachable: (host) $host"
## identify the end-point URL for distribution information
dist_upstream=$(grep "\[dists.*\]" "$source_list" | cut -d' ' -f 2-4 --output-delimiter '/')
@ -214,14 +230,14 @@ log "DEBUG" "Fetching Packages file from $dist_upstream for $archi architectures
## fetch the file - :@Ethan: there's no reason to inform the user of this operation unless it fails
wget -a "$log_file" -O "$tmp_path/Packages.gz" "$dist_upstream/binary-$archi/Packages.gz"
exit_code=$?
[[ ! $exit_code -eq 0 ]] && { echo "There was an issue retrieving the package information for the update. Please check $logs_dir/updater.log, for more information. Exiting..." ;
[[ ! $exit_code -eq 0 ]] && { [[ ! $quiet -eq 0 ]] && >&2 echo "There was an issue retrieving the package information for the update. Please check $logs_dir/updater.log, for more information. Exiting..." ;
log "DEBUG" "Encountered an issue with wget. Exiting...(4)" "$log_file" ; exit 4 ; }
log "DEBUG" "Successfully downloaded release package information." "$log_file"
log "DEBUG" "Fetching Contents-$archi.gz from ${dist_upstream::-4}" "$log_file"
wget -a "$log_file" -O "$tmp_path/Contents-$archi.gz" "${dist_upstream::-4}/Contents-$archi.gz"
exit_code=$?
[[ ! $exit_code -eq 0 ]] && { echo "There was an issue retrieving the package contents for the update. Please check $log_file/updater.log, so more information. Exiting..." ;
[[ ! $exit_code -eq 0 ]] && { [[ ! $quiet -eq 0 ]] && >&2 echo "There was an issue retrieving the package contents for the update. Please check $log_file/updater.log, so more information. Exiting..." ;
log "ERROR" "Encountered an issue with wget. Exiting...(4)" "$log_file" ; exit 4 ; }
log "DEBUG" "Successfully downloaded release contents information." "$log_file"
## decompress the packages file
@ -276,14 +292,14 @@ upgradeable=${upgradeable:-1}
if [[ $upgradeable -eq 1 ]] ; then
elapsed_time=$SECONDS
script_time="$(($elapsed_time / 60))m $(($elapsed_time % 60))s"
echo "Most recent version of Microsoft Edge ($edge_channel) [v$current_version] is already installed on this system, no need to update. Finished in $script_time"
[[ ! $quiet -eq 0 ]] && >&2 echo "Most recent version of Microsoft Edge ($edge_channel) [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 ($edge_channel) is already installed to the system (v$current_version). Completed in $script_time. Exiting...(1)" "$log_file"
[[ ! $nn_flag -eq 0 ]] && notify "Microsoft Edge ($edge_channel) Update" "There is no newer version of Microsoft Edge ($edge_channel) to install. Newest version is already installed: v$current_version"
exit 1
exit 0
fi
log "DEBUG" "There is a more recent release of Microsoft Edge ($edge_channel) available for download (v$release_version). Continuing with update." "$log_file"
echo "Identified a new release of Microsoft Edge ($edge_channel) [Current: v$current_version. New: v$release_version]. Starting the download and installation process..."
[[ ! $quiet -eq 0 ]] && >&2 echo "Identified a new release of Microsoft Edge ($edge_channel) [Current: v$current_version. New: v$release_version]. Starting the download and installation process..."
## check if msedge process is already running
is_running=$(ps -aux | grep -oc 'msedge')
@ -319,13 +335,13 @@ log "DEBUG" "Checking the following release version file end-point is accessible
### spider argument.
if ! wget --spider "$url" >/dev/null 2>&1 ; then
echo "Cannot reach end-point for latest release. Check your Internet connection and try again."
[[ ! $quiet -eq 0 ]] && >&2 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"
[[ ! $nn_flag -eq 0 ]] && notify "Microsoft Edge ($edge_channel) Update" "There seems to be an issue connecting to the end-point of Microsoft. Check your Internet connection and try again."
exit 5
fi
log "DEBUG" "Pool is known and reachable. Beginning download..." "$log_file"
echo "Host known and reachable: (download) $url"
[[ ! $quiet -eq 0 ]] && echo "Host known and reachable: (download) $url"
## set filename to not be pool address but the name of file
filename=$(echo "$url" | rev | cut -d'/' -f1 | rev)
@ -333,10 +349,9 @@ filesize=$(grep '^Size:.*' "$tmp_path/Release" | cut -d' ' -f2)
log "DEBUG" "Checking if user wishes to proceed with the update installation..." "$log_file"
## inform the user of the new download size before downloading
if [[ $yes_flag -eq 1 ]] ; then
echo -n "The following package will be installed, $filename [$filesize]. Are you sure you wish to continue? [(Y)es/No]: " && read -n 1 choice
[[ "$(echo $choice | awk '{print tolower($0)}')" == "n" ]] && { echo -e "\nThe following update to Microsoft Edge ($edge_channel) v$release_version will not be installed. Exiting updater..." ;
echo -n "The following package will be installed, $filename [$filesize]. Are you sure you wish to continue? [(Y)es/No]: " && read -n 1 choice && echo
[[ "$(echo $choice | awk '{print tolower($0)}')" == "n" ]] && { [[ ! $quiet -eq 0 ]] && >&2 echo "The following update to Microsoft Edge ($edge_channel) v$release_version will not be installed. Exiting updater..." ;
log "DEBUG" "User opted out for updating from $current_version to $release_version. Exiting...(1)" "$log_file" ; exit 1 ; }
echo
fi
log "DEBUG" "Confirmation has been given to proceed with the following update of Microsoft Edge ($edge_channel) [v$release_version]." "$log_file"
@ -344,19 +359,19 @@ log "DEBUG" "Confirmation has been given to proceed with the following update of
log "DEBUG" "Checking if the file is already downloaded to the system..." "$log_file"
if [[ ! -f "$tmp_path/$filename" ]] ; then
log "DEBUG" "Downloading the latest release version [v$release_version], file: $filename (URL: $url)" "$log_file"
echo "Downloading the following release file: $filename. This may take a moment..."
[[ ! $quiet -eq 0 ]] && echo "Downloading the following release file: $filename. This may take a moment..."
# wget_timed=$(\time --format "%x:%e" wget -a "$tmp_path/wget_dump.log" -O "$tmp_path/$filename" "$url" 2>&1)
wget -a "$tmp_path/wget_dump.log" --progress=bar --show-progress -O "$tmp_path/$filename" "$url"
exit_code=$? # $(echo $wget_timed | cut -d: -f1)
[[ ! $exit_code -eq 0 ]] && { echo "There was an issue downloading the Debian version of Microsoft Edge ($edge_channel)." \
[[ ! $exit_code -eq 0 ]] && { [[ ! $quiet -eq 0 ]] && >&2 echo "There was an issue downloading the Debian version of Microsoft Edge ($edge_channel)." \
"Please check $logs_dir/updater.log, for more information. Exiting update..." ;
log "DEBUG" "Encountered an issue with wget. Exiting...(6)" "$log_file" ; exit 6 ; }
awk -v end=$(grep -n '^\s*$' "$tmp_path/wget_dump.log" | tr '\n' ' ' | cut -d: -f1) 'NR>1*0&&NR<1*end+1' "$tmp_path/wget_dump.log" >> "$log_file"
log "DEBUG" "Download completed successfully, wget log shown above." "$log_file"
printf "Download complete. Validating checksums..."
[[ ! $quiet -eq 0 ]] && echo -n "Download complete. Validating checksums..."
else
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..."
[[ ! $quiet -eq 0 ]] && echo -n "Latest Debian release file is downloaded to the system, skipping download. Validating checksums..."
fi
## add the .deb file to garbage list
echo "$tmp_path/$filename" >> "$garbage"
@ -366,42 +381,45 @@ if [[ "$(sha256sum "$tmp_path/$filename" | cut -d' ' -f1)" != "$(grep 'SHA256:.*
## automatically remove the file - :@Ethan: it's either corrupt or hazardous to the health of the system
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"
echo -e "failed.\nThe checksum (SHA256) failed for some reason and removed either because it was corrupt or dangerous. Exiting updater...\n"
[[ ! $quiet -eq 0 ]] && echo "failed." && \
>&2 echo "The checksum (SHA256) failed for some reason and removed either because it was corrupt or dangerous. Exiting updater..."
[[ ! $nn_flag -eq 0 ]] && notify "Microsoft Edge ($edge_channel) 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
fi
echo "complete."
[[ ! $quiet -eq 0 ]] && echo "complete."
# unarchive the .deb file and extract data.tar.gz
log "DEBUG" "Unarchiving the downloaded DEB file ($tmp_path/$filename)..." "$log_file"
printf "Unzipping download..."
[[ ! $quiet -eq 0 ]] && echo -n "Unzipping download..."
## use ar to unarchive .deb file
ar vx "$tmp_path/$filename" --output "$tmp_path" >/dev/null 2>&1
exit_code=$?
[[ ! $exit_code -eq 0 ]] && { echo -e "incomplete.\nThere was an issue unarchiving $filename. Exiting updater...\n" ;
[[ ! $exit_code -eq 0 ]] && { [[ ! $quiet -eq 0 ]] && echo "incomplete." && \
>&2 echo "There was an issue unarchiving $filename. Exiting updater...\n" ;
log "ERROR" "There was an issue when unarchiving $filename. Exiting...(8)" "$log_file" ; exit 8 ; }
log "DEBUG" "Successfully unarchived $filename, decompressing the data.tar.gz file..." "$log_file"
## extract data from data.tar.gz
tar --overwrite -xf "$tmp_path/data.tar.xz" --directory "$tmp_path" 1>"$tmp_path/tar_dump.log" 2>>"$log_file"
exit_code=$?
[[ ! $exit_code -eq 0 ]] && { echo -e "incomplete.\nThere was an extracting data.tar.gz. Exiting updater...\n" ;
[[ ! $exit_code -eq 0 ]] && { [[ ! $quiet -eq 0 ]] && echo "incomplete." && \
>&2 echo "There was an extracting data.tar.gz. Exiting updater..." ;
log "ERROR" "There was an issue when decompressing data.tar.gz using tar -xvf. Exiting...(8)" "$tmp_path" ; exit 8 ; }
log "DEBUG" "Successfully extracted files from data.tar.gz." "$log_file"
echo "completed."
[[ ! $quiet -eq 0 ]] && echo "completed."
## etc/ directory only contains a cron daily directory, remove it
rm -r "$tmp_path/etc/"
r[[ ! $quiet -eq 0 ]] && m -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 ($edge_channel) for restoration on failure
log "DEBUG" "Creating an archive gzip tarball of system /opt/ and /usr/ directories..." "$log_file"
printf "Installing new version to system. This may take a few minutes..."
[[ ! $quiet -eq 0 ]] && echo -n "Installing new version to system. This may take a few minutes..."
archive_system "microsoft/msedge-$edge_channel"
exit_code=$?
if [[ ! $exit_code -eq 0 ]] ; then
### :@Ethan: there was an issue with creating an archive, should probably inform the user to make a decision (unless -y is set)
echo -e "failed.\nThere was an issue creating a backup of the system. Exiting..."
[[ ! $quiet -eq 0 ]] && echo "failed." && >&2 echo "There was an issue creating a backup of the system. Exiting..."
log "ERROR" "There was an issue creating a gzip tarball of the system. Exiting...($exit_code)" "$log_file"
exit $exit_code
fi
@ -415,7 +433,8 @@ log "DEBUG" "Copying files to there appropriate location..." "$log_file"
cp -ar "$tmp_path/opt" "/" >>"$log_file" 2>&1
exit_code=$?
if [[ ! $exit_code -eq 0 ]] ; then
echo -ne "failed.\nThere was an issue installing files to the system. Reverting to previous version..."
[[ ! $quiet -eq 0 ]] && echo "failed." && \
>&2 echo -n "There was an issue installing files to the system. Reverting to previous version..."
log "DEBUG" "There was an issue copying $tmp_path/opt/ file to system /opt/. Reverting system files and exiting...(9)" \
"$log_file"
@ -431,7 +450,8 @@ fi
cp -ar "$tmp_path/usr" "/" >>"$log_file" 2>&1
exit_code=$?
if [[ ! $exit_code -eq 0 ]] ; then
echo -ne "failed.\nThere was an issue installing files to the system. Reverting to previous version..."
[[ ! $quiet -eq 0 ]] && echo "failed." && \
>&2 echo -n "There 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"
@ -444,7 +464,7 @@ if [[ ! $exit_code -eq 0 ]] ; then
exit 9
fi
log "DEBUG" "Successfully installed files to their appropriate location." "$log_file"
echo "installation complete."
[[ ! $quiet -eq 0 ]] && echo "installation complete."
## add directories usr/ and opt/ to garbage
echo "$tmp_path/usr" >> "$garbage"
echo "$tmp_path/opt" >> "$garbage"
@ -467,7 +487,7 @@ fi
elapsed_time=$SECONDS
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"
echo "Microsoft Edge ($edge_channel) has been successfully updated to the latest version: v$current_version -> v$release_version, in $script_time."
[[ ! $quiet -eq 0 ]] && echo "Microsoft Edge ($edge_channel) has been successfully updated to the latest version: v$current_version -> v$release_version, in $script_time."
[[ ! $nn_flag -eq 0 ]] && notify "Microsoft Edge ($edge_channel) Update" "Successfully updated Microsoft Edge ($edge_channel) to the latest version: v$release_version"
# exit from script, clean up function is called by default exit of script (EOF)