Optimize installs with apt-fast and various minor cleanups. (#35)

* Fix cut regression.

Originally fixed in #17. This was reintroduced when master was sync'd to staging.

* Update pre_cache_action.sh

* Switch to CLI safe apt command.

Address concern in issue #23.

* Optimize installs with apt-fast and cleanup logging.
This commit is contained in:
Andrew Walsh 2022-07-23 17:06:17 -07:00 committed by GitHub
parent 97133735cc
commit c5df606b25
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 88 additions and 50 deletions

View file

@ -7,6 +7,9 @@ set -e
script_dir="$(dirname -- "$(realpath -- "${0}")")" script_dir="$(dirname -- "$(realpath -- "${0}")")"
source "${script_dir}/lib.sh" source "${script_dir}/lib.sh"
# Install apt-fast for optimized installs.
/bin/bash -c "$(curl -sL https://git.io/vokNn)"
# Directory that holds the cached packages. # Directory that holds the cached packages.
cache_dir="${1}" cache_dir="${1}"
@ -18,28 +21,40 @@ normalized_packages="$(normalize_package_list "${input_packages}")"
package_count=$(wc -w <<< "${normalized_packages}") package_count=$(wc -w <<< "${normalized_packages}")
log "Clean installing and caching ${package_count} package(s)." log "Clean installing and caching ${package_count} package(s)."
log_empty_line
log "Package list:" log "Package list:"
for package in ${normalized_packages}; do for package in ${normalized_packages}; do
log "- ${package}" log "- ${package}"
done done
log_empty_line
log "Updating APT package list..." log "Updating APT package list..."
sudo apt-get update > /dev/null sudo apt-fast update > /dev/null
echo "done." log "done"
log_empty_line
# Strictly contains the requested packages. # Strictly contains the requested packages.
manifest_main="" manifest_main=""
# Contains all packages including dependencies. # Contains all packages including dependencies.
manifest_all="" manifest_all=""
log "Clean installing and caching ${package_count} packages..." log "Gathering install information for ${package_count} packages..."
log_empty_line
cached_packages=""
for package in ${normalized_packages}; do for package in ${normalized_packages}; do
read package_name package_ver < <(get_package_name_ver "${package}") read package_name package_ver < <(get_package_name_ver "${package}")
# Comma delimited name:ver pairs in the main requested packages manifest. # Comma delimited name:ver pairs in the main requested packages manifest.
manifest_main="${manifest_main}${package_name}:${package_ver}," manifest_main="${manifest_main}${package_name}:${package_ver},"
cached_packages="${cached_packages} ${package_name}:${package_version}"
read dep_packages < <(get_dep_packages "${package_name}") read dep_packages < <(get_dep_packages "${package_name}")
cached_packages="${cached_packages} $(echo ${dep_packages} | tr '\n' ' ')"
if test -z "${dep_packages}"; then if test -z "${dep_packages}"; then
dep_packages_text="none"; dep_packages_text="none";
else else
@ -49,31 +64,42 @@ for package in ${normalized_packages}; do
log "- ${package_name}" log "- ${package_name}"
log " * Version: ${package_ver}" log " * Version: ${package_ver}"
log " * Dependencies: ${dep_packages_text}" log " * Dependencies: ${dep_packages_text}"
log " * Installing..." log_empty_line
# Zero interaction while installing or upgrading the system via apt. done
sudo DEBIAN_FRONTEND=noninteractive apt-get --yes install "${package_name}" > /dev/null log "done"
echo "done."
for cache_package in ${package_name}:${package_ver} ${dep_packages}; do log_empty_line
cache_filepath="${cache_dir}/${cache_package}.tar.gz"
log "Clean installing ${package_count} packages..."
# Zero interaction while installing or upgrading the system via apt.
sudo DEBIAN_FRONTEND=noninteractive apt-fast --yes install ${normalized_packages} > /dev/null
log "done"
log_empty_line
cached_package_count=$(wc -w <<< "${cached_packages}")
log "Caching ${cached_package_count} installed packages..."
for cached_package in ${cached_packages}; do
cache_filepath="${cache_dir}/${cached_package}.tar.gz"
if test ! -f "${cache_filepath}"; then if test ! -f "${cache_filepath}"; then
read cache_package_name cache_package_ver < <(get_package_name_ver "${cache_package}") read cached_package_name cached_package_ver < <(get_package_name_ver "${cached_package}")
log " * Caching ${cache_package_name} to ${cache_filepath}..." log " * Caching ${cached_package_name} to ${cache_filepath}..."
# Pipe all package files (no folders) to Tar. # Pipe all package files (no folders) to Tar.
dpkg -L "${cache_package_name}" | dpkg -L "${cached_package_name}" |
while IFS= read -r f; do while IFS= read -r f; do
if test -f $f || test -L $f; then echo "${f:1}"; fi; #${f:1} removes the leading slash that Tar disallows if test -f $f || test -L $f; then echo "${f:1}"; fi; #${f:1} removes the leading slash that Tar disallows
done | done |
xargs tar -czf "${cache_filepath}" -C / xargs tar -czf "${cache_filepath}" -C /
log "done (compressed size $(du -h "${cache_filepath}" | cut -f1))." log " done (compressed size $(du -h "${cache_filepath}" | cut -f1))."
fi fi
# Comma delimited name:ver pairs in the all packages manifest. # Comma delimited name:ver pairs in the all packages manifest.
manifest_all="${manifest_all}${cache_package_name}:${cache_package_ver}," manifest_all="${manifest_all}${cached_package_name}:${cached_package_ver},"
done
done done
log "done." log "done (total cache size $(du -h ${cache_dir} | tail -1 | awk '{print $1}'))"
log_empty_line
write_manifest "all" "${manifest_all}" "${cache_dir}/manifest_all.log" write_manifest "all" "${manifest_all}" "${cache_dir}/manifest_all.log"
write_manifest "main" "${manifest_main}" "${cache_dir}/manifest_main.log" write_manifest "main" "${manifest_main}" "${cache_dir}/manifest_main.log"

13
lib.sh
View file

@ -9,10 +9,10 @@ function normalize_package_list {
echo "${sorted}" echo "${sorted}"
} }
# Gets a package list of dependencies as common delimited pairs # Gets a package list of dependencies as newline delimited pairs
# <name>:<version>,<name:version>... # <name>:<version>\n<name:version>...
function get_dep_packages { function get_dep_packages {
echo $(apt-get install --dry-run --yes "${1}" | \ echo $(apt-fast install --dry-run --yes "${1}" | \
grep "^Inst" | sort | awk '{print $2 $3}' | \ grep "^Inst" | sort | awk '{print $2 $3}' | \
tr '(' ':' | grep -v "${1}:") tr '(' ':' | grep -v "${1}:")
} }
@ -22,16 +22,19 @@ function get_package_name_ver {
IFS=\: read name ver <<< "${1}" IFS=\: read name ver <<< "${1}"
# If version not found in the fully qualified package value. # If version not found in the fully qualified package value.
if test -z "${ver}"; then if test -z "${ver}"; then
ver="$(grep "Version:" <<< "$(apt show ${name})" | awk '{print $2}')" ver="$(grep "Version:" <<< "$(apt-cache show ${name})" | awk '{print $2}')"
fi fi
echo "${name}" "${ver}" echo "${name}" "${ver}"
} }
function log { echo "$(date +%H:%M:%S)" "${@}"; } function log { echo "$(date +%H:%M:%S)" "${@}"; }
function log_empty_line { echo ""; }
# Writes the manifest to a specified file.
function write_manifest { function write_manifest {
log "Writing ${1} packages manifest to ${3}..." log "Writing ${1} packages manifest to ${3}..."
# 0:-1 to remove trailing comma, delimit by newline and sort # 0:-1 to remove trailing comma, delimit by newline and sort
echo "${2:0:-1}" | tr ',' '\n' | sort > ${3} echo "${2:0:-1}" | tr ',' '\n' | sort > ${3}
log "done." log "done"
} }

