|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +set -euo pipefail |
| 4 | +eval "set -- $1" |
| 5 | +"${@}" |
| 6 | + |
| 7 | +# Don't mirror restricted distfiles. This doesn't check mirror+ overrides, but |
| 8 | +# Flatcar will almost certainly never encounter one. |
| 9 | +for value in ${RESTRICT}; do |
| 10 | + [[ ${value} == mirror ]] && exit |
| 11 | +done |
| 12 | + |
| 13 | +if [[ ${1-} != wget ]]; then |
| 14 | + echo "* Fetched with a tool other than wget. Cannot mirror." >&2 |
| 15 | + exit |
| 16 | +fi |
| 17 | + |
| 18 | +while [[ 0 -lt $# ]]; do |
| 19 | + case $1 in |
| 20 | + --output-document=*) |
| 21 | + file=${1#*=} shift ;; |
| 22 | + -O) |
| 23 | + file=${2-}; shift 2 ;; |
| 24 | + -*) |
| 25 | + shift ;; |
| 26 | + *) |
| 27 | + url=$1; shift ;; |
| 28 | + esac |
| 29 | +done |
| 30 | + |
| 31 | +if [[ ${file-} != /*.__download__ || ${url-} != *://* ]]; then |
| 32 | + echo "* Could not parse wget arguments. Cannot mirror." >&2 |
| 33 | + exit |
| 34 | +fi |
| 35 | + |
| 36 | +if [[ ${url} == */layout.conf ]]; then |
| 37 | + # distfiles layout configuration shouldn't be mirrored. |
| 38 | + exit |
| 39 | +fi |
| 40 | + |
| 41 | +# GENTOO_MIRRORS is filtered by Portage. We cannot get the real value if it has |
| 42 | +# been overridden, so don't do that when the S3 credentials are set. |
| 43 | +GENTOO_MIRRORS=$(unset PORTAGE_REPOSITORIES; portageq envvar GENTOO_MIRRORS) |
| 44 | +GENTOO_MIRRORS=${GENTOO_MIRRORS%% *} |
| 45 | + |
| 46 | +# This shouldn't happen, but if it does, it would cost money, so double check. |
| 47 | +if [[ ${GENTOO_MIRRORS} != https://mirror.release.flatcar-linux.net ]]; then |
| 48 | + echo "* GENTOO_MIRRORS has been modified. Cannot mirror." >&2 |
| 49 | + exit |
| 50 | +fi |
| 51 | + |
| 52 | +if [[ ${url} == "${GENTOO_MIRRORS}"/distfiles/* ]]; then |
| 53 | + # Already mirrored. |
| 54 | + exit |
| 55 | +fi |
| 56 | + |
| 57 | +if ! [[ |
| 58 | + ${RCLONE_S3_PROVIDER-} == Cloudflare && \ |
| 59 | + ${RCLONE_S3_ACCESS_KEY_ID-} && \ |
| 60 | + ${RCLONE_S3_SECRET_ACCESS_KEY-} && \ |
| 61 | + ${RCLONE_S3_ENDPOINT-} \ |
| 62 | +]]; then |
| 63 | + echo "* Cloudflare credentials not present. Cannot mirror." >&2 |
| 64 | + exit |
| 65 | +fi |
| 66 | + |
| 67 | +basename=${file##*/} |
| 68 | +basename=${basename%.__download__} |
| 69 | + |
| 70 | +# Upstream Gentoo uses the filename-hash layout, but this has little benefit |
| 71 | +# over the flat layout when hosted on Cloudflare R2. However, the content-hash |
| 72 | +# layout handles different versions of files with the same filename, which could |
| 73 | +# be useful when maintaining several Flatcar release channels. |
| 74 | +path=$(python3 - "${EBUILD%/*}" "${basename}" <<-'EOF' |
| 75 | + import sys |
| 76 | + from portage.manifest import Manifest |
| 77 | + from portage.package.ebuild.fetch import ContentHashLayout, DistfileName |
| 78 | +
|
| 79 | + dir, file = sys.argv[1:] |
| 80 | + digests = Manifest(dir).getTypeDigests("DIST")[file] |
| 81 | + filename = DistfileName(file, digests=digests) |
| 82 | + print(ContentHashLayout("BLAKE2B", "8").get_path(filename)) |
| 83 | +EOF |
| 84 | +) || : |
| 85 | + |
| 86 | +if [[ -z ${path} ]]; then |
| 87 | + echo "* Could not determine content-hash path for ${basename}. Cannot mirror." >&2 |
| 88 | + exit |
| 89 | +fi |
| 90 | + |
| 91 | +# The last part of the path should be the BLAKE2B digest. |
| 92 | +if ! b2sum --check --status <<< "${path##*/} *${file}"; then |
| 93 | + echo "* Checksum mismatch for ${basename}. Refusing to mirror." >&2 |
| 94 | + exit |
| 95 | +fi |
| 96 | + |
| 97 | +echo "* Attempting to mirror ${basename} ..." >&2 |
| 98 | + |
| 99 | +curl \ |
| 100 | + --location \ |
| 101 | + --fail-with-body \ |
| 102 | + --aws-sigv4 "aws:amz:auto:s3" \ |
| 103 | + --variable %RCLONE_S3_ACCESS_KEY_ID \ |
| 104 | + --variable %RCLONE_S3_SECRET_ACCESS_KEY \ |
| 105 | + --expand-user "{{RCLONE_S3_ACCESS_KEY_ID}}:{{RCLONE_S3_SECRET_ACCESS_KEY}}" \ |
| 106 | + --upload-file "${file}" \ |
| 107 | + "${RCLONE_S3_ENDPOINT}/flatcar/mirror/distfiles/${path}" | cat >&2 || : |
| 108 | + |
| 109 | +echo >&2 |
0 commit comments