Skip to content

Commit 052753f

Browse files
committed
Mirror distfiles to Cloudflare on the fly when S3 creds are present
Care is taken to not include the credentials in command line arguments or write them to files. If mirroring fails for any reason, such as the credentials not being present because it's not CI, the script exits non-fatally. We don't want it to break builds. The requested URL is checked to ensure we don't upload something that we just downloaded from the same mirror. That would be slow and expensive. The downloaded file is checked against the Manifest file (BLAKE2B only) before Portage does its own check to ensure we don't mirror a corrupted file. I tried to implement this as a Portage phase hook so that it would only upload after Portage had checked against the Manifest, but I bumped into the network sandbox. It was also awkward for other reasons. Unlike before, files for the different Portage repositories are mirrored together under the same distfiles directory. There is no benefit in separating them, and this would incur a performance cost because Portage would sometimes have to check more than one location to find the file. Flatcar previously mirrored using the traditional flat layout with all the files in a single directory. Gentoo now uses the filename-hash layout, but this has little benefit over the flat layout when hosted on Cloudflare R2. However, I have chosen the content-hash layout because it handles different versions of files with the same filename, which could be useful when maintaining several Flatcar release channels. The hosted files are named with their digest rather than their regular filename, but this shouldn't be an issue, as Portage does the translation. This requires a distfiles/layout.conf file to be placed on the mirror. For future reference, it looks like this: [structure] 0=content-hash BLAKE2B 8 Signed-off-by: James Le Cuirot <jlecuirot@microsoft.com>
1 parent 044d109 commit 052753f

10 files changed

Lines changed: 134 additions & 13 deletions

File tree

‎bootstrap_sdk_container‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ fi
8181

8282
# bootstrap_sdk needs FLATCAR_SDK_VERSION set to the seed version
8383
failed=''
84-
./run_sdk_container "${cleanup[@]}" -V "$seed_version" -v "$target_version" \
84+
./run_sdk_container "${cleanup[@]}" -V "$seed_version" -v "$target_version" -e "${!RCLONE_S3_*}" \
8585
sudo -E ./bootstrap_sdk "${bootstrap_args[@]}" || failed=x
8686

8787
# Update versionfile to the actual SDK version

‎build_library/catalyst.sh‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,21 +62,22 @@ sharedir="/usr/share/catalyst"
6262
storedir="$CATALYST_ROOT"
6363
distdir="$DISTDIR"
6464
envscript="$TEMPDIR/catalystrc"
65+
envvars=[$(printf '"%s", ' $(< sdk_lib/env_rclone.txt))]
6566
port_logdir="$CATALYST_ROOT/log"
6667
repo_basedir="/mnt/host/source/src/third_party"
6768
repo_name="gentoo"
6869
EOF
6970
}
7071

7172
catalystrc() {
72-
local load=$((NUM_JOBS * 2))
73-
cat <<EOF
74-
export TERM='${TERM}'
73+
portageq envvar -v {FETCH,RESUME}COMMAND | sed 's:^:export :'
74+
local load=$((NUM_JOBS * 2))
75+
cat <<EOF
7576
export MAKEOPTS='--jobs=${NUM_JOBS} --load-average=${load}'
7677
export EMERGE_DEFAULT_OPTS="--verbose \$MAKEOPTS"
7778
export PORTAGE_USERNAME=portage
7879
export PORTAGE_GRPNAME=portage
79-
export GENTOO_MIRRORS='$(portageq envvar GENTOO_MIRRORS)'
80+
export FETCH_WRAPPER="/etc/portage/bin/fetch-wrapper"
8081
export ac_cv_posix_semaphores_enabled=yes
8182
EOF
8283
}
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
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+
--output-document|-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}" >&2 || :
108+
109+
echo >&2

