diff --git a/updater b/updater index b8692b6..545d667 100755 --- a/updater +++ b/updater @@ -20,9 +20,9 @@ # and potentially both. # # # # Author: Ethan Smith-Coss # -# Version: 0.1.4 # +# Version: 0.2.0 # # Created: 2021-05-20T16:47+0100 # -# Last Modified: 2021-06-22T17:18+0100 # +# Last Modified: 2021-06-23T23:50+0100 # # # # #################################### # # # @@ -46,16 +46,19 @@ ## program name prog_name="Microsoft Edge Updater" ## version -version="0.1.4" +version="0.2.0, alpha" ## usage statement 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. -DISCLAIMER: Currently this program can only update the Beta release of Microsoft Edge for amd64 architectures. +DISCLAIMER: Currently this program has only updated Microsoft Edge for amd64 architectures, other architectures might be possible but not tested. Options: -nn, --no-notify Prevent notifications from being set after a session. - --mode=[debug] Set the mode of the program to be ran in. Default mode occurs without this flag. + -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. + --mode= Set the mode of the program to be ran in. Default mode occurs without this flag. * debug - run program in debug mode. -y, --yes-all Assume yes to all yes/no decisions. -v, --version Print out the version of the script and exit. @@ -79,7 +82,7 @@ SECONDS=0 function clean_up { log "DEBUG" "EXIT signal was raised, cleaning up system after session before exiting..." "$log_file" printf "Cleaning up system after session..." - cp "$log_file" "$logs_dir/updater.log" # && rm "$log_file" + cp "$log_file" "$logs_dir/updater.log" while read -r garbage_collection ; do [[ -f "$garbage_collection" || -d "$garbage_collection" ]] && rm -r "$garbage_collection" @@ -106,9 +109,9 @@ function check_restoration { "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." \ + :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..." - [[ ! $nn_flag -eq 0 ]] && notify "Microsoft Edge (Beta) Update" "There was an issue installing the update to your system." \ + [[ ! $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" } @@ -116,6 +119,20 @@ function check_restoration { ## iterate over all user inputs while test $# -gt 0 ; do case $1 in + -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) + ## if short flag, check if the next value is beta or dev, set flag respectfully if so + [[ "$1" == "-c" && "$2" == "beta" || "$2" == "dev" ]] && edge_channel="$2" + shift + ;; -y | --yes-all) yes_flag=0 ; shift ;; @@ -127,13 +144,6 @@ while test $# -gt 0 ; do echo "$prog_name ($version)" exit 0 ;; - -nn | --no-notify) - nn_flag=0 ; shift - ;; - --mode=*) - [[ "$1" == "--mode=debug" ]] && debug_run=0 - shift - ;; *) shift ;; @@ -160,13 +170,6 @@ nn_flag=${nn_flag:-1} 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" -## check if Microsoft Edge (Beta) is already installed -command -v microsoft-edge-beta &>/dev/null -exit_code=$? -[[ ! $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) is installed to system. Proceeding with update..." "$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" @@ -177,6 +180,19 @@ log "ERROR" "Cannot find source.list in runtime directory ($source_list). Exitin log "DEBUG" "Found a souce.list in $runtime_dir." "$log_file" 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." ; +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..." ; + 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" @@ -184,7 +200,7 @@ echo "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..." 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." + [[ ! $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 @@ -193,7 +209,7 @@ 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 '/') -archi=$(grep -o "\[dists.*\]" "$source_list" | sed -E 's/\[dists=(.*)\]/\1/') +archi=$(grep -o "\[dists.*\]" "$source_list" | sed -E 's/\[dists=(.*)\]/\1/' | cut -d, -f1) log "DEBUG" "Fetching Packages file from $dist_upstream for $archi architectures..." "$log_file" ## 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" @@ -217,7 +233,7 @@ log "DEBUG" "Successfully uncompressed content." "$log_file" ## get the most recent package entry from Packages list for Beta version of browser log "DEBUG" "Identifying the most recent package information from Packages catelogue..." "$log_file" ## find the earliest entry for Edge Beta release -start_point=$(grep -n 'Package:.*-beta' "$tmp_path/Packages" | tr '\n' ' ' | cut -d: -f1) +start_point=$(grep -n "Package:.*-$edge_channel" "$tmp_path/Packages" | tr '\n' ' ' | cut -d: -f1) ## make a list of all lines which are blank - used to identify end of package information entry end_points=( $(grep -n '^\s*$' "$tmp_path/Packages" | sed 's/://g') ) ## loop over all potential end points @@ -244,7 +260,7 @@ sed 's/^/ (Release Info) /' "$tmp_path/Release" | sed -E 's/(^\s*\(Release In ### check dependences, and just ensure the user wants to go ahead with the update. ## get the current version number and the version in Release -current_version=$(microsoft-edge-beta --version | cut -d' ' -f3) +current_version=$(microsoft-edge-$edge_channel --version | cut -d' ' -f3) release_version=$(grep 'Version' "$tmp_path/Release" | sed -E 's/Version:\s*(.*)-.*/\1/') log "DEBUG" "Checking if there is an update available (release: $release_version) or if most recent version is already installed to the system..." "$log_file" ## compare the versions - using sort -V. @@ -260,33 +276,33 @@ 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 (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" - [[ ! $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" + 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 fi -log "DEBUG" "There is a more recent release of Microsoft Edge (Beta) available for download (v$release_version). Continuing with update." "$log_file" -echo "Identified a new release of Microsoft Edge (Beta) [Current: v$current_version. New: v$release_version]. Starting the download and installation process..." +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..." ## check if msedge process is already running is_running=$(ps -aux | grep -oc 'msedge') if [[ $is_running -gt 1 ]] ; then log "DEBUG" "Identified running processes for msedge. Determining how script should handle process..." "$log_file" if [[ ! $yes_flag -eq 0 ]] ; then - echo -n "Microsoft Edge (Beta) is already open. To continue update, the browser must be closed. Are you sure you wish to proceed? [(Y)es/No]: " && read -n 1 choice ; echo - [[ $(echo $choice | awk '{print tolower($0)}') == "n" ]] && { log "DEBUG" "User opted against allowing the script to close Microsoft Edge (Beta) by default. Exiting...(1)" "$log_file" ; exit 1 ; } + echo -n "Microsoft Edge ($edge_channel) is already open. To continue update, the browser must be closed. Are you sure you wish to proceed? [(Y)es/No]: " && read -n 1 choice ; echo + [[ $(echo $choice | awk '{print tolower($0)}') == "n" ]] && { log "DEBUG" "User opted against allowing the script to close Microsoft Edge ($edge_channel) by default. Exiting...(1)" "$log_file" ; exit 1 ; } fi - log "DEBUG" "Confirmation has been given to proceed with killing Microsoft Edge (Beta) processes. Killing msedge..." "$log_file" + log "DEBUG" "Confirmation has been given to proceed with killing Microsoft Edge ($edge_channel) processes. Killing msedge..." "$log_file" killall msedge exit_code=$? [[ ! $exit_code -eq 0 ]] && { log "ERROR" "Process is refusing to exit. Sending SIGKILL signal." "$log_file" ; pkill -9 msedge ; } fi -log "DEBUG" "Microsoft Edge (Beta) has been closed, either by yes flag enabled or by user decision. Process may not have been running thus never killed." "$log_file" +log "DEBUG" "Microsoft Edge ($edge_channel) has been closed, either by yes flag enabled or by user decision. Process may not have been running thus never killed." "$log_file" -# Download the newest version of Microsoft Edge (Beta) +# Download the newest version of Microsoft Edge ($edge_channel) ## get the end-point for .deb file location and filename pool=$(grep '\[pool\]' "$source_list" | cut -d' ' -f2) filename=$(grep 'Filename:.*' "$tmp_path/Release" | cut -d' ' -f2) @@ -305,7 +321,7 @@ log "DEBUG" "Checking the following release version file end-point is accessible if ! wget --spider "$url" >/dev/null 2>&1 ; then 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 (Beta) Update" "There seems to be an issue connecting to the end-point of Microsoft. Check your Internet connection and try again." + [[ ! $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" @@ -318,11 +334,11 @@ log "DEBUG" "Checking if user wishes to proceed with the update installation..." ## 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 (Beta) v$release_version will not be installed. Exiting updater..." ; + [[ "$(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..." ; 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 (Beta) [v$release_version]." "$log_file" +log "DEBUG" "Confirmation has been given to proceed with the following update of Microsoft Edge ($edge_channel) [v$release_version]." "$log_file" # download the debian file log "DEBUG" "Checking if the file is already downloaded to the system..." "$log_file" @@ -332,7 +348,7 @@ if [[ ! -f "$tmp_path/$filename" ]] ; then # 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 (Beta)." \ + [[ ! $exit_code -eq 0 ]] && { 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" @@ -351,7 +367,7 @@ if [[ "$(sha256sum "$tmp_path/$filename" | cut -d' ' -f1)" != "$(grep 'SHA256:.* 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" - [[ ! $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." + [[ ! $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." @@ -378,10 +394,10 @@ 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 ($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..." -archive_system "microsoft/msedge-beta" +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) @@ -404,7 +420,7 @@ if [[ ! $exit_code -eq 0 ]] ; then "$log_file" ## attempt restoration of system - restore_files "microsoft_msedge-beta" + restore_files "microsoft_msedge-$edge_channel" return_code=$? ## check the restoration was successful check_restoration $return_code 9 @@ -420,7 +436,7 @@ if [[ ! $exit_code -eq 0 ]] ; then "$log_file" ## attempt restoration of system - restore_files "microsoft_msedge-beta" + restore_files "microsoft_msedge-$edge_channel" return_code=$? ## check the restoration was successful check_restoration $return_code 9 @@ -434,12 +450,12 @@ echo "$tmp_path/usr" >> "$garbage" echo "$tmp_path/opt" >> "$garbage" ## confirm the update was successful - try to restore system otherwise -if [[ "$(microsoft-edge-beta --version | cut -d' ' -f3)" != "$release_version" ]] ; then +if [[ "$(microsoft-edge-$edge_channel --version | cut -d' ' -f3)" != "$release_version" ]] ; then 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..." ## attempt restoration of system - restore_files "microsoft_msedge-beta" + restore_files "microsoft_msedge-$edge_channel" return_code=$? ## check the restoration was successful check_restoration $return_code 11 @@ -451,7 +467,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 (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" +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 of script, clean up function is called now by default exit of script (EOF) +# exit from script, clean up function is called by default exit of script (EOF)