This commit is contained in:
awalsh128 2022-06-29 23:53:30 -07:00
commit f1c5c6fb4a
4 changed files with 94 additions and 45 deletions

View file

@ -3,49 +3,66 @@
# Fail on any error. # Fail on any error.
set -e set -e
# Include library.
script_dir="$(dirname -- "$(realpath -- "${0}")")"
source "${script_dir}/lib.sh"
# 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}"
package_count=$(echo $packages | wc -w) # Trim commas, excess spaces, and sort.
echo "Clean installing and caching $package_count package(s)." packages="$(normalize_package_list "${input_packages}")"
package_count=$(echo "${packages}" | wc -w)
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
echo -n "Updating APT package list..." echo -n "Updating APT package list..."
sudo apt-get update > /dev/null sudo apt-get update > /dev/null
echo "done." echo "done."
echo "Clean installing and caching $(echo $packages | wc -w) packages..." manifest=""
for package in $packages; do echo "Clean installing and caching ${package_count} packages..."
cache_filepath=$cache_dir/$package.tar.gz for package in "${packages}"; do
get_package_name_ver "${package}" # -> package_name, package_ver
package_deps="$(apt-get install --dry-run --yes "${package_name}" | grep "^Inst" | awk '{print $2}')"
echo "- $package" echo "- ${package_name}"
echo -n " Installing..." echo " * Version: ${package_ver}"
sudo apt-get --yes install $package > /dev/null 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
echo "done." echo "done."
echo -n " Caching to $cache_filepath..." for cache_package in "${package_deps}"; do
# Pipe all package files (no folders) to Tar. cache_filepath="${cache_dir}/${cache_package}.tar.gz"
dpkg -L $package |
while IFS= read -r f; do if test ! -f "${cache_filepath}"; then
if test -f $f; then echo ${f:1}; fi; #${f:1} removes the leading slash that Tar disallows get_package_name_ver "${cache_package}" # -> package_name, package_ver
done | echo -n " Caching ${package_name} to ${cache_filepath}..."
xargs tar -czf $cache_filepath -C / # Pipe all package files (no folders) to Tar.
echo "done." dpkg -L "${package_name}" |
while IFS= read -r f; do
if test -f $f; then echo "${f:1}"; fi; #${f:1} removes the leading slash that Tar disallows
done |
xargs tar -czf "${cache_filepath}" -C /
echo "done."
# Add package to manifest
manifest="${manifest}${package_name}:$(dpkg -s "${package_name}" | grep Version | awk '{print $2}'),"
fi
done
done done
echo "done." 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}..."
manifest= # Remove trailing comma and write to manifest file.
for package in $packages; do echo "${manifest:0:-1}" > "${manifest_filepath}"
manifest=$manifest$package:$(dpkg -s $package | grep Version | awk '{print $2}'),
done
# Remove trailing comma.
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.sh"
# Directory that holds the cached packages. # Directory that holds the cached packages.
cache_dir=$1 cache_dir=$1
@ -7,34 +11,39 @@ cache_dir=$1
version=$2 version=$2
# List of the packages to use. # List of the packages to use.
packages=${@:3} input_packages="${@:3}"
# Trim commas, excess spaces, and sort.
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='$version', packages='$packages')..."; echo -n "Validating action arguments (version='$version', packages='$packages')...";
echo $version | grep -o " " > /dev/null if grep -q " " <<< "${cache_version}"; then
if [ $? -eq 0 ]; then
echo "aborted." echo "aborted."
echo "Version value '$version' cannot contain spaces." >&2 echo "Version value '$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
fi fi
echo "done." echo "done."
versioned_packages=""
echo -n "Verifying packages..." echo -n "Verifying packages..."
for package in $packages; do for package in ${packages}; do
escaped=$(echo $package | sed 's/+/\\+/g') if test ! "$(apt show "${package}")"; then
apt-cache search ^$escaped$ | grep $package > /dev/null
if [ $? -ne 0 ]; then
echo "aborted." echo "aborted."
echo "Package '$package' not found." >&2 echo "Package '$package' not found." >&2
exit 3 exit 3
fi fi
get_package_name_ver "${package}" # -> package_name, package_ver
versioned_packages="${versioned_packages} ${package_name}=${package_ver}"
done done
echo "done." echo "done."
@ -43,15 +52,15 @@ set -e
echo "Creating cache key..." echo "Creating cache key..."
# Remove package delimiters, sort (requires newline) and then convert back to inline list. # TODO Can we prove this will happen again?
normalized_list=$(echo $packages | sed 's/[\s,]+/ /g' | tr ' ' '\n' | sort | tr '\n' ' ') normalized_versioned_packages="$(normalize_package_list "${versioned_packages}")"
echo "- Normalized package list is '$normalized_list'." echo "- Normalized package list is '${normalized_versioned_packages}'."
value=$(echo $normalized_list @ $version) value="$(echo "${normalized_versioned_packages} @ ${cache_version}")"
echo "- Value to hash is '$value'." echo "- Value to hash is '${value}'."
key=$(echo $value | md5sum | 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,8 +17,8 @@ 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."