View file

@ -27,4 +27,5 @@ if [ "$cache_hit" == true ]; then
else else
${script_dir}/install_and_cache_pkgs.sh ~/cache-apt-pkgs ${packages} ${script_dir}/install_and_cache_pkgs.sh ~/cache-apt-pkgs ${packages}
fi fi
echo ""
log_empty_line

View file

@ -21,31 +21,35 @@ mkdir -p ${cache_dir}
log -n "Validating action arguments (version='${version}', packages='${packages}')..."; log -n "Validating action arguments (version='${version}', packages='${packages}')...";
if grep -q " " <<< "${version}"; then if grep -q " " <<< "${version}"; then
log "aborted." log "aborted"
log "Version value '${version}' cannot contain spaces." >&2 log "Version value '${version}' cannot contain spaces." >&2
exit 1 exit 1
fi fi
# Is length of string zero? # Is length of string zero?
if test -z "${packages}"; then if test -z "${packages}"; then
log "aborted." log "aborted"
log "Packages argument cannot be empty." >&2 log "Packages argument cannot be empty." >&2
exit 2 exit 2
fi fi
log "done." log "done"
log_empty_line
versioned_packages="" versioned_packages=""
log -n "Verifying packages..." log -n "Verifying packages..."
for package in ${packages}; do for package in ${packages}; do
if test ! "$(apt show "${package}")"; then if test ! "$(apt show "${package}")"; then
echo "aborted." echo "aborted"
log "Package '${package}' not found." >&2 log "Package '${package}' not found." >&2
exit 3 exit 3
fi fi
read package_name package_ver < <(get_package_name_ver "${package}") read package_name package_ver < <(get_package_name_ver "${package}")
versioned_packages=""${versioned_packages}" "${package_name}"="${package_ver}"" versioned_packages=""${versioned_packages}" "${package_name}"="${package_ver}""
done done
echo "done." echo "done"
log_empty_line
# Abort on any failure at this point. # Abort on any failure at this point.
set -e set -e
@ -62,7 +66,7 @@ log "- Value to hash is '${value}'."
key="$(echo "${value}" | md5sum | cut -f1 -d' ')" key="$(echo "${value}" | md5sum | cut -f1 -d' ')"
log "- Value hashed as '${key}'." log "- Value hashed as '${key}'."
log "done." log "done"
key_filepath="${cache_dir}/cache_key.md5" key_filepath="${cache_dir}/cache_key.md5"
echo ${key} > ${key_filepath} echo ${key} > ${key_filepath}

