Skip to content

Commit db54392

Browse files
authored
Merge pull request #4404 from flatcar/chewi/distfiles-mirror
Mirror distfiles to Cloudflare on the fly when S3 credentials are present
2 parents 4dd1817 + 052753f commit db54392

13 files changed

Lines changed: 156 additions & 61 deletions

File tree

‎bootstrap_sdk‎

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,6 @@ EOF
5454
catalyst_stage_default 4
5555
}
5656

57-
# Switch to HTTP because early boostrap stages do not have SSL support.
58-
GENTOO_MIRRORS=$(portageq envvar GENTOO_MIRRORS)
59-
GENTOO_MIRRORS="${GENTOO_MIRRORS//https:\/\//http://}"
60-
export GENTOO_MIRRORS
61-
6257
catalyst_init "$@"
6358
ROOT_OVERLAY=${TEMPDIR}/stage4_overlay
6459

‎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/release.sh‎

Lines changed: 16 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -156,41 +156,26 @@ function copy_from_bincache_to_bucket() {
156156
local arch="${2}"
157157
local version="${3}"
158158

159-
rclone_configuration_file="$(mktemp)"
160-
chmod 600 "${rclone_configuration_file}"
161-
162-
(
163-
trap "rm -f ${rclone_configuration_file}" EXIT
164-
echo "${RCLONE_CONFIGURATION_FILE}" | base64 --decode > "${rclone_configuration_file}"
165-
166-
echo "Copying the images from bincache to CloudFlare bucket"
167-
docker run --rm --net host \
168-
-v "${rclone_configuration_file}:/opt/rclone.conf:ro" \
169-
docker.io/rclone/rclone:1.71.1 \
170-
--config "/opt/rclone.conf" \
159+
echo "Copying the images from bincache to Cloudflare bucket"
160+
docker run --rm --net host --env-file="sdk_lib/env_rclone.txt" \
161+
docker.io/rclone/rclone:1.76.0 \
171162
sync \
172-
--http-url "https://${BUILDCACHE_SERVER}/images/${arch}/${version}" :http: "r2:flatcar/${channel}/${arch}-usr/${version}" || { echo "ERROR: Skipping images copy to bucket due to failure" ; }
163+
--http-url "https://${BUILDCACHE_SERVER}/images/${arch}/${version}" :http: ":s3:flatcar/${channel}/${arch}-usr/${version}" || { echo "ERROR: Skipping images copy to bucket due to failure" ; }
173164

174-
echo "Copying the binary packages from bincache to CloudFlare bucket"
175-
docker run --rm --net host \
176-
-v "${rclone_configuration_file}:/opt/rclone.conf:ro" \
177-
docker.io/rclone/rclone:1.71.1 \
178-
--config "/opt/rclone.conf" \
165+
echo "Copying the binary packages from bincache to Cloudflare bucket"
166+
docker run --rm --net host --env-file="sdk_lib/env_rclone.txt" \
167+
docker.io/rclone/rclone:1.76.0 \
179168
sync \
180-
--http-url "https://${BUILDCACHE_SERVER}/boards/${arch}-usr/${version}" :http: "r2:flatcar/mirror/boards/${arch}-usr/${version}" || { echo "ERROR: Skipping binary packages copy to bucket due to failure" ; }
169+
--http-url "https://${BUILDCACHE_SERVER}/boards/${arch}-usr/${version}" :http: ":s3:flatcar/mirror/boards/${arch}-usr/${version}" || { echo "ERROR: Skipping binary packages copy to bucket due to failure" ; }
181170

182171
# Only copy once the 'containers' artifacts
183172
if [ "${arch}" = "amd64" ]; then
184-
echo "Copying SDK and packages containers from bincache to CloudFlare bucket"
185-
docker run --rm --net host \
186-
-v "${rclone_configuration_file}:/opt/rclone.conf:ro" \
187-
docker.io/rclone/rclone:1.71.1 \
188-
--config "/opt/rclone.conf" \
173+
echo "Copying SDK and packages containers from bincache to Cloudflare bucket"
174+
docker run --rm --net host --env-file="sdk_lib/env_rclone.txt" \
175+
docker.io/rclone/rclone:1.76.0 \
189176
sync \
190-
--http-url "https://${BUILDCACHE_SERVER}/containers/${version}" :http: "r2:flatcar/mirror/containers/${version}" || { echo "ERROR: Skipping containers copy (SDK / packages) to bucket due to failure" ; }
177+
--http-url "https://${BUILDCACHE_SERVER}/containers/${version}" :http: ":s3:flatcar/mirror/containers/${version}" || { echo "ERROR: Skipping containers copy (SDK / packages) to bucket due to failure" ; }
191178
fi
192-
193-
)
194179
}
195180

196181
function publish_sdk() {
@@ -215,21 +200,11 @@ function publish_sdk() {
215200
docker push "${sdk_container_common_registry}/flatcar-sdk-${a}:${docker_sdk_vernum}"
216201
done
217202

218-
rclone_configuration_file="$(mktemp)"
219-
chmod 600 "${rclone_configuration_file}"
220-
221-
(
222-
trap "rm -f ${rclone_configuration_file}" EXIT
223-
echo "${RCLONE_CONFIGURATION_FILE}" | base64 --decode > "${rclone_configuration_file}"
224-
225-
echo "Copying the SDK from bincache to CloudFlare bucket"
226-
docker run --rm --net host \
227-
-v "${rclone_configuration_file}:/opt/rclone.conf:ro" \
228-
docker.io/rclone/rclone:1.71.1 \
229-
--config "/opt/rclone.conf" \
203+
echo "Copying the SDK from bincache to Cloudflare bucket"
204+
docker run --rm --net host --env-file="sdk_lib/env_rclone.txt" \
205+
docker.io/rclone/rclone:1.76.0 \
230206
sync \
231-
--http-url "https://${BUILDCACHE_SERVER}/sdk/amd64/${docker_sdk_vernum}" :http: "r2:flatcar/mirror/sdk/amd64/${docker_sdk_vernum}" || { echo "ERROR: Skipping SDK copy to bucket due to failure" ; }
232-
)
207+
--http-url "https://${BUILDCACHE_SERVER}/sdk/amd64/${docker_sdk_vernum}" :http: ":s3:flatcar/mirror/sdk/amd64/${docker_sdk_vernum}" || { echo "ERROR: Skipping SDK copy to bucket due to failure" ; }
233208
}
234209

235210
function _release_build_impl() {

‎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

0 commit comments

Comments
 (0)