2013-10-24 16:49:15 +02:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
# Usage: script/mirror update <COMMIT-RANGE>
|
|
|
|
# script/mirror stats
|
|
|
|
set -e
|
|
|
|
|
|
|
|
commit_range="${1?}"
|
|
|
|
|
|
|
|
eval "$(grep RUBY_BUILD_MIRROR_URL= ./bin/ruby-build | head -1)"
|
|
|
|
|
|
|
|
test_mirrored() {
|
|
|
|
curl -qsSfIL "$RUBY_BUILD_MIRROR_URL/$1" >/dev/null 2>&1
|
|
|
|
}
|
|
|
|
|
2014-04-15 16:45:57 +02:00
|
|
|
compute_sha2() {
|
|
|
|
local output="$(openssl dgst -sha256)"
|
2013-10-24 16:49:15 +02:00
|
|
|
echo "${output##* }" | tr '[A-Z]' '[a-z]'
|
|
|
|
}
|
|
|
|
|
|
|
|
download_package() {
|
|
|
|
curl -qsSfL -o "$2" "$1"
|
|
|
|
}
|
|
|
|
|
|
|
|
download_and_verify() {
|
|
|
|
local checksum
|
|
|
|
local url="$1"
|
|
|
|
local file="$2"
|
|
|
|
local expected="$3"
|
|
|
|
download_package "$url" "$file"
|
2014-04-15 16:45:57 +02:00
|
|
|
checksum="$(compute_sha2 < "$file")"
|
2013-10-24 16:49:15 +02:00
|
|
|
if [ "$checksum" != "$expected" ]; then
|
|
|
|
echo "Error: $url doesn't match its checksum $expected" >&2
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
changed_files() {
|
|
|
|
git diff --name-only --diff-filter=ACMR "$@"
|
|
|
|
}
|
|
|
|
|
|
|
|
potentially_new_packages() {
|
2013-10-24 17:57:52 +02:00
|
|
|
local files="$(changed_files "$1" -- ./share/ruby-build)"
|
|
|
|
[ -n "$files" ] && extract_urls $files
|
2013-10-24 16:49:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
extract_urls() {
|
2014-04-18 02:51:26 +02:00
|
|
|
$(type -p ggrep grep | head -1) -hoe 'http[^"]\+#[^"]\+' "$@" | sort | uniq
|
2013-10-24 16:49:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
update() {
|
|
|
|
local url
|
|
|
|
local checksum
|
|
|
|
local file
|
|
|
|
for url in $(potentially_new_packages "$1"); do
|
|
|
|
checksum="${url#*#}"
|
|
|
|
url="${url%#*}"
|
|
|
|
if test_mirrored "$checksum"; then
|
|
|
|
echo "Already mirrored: $url"
|
|
|
|
else
|
|
|
|
echo "Mirroring: $url"
|
|
|
|
file="${TMPDIR:-/tmp}/$checksum"
|
|
|
|
download_and_verify "$url" "$file" "$checksum"
|
|
|
|
./script/s3-put "$file" "${AMAZON_S3_BUCKET?}"
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
2014-09-08 18:14:43 +02:00
|
|
|
verify() {
|
|
|
|
local url
|
|
|
|
local checksum
|
|
|
|
local file
|
|
|
|
for url in $(potentially_new_packages "$1"); do
|
|
|
|
checksum="${url#*#}"
|
|
|
|
url="${url%#*}"
|
|
|
|
echo "Verifying checksum for $url"
|
|
|
|
file="${TMPDIR:-/tmp}/$checksum"
|
|
|
|
download_and_verify "$url" "$file" "$checksum"
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
2013-10-24 16:49:15 +02:00
|
|
|
stats() {
|
|
|
|
local packages=( $(extract_urls ./share/ruby-build/*) )
|
|
|
|
local total="${#packages[@]}"
|
|
|
|
local confirmed=0
|
|
|
|
local checksum
|
|
|
|
for url in "${packages[@]}"; do
|
|
|
|
checksum="${url#*#}"
|
|
|
|
if test_mirrored "$checksum"; then
|
|
|
|
confirmed="$((confirmed + 1))"
|
|
|
|
else
|
|
|
|
echo "failed: $url" >&2
|
|
|
|
fi
|
|
|
|
echo -n "."
|
|
|
|
done
|
|
|
|
echo
|
|
|
|
echo "$confirmed/$total mirrored"
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd="${1?}"
|
|
|
|
shift 1
|
|
|
|
"$cmd" "$@"
|