[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.
This commit is contained in:
xumia 2022-03-14 11:20:20 +08:00 committed by GitHub
parent b73da484c4
commit 7178c668dd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 92 additions and 26 deletions

View File

@ -0,0 +1,5 @@
#!/bin/bash
. /usr/local/share/buildinfo/scripts/buildinfo_base.sh
APT_REAL_COMMAND=$(get_command apt) $(dirname "$0")/apt-get "$@"

View File

@ -3,36 +3,23 @@
INSTALL= INSTALL=
. /usr/local/share/buildinfo/scripts/buildinfo_base.sh . /usr/local/share/buildinfo/scripts/buildinfo_base.sh
REAL_COMMAND=$(get_command apt-get) REAL_COMMAND=$APT_REAL_COMMAND
[ -z "$REAL_COMMAND" ] && REAL_COMMAND=$(get_command apt-get)
if [ -z "$REAL_COMMAND" ]; then if [ -z "$REAL_COMMAND" ]; then
echo "The command apt-get does not exist." 1>&2 echo "The command apt-get does not exist." 1>&2
exit 1 exit 1
fi fi
VERSION_FILE="/usr/local/share/buildinfo/versions/versions-deb" INSTALL=$(check_apt_install)
if [ "$ENABLE_VERSION_CONTROL_DEB" == "y" ]; then COMMAND_INFO="Locked by command: $REAL_COMMAND $@"
for para in $@ if [ "$INSTALL" == y ]; then
do check_apt_version
if [[ "$para" != -* ]]; then lock_result=$(acquire_apt_installation_lock "$COMMAND_INFO" )
continue $REAL_COMMAND "$@"
fi command_result=$?
if [ ! -z "$INSTALL" ]; then [ "$lock_result" == y ] && release_apt_installation_lock
if [[ "$para" == *=* ]]; then exit $command_result
continue else
elif [[ "$para" == *=* ]]; then $REAL_COMMAND "$@"
continue
else
package=$para
if ! grep -q "^${package}=" $VERSION_FILE; then
echo "The version of the package ${package} is not specified."
exit 1
fi
fi
elif [[ "$para" == "install" ]]; then
INSTALL=y
fi
done
fi fi
$REAL_COMMAND "$@"

View File

@ -12,6 +12,7 @@ VERSION_DEB_PREFERENCE=$BUILDINFO_PATH/versions/01-versions-deb
WEB_VERSION_FILE=$VERSION_PATH/versions-web WEB_VERSION_FILE=$VERSION_PATH/versions-web
BUILD_WEB_VERSION_FILE=$BUILD_VERSION_PATH/versions-web BUILD_WEB_VERSION_FILE=$BUILD_VERSION_PATH/versions-web
REPR_MIRROR_URL_PATTERN='http:\/\/packages.trafficmanager.net\/debian' REPR_MIRROR_URL_PATTERN='http:\/\/packages.trafficmanager.net\/debian'
DPKG_INSTALLTION_LOCK_FILE=/tmp/.dpkg_installation.lock
. $BUILDINFO_PATH/config/buildinfo.config . $BUILDINFO_PATH/config/buildinfo.config
@ -182,6 +183,79 @@ run_pip_command()
return $result return $result
} }
# Check if the command is to install the debian packages
# The apt/apt-get command format: apt/apt-get [options] {update|install}
check_apt_install()
{
for para in "$@"
do
if [[ "$para" == -* ]]; then
continue
fi
if [[ "$para" == "install" ]]; then
echo y
fi
break
done
}
# Print warning message if a debian package version not specified when debian version control enabled.
check_apt_version()
{
VERSION_FILE="/usr/local/share/buildinfo/versions/versions-deb"
local install=$(check_apt_install "$@")
if [ "$ENABLE_VERSION_CONTROL_DEB" == "y" ] && [ "$install" == "y" ]; then
for para in "$@"
do
if [[ "$para" == -* ]]; then
continue
fi
if [[ "$para" == *=* ]]; then
continue
else
package=$para
if ! grep -q "^${package}=" $VERSION_FILE; then
echo "Warning: the version of the package ${package} is not specified." 1>&2
fi
fi
done
fi
}
acquire_apt_installation_lock()
{
local result=n
local wait_in_second=10
local count=60
local info="$1"
for ((i=1; i<=$count; i++)); do
if [ -f $DPKG_INSTALLTION_LOCK_FILE ]; then
local lock_info=$(cat $DPKG_INSTALLTION_LOCK_FILE || true)
echo "Waiting dpkg lock for $wait_in_second, $i/$count, info: $lock_info" 1>&2
sleep $wait_in_second
else
# Create file in an atomic operation
if (set -o noclobber; echo "$info">$DPKG_INSTALLTION_LOCK_FILE) &>/dev/null; then
result=y
break
else
echo "Failed to creat lock, Waiting dpkg lock for $wait_in_second, $i/$count, info: $lock_info" 1>&2
sleep $wait_in_second
fi
fi
done
echo $result
}
release_apt_installation_lock()
{
rm -f $DPKG_INSTALLTION_LOCK_FILE
}
ENABLE_VERSION_CONTROL_DEB=$(check_version_control "deb") ENABLE_VERSION_CONTROL_DEB=$(check_version_control "deb")
ENABLE_VERSION_CONTROL_PY2=$(check_version_control "py2") ENABLE_VERSION_CONTROL_PY2=$(check_version_control "py2")
ENABLE_VERSION_CONTROL_PY3=$(check_version_control "py3") ENABLE_VERSION_CONTROL_PY3=$(check_version_control "py3")