Skip to content

Commit 5e4a866

Browse files
authored
[Build] Support Debian snapshot mirror to improve build stability (#13097)
Why I did it [Build] Support Debian snapshot mirror to improve build stability It is to enhance the reproducible build, supports the Debian snapshot mirror. It guarantees all the docker images using the same Debian mirror snapshot and fixes the temporary build failure which is caused by remote Debain mirror indexes changed during the build. It is also to fix the version conflict issue caused by no fixed versions of some of the Debian packages. How I did it Add a new feature to support the Debian snapshot mirror. How to verify it
1 parent 22fcc76 commit 5e4a866

File tree

8 files changed

+77
-4
lines changed

8 files changed

+77
-4
lines changed

Makefile.work

+2
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,7 @@ $(shell \
198198
SONIC_VERSION_CACHE=$(SONIC_VERSION_CACHE) \
199199
SONIC_VERSION_CACHE_SOURCE=$(SONIC_VERSION_CACHE_SOURCE) \
200200
DBGOPT='$(DBGOPT)' \
201+
MIRROR_SNAPSHOT=$(MIRROR_SNAPSHOT) \
201202
scripts/generate_buildinfo_config.sh)
202203

203204
# Generate the slave Dockerfile, and prepare build info for it
@@ -533,6 +534,7 @@ SONIC_BUILD_INSTRUCTION := $(MAKE) \
533534
MIRROR_URLS=$(MIRROR_URLS) \
534535
MIRROR_SECURITY_URLS=$(MIRROR_SECURITY_URLS) \
535536
GZ_COMPRESS_PROGRAM=$(GZ_COMPRESS_PROGRAM) \
537+
MIRROR_SNAPSHOT=$(MIRROR_SNAPSHOT) \
536538
$(SONIC_OVERRIDE_BUILD_VARS)
537539

538540
.PHONY: sonic-slave-build sonic-slave-bash init reset

rules/config

+3
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,9 @@ TRUSTED_GPG_URLS = https://packages.trafficmanager.net/debian/public_key.gpg,htt
234234
# docker: docker base images
235235
SONIC_VERSION_CONTROL_COMPONENTS ?= none
236236

237+
# MIRROR_SNAPSHOT - support mirror snapshot flag
238+
MIRROR_SNAPSHOT ?= n
239+
237240
# SONIC_VERSION_CACHE allows the .deb,.py, wget, git, docker and go files to be stored in the cache path. This allows the submodules to
238241
# cache standard installation package and restored back to avoid the package download every time.
239242
# SONIC_VERSION_CACHE - Method of deb package caching

scripts/build_mirror_config.sh

+28
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,40 @@ CONFIG_PATH=$1
55
export ARCHITECTURE=$2
66
export DISTRIBUTION=$3
77

8+
DEFAULT_MIRROR_URL_PREFIX=http://packages.trafficmanager.net
9+
MIRROR_VERSION_FILE=files/build/versions/default/versions-mirror
10+
[ -f target/versions/default/versions-mirror ] && MIRROR_VERSION_FILE=target/versions/default/versions-mirror
11+
812
# The default mirror urls
913
DEFAULT_MIRROR_URLS=http://debian-archive.trafficmanager.net/debian/,http://packages.trafficmanager.net/debian/debian/
1014
DEFAULT_MIRROR_SECURITY_URLS=http://debian-archive.trafficmanager.net/debian-security/,http://packages.trafficmanager.net/debian/debian-security/
1115

16+
1217
# The debian-archive.trafficmanager.net does not support armhf, use debian.org instead
1318
if [ "$ARCHITECTURE" == "armhf" ]; then
1419
DEFAULT_MIRROR_URLS=http://deb.debian.org/debian/,http://packages.trafficmanager.net/debian/debian/
1520
DEFAULT_MIRROR_SECURITY_URLS=http://deb.debian.org/debian-security/,http://packages.trafficmanager.net/debian/debian-security/
1621
fi
1722

23+
if [ "$MIRROR_SNAPSHOT" == y ]; then
24+
if [ -f $MIRROR_VERSION_FILE ]; then
25+
DEBIAN_TIMESTAMP=$(grep "^debian==" $MIRROR_VERSION_FILE | tail -n 1 | sed 's/.*==//')
26+
DEBIAN_SECURITY_TIMESTAMP=$(grep "^debian-security==" $MIRROR_VERSION_FILE | tail -n 1 | sed 's/.*==//')
27+
elif [ -z "$DEBIAN_TIMESTAMP" ] || [ -z "$DEBIAN_SECURITY_TIMESTAMP" ]; then
28+
DEBIAN_TIMESTAMP=$(curl $DEFAULT_MIRROR_URL_PREFIX/snapshot/debian/latest/timestamp)
29+
DEBIAN_SECURITY_TIMESTAMP=$(curl $DEFAULT_MIRROR_URL_PREFIX/snapshot/debian-security/latest/timestamp)
30+
fi
31+
32+
DEFAULT_MIRROR_URLS=http://deb.debian.org/debian/,http://packages.trafficmanager.net/snapshot/debian/$DEBIAN_TIMESTAMP/
33+
DEFAULT_MIRROR_SECURITY_URLS=http://deb.debian.org/debian-security/,http://packages.trafficmanager.net/snapshot/debian-security/$DEBIAN_SECURITY_TIMESTAMP/
34+
35+
mkdir -p target/versions/default
36+
if [ ! -f target/versions/default/versions-mirror ]; then
37+
echo "debian==$DEBIAN_TIMESTAMP" > target/versions/default/versions-mirror
38+
echo "debian-security==$DEBIAN_SECURITY_TIMESTAMP" >> target/versions/default/versions-mirror
39+
fi
40+
fi
41+
1842
[ -z "$MIRROR_URLS" ] && MIRROR_URLS=$DEFAULT_MIRROR_URLS
1943
[ -z "$MIRROR_SECURITY_URLS" ] && MIRROR_SECURITY_URLS=$DEFAULT_MIRROR_SECURITY_URLS
2044

@@ -24,3 +48,7 @@ TEMPLATE=files/apt/sources.list.j2
2448
[ -f $CONFIG_PATH/sources.list.$ARCHITECTURE.j2 ] && TEMPLATE=$CONFIG_PATH/sources.list.$ARCHITECTURE.j2
2549

2650
MIRROR_URLS=$MIRROR_URLS MIRROR_SECURITY_URLS=$MIRROR_SECURITY_URLS j2 $TEMPLATE | sed '/^$/N;/^\n$/D' > $CONFIG_PATH/sources.list.$ARCHITECTURE
51+
if [ "$MIRROR_SNAPSHOT" == y ]; then
52+
# Set the snapshot mirror, and add the SET_REPR_MIRRORS flag
53+
sed -i -e "/^#*deb.*packages.trafficmanager.net/! s/^#*deb/#&/" -e "\$a#SET_REPR_MIRRORS" $CONFIG_PATH/sources.list.$ARCHITECTURE
54+
fi

scripts/generate_buildinfo_config.sh

+1
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,4 @@ echo "export SONIC_VERSION_CONTROL_COMPONENTS=$SONIC_VERSION_CONTROL_COMPONENTS"
1111
echo "export SONIC_VERSION_CACHE=${SONIC_VERSION_CACHE}" >> $BUILDINFO_CONFIG
1212
echo "export SONIC_VERSION_CACHE_SOURCE=${SONIC_VERSION_CACHE_SOURCE}" >> $BUILDINFO_CONFIG
1313
echo "export DISTRO=${DISTRO}" >> $BUILDINFO_CONFIG
14+
echo "export MIRROR_SNAPSHOT=$MIRROR_SNAPSHOT" >> $BUILDINFO_CONFIG

scripts/versions_manager.py

+6-3
Original file line numberDiff line numberDiff line change
@@ -562,12 +562,13 @@ def filter(self, ctypes=[]):
562562
module.filter(ctypes=ctypes)
563563

564564
def get_default_module(self):
565-
if DEFAULT_MODULE in self.modules:
566-
return self.modules[DEFAULT_MODULE]
565+
default_module = self.modules.get(DEFAULT_MODULE, VersionModule(DEFAULT_MODULE, []))
567566
ctypes = self.get_component_types()
568567
dists = self.get_dists()
569568
components = []
570569
for ctype in ctypes:
570+
if ctype in DEFAULT_OVERWRITE_COMPONENTS:
571+
continue
571572
if ctype == 'deb':
572573
for dist in dists:
573574
versions = self._get_versions(ctype, dist)
@@ -579,7 +580,9 @@ def get_default_module(self):
579580
common_versions = self._get_common_versions(versions)
580581
component = Component(self.verbose, common_versions, ctype)
581582
components.append(component)
582-
return VersionModule(self.verbose, DEFAULT_MODULE, components)
583+
module = VersionModule(self.verbose, DEFAULT_MODULE, components)
584+
module.overwrite(default_module, True, True)
585+
return module
583586

584587
def get_aggregatable_modules(self):
585588
modules = {}

slave.mk

+1
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ export CROSS_BUILD_ENVIRON
8787
export BLDENV
8888
export BUILD_WORKDIR
8989
export GZ_COMPRESS_PROGRAM
90+
export MIRROR_SNAPSHOT
9091

9192
###############################################################################
9293
## Utility rules

src/sonic-build-hooks/scripts/buildinfo_base.sh

+26-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ POST_VERSION_PATH=$BUILDINFO_PATH/post-versions
1111
VERSION_DEB_PREFERENCE=$BUILDINFO_PATH/versions/01-versions-deb
1212
WEB_VERSION_FILE=$VERSION_PATH/versions-web
1313
BUILD_WEB_VERSION_FILE=$BUILD_VERSION_PATH/versions-web
14-
REPR_MIRROR_URL_PATTERN='http:\/\/packages.trafficmanager.net\/debian'
14+
REPR_MIRROR_URL_PATTERN='http:\/\/packages.trafficmanager.net\/'
1515
DPKG_INSTALLTION_LOCK_FILE=/tmp/.dpkg_installation.lock
1616

1717
. $BUILDINFO_PATH/config/buildinfo.config
@@ -112,14 +112,39 @@ set_reproducible_mirrors()
112112
{
113113
# Remove the charater # in front of the line if matched
114114
local expression="s/^#\(.*$REPR_MIRROR_URL_PATTERN\)/\1/"
115+
# Add the character # in front of the line, if not match the URL pattern condition
116+
local expression2="/^#*deb.*$REPR_MIRROR_URL_PATTERN/! s/^#*deb/#&/"
117+
local expression3="\$a#SET_REPR_MIRRORS"
115118
if [ "$1" = "-d" ]; then
116119
# Add the charater # in front of the line if match
117120
expression="s/^deb.*$REPR_MIRROR_URL_PATTERN/#\0/"
121+
# Remove the character # in front of the line, if not match the URL pattern condition
122+
expression2="/^#*deb.*$REPR_MIRROR_URL_PATTERN/! s/^#(#*deb)/\1/"
123+
expression3="/#SET_REPR_MIRRORS/d"
118124
fi
119125

120126
local mirrors="/etc/apt/sources.list $(find /etc/apt/sources.list.d/ -type f)"
121127
for mirror in $mirrors; do
128+
if ! grep -iq "$REPR_MIRROR_URL_PATTERN" "$mirror"; then
129+
continue
130+
fi
131+
132+
# Make sure no duplicate operations on the mirror config file
133+
if ([ "$1" == "-d" ] && ! grep -iq "#SET_REPR_MIRRORS" "$mirror") ||
134+
([ "$1" != "-d" ] && grep -iq "#SET_REPR_MIRRORS" "$mirror"); then
135+
continue
136+
fi
137+
138+
# Enable or disable the reproducible mirrors
122139
$SUDO sed -i "$expression" "$mirror"
140+
141+
# Enable or disable the none reproducible mirrors
142+
if [ "$MIRROR_SNAPSHOT" == y ]; then
143+
$SUDO sed -ri "$expression2" "$mirror"
144+
fi
145+
146+
# Add or remove the SET_REPR_MIRRORS flag
147+
$SUDO sed -i "$expression3" "$mirror"
123148
done
124149
}
125150

src/sonic-build-hooks/scripts/collect_version_files

+10
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,13 @@ dpkg-query -W -f '${Package}==${Version}\n' | grep -Ev "${SKIP_VERSION_PACKAGE}"
2525
## Add the the packages purged
2626
[ -f $POST_VERSION_PATH/purge-versions-deb ] && cat $POST_VERSION_PATH/purge-versions-deb >> "${TARGET_PATH}/versions-deb-${DIST}-${ARCH}"
2727

28+
## Add mirror versions
29+
while read -r line; do
30+
mirror=$(echo "$line" | sed "s/.*\///" | sed "s/_InRelease.*//")
31+
date=$(date --date="$(echo "$line" | cut -d: -f3-)" +%Y-%m-%dT%H:%M:%SZ)
32+
echo "$mirror==$date" >> ${TARGET_PATH}/versions-mirror
33+
done < <(grep Date: /var/lib/apt/lists/*_InRelease 2>/dev/null)
34+
2835
## Print the unique and sorted result
2936
sort -u "${TARGET_PATH}/versions-deb-${DIST}-${ARCH}" -o "${TARGET_PATH}/versions-deb-${DIST}-${ARCH}"
3037
if [ -e "${TARGET_PATH}/versions-py2-${DIST}-${ARCH}" ]; then
@@ -33,5 +40,8 @@ fi
3340
if [ -e "${TARGET_PATH}/versions-py3-${DIST}-${ARCH}" ]; then
3441
sort -u "${TARGET_PATH}/versions-py3-${DIST}-${ARCH}" -o "${TARGET_PATH}/versions-py3-${DIST}-${ARCH}"
3542
fi
43+
if [ -e "${TARGET_PATH}/versions-mirror" ]; then
44+
sort -u "${TARGET_PATH}/versions-mirror" -o "${TARGET_PATH}/versions-mirror"
45+
fi
3646

3747
exit 0

0 commit comments

Comments
 (0)