Check APT Package Versions

Make sure to keep the cache up to date as package versions change
upstream. Also, allow specific package versions to be selected.
This commit is contained in:
Nate Bohman 2022-03-16 11:15:25 -06:00
parent c7ec949338
commit a56b21237f
No known key found for this signature in database
GPG key ID: C10546A54ABA1CE5
2 changed files with 41 additions and 24 deletions

View file

@ -31,7 +31,7 @@ for package in $packages; do
echo -n " Caching to $cache_filepath..." echo -n " Caching to $cache_filepath..."
# Pipe all package files (no folders) to Tar. # Pipe all package files (no folders) to Tar.
dpkg -L $package | dpkg -L "$(echo "${package}" | cut -d"=" -f1)" |
while IFS= read -r f; do while IFS= read -r f; do
if test -f $f; then echo ${f:1}; fi; #${f:1} removes the leading slash that Tar disallows if test -f $f; then echo ${f:1}; fi; #${f:1} removes the leading slash that Tar disallows
done | done |
@ -44,6 +44,7 @@ manifest_filepath="$cache_dir/manifest.log"
echo -n "Writing package manifest to $manifest_filepath..." echo -n "Writing package manifest to $manifest_filepath..."
manifest= manifest=
for package in $packages; do for package in $packages; do
package="$(echo "${package}" | cut -d"=" -f1)"
manifest=$manifest$package:$(dpkg -s $package | grep Version | awk '{print $2}'), manifest=$manifest$package:$(dpkg -s $package | grep Version | awk '{print $2}'),
done done
# Remove trailing comma. # Remove trailing comma.

View file

@ -1,40 +1,56 @@
#!/bin/bash #!/bin/bash
# Directory that holds the cached packages. # Directory that holds the cached packages.
cache_dir=$1 cache_dir="${1}"
# Version of the cache to create or load. # Version of the cache to create or load.
version=$2 version="${2}"
# List of the packages to use. # List of the packages to use.
packages=${@:3} packages="${@:3}"
# Sort these packages by name and split on commas.
packages=$(echo "${packages}" | sed 's/[\s,]+/ /g' | tr ' ' '\n' | sort | tr '\n' ' ')
# Create cache directory so artifacts can be saved. # Create cache directory so artifacts can be saved.
mkdir -p $cache_dir mkdir -p "${cache_dir}"
echo -n "Validating action arguments (version='$version', packages='$packages')..."; echo -n "Validating action arguments (version='${version}', packages='${packages}')...";
echo $version | grep -o " " > /dev/null
if [ $? -eq 0 ]; then if echo "${version}" | grep -q " " > /dev/null; then
echo "aborted." echo "aborted."
echo "Version value '$version' cannot contain spaces." >&2 echo "Version value '${version}' cannot contain spaces." >&2
exit 1 exit 1
fi fi
if [ "$packages" == "" ]; then if [ "${packages}" == "" ]; then
echo "aborted." echo "aborted."
echo "Packages argument cannot be empty." >&2 echo "Packages argument cannot be empty." >&2
exit 2 exit 2
fi fi
echo "done." echo "done."
echo -n "Updating APT package list..."
sudo apt-get update > /dev/null
echo "done."
echo -n "Verifying packages..." echo -n "Verifying packages..."
for package in $packages; do for package in ${packages}; do
escaped=$(echo $package | sed 's/+/\\+/g') if echo "${package}" | grep -q "="; then
apt-cache search ^$escaped$ | grep $package > /dev/null pkg_name=$(echo "${package}" | cut -d "=" -f1)
if [ $? -ne 0 ]; then pkg_ver=$(echo "${package}" | cut -d "=" -f2)
else
pkg_name="${package}"
fi
apt_show=$(apt show "${package}")
if echo ${apt_show} | grep -qi "No packages found" > /dev/null; then
echo "aborted." echo "aborted."
echo "Package '$package' not found." >&2 echo "Package '${package}' not found." >&2
exit 3 exit 3
fi fi
if [ -z "${pkg_ver}" ]; then
pkg_ver=$(echo "${apt_show}" | grep -Po "(?<=Version: )[^\s]+")
fi
package_list="${package_list} ${pkg_name}=${pkg_ver}"
done done
echo "done." echo "done."
@ -43,18 +59,18 @@ set -e
echo "Creating cache key..." echo "Creating cache key..."
# Remove package delimiters, sort (requires newline) and then convert back to inline list. # Remove extraneous spaces
normalized_list=$(echo $packages | sed 's/[\s,]+/ /g' | tr ' ' '\n' | sort | tr '\n' ' ') package_list="$(echo "${package_list}" | sed 's/\s\+/ /g;s/^\s\+//g;s/\s\+$//g')"
echo "- Normalized package list is '$normalized_list'." echo "- Normalized package list is '$package_list'."
value=$(echo $normalized_list @ $version) value=$(echo "${package_list} @ ${version}")
echo "- Value to hash is '$value'." echo "- Value to hash is '${value}'."
key=$(echo $value | md5sum | /bin/cut -f1 -d' ') key=$(echo "${value}" | md5sum | /bin/cut -f1 -d' ')
echo "- Value hashed as '$key'." echo "- Value hashed as '$key'."
echo "done." echo "done."
key_filepath="$cache_dir/cache_key.md5" key_filepath="${cache_dir}/cache_key.md5"
echo $key > $key_filepath echo "${key}" > "${key_filepath}"
echo "Hash value written to $key_filepath" echo "Hash value written to ${key_filepath}"