2021-10-22 03:57:52 +00:00
|
|
|
#!/bin/bash
|
|
|
|
|
2022-03-26 19:42:40 +00:00
|
|
|
# Include library.
|
|
|
|
script_dir="$(dirname -- "$(realpath -- "${0}")")"
|
2022-03-26 19:48:16 +00:00
|
|
|
source "${script_dir}/lib.sh"
|
2022-03-26 19:42:40 +00:00
|
|
|
|
2021-10-22 03:57:52 +00:00
|
|
|
# Directory that holds the cached packages.
|
2022-06-30 07:51:24 +00:00
|
|
|
cache_dir="${1}"
|
2021-10-22 03:57:52 +00:00
|
|
|
|
|
|
|
# Version of the cache to create or load.
|
2022-06-30 07:51:24 +00:00
|
|
|
version="${2}"
|
2021-10-22 03:57:52 +00:00
|
|
|
|
|
|
|
# List of the packages to use.
|
2022-03-26 19:42:40 +00:00
|
|
|
input_packages="${@:3}"
|
2022-03-16 17:15:25 +00:00
|
|
|
|
2022-03-26 19:42:40 +00:00
|
|
|
# Trim commas, excess spaces, and sort.
|
|
|
|
packages="$(normalize_package_list "${input_packages}")"
|
2021-10-22 03:57:52 +00:00
|
|
|
|
2021-10-22 04:12:42 +00:00
|
|
|
# Create cache directory so artifacts can be saved.
|
2022-06-30 07:30:31 +00:00
|
|
|
mkdir -p ${cache_dir}
|
2021-10-22 04:12:42 +00:00
|
|
|
|
2022-06-30 14:13:58 +00:00
|
|
|
log -n "Validating action arguments (version='${version}', packages='${packages}')...";
|
2022-06-30 08:17:13 +00:00
|
|
|
if grep -q " " <<< "${version}"; then
|
2022-06-30 14:13:58 +00:00
|
|
|
log "aborted."
|
|
|
|
log "Version value '${version}' cannot contain spaces." >&2
|
2021-10-22 03:57:52 +00:00
|
|
|
exit 1
|
|
|
|
fi
|
2022-06-04 04:04:43 +00:00
|
|
|
|
2022-03-26 19:42:40 +00:00
|
|
|
# Is length of string zero?
|
|
|
|
if test -z "${packages}"; then
|
2022-06-30 14:13:58 +00:00
|
|
|
log "aborted."
|
|
|
|
log "Packages argument cannot be empty." >&2
|
2021-10-22 03:57:52 +00:00
|
|
|
exit 2
|
|
|
|
fi
|
2022-06-30 14:13:58 +00:00
|
|
|
log "done."
|
2021-10-22 03:57:52 +00:00
|
|
|
|
2022-03-26 19:42:40 +00:00
|
|
|
versioned_packages=""
|
2022-06-30 14:13:58 +00:00
|
|
|
log -n "Verifying packages..."
|
2022-03-26 19:42:40 +00:00
|
|
|
for package in ${packages}; do
|
|
|
|
if test ! "$(apt show "${package}")"; then
|
2021-10-22 03:57:52 +00:00
|
|
|
echo "aborted."
|
2022-06-30 14:13:58 +00:00
|
|
|
log "Package '${package}' not found." >&2
|
2021-10-22 03:57:52 +00:00
|
|
|
exit 3
|
2022-06-30 08:17:13 +00:00
|
|
|
fi
|
2022-06-30 08:27:20 +00:00
|
|
|
read package_name package_ver < <(get_package_name_ver "${package}")
|
2022-06-30 08:17:13 +00:00
|
|
|
versioned_packages=""${versioned_packages}" "${package_name}"="${package_ver}""
|
2021-10-22 03:57:52 +00:00
|
|
|
done
|
|
|
|
echo "done."
|
|
|
|
|
|
|
|
# Abort on any failure at this point.
|
|
|
|
set -e
|
|
|
|
|
2022-06-30 14:13:58 +00:00
|
|
|
log "Creating cache key..."
|
2021-10-22 03:57:52 +00:00
|
|
|
|
2022-03-26 19:42:40 +00:00
|
|
|
# TODO Can we prove this will happen again?
|
|
|
|
normalized_versioned_packages="$(normalize_package_list "${versioned_packages}")"
|
2022-06-30 14:13:58 +00:00
|
|
|
log "- Normalized package list is '${normalized_versioned_packages}'."
|
2021-10-22 03:57:52 +00:00
|
|
|
|
2022-06-30 08:17:13 +00:00
|
|
|
value="$(echo "${normalized_versioned_packages} @ ${version}")"
|
2022-06-30 14:13:58 +00:00
|
|
|
log "- Value to hash is '${value}'."
|
2021-10-22 03:57:52 +00:00
|
|
|
|
2022-03-26 19:42:40 +00:00
|
|
|
key="$(echo "${value}" | md5sum | /bin/cut -f1 -d' ')"
|
2022-06-30 14:13:58 +00:00
|
|
|
log "- Value hashed as '${key}'."
|
2021-10-22 03:57:52 +00:00
|
|
|
|
2022-06-30 14:13:58 +00:00
|
|
|
log "done."
|
2021-10-22 03:57:52 +00:00
|
|
|
|
2022-06-30 07:30:31 +00:00
|
|
|
key_filepath="${cache_dir}/cache_key.md5"
|
|
|
|
echo ${key} > ${key_filepath}
|
2022-06-30 14:13:58 +00:00
|
|
|
log "Hash value written to ${key_filepath}"
|