Experimental version. Cleaned up syntax and created common library functions.

This commit is contained in:
awalsh128 2022-03-26 12:42:40 -07:00
parent ea3bc969b2
commit e68df30bd6
4 changed files with 67 additions and 44 deletions

View file

@ -3,22 +3,23 @@
# Fail on any error. # Fail on any error.
set -e set -e
# Include library.
script_dir="$(dirname -- "$(realpath -- "${0}")")"
source "${script_dir}/lib"
# Directory that holds the cached packages. # Directory that holds the cached packages.
cache_dir="${1}" cache_dir="${1}"
# List of the packages to use. # List of the packages to use.
packages="${@:2}" input_packages="${@:2}"
# Sort these packages by name and split on commas. # Trim commas, excess spaces, and sort.
packages=$(echo "${packages}" | sed 's/[\s,]+/ /g' | tr ' ' '\n' | sort | tr '\n' ' ') packages="$(normalize_package_list "${input_packages}")"
# Remove extraneous spaces
packages="$(echo "${packages}" | sed 's/\s\+/ /g;s/^\s\+//g;s/\s\+$//g')"
package_count=$(echo "${packages}" | wc -w) package_count=$(echo "${packages}" | wc -w)
echo "Clean installing and caching ${package_count} package(s)." echo "Clean installing and caching ${package_count} package(s)."
echo "Package list:" echo "Package list:"
for package in ${packages}; do for package in "${packages}"; do
echo "- ${package}" echo "- ${package}"
done done
@ -28,21 +29,23 @@ echo "done."
manifest= manifest=
echo "Clean installing and caching ${package_count} packages..." echo "Clean installing and caching ${package_count} packages..."
for package in ${packages}; do for package in "${packages}"; do
echo "- ${package}" get_package_name_ver "${package}" # -> package_name, package_ver
echo -n " Installing..." package_deps="$(apt-get install --dry-run --yes "${package_name}" | grep "^Inst" | awk '{print $2}')"
# Gather a list of all packages apt installs to make this package work echo "- ${package_name}"
required_packages=$(apt-get install --dry-run --yes "${package}" | grep -Po "(?<=Inst )[^\s]+" || echo "${package}") echo " * Version: ${package_ver}"
echo "Package ${package} installs the following packages: ${required_packages//$'\n'/, }" echo " * Dependencies: ${package_deps}"
echo -n " * Installing..."
# Zero interaction while installing or upgrading the system via apt.
sudo DEBIAN_FRONTEND=noninteractive apt-get --yes install "${package}" > /dev/null sudo DEBIAN_FRONTEND=noninteractive apt-get --yes install "${package}" > /dev/null
echo "done." echo "done."
for cache_package in ${required_packages}; do for cache_package in "${package_deps}"; do
cache_filepath="${cache_dir}/${cache_package}.tar.gz" cache_filepath="${cache_dir}/${cache_package}.tar.gz"
if [ ! -f "${cache_filepath}" ]; then
package_name="$(echo "${cache_package}" | cut -d"=" -f1)" if test ! -f "${cache_filepath}"; then
get_package_name_ver "${cache_package}" # -> package_name, package_ver
echo -n " Caching ${package_name} to ${cache_filepath}..." echo -n " Caching ${package_name} to ${cache_filepath}..."
# Pipe all package files (no folders) to Tar. # Pipe all package files (no folders) to Tar.
dpkg -L "${package_name}" | dpkg -L "${package_name}" |
@ -60,6 +63,6 @@ echo "done."
manifest_filepath="${cache_dir}/manifest.log" manifest_filepath="${cache_dir}/manifest.log"
echo -n "Writing package manifest to ${manifest_filepath}..." echo -n "Writing package manifest to ${manifest_filepath}..."
# Remove trailing comma. # Remove trailing comma and write to manifest file.
echo ${manifest:0:-1} > ${manifest_filepath} echo "${manifest:0:-1}" > "${manifest_filepath}"
echo "done." echo "done."

23
lib.sh Normal file
View file