‎ci-automation/packages.sh‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@
3838
#
3939
# 4. A file ../scripts.patch to apply with "git am -3" for the scripts repo.
4040
#
41+
# 5. RCLONE_S3_*. Environment variables. Credentials for mirroring Portage distfiles. See rclone's documentation.
42+
#
4143
# OUTPUT:
4244
#
4345
# 1. Exported container image "flatcar-packages-[ARCH]-[VERSION].tar.gz" with binary packages
@@ -91,7 +93,7 @@ function _packages_build_impl() {
9193
# Build packages; store packages in container
9294
failed=''
9395
./run_sdk_container -x ./ci-cleanup.sh -n "${packages_container}" -v "${vernum}" \
94-
-C "${sdk_image}" \
96+
-C "${sdk_image}" -e "${!RCLONE_S3_*}" \
9597
./build_packages --board="${arch}-usr" || failed=x
9698

9799
# Copy the build logs to a directory accessible for us.

‎ci-automation/sdk_bootstrap.sh‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@
4545
# 8. SEED_TARBALL. Environment variable. Overrides the default seed tarball
4646
# from the given seed SDK. A Gentoo stage3 tarball URL can be used here.
4747
#
48+
# 9. RCLONE_S3_*. Environment variables. Credentials for mirroring Portage distfiles. See rclone's documentation.
49+
#
4850
# OUTPUT:
4951
#
5052
# 1. SDK tarball (gentoo catalyst output) of the new SDK, pushed to buildcache.

‎sdk_container/src/third_party/coreos-overlay/profiles/coreos/base/make.defaults‎

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,13 @@ USE="${USE} bindist"
7171
ACCEPT_LICENSE="${ACCEPT_LICENSE} no-source-code
7272
linux-fw-redistributable freedist intel-ucode NVIDIA-2023 NVIDIA-2025"
7373

74-
# Favor our own mirrors over Gentoo's
75-
GENTOO_MIRRORS="
76-
https://mirror.release.flatcar-linux.net/coreos/
77-
http://distfiles.gentoo.org/
78-
"
74+
# Flatcar's mirror has sources from all recent CI builds. Ideally, we wouldn't
75+
# include Gentoo's mirror because we don't want to spam them with requests for
76+
# Flatcar-only sources, but their own gitweb instance is the upstream source for
77+
# many packages and is currently very unreliable. Other mirrors presumably
78+
# manage to download these after several tries. *DO NOT* add extra whitespace
79+
# here. If this is modified, you *MUST* update the fetch-wrapper script.
80+
GENTOO_MIRRORS="https://mirror.release.flatcar-linux.net https://distfiles.gentoo.org"
7981

8082
# The following files are modified by make_chroot, and we do not want to
8183
# clobber them. All other config files should not be protected, so we

‎sdk_container/src/third_party/coreos-overlay/profiles/coreos/base/profile.bashrc‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
CROS_BUILD_BOARD_TREE="${SYSROOT}/build"
44
CROS_ADDONS_TREE="/mnt/host/source/src/third_party/coreos-overlay/coreos"
55

6+
# Ensure sensitive credentials used for mirroring don't make it into the build
7+
# environment where they could be stolen. fetch-wrapper is unaffected by this.
8+
unset "${!RCLONE_S3_@}"
9+
610
# Are we merging for the board sysroot, or for the SDK, or for
711
# the images? Returns a string in a passed variable:
812
#

‎sdk_lib/Dockerfile.sdk-import‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ COPY --chown=sdk:sdk . /mnt/host/source/src/scripts
2929
RUN chown sdk:sdk /mnt/host/source
3030

3131
RUN --mount=type=bind,target=/mnt/host/source/src/scripts \
32-
cat /mnt/host/source/src/scripts/sdk_lib/env_{pass,keep}.txt | \
32+
cat /mnt/host/source/src/scripts/sdk_lib/env_{pass,keep,rclone}.txt | \
3333
sed -e '1i Defaults env_keep += " \\' -e 's:$: \\:' -e '$a "' | \
3434
install -m0440 /dev/stdin /etc/sudoers.d/90_env_keep
3535

‎setup_board‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@ sudo_clobber "${BOARD_ETC}/portage/make.conf" <<EOF
295295
# Settings derived from the host environment
296296
CBUILD="$(portageq envvar CHOST)"
297297
HOSTCC="$(portageq envvar CHOST)-gcc"
298-
$(portageq envvar -v DISTDIR GLSA_DIR MAKEOPTS PORTAGE_USERNAME FETCHCOMMAND RESUMECOMMAND | sed 's:\$:\\$:g')
298+
$(portageq envvar -v DISTDIR FETCH_WRAPPER GLSA_DIR MAKEOPTS PORTAGE_USERNAME FETCHCOMMAND RESUMECOMMAND | sed 's:\$:\\$:g')
299299
300300
# Board specific settings
301301
CHOST="${BOARD_CHOST}"

‎update_chroot‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ PORTAGE_USERNAME="${PORTAGE_USERNAME}"
9494
MAKEOPTS="--jobs=${NUM_JOBS} --load-average=$((NUM_JOBS * 2))"
9595
FETCHCOMMAND='wget -t 3 -T 60 --passive-ftp -U "Portage (Flatcar SDK ${FLATCAR_SDK_VERSION}, flatcar.org) distfile-fetch" -O "\\\${DISTDIR}/\\\${FILE}" "\\\${URI}"'
9696
RESUMECOMMAND='wget -c -t 3 -T 60 --passive-ftp -U "Portage (Flatcar SDK ${FLATCAR_SDK_VERSION}, flatcar.org) distfile-fetch" -O "\\\${DISTDIR}/\\\${FILE}" "\\\${URI}"'
97+
FETCH_WRAPPER="${BUILD_LIBRARY_DIR}/portage/bin/fetch-wrapper"
9798
9899
# Generally there isn't any need to add packages to @world by default.
99100
# You can use --select to override this.

0 commit comments

Comments
 (0)