Pull request: scripts: imp install, sup wget

Closes #3637.

Squashed commit of the following:

commit 453094d9a016b0d5a7b7f584b2b492244703064d
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date:   Wed Feb 9 19:44:51 2022 +0300

    scripts: fix wget

commit d16f8d9ad4ac62ce0bc88bab7ceb24a4089e93c3
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date:   Wed Feb 9 19:17:22 2022 +0300

    scripts: imp install, sup wget
This commit is contained in:
Ainar Garipov 2022-02-09 19:51:26 +03:00
parent 6d0a43aad6
commit b290eddc70
1 changed files with 56 additions and 15 deletions

View File

@ -46,33 +46,31 @@ is_little_endian() {
# Function check_required checks if the required software is available on the # Function check_required checks if the required software is available on the
# machine. The required software: # machine. The required software:
# #
# curl
# unzip (macOS) / tar (other unixes) # unzip (macOS) / tar (other unixes)
# #
# curl/wget are checked in function configure.
check_required() { check_required() {
required_darwin="unzip" required_darwin="unzip"
required_unix="tar" required_unix="tar"
readonly required_darwin required_unix readonly required_darwin required_unix
# Split with space.
required="curl"
case "$os" case "$os"
in in
('freebsd'|'linux'|'openbsd') ('freebsd'|'linux'|'openbsd')
required="$required $required_unix" required="$required_unix"
;; ;;
('darwin') ('darwin')
required="$required $required_darwin" required="$required_darwin"
;; ;;
(*) (*)
# Generally shouldn't happen, since the OS has already been # Generally shouldn't happen, since the OS has already been validated.
# validated.
error_exit "unsupported operating system: '$os'" error_exit "unsupported operating system: '$os'"
;; ;;
esac esac
readonly required
# Don't use quotes to get word splitting. # Don't use quotes to get word splitting.
for cmd in ${required} for cmd in $required
do do
log "checking $cmd" log "checking $cmd"
if ! is_command "$cmd" if ! is_command "$cmd"
@ -189,6 +187,9 @@ set_os() {
('OpenBSD') ('OpenBSD')
os='openbsd' os='openbsd'
;; ;;
(*)
error_exit "unsupported operating system: '$os'"
;;
esac esac
fi fi
@ -240,6 +241,9 @@ set_cpu() {
fi fi
cpu="${cpu}_softfloat" cpu="${cpu}_softfloat"
;; ;;
(*)
error_exit "unsupported cpu type: $cpu"
;;
esac esac
fi fi
@ -297,6 +301,41 @@ fix_freebsd() {
fi fi
} }
# download_curl uses curl(1) to download a file. The first argument is the URL.
# The second argument is optional and is the output file.
download_curl() {
curl_output="${2:-}"
if [ "$curl_output" = '' ]
then
curl -L -S -s "$1"
else
curl -L -S -o "$curl_output" -s "$1"
fi
}
# download_wget uses wget(1) to download a file. The first argument is the URL.
# The second argument is optional and is the output file.
download_wget() {
wget_output="${2:--}"
wget --no-verbose -O "$wget_output" "$1"
}
# Function set_download_func sets the appropriate function for downloading
# files.
set_download_func() {
if is_command 'curl'
then
# Go on and use the default, download_curl.
return 0
elif is_command 'wget'
then
download_func='download_wget'
else
error_exit "either curl or wget is required to install AdGuard Home via this script"
fi
}
# Function set_sudo_cmd sets the appropriate command to run a command under # Function set_sudo_cmd sets the appropriate command to run a command under
# superuser privileges. # superuser privileges.
set_sudo_cmd() { set_sudo_cmd() {
@ -320,6 +359,7 @@ configure() {
set_os set_os
set_cpu set_cpu
fix_darwin fix_darwin
set_download_func
set_sudo_cmd set_sudo_cmd
check_out_dir check_out_dir
@ -384,11 +424,11 @@ rerun_with_root() {
log 'restarting with root privileges' log 'restarting with root privileges'
# Group curl together with an echo, so that if curl fails before producing # Group curl/wget together with an echo, so that if the former fails before
# any output, the echo prints an exit command for the following shell to # producing any output, the latter prints an exit command for the following
# execute to prevent it from getting an empty input and exiting with a zero # shell to execute to prevent it from getting an empty input and exiting
# code in that case. # with a zero code in that case.
{ curl -L -S -s "$script_url" || echo 'exit 1'; }\ { "$download_func" "$script_url" || echo 'exit 1'; }\
| $sudo_cmd sh -s -- -c "$channel" -C "$cpu" -O "$os" -o "$out_dir" "$r" "$u" "$v" | $sudo_cmd sh -s -- -c "$channel" -C "$cpu" -O "$os" -o "$out_dir" "$r" "$u" "$v"
# Exit the script. Since if the code of the previous pipeline is non-zero, # Exit the script. Since if the code of the previous pipeline is non-zero,
@ -401,7 +441,7 @@ rerun_with_root() {
download() { download() {
log "downloading package from $url -> $pkg_name" log "downloading package from $url -> $pkg_name"
if ! curl -s "$url" --output "$pkg_name" if ! "$download_func" "$url" "$pkg_name"
then then
error_exit "cannot download the package from $url into $pkg_name" error_exit "cannot download the package from $url into $pkg_name"
fi fi
@ -450,7 +490,7 @@ handle_existing() {
return 0 return 0
fi fi
if [ "$( ls -1 -A $agh_dir )" != '' ] if [ "$( ls -1 -A "$agh_dir" )" != '' ]
then then
log 'the existing AdGuard Home installation is detected' log 'the existing AdGuard Home installation is detected'
@ -522,6 +562,7 @@ cpu=''
os='' os=''
out_dir='/opt' out_dir='/opt'
pkg_ext='tar.gz' pkg_ext='tar.gz'
download_func='download_curl'
sudo_cmd='sudo' sudo_cmd='sudo'
parse_opts "$@" parse_opts "$@"