Skip to content

Commit f23d3c7

Browse files
authored
Merge pull request #3318 from joolswills/network_errors
Rework the checks and error messages displayed on network error
2 parents 27af2cd + 833721f commit f23d3c7

File tree

3 files changed

+74
-37
lines changed

3 files changed

+74
-37
lines changed

scriptmodules/admin/setup.sh

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -179,13 +179,13 @@ function package_setup() {
179179
local has_binary=0
180180
local has_net=0
181181

182-
local ip="$(getIPAddress)"
183-
[[ -n "$ip" ]] && has_net=1
182+
isConnected && has_net=1
184183

185184
# for modules with nonet flag that don't need to download data, we force has_net to 1, so we get install options
186185
hasFlag "${__mod_info[$id/flags]}" "nonet" && has_net=1
187186

188187
if [[ "$has_net" -eq 1 ]]; then
188+
dialog --backtitle "$__backtitle" --infobox "Checking for updates for $id ..." 3 60 >/dev/tty
189189
rp_hasBinary "$id"
190190
local ret="$?"
191191
[[ "$ret" -eq 0 ]] && has_binary=1
@@ -253,7 +253,7 @@ function package_setup() {
253253
options+=(S "${option_msgs[S]}")
254254
fi
255255
else
256-
status+="\nInstall options disabled (Unable to access internet)"
256+
status+="\nInstall options disabled:\n$__NET_ERRMSG"
257257
fi
258258

259259
if [[ "$is_installed" -eq 1 ]]; then
@@ -372,9 +372,8 @@ function section_gui_setup() {
372372
local pkgs=()
373373

374374
status="Please choose a package from below"
375-
local ip="$(getIPAddress)"
376-
if [[ -z "$ip" ]]; then
377-
status+="\nInstall options disabled (Unable to access internet)"
375+
if ! isConnected; then
376+
status+="\nInstall options disabled ($__NET_ERRMSG)"
378377
has_net=0
379378
fi
380379

scriptmodules/helpers.sh

Lines changed: 61 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1003,6 +1003,48 @@ function applyPatch() {
10031003
return 0
10041004
}
10051005
1006+
## @fn runCurl
1007+
## @params ... commandline arguments to pass to curl
1008+
## @brief Run curl with chosen parameters and handle curl errors
1009+
## @details Runs curl with the provided parameters, whilst also capturing the output and extracting
1010+
## any error message, which is stored in the global variable __NET_ERRMSG. Function returns the return
1011+
## code provided by curl. The environment variable __curl_opts can be set to override default curl
1012+
## parameters, eg - timeouts etc.
1013+
## @retval curl return value
1014+
function runCurl() {
1015+
local params=("$@")
1016+
# add any user supplied curl opts - timeouts can be overridden as curl uses the last parameters given
1017+
[[ -z "$__curl_opts" ]] && params+=($__curl_opts)
1018+
1019+
local cmd_err
1020+
local ret
1021+
1022+
# get the last non zero exit status (ignoring tee)
1023+
set -o pipefail
1024+
1025+
# set up additional file descriptor for stdin
1026+
exec 3>&1
1027+
1028+
# capture stderr - while passing both stdout and stderr to terminal
1029+
# curl like wget outputs the progress meter to stderr, so we will extract the error line later
1030+
cmd_err=$(curl "${params[@]}" 2>&1 1>&3 | tee /dev/stderr)
1031+
ret="$?"
1032+
1033+
# remove stdin copy
1034+
exec 3>&-
1035+
1036+
set +o pipefail
1037+
1038+
# if there was an error, extract it and put in __NET_ERRMSG
1039+
if [[ "$ret" -ne 0 ]]; then
1040+
# as we also capture the curl progress output, extract the last line which contains the error
1041+
__NET_ERRMSG="${cmd_err##*$'\n'}"
1042+
else
1043+
__NET_ERRMSG=""
1044+
fi
1045+
return "$ret"
1046+
}
1047+
10061048
## @fn download()
10071049
## @param url url of file
10081050
## @param dest destination name (optional), use - for stdout
@@ -1030,43 +1072,21 @@ function download() {
10301072
printMsgs "console" "Downloading $url to $dest ..."
10311073
params+=(-o "$dest")
10321074
fi
1033-
params+=(--connect-timeout 60 --speed-limit 1 --speed-time 60)
1034-
# add any user supplied curl opts - timeouts can be overridden as curl uses the last parameters given
1035-
[[ -z "$__curl_opts" ]] && params+=($__curl_opts)
1075+
params+=(--connect-timeout 10 --speed-limit 1 --speed-time 60)
10361076
# add the url
10371077
params+=("$url")
10381078
1039-
local cmd_err
10401079
local ret
1041-
1042-
# get the last non zero exit status (ignoring tee)
1043-
set -o pipefail
1044-
1045-
# capture stderr - while passing both stdout and stderr to terminal
1046-
# curl like wget outputs the progress meter to stderr, so we will extract the error line later
1047-
1048-
# set up additional file descriptor for stdin
1049-
exec 3>&1
1050-
1051-
cmd_err=$(curl "${params[@]}" 2>&1 1>&3 | tee /dev/stderr)
1080+
runCurl "${params[@]}"
10521081
ret="$?"
10531082
1054-
# remove stdin copy
1055-
exec 3>&-
1056-
1057-
set +o pipefail
1058-
10591083
# if download failed, remove file, log error and return error code
10601084
if [[ "$ret" -ne 0 ]]; then
10611085
# remove dest if not set to stdout and exists
10621086
[[ "$dest" != "-" && -f "$dest" ]] && rm "$dest"
1063-
# as we also capture the curl progress output, extract the last line which contains the error
1064-
cmd_err="${cmd_err##*$'\n'}"
1065-
1066-
md_ret_errors+=("URL $url failed to download.\n\n$cmd_err")
1067-
return "$ret"
1087+
md_ret_errors+=("URL $url failed to download.\n\n$__NET_ERRMSG")
10681088
fi
1069-
return 0
1089+
return "$ret"
10701090
}
10711091
10721092
## @fn downloadAndVerify()
@@ -1546,6 +1566,21 @@ function getIPAddress() {
15461566
[[ -n "$ip_route" ]] && grep -oP "src \K[^\s]+" <<< "$ip_route"
15471567
}
15481568
1569+
## @fn isConnected()
1570+
## @brief Simple check to see if there is a connection to the Internet.
1571+
## @details Uses the getIPAddress function to check if we have a route to the Internet. Also sets
1572+
## __NET_ERRMSG with an error message for use in packages / setup to display to the user if not.
1573+
## @retval 0 on success
1574+
## @retval 1 on failure
1575+
function isConnected() {
1576+
local ip="$(getIPAddress)"
1577+
if [[ -z "$ip" ]]; then
1578+
__NET_ERRMSG="Not connected to the Internet"
1579+
return 1
1580+
fi
1581+
return 0
1582+
}
1583+
15491584
## @fn adminRsync()
15501585
## @param src src folder on local system - eg "$__tmpdir/stats/"
15511586
## @param dest destination folder on remote system - eg "stats/"

scriptmodules/packages.sh

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ declare -A __sections=(
1919
[depends]="dependency"
2020
)
2121

22+
__NET_ERRMSG=""
23+
2224
function rp_listFunctions() {
2325
local id
2426
local desc
@@ -133,8 +135,7 @@ function rp_callModule() {
133135
local has_binary=0
134136
local has_net=0
135137
136-
local ip="$(getIPAddress)"
137-
[[ -n "$ip" ]] && has_net=1
138+
isConnected && has_net=1
138139
139140
# for modules with nonet flag that don't need to download data, we force has_net to 1
140141
hasFlag "${__mod_info[$id/flags]}" "nonet" && has_net=1
@@ -148,7 +149,7 @@ function rp_callModule() {
148149
149150
# fail if we don't seem to be connected
150151
if [[ "$has_net" -eq 0 ]]; then
151-
__ERRMSGS+=("Can't install/update $md_id - unable to connect to the internet")
152+
__ERRMSGS+=("Can't install/update $md_id - $__NET_ERRMSG")
152153
return 1
153154
fi
154155
@@ -424,7 +425,9 @@ function rp_getBinaryUrl() {
424425
function rp_remoteFileExists() {
425426
local url="$1"
426427
local ret
427-
curl --max-time 10 -o /dev/null -sfI "$url"
428+
# runCurl will cause stderr to be copied to output so we redirect both stdout/stderr to /dev/null.
429+
# any errors will have been captured by runCurl
430+
runCurl --max-time 10 --silent --show-error --fail --head "$url" &>/dev/null
428431
ret="$?"
429432
if [[ "$ret" -eq 0 ]]; then
430433
return 0
@@ -468,7 +471,7 @@ function rp_getFileDate() {
468471
[[ -z "$url" ]] && return 1
469472
470473
# get last-modified date stripping any CR in the output
471-
local file_date=$(curl -sfI --no-styled-output "$url" | tr -d "\r" | grep -ioP "last-modified: \K.+")
474+
local file_date=$(runCurl --silent --fail --head --no-styled-output "$url" | tr -d "\r" | grep -ioP "last-modified: \K.+")
472475
# if there is a date set in last-modified header, then convert to iso-8601 format
473476
if [[ -n "$file_date" ]]; then
474477
file_date="$(date -Iseconds --date="$file_date")"

0 commit comments

Comments
 (0)