2021-10-21 20:57:52 -07:00
|
|
|
#!/bin/bash
|
|
|
|
|
|
|
|
# Directory that holds the cached packages.
|
2022-03-16 10:15:25 -07:00
|
|
|
cache_dir="${1}"
|
2021-10-21 20:57:52 -07:00
|
|
|
|
|
|
|
# Version of the cache to create or load.
|
2022-03-16 10:15:25 -07:00
|
|
|
version="${2}"
|
2021-10-21 20:57:52 -07:00
|
|
|
|
|
|
|
# List of the packages to use.
|
2022-03-16 10:15:25 -07:00
|
|
|
packages="${@:3}"
|
|
|
|
|
|
|
|
# Sort these packages by name and split on commas.
|
|
|
|
packages=$(echo "${packages}" | sed 's/[\s,]+/ /g' | tr ' ' '\n' | sort | tr '\n' ' ')
|
2021-10-21 20:57:52 -07:00
|
|
|
|
2021-10-21 21:12:42 -07:00
|
|
|
# Create cache directory so artifacts can be saved.
|
2022-03-16 10:15:25 -07:00
|
|
|
mkdir -p "${cache_dir}"
|
|
|
|
|
|
|
|
echo -n "Validating action arguments (version='${version}', packages='${packages}')...";
|
2021-10-21 21:12:42 -07:00
|
|
|
|
2022-03-16 10:15:25 -07:00
|
|
|
if echo "${version}" | grep -q " " > /dev/null; then
|
2021-10-21 20:57:52 -07:00
|
|
|
echo "aborted."
|
2022-03-16 10:15:25 -07:00
|
|
|
echo "Version value '${version}' cannot contain spaces." >&2
|
2021-10-21 20:57:52 -07:00
|
|
|
exit 1
|
|
|
|
fi
|
2022-03-16 10:15:25 -07:00
|
|
|
if [ "${packages}" == "" ]; then
|
2021-10-21 20:57:52 -07:00
|
|
|
echo "aborted."
|
|
|
|
echo "Packages argument cannot be empty." >&2
|
|
|
|
exit 2
|
|
|
|
fi
|
|
|
|
echo "done."
|
|
|
|
|
2022-03-16 10:15:25 -07:00
|
|
|
echo -n "Updating APT package list..."
|
|
|
|
sudo apt-get update > /dev/null
|
|
|
|
echo "done."
|
|
|
|
|
2021-10-21 20:57:52 -07:00
|
|
|
echo -n "Verifying packages..."
|
2022-03-16 10:15:25 -07:00
|
|
|
for package in ${packages}; do
|
|
|
|
if echo "${package}" | grep -q "="; then
|
|
|
|
pkg_name=$(echo "${package}" | cut -d "=" -f1)
|
|
|
|
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
|
2021-10-21 20:57:52 -07:00
|
|
|
echo "aborted."
|
2022-03-16 10:15:25 -07:00
|
|
|
echo "Package '${package}' not found." >&2
|
2021-10-21 20:57:52 -07:00
|
|
|
exit 3
|
|
|
|
fi
|
2022-03-16 10:15:25 -07:00
|
|
|
if [ -z "${pkg_ver}" ]; then
|
|
|
|
pkg_ver=$(echo "${apt_show}" | grep -Po "(?<=Version: )[^\s]+")
|
|
|
|
fi
|
|
|
|
package_list="${package_list} ${pkg_name}=${pkg_ver}"
|
2021-10-21 20:57:52 -07:00
|
|
|
done
|
|
|
|
echo "done."
|
|
|
|
|
|
|
|
# Abort on any failure at this point.
|
|
|
|
set -e
|
|
|
|
|
|
|
|
echo "Creating cache key..."
|
|
|
|
|
2022-03-16 10:15:25 -07:00
|
|
|
# Remove extraneous spaces
|
|
|
|
package_list="$(echo "${package_list}" | sed 's/\s\+/ /g;s/^\s\+//g;s/\s\+$//g')"
|
|
|
|
echo "- Normalized package list is '$package_list'."
|
2021-10-21 20:57:52 -07:00
|
|
|
|
2022-03-16 10:15:25 -07:00
|
|
|
value=$(echo "${package_list} @ ${version}")
|
|
|
|
echo "- Value to hash is '${value}'."
|
2021-10-21 20:57:52 -07:00
|
|
|
|
2022-03-16 10:15:25 -07:00
|
|
|
key=$(echo "${value}" | md5sum | /bin/cut -f1 -d' ')
|
2021-10-21 20:57:52 -07:00
|
|
|
echo "- Value hashed as '$key'."
|
|
|
|
|
|
|
|
echo "done."
|
|
|
|
|
2022-03-16 10:15:25 -07:00
|
|
|
key_filepath="${cache_dir}/cache_key.md5"
|
|
|
|
echo "${key}" > "${key_filepath}"
|
|
|
|
echo "Hash value written to ${key_filepath}"
|