Skip to content

Commit 4546528

Browse files
committed
[Build] Support Debian snapshot mirror to improve build stability (sonic-net#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 5b31291 commit 4546528

File tree

7 files changed

+75
-4
lines changed

7 files changed

+75
-4
lines changed

rules/config

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

189+
# MIRROR_SNAPSHOT - support mirror snapshot flag
190+
MIRROR_SNAPSHOT ?= n
191+
189192
# SONiC docker registry
190193
#
191194
# Set the env variable ENABLE_DOCKER_BASE_PULL = y to enable pulling sonic-slave docker from registry

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
@@ -8,3 +8,4 @@ mkdir -p $BUILDINFO_PATH/buildinfo/config
88

99
echo "PACKAGE_URL_PREFIX=$PACKAGE_URL_PREFIX" > $BUILDINFO_CONFIG
1010
echo "SONIC_VERSION_CONTROL_COMPONENTS=$SONIC_VERSION_CONTROL_COMPONENTS" >> $BUILDINFO_CONFIG
11+
echo "export MIRROR_SNAPSHOT=$MIRROR_SNAPSHOT" >> $BUILDINFO_CONFIG

scripts/versions_manager.py

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

477477
def get_default_module(self):
478-
if DEFAULT_MODULE in self.modules:
479-
return self.modules[DEFAULT_MODULE]
478+
default_module = self.modules.get(DEFAULT_MODULE, VersionModule(DEFAULT_MODULE, []))
480479
ctypes = self.get_component_types()
481480
dists = self.get_dists()
482481
components = []
483482
for ctype in ctypes:
483+
if ctype in DEFAULT_OVERWRITE_COMPONENTS:
484+
continue
484485
if ctype == 'deb':
485486
for dist in dists:
486487
versions = self._get_versions(ctype, dist)
@@ -492,7 +493,9 @@ def get_default_module(self):
492493
common_versions = self._get_common_versions(versions)
493494
component = Component(common_versions, ctype)
494495
components.append(component)
495-
return VersionModule(DEFAULT_MODULE, components)
496+
module = VersionModule(DEFAULT_MODULE, components)
497+
module.overwrite(default_module, True, True)
498+
return module
496499

497500
def get_aggregatable_modules(self):
498501
modules = {}

slave.mk

+1
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ export IMAGE_DISTRO
7575
export IMAGE_DISTRO_DEBS_PATH
7676
export MULTIARCH_QEMU_ENVIRON
7777
export DOCKER_BASE_ARCH
78+
export MIRROR_SNAPSHOT
7879

7980
###############################################################################
8081
## 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
@@ -72,14 +72,39 @@ set_reproducible_mirrors()
7272
{
7373
# Remove the charater # in front of the line if matched
7474
local expression="s/^#\(.*$REPR_MIRROR_URL_PATTERN\)/\1/"
75+
# Add the character # in front of the line, if not match the URL pattern condition
76+
local expression2="/^#*deb.*$REPR_MIRROR_URL_PATTERN/! s/^#*deb/#&/"
77+
local expression3="\$a#SET_REPR_MIRRORS"
7578
if [ "$1" = "-d" ]; then
7679
# Add the charater # in front of the line if match
7780
expression="s/^deb.*$REPR_MIRROR_URL_PATTERN/#\0/"
81+
# Remove the character # in front of the line, if not match the URL pattern condition
82+
expression2="/^#*deb.*$REPR_MIRROR_URL_PATTERN/! s/^#(#*deb)/\1/"
83+
expression3="/#SET_REPR_MIRRORS/d"
7884
fi
7985

8086
local mirrors="/etc/apt/sources.list $(find /etc/apt/sources.list.d/ -type f)"
8187
for mirror in $mirrors; do
88+
if ! grep -iq "$REPR_MIRROR_URL_PATTERN" "$mirror"; then
89+
continue
90+
fi
91+
92+
# Make sure no duplicate operations on the mirror config file
93+
if ([ "$1" == "-d" ] && ! grep -iq "#SET_REPR_MIRRORS" "$mirror") ||
94+
([ "$1" != "-d" ] && grep -iq "#SET_REPR_MIRRORS" "$mirror"); then
95+
continue
96+
fi
97+
98+
# Enable or disable the reproducible mirrors
8299
$SUDO sed -i "$expression" "$mirror"
100+
101+
# Enable or disable the none reproducible mirrors
102+
if [ "$MIRROR_SNAPSHOT" == y ]; then
103+
$SUDO sed -ri "$expression2" "$mirror"
104+
fi
105+
106+
# Add or remove the SET_REPR_MIRRORS flag
107+
$SUDO sed -i "$expression3" "$mirror"
83108
done
84109
}
85110

src/sonic-build-hooks/scripts/collect_version_files

+10
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,13 @@ dpkg-query -W -f '${Package}==${Version}\n' >> "${TARGET_PATH}/versions-deb-${DI
1818
## Add the the packages purged
1919
[ -f $POST_VERSION_PATH/purge-versions-deb ] && cat $POST_VERSION_PATH/purge-versions-deb >> "${TARGET_PATH}/versions-deb-${DIST}-${ARCH}"
2020

21+
## Add mirror versions
22+
while read -r line; do
23+
mirror=$(echo "$line" | sed "s/.*\///" | sed "s/_InRelease.*//")
24+
date=$(date --date="$(echo "$line" | cut -d: -f3-)" +%Y-%m-%dT%H:%M:%SZ)
25+
echo "$mirror==$date" >> ${TARGET_PATH}/versions-mirror
26+
done < <(grep Date: /var/lib/apt/lists/*_InRelease 2>/dev/null)
27+
2128
## Print the unique and sorted result
2229
sort -u "${TARGET_PATH}/versions-deb-${DIST}-${ARCH}" -o "${TARGET_PATH}/versions-deb-${DIST}-${ARCH}"
2330
if [ -e "${TARGET_PATH}/versions-py2-${DIST}-${ARCH}" ]; then
@@ -26,5 +33,8 @@ fi
2633
if [ -e "${TARGET_PATH}/versions-py3-${DIST}-${ARCH}" ]; then
2734
sort -u "${TARGET_PATH}/versions-py3-${DIST}-${ARCH}" -o "${TARGET_PATH}/versions-py3-${DIST}-${ARCH}"
2835
fi
36+
if [ -e "${TARGET_PATH}/versions-mirror" ]; then
37+
sort -u "${TARGET_PATH}/versions-mirror" -o "${TARGET_PATH}/versions-mirror"
38+
fi
2939

3040
exit 0

0 commit comments

Comments
 (0)