2021-10-22 03:57:52 +00:00
|
|
|
#!/bin/bash
|
|
|
|
|
2023-01-02 22:15:37 +00:00
|
|
|
# Fail on any error.
|
2022-11-24 16:59:56 +00:00
|
|
|
set -e
|
|
|
|
|
|
|
|
# Include library.
|
|
|
|
script_dir="$(dirname -- "$(realpath -- "${0}")")"
|
|
|
|
source "${script_dir}/lib.sh"
|
|
|
|
|
2022-11-24 06:24:00 +00:00
|
|
|
# Debug mode for diagnosing issues.
|
|
|
|
# Setup first before other operations.
|
2023-01-02 22:15:37 +00:00
|
|
|
debug="${1}"
|
2022-11-24 06:24:00 +00:00
|
|
|
validate_bool "${debug}" debug 1
|
|
|
|
test ${debug} == "true" && set -x
|
|
|
|
|
2021-10-22 03:57:52 +00:00
|
|
|
# Directory that holds the cached packages.
|
2023-01-02 22:15:37 +00:00
|
|
|
cache_dir="${2}"
|
2021-10-22 03:57:52 +00:00
|
|
|
|
|
|
|
# Version of the cache to create or load.
|
2023-01-02 22:15:37 +00:00
|
|
|
version="${3}"
|
2021-10-22 03:57:52 +00:00
|
|
|
|
2023-01-02 22:15:37 +00:00
|
|
|
# Additional repositories to include.
|
|
|
|
add_repositories="${4}"
|
2022-11-24 06:24:00 +00:00
|
|
|
|
2023-01-02 22:15:37 +00:00
|
|
|
# Trim commas, excess spaces, and sort.
|
|
|
|
repositories="$(normalize_list "${add_repositories}")"
|
|
|
|
|
|
|
|
# Execute post-installation script.
|
|
|
|
execute_install_scripts="${5}"
|
2022-11-24 06:24:00 +00:00
|
|
|
|
2021-10-22 03:57:52 +00:00
|
|
|
# List of the packages to use.
|
2023-01-02 22:15:37 +00:00
|
|
|
packages="${@:6}"
|
2022-03-16 17:15:25 +00:00
|
|
|
|
2022-03-26 19:42:40 +00:00
|
|
|
# Trim commas, excess spaces, and sort.
|
2023-01-02 22:15:37 +00:00
|
|
|
normalized_packages="$(normalize_list "${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-07-20 03:42:48 +00:00
|
|
|
mkdir -p ${cache_dir}
|
2021-10-22 04:12:42 +00:00
|
|
|
|
2023-01-02 22:15:37 +00:00
|
|
|
log "Validating action arguments (version='${version}', packages='${normalized_packages}')...";
|
2022-07-20 03:42:48 +00:00
|
|
|
if grep -q " " <<< "${version}"; then
|
2022-07-24 00:06:17 +00:00
|
|
|
log "aborted"
|
2022-07-20 03:42:48 +00:00
|
|
|
log "Version value '${version}' cannot contain spaces." >&2
|
2022-11-24 06:24:00 +00:00
|
|
|
exit 2
|
2021-10-22 03:57:52 +00:00
|
|
|
fi
|
2022-06-04 04:04:43 +00:00
|
|
|
|
2022-03-26 19:42:40 +00:00
|
|
|
# Is length of string zero?
|
2023-01-02 22:15:37 +00:00
|
|
|
if test -z "${normalized_packages}"; then
|
2022-07-24 00:06:17 +00:00
|
|
|
log "aborted"
|
2022-07-20 03:42:48 +00:00
|
|
|
log "Packages argument cannot be empty." >&2
|
2022-11-24 06:24:00 +00:00
|
|
|
exit 3
|
2021-10-22 03:57:52 +00:00
|
|
|
fi
|
2022-08-03 04:14:51 +00:00
|
|
|
|
2022-11-24 06:24:00 +00:00
|
|
|
validate_bool "${execute_install_scripts}" execute_install_scripts 4
|
|
|
|
|
2022-08-03 04:14:51 +00:00
|
|
|
log "done"
|
|
|
|
|
|
|
|
log_empty_line
|
|
|
|
|
2022-03-26 19:42:40 +00:00
|
|
|
versioned_packages=""
|
2022-08-03 04:14:51 +00:00
|
|
|
log "Verifying packages..."
|
2023-01-02 22:15:37 +00:00
|
|
|
for package in ${normalized_packages}; do
|
2022-08-03 04:14:51 +00:00
|
|
|
if test ! "$(apt-cache show "${package}")"; then
|
2022-07-24 00:06:17 +00:00
|
|
|
echo "aborted"
|
2022-07-20 03:42:48 +00:00
|
|
|
log "Package '${package}' not found." >&2
|
2022-11-24 06:24:00 +00:00
|
|
|
exit 5
|
2021-10-22 03:57:52 +00:00
|
|
|
fi
|
2022-07-20 03:42:48 +00:00
|
|
|
read package_name package_ver < <(get_package_name_ver "${package}")
|
|
|
|
versioned_packages=""${versioned_packages}" "${package_name}"="${package_ver}""
|
2021-10-22 03:57:52 +00:00
|
|
|
done
|
2022-08-03 04:14:51 +00:00
|
|
|
log "done"
|
2022-07-24 00:06:17 +00:00
|
|
|
|
|
|
|
log_empty_line
|
2021-10-22 03:57:52 +00:00
|
|
|
|
|
|
|
# Abort on any failure at this point.
|
|
|
|
set -e
|
|
|
|
|
2022-07-20 03:42:48 +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?
|
2023-01-02 22:15:37 +00:00
|
|
|
normalized_versioned_packages="$(normalize_list "${versioned_packages}")"
|
2022-07-20 03:42:48 +00:00
|
|
|
log "- Normalized package list is '${normalized_versioned_packages}'."
|
2021-10-22 03:57:52 +00:00
|
|
|
|
2023-01-02 22:15:37 +00:00
|
|
|
# Create value to hash for cache key.
|
|
|
|
value_to_hash="${normalized_versioned_packages}"
|
|
|
|
if test -n "${repositories}"; then
|
|
|
|
log "- Added repository list is '${repositories}'."
|
|
|
|
value_to_hash="${value_to_hash} add-repositories:'${repositories}'"
|
|
|
|
fi
|
|
|
|
value_to_hash="${value_to_hash}@ ${version}"
|
2021-10-22 03:57:52 +00:00
|
|
|
|
2023-01-02 22:15:37 +00:00
|
|
|
# Hash the value and set the key.
|
|
|
|
log "- Value to hash is '${value_to_hash}'."
|
|
|
|
hash_key="$(echo "${value_to_hash}" | md5sum | cut -f1 -d' ')"
|
|
|
|
log "- Value hashed as '${hash_key}'."
|
2021-10-22 03:57:52 +00:00
|
|
|
|
2023-01-02 22:15:37 +00:00
|
|
|
# Write the key out to match for future runs.
|
|
|
|
hash_key_filepath="${cache_dir}/cache_key.md5"
|
|
|
|
echo ${hash_key} > ${hash_key_filepath}
|
|
|
|
log "Hash value written to ${hash_key_filepath}"
|