2013-10-24 16:49:15 +02:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
# Usage: script/mirror update <COMMIT-RANGE>
|
2014-09-08 18:15:52 +02:00
|
|
|
# script/mirror verify <COMMIT-RANGE>
|
2013-10-24 16:49:15 +02:00
|
|
|
# script/mirror stats
|
|
|
|
set -e
|
|
|
|
|
|
|
|
eval "$(grep RUBY_BUILD_MIRROR_URL= ./bin/ruby-build | head -1)"
|
|
|
|
|
2014-09-08 18:15:52 +02:00
|
|
|
help_text() {
|
|
|
|
sed -ne '/^#/!q;s/.\{1,2\}//;1d;p' < "$0"
|
|
|
|
}
|
|
|
|
|
2013-10-24 16:49:15 +02:00
|
|
|
test_mirrored() {
|
|
|
|
curl -qsSfIL "$RUBY_BUILD_MIRROR_URL/$1" >/dev/null 2>&1
|
|
|
|
}
|
|
|
|
|
2014-04-15 16:45:57 +02:00
|
|
|
compute_sha2() {
|
2021-07-16 17:26:30 +02:00
|
|
|
local output
|
|
|
|
output="$(openssl dgst -sha256)"
|
|
|
|
tr '[:upper:]' '[:lower:]' <<<"${output##* }"
|
2013-10-24 16:49:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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() {
|
2021-07-16 17:26:30 +02:00
|
|
|
local head="${1#*..}"
|
|
|
|
local files
|
|
|
|
IFS=$'\n' read -d '' -r -a files < <(changed_files "$1" -- ./share/ruby-build)
|
|
|
|
[ ${#files[@]} -gt 0 ] || return 0
|
|
|
|
extract_urls "${head:-HEAD}" -- "${files[@]}"
|
2013-10-24 16:49:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
extract_urls() {
|
2021-07-16 17:26:30 +02:00
|
|
|
git grep -hoe 'http[^"]\+#[^"]\+' "$@" | sort | uniq
|
2013-10-24 16:49:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
update() {
|
|
|
|
local url
|
|
|
|
local checksum
|
|
|
|
local file
|
2021-07-15 20:57:14 +02:00
|
|
|
local tmp_path
|
2013-10-24 16:49:15 +02:00
|
|
|
for url in $(potentially_new_packages "$1"); do
|
|
|
|
checksum="${url#*#}"
|
|
|
|
url="${url%#*}"
|
|
|
|
if test_mirrored "$checksum"; then
|
|
|
|
echo "Already mirrored: $url"
|
|
|
|
else
|
2021-07-15 20:57:14 +02:00
|
|
|
echo "Will mirror: $url"
|
|
|
|
[ -n "$tmp_path" ] || tmp_path="$(mktemp -d "${TMPDIR:-/tmp}/s3-sync.XXXXX")"
|
|
|
|
file="$tmp_path/$checksum"
|
2013-10-24 16:49:15 +02:00
|
|
|
download_and_verify "$url" "$file" "$checksum"
|
|
|
|
fi
|
|
|
|
done
|
2021-07-15 20:57:14 +02:00
|
|
|
if [ -n "$tmp_path" ]; then
|
|
|
|
echo "Uploading..."
|
2021-07-31 13:43:42 +02:00
|
|
|
aws s3 sync --acl=public-read --size-only --no-progress "$tmp_path" "s3://${AMAZON_S3_BUCKET?}"
|
2021-07-15 20:57:14 +02:00
|
|
|
fi
|
2013-10-24 16:49:15 +02:00
|
|
|
}
|
|
|
|
|
2014-09-08 18:14:43 +02:00
|
|
|
verify() {
|
|
|
|
local url
|
|
|
|
local checksum
|
|
|
|
local file
|
2021-07-15 20:57:14 +02:00
|
|
|
local status=0
|
2014-09-08 18:14:43 +02:00
|
|
|
for url in $(potentially_new_packages "$1"); do
|
|
|
|
checksum="${url#*#}"
|
|
|
|
url="${url%#*}"
|
|
|
|
echo "Verifying checksum for $url"
|
|
|
|
file="${TMPDIR:-/tmp}/$checksum"
|
2021-07-15 20:57:14 +02:00
|
|
|
download_and_verify "$url" "$file" "$checksum" || status=$?
|
2014-09-08 18:14:43 +02:00
|
|
|
done
|
2021-07-15 20:57:14 +02:00
|
|
|
return $status
|
2014-09-08 18:14:43 +02:00
|
|
|
}
|
|
|
|
|
2013-10-24 16:49:15 +02:00
|
|
|
stats() {
|
2021-07-16 17:26:30 +02:00
|
|
|
local packages
|
|
|
|
IFS=$'\n' read -d '' -r -a packages < <(extract_urls -- ./share/ruby-build/\*)
|
2013-10-24 16:49:15 +02:00
|
|
|
local total="${#packages[@]}"
|
|
|
|
local confirmed=0
|
|
|
|
local checksum
|
|
|
|
for url in "${packages[@]}"; do
|
|
|
|
checksum="${url#*#}"
|
|
|
|
if test_mirrored "$checksum"; then
|
2021-07-15 20:57:14 +02:00
|
|
|
: $((confirmed++))
|
2013-10-24 16:49:15 +02:00
|
|
|
else
|
|
|
|
echo "failed: $url" >&2
|
|
|
|
fi
|
|
|
|
echo -n "."
|
|
|
|
done
|
|
|
|
echo
|
|
|
|
echo "$confirmed/$total mirrored"
|
|
|
|
}
|
|
|
|
|
2014-09-08 18:15:52 +02:00
|
|
|
cmd="$1"
|
|
|
|
|
|
|
|
case "$cmd" in
|
|
|
|
update | verify | stats )
|
|
|
|
shift 1
|
|
|
|
"$cmd" "$@"
|
|
|
|
;;
|
|
|
|
-h | --help )
|
|
|
|
help_text
|
|
|
|
exit 0
|
|
|
|
;;
|
|
|
|
* )
|
|
|
|
help_text >&2
|
|
|
|
exit 1
|
|
|
|
;;
|
|
|
|
esac
|