apt-download: download idx to tmp dir then apply after success

This commit is contained in:
bigeagle 2016-05-16 14:36:48 +08:00
parent dda03ab110
commit 9763c67384
No known key found for this signature in database
GPG Key ID: 9171A4571C27920A

View File

@ -1,18 +1,15 @@
#!/bin/bash #!/bin/bash
set -e set -e
set -u
set -o pipefail set -o pipefail
LOADED_APT_DOWNLOAD="yes" LOADED_APT_DOWNLOAD="yes"
function check-and-download() { function check-and-download() {
remote_file=$1 remote_file=$1
local_file=$2 local_file=$2
timeout -s INT 60 wget -q --spider ${remote_file}
if [ $? -eq 0 ]; then
echo "downloading ${remote_file}" echo "downloading ${remote_file}"
timeout -s INT 300 wget -q -N -O ${local_file} ${remote_file} timeout -s INT 300 wget -q -N -O ${local_file} ${remote_file}
return return
fi
return 0
} }
@ -28,17 +25,25 @@ function apt-download-binary() {
fi fi
echo "Started mirroring ${base_url} ${dist}, ${repo}, ${arch}!" echo "Started mirroring ${base_url} ${dist}, ${repo}, ${arch}!"
dest_dir="${dest_base_dir}/dists/${dist}" dist_dir="${dest_base_dir}/dists/${dist}"
[ ! -d "$dest_dir" ] && mkdir -p "$dest_dir" [ ! -d "$dist_dir" ] && mkdir -p "$dist_dir"
check-and-download "${base_url}/dists/${dist}/Contents-${arch}.gz" "${dest_dir}/Contents-${arch}.gz" || true dist_tmp_dir="${dist_dir}/.tmp"
check-and-download "${base_url}/dists/${dist}/InRelease" "${dest_dir}/InRelease" || true [ ! -d "$dist_tmp_dir" ] && mkdir -p "$dist_tmp_dir"
check-and-download "${base_url}/dists/${dist}/Release" "${dest_dir}/Release" rm -rf ${dist_tmp_dir}/*
check-and-download "${base_url}/dists/${dist}/Release.gpg" "${dest_dir}/Release.gpg" || true
check-and-download "${base_url}/dists/${dist}/Contents-${arch}.gz" "${dist_tmp_dir}/Contents-${arch}.gz" || true
check-and-download "${base_url}/dists/${dist}/InRelease" "${dist_tmp_dir}/InRelease" || true
check-and-download "${base_url}/dists/${dist}/Release" "${dist_tmp_dir}/Release"
check-and-download "${base_url}/dists/${dist}/Release.gpg" "${dist_tmp_dir}/Release.gpg" || true
# Load Package Index URLs from Release file # Load Package Index URLs from Release file
release_file="${dest_dir}/Release" release_file="${dist_tmp_dir}/Release"
dest_dir="${dest_base_dir}/dists/${dist}/${repo}/binary-${arch}"
[ ! -d "$dest_dir" ] && mkdir -p "$dest_dir" pkgidx_dir="${dest_base_dir}/dists/${dist}/${repo}/binary-${arch}"
pkgidx_tmp_dir="${pkgidx_dir}/.tmp"
[ ! -d "$pkgidx_dir" ] && mkdir -p "$pkgidx_dir"
[ ! -d "$pkgidx_tmp_dir" ] && mkdir -p "$pkgidx_tmp_dir"
rm -rf ${pkgidx_tmp_dir}/*
declare pkgidx_content="" declare pkgidx_content=""
declare cnt_start=false declare cnt_start=false
@ -62,11 +67,16 @@ function apt-download-binary() {
filename=${tokens[2]} filename=${tokens[2]}
if [[ "$filename" =~ ${repo}/binary-${arch} ]]; then if [[ "$filename" =~ ${repo}/binary-${arch} ]]; then
# Load package list from Packages file # Load package list from Packages file
pkgidx_file="${dest_base_dir}/dists/${dist}/${filename}" pkgidx_filename=`basename $filename`
dest_dir=`dirname ${pkgidx_file}` pkgidx_file="${pkgidx_tmp_dir}/${pkgidx_filename}"
[ ! -d "$dest_dir" ] && mkdir -p "$dest_dir" # dest_dir=`dirname ${pkgidx_file}`
# [ ! -d "$dest_dir" ] && mkdir -p "$dest_dir"
pkglist_url="${base_url}/dists/${dist}/${filename}" pkglist_url="${base_url}/dists/${dist}/${filename}"
check-and-download "${pkglist_url}" ${pkgidx_file} || true check-and-download "${pkglist_url}" ${pkgidx_file} || {
printf "Failed to download: %s\n" ${pkglist_url}
return 1
}
echo "${checksum} ${pkgidx_file}" | ${checksum_cmd} -c - echo "${checksum} ${pkgidx_file}" | ${checksum_cmd} -c -
if [ -z "${pkgidx_content}" -a -f ${pkgidx_file} ]; then if [ -z "${pkgidx_content}" -a -f ${pkgidx_file} ]; then
echo "getting packages index content" echo "getting packages index content"
@ -127,6 +137,11 @@ function apt-download-binary() {
done done
done done
mv ${pkgidx_tmp_dir}/* ${pkgidx_dir}
mv ${dist_tmp_dir}/* ${dist_dir}
rmdir ${pkgidx_tmp_dir} ${dist_tmp_dir}
echo "Mirroring ${base_url} ${dist}, ${repo}, ${arch} done!" echo "Mirroring ${base_url} ${dist}, ${repo}, ${arch} done!"
} }