core: 完善 url 检测

This commit is contained in:
bin456789 2023-07-15 23:28:20 +08:00
parent 2c8875b78e
commit f2af2dfcfd
No known key found for this signature in database
GPG Key ID: EE301B386DE6C11B

View File

@ -29,7 +29,7 @@ error_and_exit() {
} }
curl() { curl() {
command curl --connect-timeout 10 --retry 5 --retry-delay 0 "$@" command curl --connect-timeout 5 --retry 3 --retry-delay 0 "$@"
} }
is_in_china() { is_in_china() {
@ -72,24 +72,28 @@ test_url() {
echo $url echo $url
tmp_file=/tmp/reinstall-img-test tmp_file=/tmp/reinstall-img-test
install_pkg file
http_code=$(curl -Ls -r 0-1048575 -w "%{http_code}" -o $tmp_file $url) http_code=$(command curl --connect-timeout 5 -Ls -r 0-1048575 -w "%{http_code}" -o $tmp_file $url)
if [ "$http_code" != 200 ] && [ "$http_code" != 206 ]; then if [ "$http_code" != 200 ] && [ "$http_code" != 206 ]; then
error_and_exit "$url not accessible" error "$url not accessible"
return 1
fi fi
# gzip的mime有很多种写法 if [ -n "$expect_type" ]; then
# centos7中显示为 x-gzip在其他系统中显示为 gzip可能还有其他 # gzip的mime有很多种写法
# 所以不用mime判断 # centos7中显示为 x-gzip在其他系统中显示为 gzip可能还有其他
# https://www.digipres.org/formats/sources/tika/formats/#application/gzip # 所以不用mime判断
# https://www.digipres.org/formats/sources/tika/formats/#application/gzip
# 有些 file 版本输出的是 # ISO 9660 CD-ROM filesystem data ,要去掉开头的井号 # 有些 file 版本输出的是 # ISO 9660 CD-ROM filesystem data ,要去掉开头的井号
real_type=$(file -b $tmp_file | sed 's/^# //' | cut -d' ' -f1 | tr '[:upper:]' '[:lower:]') install_pkg file
[ -n "$var_to_eval" ] && eval $var_to_eval=$real_type real_type=$(file -b $tmp_file | sed 's/^# //' | cut -d' ' -f1 | tr '[:upper:]' '[:lower:]')
[ -n "$var_to_eval" ] && eval $var_to_eval=$real_type
if ! echo $expect_type | grep -wo "$real_type"; then if ! echo $expect_type | grep -wo "$real_type"; then
error_and_exit "$url expect: $expect_type. real: $real_type." error "$url expect: $expect_type. real: $real_type."
return 1
fi
fi fi
} }
@ -203,7 +207,7 @@ setos() {
filename=$(curl $mirror | grep -oP "ubuntu-$releasever.*?-live-server-$basearch_alt.iso" | head -1) filename=$(curl $mirror | grep -oP "ubuntu-$releasever.*?-live-server-$basearch_alt.iso" | head -1)
iso=$mirror$filename iso=$mirror$filename
test_url $iso iso test_url $iso iso || exit 1
eval ${step}_iso=$iso eval ${step}_iso=$iso
eval ${step}_ks=$confhome/user-data eval ${step}_ks=$confhome/user-data
} }
@ -217,7 +221,7 @@ setos() {
if [ "$(echo "$image_name" | wc -w)" -lt 3 ]; then if [ "$(echo "$image_name" | wc -w)" -lt 3 ]; then
error_and_exit "--image-name wrong." error_and_exit "--image-name wrong."
fi fi
test_url $iso iso test_url $iso iso || exit 1
eval "${step}_iso='$iso'" eval "${step}_iso='$iso'"
eval "${step}_image_name='$image_name'" eval "${step}_image_name='$image_name'"
} }
@ -227,7 +231,7 @@ setos() {
if [ -z "$img" ]; then if [ -z "$img" ]; then
error_and_exit "dd need --img" error_and_exit "dd need --img"
fi fi
test_url $img 'xz|gzip' img_type test_url $img 'xz|gzip' img_type || exit 1
eval "${step}_img='$img'" eval "${step}_img='$img'"
eval "${step}_img_type='$img_type'" eval "${step}_img_type='$img_type'"
} }