@ -0,0 +1,23 @@
#!/bin/bash
# Sort these packages by name and split on commas.
function normalize_package_list {
stripped="$(echo \"${0}\" | sed 's/,//g')"
# Remove extraneous spaces at the middle, beginning, and end.
trimmed="$(echo \"${stripped}\" | sed 's/\s\+/ /g; s/^\s\+//g; s/\s\+$//g')"
echo "$(\"${trimmed}\" | sort)"
}
# Split fully qualified package into name and version
function get_package_name_ver {
IFS=\= read name ver <<< "${0}"
# If version not found in the fully qualified package value.
if test -z "${ver}"; then
ver="$(grep "Version:" <<< "$(apt show ${name})" | awk '{print $2}')"
fi
echo 'package_name="${name}"; package_ver="${ver}"'
}
function blah {
> /dev/null 2>&1
}

View file

@ -1,5 +1,9 @@
#!/bin/bash #!/bin/bash
# Include library.
script_dir="$(dirname -- "$(realpath -- "${0}")")"
source "${script_dir}/lib"
# Directory that holds the cached packages. # Directory that holds the cached packages.
cache_dir="${1}" cache_dir="${1}"
@ -7,22 +11,23 @@ cache_dir="${1}"
cache_version="${2}" cache_version="${2}"
# List of the packages to use. # List of the packages to use.
packages="${@:3}" input_packages="${@:3}"
# Sort these packages by name and split on commas. # Trim commas, excess spaces, and sort.
packages=$(echo "${packages}" | sed 's/[\s,]+/ /g' | tr ' ' '\n' | sort | tr '\n' ' ') packages="$(normalize_package_list "${input_packages}")"
# 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='${cache_version}', packages='${packages}')..."; echo -n "Validating action arguments (version='${cache_version}', packages='${packages}')...";
if echo "${cache_version}" | grep -q " " > /dev/null; then if grep -q " " <<< "${cache_version}"; then
echo "aborted." echo "aborted."
echo "Version value '${cache_version}' cannot contain spaces." >&2 echo "Version value '${cache_version}' cannot contain spaces." >&2
exit 1 exit 1
fi fi
if [ "${packages}" == "" ]; then # Is length of string zero?
if test -z "${packages}"; then
echo "aborted." echo "aborted."
echo "Packages argument cannot be empty." >&2 echo "Packages argument cannot be empty." >&2
exit 2 exit 2
@ -33,24 +38,16 @@ echo -n "Updating APT package list..."
sudo apt-get update > /dev/null sudo apt-get update > /dev/null
echo "done." echo "done."
versioned_packages=""
echo -n "Verifying packages..." echo -n "Verifying packages..."
for package in ${packages}; do for package in ${packages}; do
if echo "${package}" | grep -q "="; then if test ! "$(apt show "${package}")"; then
package_name=$(echo "${package}" | cut -d "=" -f1)
package_ver=$(echo "${package}" | cut -d "=" -f2)
else
package_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 "${package_ver}" ]; then get_package_name_ver "${package}" # -> package_name, package_ver
package_ver=$(echo "${apt_show}" | grep -Po "(?<=Version: )[^\s]+") versioned_packages="${versioned_packages} ${package_name}=${package_ver}"
fi
package_list="${package_list} ${package_name}=${package_ver}"
done done
echo "done." echo "done."
@ -59,15 +56,15 @@ set -e
echo "Creating cache key..." echo "Creating cache key..."
# Remove extraneous spaces # TODO Can we prove this will happen again?
package_list="$(echo "${package_list}" | sed 's/\s\+/ /g;s/^\s\+//g;s/\s\+$//g')" normalized_versioned_packages="$(normalize_package_list "${versioned_packages}")"
echo "- Normalized package list is '$package_list'." echo "- Normalized package list is '${normalized_versioned_packages}'."
value=$(echo "${package_list} @ ${cache_version}") value="$(echo "${normalized_versioned_packages} @ ${cache_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."

View file

@ -17,7 +17,7 @@ for cache_filepath in ${cache_filepaths}; do
done done
echo "Reading from manifest..." echo "Reading from manifest..."
for logline in $(cat "${cache_dir}/manifest.log" | tr ',' '\n' ); do for logline in "$(cat "${cache_dir}/manifest.log" | tr ',' '\n' )"; do
echo "- $(echo "${logline}" | tr ':' ' ')" echo "- $(echo "${logline}" | tr ':' ' ')"
done done
echo "done." echo "done."