View file

@ -20,19 +20,23 @@ for cache_filepath in ${cache_filepaths}; do
log "- "$(basename ${cache_filepath})"" log "- "$(basename ${cache_filepath})""
done done
log_empty_line
log "Reading from main requested packages manifest..." log "Reading from main requested packages manifest..."
for logline in $(cat "${cache_dir}/manifest_main.log" | tr ',' '\n' ); do for logline in $(cat "${cache_dir}/manifest_main.log" | tr ',' '\n' ); do
log "- $(echo "${logline}" | tr ':' ' ')" log "- $(echo "${logline}" | tr ':' ' ')"
done done
log "done." log "done"
log_empty_line
# Only search for archived results. Manifest and cache key also live here. # Only search for archived results. Manifest and cache key also live here.
cache_pkg_filepaths=$(ls -1 "${cache_dir}"/*.tar.gz | sort) cached_pkg_filepaths=$(ls -1 "${cache_dir}"/*.tar.gz | sort)
cache_pkg_filecount=$(echo ${cache_pkg_filepaths} | wc -w) cached_pkg_filecount=$(echo ${cached_pkg_filepaths} | wc -w)
log "Restoring ${cache_pkg_filecount} packages from cache..." log "Restoring ${cached_pkg_filecount} packages from cache..."
for cache_pkg_filepath in ${cache_pkg_filepaths}; do for cached_pkg_filepath in ${cached_pkg_filepaths}; do
log "- $(basename "${cache_pkg_filepath}") restoring..." log "- $(basename "${cached_pkg_filepath}") restoring..."
sudo tar -xf "${cache_pkg_filepath}" -C "${cache_restore_root}" > /dev/null sudo tar -xf "${cached_pkg_filepath}" -C "${cache_restore_root}" > /dev/null
log "done." log " done"
done done
log "done." log "done"