Skip to content

Commit 7178c66

Browse files
authored
[Build]: Fix installing dpkg packages in parallel issue (#10175)
Why I did it Fix the debian packages installing in parallel issue. Add apt hook command to support apt to print no version control info.
1 parent b73da48 commit 7178c66

3 files changed

Lines changed: 92 additions & 26 deletions

File tree

src/sonic-build-hooks/hooks/apt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/bin/bash
2+
3+
. /usr/local/share/buildinfo/scripts/buildinfo_base.sh
4+
5+
APT_REAL_COMMAND=$(get_command apt) $(dirname "$0")/apt-get "$@"

src/sonic-build-hooks/hooks/apt-get

Lines changed: 13 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,36 +3,23 @@
33
INSTALL=
44
. /usr/local/share/buildinfo/scripts/buildinfo_base.sh
55

6-
REAL_COMMAND=$(get_command apt-get)
6+
REAL_COMMAND=$APT_REAL_COMMAND
7+
[ -z "$REAL_COMMAND" ] && REAL_COMMAND=$(get_command apt-get)
78
if [ -z "$REAL_COMMAND" ]; then
89
echo "The command apt-get does not exist." 1>&2
910
exit 1
1011
fi
1112

12-
VERSION_FILE="/usr/local/share/buildinfo/versions/versions-deb"
13-
if [ "$ENABLE_VERSION_CONTROL_DEB" == "y" ]; then
14-
for para in $@
15-
do
16-
if [[ "$para" != -* ]]; then
17-
continue
18-
fi
19-
if [ ! -z "$INSTALL" ]; then
20-
if [[ "$para" == *=* ]]; then
21-
continue
22-
elif [[ "$para" == *=* ]]; then
23-
continue
24-
else
25-
package=$para
26-
if ! grep -q "^${package}=" $VERSION_FILE; then
27-
echo "The version of the package ${package} is not specified."
28-
exit 1
29-
fi
30-
fi
31-
elif [[ "$para" == "install" ]]; then
32-
INSTALL=y
33-
fi
34-
done
13+
INSTALL=$(check_apt_install)
14+
COMMAND_INFO="Locked by command: $REAL_COMMAND $@"
15+
if [ "$INSTALL" == y ]; then
16+
check_apt_version
17+
lock_result=$(acquire_apt_installation_lock "$COMMAND_INFO" )
18+
$REAL_COMMAND "$@"
19+
command_result=$?
20+
[ "$lock_result" == y ] && release_apt_installation_lock
21+
exit $command_result
22+
else
23+
$REAL_COMMAND "$@"
3524
fi
3625

37-
38-
$REAL_COMMAND "$@"

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

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ 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
1414
REPR_MIRROR_URL_PATTERN='http:\/\/packages.trafficmanager.net\/debian'
15+
DPKG_INSTALLTION_LOCK_FILE=/tmp/.dpkg_installation.lock
1516

1617
. $BUILDINFO_PATH/config/buildinfo.config
1718

@@ -182,6 +183,79 @@ run_pip_command()
182183
return $result
183184
}
184185

186+
# Check if the command is to install the debian packages
187+
# The apt/apt-get command format: apt/apt-get [options] {update|install}
188+
check_apt_install()
189+
{
190+
for para in "$@"
191+
do
192+
if [[ "$para" == -* ]]; then
193+
continue
194+
fi
195+
196+
if [[ "$para" == "install" ]]; then
197+
echo y
198+
fi
199+
200+
break
201+
done
202+
}
203+
204+
# Print warning message if a debian package version not specified when debian version control enabled.
205+
check_apt_version()
206+
{
207+
VERSION_FILE="/usr/local/share/buildinfo/versions/versions-deb"
208+
local install=$(check_apt_install "$@")
209+
if [ "$ENABLE_VERSION_CONTROL_DEB" == "y" ] && [ "$install" == "y" ]; then
210+
for para in "$@"
211+
do
212+
if [[ "$para" == -* ]]; then
213+
continue
214+
fi
215+
216+
if [[ "$para" == *=* ]]; then
217+
continue
218+
else
219+
package=$para
220+
if ! grep -q "^${package}=" $VERSION_FILE; then
221+
echo "Warning: the version of the package ${package} is not specified." 1>&2
222+
fi
223+
fi
224+
done
225+
fi
226+
}
227+
228+
acquire_apt_installation_lock()
229+
{
230+
local result=n
231+
local wait_in_second=10
232+
local count=60
233+
local info="$1"
234+
for ((i=1; i<=$count; i++)); do
235+
if [ -f $DPKG_INSTALLTION_LOCK_FILE ]; then
236+
local lock_info=$(cat $DPKG_INSTALLTION_LOCK_FILE || true)
237+
echo "Waiting dpkg lock for $wait_in_second, $i/$count, info: $lock_info" 1>&2
238+
sleep $wait_in_second
239+
else
240+
# Create file in an atomic operation
241+
if (set -o noclobber; echo "$info">$DPKG_INSTALLTION_LOCK_FILE) &>/dev/null; then
242+
result=y
243+
break
244+
else
245+
echo "Failed to creat lock, Waiting dpkg lock for $wait_in_second, $i/$count, info: $lock_info" 1>&2
246+
sleep $wait_in_second
247+
fi
248+
fi
249+
done
250+
251+
echo $result
252+
}
253+
254+
release_apt_installation_lock()
255+
{
256+
rm -f $DPKG_INSTALLTION_LOCK_FILE
257+
}
258+
185259
ENABLE_VERSION_CONTROL_DEB=$(check_version_control "deb")
186260
ENABLE_VERSION_CONTROL_PY2=$(check_version_control "py2")
187261
ENABLE_VERSION_CONTROL_PY3=$(check_version_control "py3")

0 commit comments

Comments
 (0)