2021-10-21 20:57:52 -07:00
|
|
|
#!/bin/bash
|
|
|
|
|
|
|
|
# Directory that holds the cached packages.
|
2022-06-03 20:30:40 -07:00
|
|
|
cache_dir=$1
|
2021-10-21 20:57:52 -07:00
|
|
|
|
|
|
|
# Version of the cache to create or load.
|
2022-06-03 20:30:40 -07:00
|
|
|
version=$2
|
2021-10-21 20:57:52 -07:00
|
|
|
|
|
|
|
# List of the packages to use.
|
2022-06-03 20:30:40 -07:00
|
|
|
packages=${@:3}
|
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-06-03 20:30:40 -07:00
|
|
|
mkdir -p $cache_dir
|
2021-10-21 21:12:42 -07:00
|
|
|
|
2022-06-03 20:30:40 -07:00
|
|
|
echo -n "Validating action arguments (version='$version', packages='$packages')...";
|
|
|
|
echo $version | grep -o " " > /dev/null
|
|
|
|
if [ $? -eq 0 ]; then
|
2021-10-21 20:57:52 -07:00
|
|
|
echo "aborted."
|
2022-06-03 20:30:40 -07:00
|
|
|
echo "Version value '$version' cannot contain spaces." >&2
|
2021-10-21 20:57:52 -07:00
|
|
|
exit 1
|
|
|
|
fi
|
2022-06-03 20:30:40 -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."
|
|
|
|
|
|
|
|
echo -n "Verifying packages..."
|
2022-06-03 20:30:40 -07:00
|
|
|
for package in $packages; do
|
|
|
|
apt-cache search ^$package$ | grep $package > /dev/null
|
|
|
|
if [ $? -ne 0 ]; then
|
2021-10-21 20:57:52 -07:00
|
|
|
echo "aborted."
|
2022-06-03 20:30:40 -07:00
|
|
|
echo "Package '$package' not found." >&2
|
2021-10-21 20:57:52 -07:00
|
|
|
exit 3
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
echo "done."
|
|
|
|
|
|
|
|
# Abort on any failure at this point.
|
|
|
|
set -e
|
|
|
|
|
|
|
|
echo "Creating cache key..."
|
|
|
|
|
2022-06-03 20:30:40 -07:00
|
|
|
# Remove package delimiters, sort (requires newline) and then convert back to inline list.
|
|
|
|
normalized_list=$(echo $packages | sed 's/[\s,]+/ /g' | tr ' ' '\n' | sort | tr '\n' ' ')
|
|
|
|
echo "- Normalized package list is '$normalized_list'."
|
2021-10-21 20:57:52 -07:00
|
|
|
|
2022-06-03 20:30:40 -07:00
|
|
|
value=$(echo $normalized_list @ $version)
|
|
|
|
echo "- Value to hash is '$value'."
|
2021-10-21 20:57:52 -07:00
|
|
|
|
2022-06-03 20:30:40 -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-06-03 20:30:40 -07:00
|
|
|
key_filepath="$cache_dir/cache_key.md5"
|
|
|
|
echo $key > $key_filepath
|
|
|
|
echo "Hash value written to $key_filepath"
|