From 6e54cc6cfed9a09b517ceeb3a10515745e32824d Mon Sep 17 00:00:00 2001 From: Andrew Walsh Date: Fri, 18 Mar 2022 22:24:55 -0700 Subject: [PATCH 01/30] Revert "Staging" --- action.yml | 4 --- install_and_cache_pkgs.sh | 66 +++++++++++++++------------------------ post_cache_action.sh | 14 ++++----- pre_cache_action.sh | 62 ++++++++++++++---------------------- restore_pkgs.sh | 28 ++++++++--------- 5 files changed, 70 insertions(+), 104 deletions(-) diff --git a/action.yml b/action.yml index 38d4e13..e717de5 100644 --- a/action.yml +++ b/action.yml @@ -25,9 +25,6 @@ outputs: # This compound expression is needed because lhs can be empty. # Need to output true and false instead of true and nothing. value: ${{ steps.load-cache.outputs.cache-hit || false }} - cache-key: - description: 'The key where the cache is stored with packages, their versions, and the cache version (i.e. package=version package=version @ 1).' - value: ${{ steps.pre-cache.outputs.cache-key }} package-version-list: description: 'The packages and versions that are installed as a comma delimited list with colon delimit on the package version (i.e. ::).' value: ${{ steps.post-cache.outputs.package-version-list }} @@ -42,7 +39,6 @@ runs: "${{ inputs.version }}" \ ${{ inputs.packages }} echo "CACHE_KEY=$(cat ~/cache-apt-pkgs/cache_key.md5)" >> $GITHUB_ENV - echo "::set-output name=cache-key::$(cat ~/cache-apt-pkgs/cache_key.md5)" shell: bash - id: load-cache diff --git a/install_and_cache_pkgs.sh b/install_and_cache_pkgs.sh index ca6c838..bbf54dc 100755 --- a/install_and_cache_pkgs.sh +++ b/install_and_cache_pkgs.sh @@ -4,62 +4,48 @@ set -e # Directory that holds the cached packages. -cache_dir="${1}" +cache_dir=$1 # List of the packages to use. packages="${@:2}" -# Sort these packages by name and split on commas. -packages=$(echo "${packages}" | sed 's/[\s,]+/ /g' | tr ' ' '\n' | sort | tr '\n' ' ') - -# Remove extraneous spaces -packages="$(echo "${packages}" | sed 's/\s\+/ /g;s/^\s\+//g;s/\s\+$//g')" - -package_count=$(echo "${packages}" | wc -w) -echo "Clean installing and caching ${package_count} package(s)." +package_count=$(echo $packages | wc -w) +echo "Clean installing and caching $package_count package(s)." echo "Package list:" -for package in ${packages}; do - echo "- ${package}" +for package in $packages; do + echo "- $package" done echo -n "Updating APT package list..." sudo apt-get update > /dev/null echo "done." -manifest= -echo "Clean installing and caching ${package_count} packages..." -for package in ${packages}; do - echo "- ${package}" +echo "Clean installing and caching $(echo $packages | wc -w) packages..." +for package in $packages; do + cache_filepath=$cache_dir/$package.tar.gz + + echo "- $package" echo -n " Installing..." - - # Gather a list of all packages apt installs to make this package work - required_packages=$(apt-get install --dry-run --yes "${package}" | grep -Po "(?<=Inst )[^\s]+" || echo "${package}") - echo "Package ${package} installs the following packages: ${required_packages//$'\n'/, }" - sudo DEBIAN_FRONTEND=noninteractive apt-get --yes install "${package}" > /dev/null - + sudo apt-get --yes install $package > /dev/null echo "done." - for cache_package in ${required_packages}; do - cache_filepath="${cache_dir}/${cache_package}.tar.gz" - if [ ! -f "${cache_filepath}" ]; then - package_name="$(echo "${cache_package}" | cut -d"=" -f1)" - echo -n " Caching ${package_name} to ${cache_filepath}..." - # Pipe all package files (no folders) to Tar. - 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 + echo -n " Caching to $cache_filepath..." + # Pipe all package files (no folders) to Tar. + dpkg -L $package | + 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." done echo "done." -manifest_filepath="${cache_dir}/manifest.log" -echo -n "Writing package manifest to ${manifest_filepath}..." +manifest_filepath="$cache_dir/manifest.log" +echo -n "Writing package manifest to $manifest_filepath..." +manifest= +for package in $packages; do + manifest=$manifest$package:$(dpkg -s $package | grep Version | awk '{print $2}'), +done # Remove trailing comma. -echo ${manifest:0:-1} > ${manifest_filepath} +echo ${manifest:0:-1} > $manifest_filepath echo "done." \ No newline at end of file diff --git a/post_cache_action.sh b/post_cache_action.sh index 0bb091f..5503013 100755 --- a/post_cache_action.sh +++ b/post_cache_action.sh @@ -4,23 +4,23 @@ set -e # Directory that holds the cached packages. -cache_dir="${1}" +cache_dir=$1 # Root directory to untar the cached packages to. # Typically filesystem root '/' but can be changed for testing. -cache_restore_root="${2}" +cache_restore_root=$2 # Indicates that the cache was found. -cache_hit="${3}" +cache_hit=$3 # List of the packages to use. packages="${@:4}" -script_dir="$(dirname -- "$(realpath -- "${0}")")" +script_dir=$(dirname $0) -if [ "${cache_hit}" == true ]; then - "${script_dir}/restore_pkgs.sh" ~/cache-apt-pkgs "${cache_restore_root}" +if [ "$cache_hit" == true ]; then + $script_dir/restore_pkgs.sh ~/cache-apt-pkgs $cache_restore_root else - "${script_dir}/install_and_cache_pkgs.sh" ~/cache-apt-pkgs "${packages}" + $script_dir/install_and_cache_pkgs.sh ~/cache-apt-pkgs $packages fi echo "" diff --git a/pre_cache_action.sh b/pre_cache_action.sh index d9dd664..c4f764b 100755 --- a/pre_cache_action.sh +++ b/pre_cache_action.sh @@ -1,56 +1,40 @@ #!/bin/bash # Directory that holds the cached packages. -cache_dir="${1}" +cache_dir=$1 # Version of the cache to create or load. -cache_version="${2}" +version=$2 # List of the packages to use. -packages="${@:3}" - -# Sort these packages by name and split on commas. -packages=$(echo "${packages}" | sed 's/[\s,]+/ /g' | tr ' ' '\n' | sort | tr '\n' ' ') +packages=${@:3} # 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}')..."; - -if echo "${cache_version}" | grep -q " " > /dev/null; then +echo -n "Validating action arguments (version='$version', packages='$packages')..."; +echo $version | grep -o " " > /dev/null +if [ $? -eq 0 ]; then echo "aborted." - echo "Version value '${cache_version}' cannot contain spaces." >&2 + echo "Version value '$version' cannot contain spaces." >&2 exit 1 fi -if [ "${packages}" == "" ]; then +if [ "$packages" == "" ]; then echo "aborted." echo "Packages argument cannot be empty." >&2 exit 2 fi echo "done." -echo -n "Updating APT package list..." -sudo apt-get update > /dev/null -echo "done." - echo -n "Verifying packages..." -for package in ${packages}; do - if echo "${package}" | grep -q "="; 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 +for package in $packages; do + escaped=$(echo $package | sed 's/+/\\+/g') + apt-cache search ^$escaped$ | grep $package > /dev/null + if [ $? -ne 0 ]; then echo "aborted." - echo "Package '${package}' not found." >&2 + echo "Package '$package' not found." >&2 exit 3 fi - if [ -z "${package_ver}" ]; then - package_ver=$(echo "${apt_show}" | grep -Po "(?<=Version: )[^\s]+") - fi - package_list="${package_list} ${package_name}=${package_ver}" done echo "done." @@ -59,18 +43,18 @@ set -e echo "Creating cache key..." -# Remove extraneous spaces -package_list="$(echo "${package_list}" | sed 's/\s\+/ /g;s/^\s\+//g;s/\s\+$//g')" -echo "- Normalized package list is '$package_list'." +# Remove package delimiters, sort (requires newline) and then convert back to inline list. +normalized_list=$(echo $packages | sed 's/[\s,]+/ /g' | tr ' ' '\n' | sort | tr '\n' ' ') +echo "- Normalized package list is '$normalized_list'." -value=$(echo "${package_list} @ ${cache_version}") -echo "- Value to hash is '${value}'." +value=$(echo $normalized_list @ $version) +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 "done." -key_filepath="${cache_dir}/cache_key.md5" -echo "${key}" > "${key_filepath}" -echo "Hash value written to ${key_filepath}" +key_filepath="$cache_dir/cache_key.md5" +echo $key > $key_filepath +echo "Hash value written to $key_filepath" diff --git a/restore_pkgs.sh b/restore_pkgs.sh index 4fdbcc6..ab87879 100755 --- a/restore_pkgs.sh +++ b/restore_pkgs.sh @@ -4,31 +4,31 @@ set -e # Directory that holds the cached packages. -cache_dir="${1}" +cache_dir=$1 # Root directory to untar the cached packages to. # Typically filesystem root '/' but can be changed for testing. -cache_restore_root="${2}" +cache_restore_root=$2 -cache_filepaths="$(ls -1 "${cache_dir}" | sort)" -echo "Found $(echo "${cache_filepaths}" | wc -w) files in the cache." -for cache_filepath in ${cache_filepaths}; do - echo "- $(basename "${cache_filepath}")" +cache_filepaths=$(ls -1 $cache_dir | sort) +echo "Found $(echo $cache_filepaths | wc -w) files in the cache." +for cache_filepath in $cache_filepaths; do + echo "- $(basename $cache_filepath)" done echo "Reading from manifest..." -for logline in $(cat "${cache_dir}/manifest.log" | tr ',' '\n' ); do - echo "- $(echo "${logline}" | tr ':' ' ')" +for logline in $(cat $cache_dir/manifest.log | tr ',' '\n' ); do + echo "- $(echo $logline | tr ':' ' ')" done echo "done." # Only search for archived results. Manifest and cache key also live here. -cache_pkg_filepaths=$(ls -1 ${cache_dir}/*.tar.gz | sort) -cache_pkg_filecount=$(echo "${cache_pkg_filepaths}" | wc -w) -echo "Restoring ${cache_pkg_filecount} packages from cache..." -for cache_pkg_filepath in ${cache_pkg_filepaths}; do - echo -n "- $(basename "${cache_pkg_filepath}") restoring..." - sudo tar -xf ${cache_pkg_filepath} -C ${cache_restore_root} > /dev/null +cache_pkg_filepaths=$(ls -1 $cache_dir/*.tar.gz | sort) +cache_pkg_filecount=$(echo $cache_pkg_filepaths | wc -w) +echo "Restoring $cache_pkg_filecount packages from cache..." +for cache_pkg_filepath in $cache_pkg_filepaths; do + echo -n "- $(basename $cache_pkg_filepath) restoring..." + sudo tar -xf $cache_pkg_filepath -C $cache_restore_root > /dev/null echo "done." done echo "done." From b229047610d2c1ab53992953a8fb34360de83c31 Mon Sep 17 00:00:00 2001 From: Andrew Walsh Date: Sat, 26 Mar 2022 12:53:58 -0700 Subject: [PATCH 02/30] Create pub_dev_push_event.yml --- .github/workflows/pub_dev_push_event.yml | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 .github/workflows/pub_dev_push_event.yml diff --git a/.github/workflows/pub_dev_push_event.yml b/.github/workflows/pub_dev_push_event.yml new file mode 100644 index 0000000..3600112 --- /dev/null +++ b/.github/workflows/pub_dev_push_event.yml @@ -0,0 +1,19 @@ +name: Publish Dev Push Event +on: + workflow_dispatch: + push: + branches: + - dev + +jobs: + publish_event: + runs-on: ubuntu-latest + name: Publish dev push + steps: + - run: | + curl -i \ + -X POST \ + -H "Accept: application/vnd.github.v3+json" \ + -H "Authorization: token ${{ secrets.PUBLISH_PUSH_TOKEN }}" \ + https://api.github.com/repos/awalsh128/cache-apt-pkgs-action-ci/dispatches \ + -d '{"event_type":"dev_push"}' From c8f8c819da45f0bfa5cbe2857596a76626b868ad Mon Sep 17 00:00:00 2001 From: Andrew Walsh Date: Sat, 26 Mar 2022 12:54:33 -0700 Subject: [PATCH 03/30] Update pub_master_push_event.yml --- .github/workflows/pub_master_push_event.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/pub_master_push_event.yml b/.github/workflows/pub_master_push_event.yml index b026e65..0339a05 100644 --- a/.github/workflows/pub_master_push_event.yml +++ b/.github/workflows/pub_master_push_event.yml @@ -8,7 +8,7 @@ on: jobs: publish_event: runs-on: ubuntu-latest - name: Publish staging push + name: Publish master push steps: - run: | curl -i \ @@ -16,4 +16,4 @@ jobs: -H "Accept: application/vnd.github.v3+json" \ -H "Authorization: token ${{ secrets.PUBLISH_PUSH_TOKEN }}" \ https://api.github.com/repos/awalsh128/cache-apt-pkgs-action-ci/dispatches \ - -d '{"event_type":"master_push"}' \ No newline at end of file + -d '{"event_type":"master_push"}' From 01059849f8f2bb5d05954370245c224acd0b8409 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20=C5=81ukawski?= Date: Fri, 3 Jun 2022 17:41:16 +0200 Subject: [PATCH 04/30] Drop path to `cut` command Command not found on GHA Ubuntu 18.04 runners. --- pre_cache_action.sh | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pre_cache_action.sh b/pre_cache_action.sh index c4f764b..ba09879 100755 --- a/pre_cache_action.sh +++ b/pre_cache_action.sh @@ -15,12 +15,12 @@ mkdir -p $cache_dir echo -n "Validating action arguments (version='$version', packages='$packages')..."; echo $version | grep -o " " > /dev/null if [ $? -eq 0 ]; then - echo "aborted." + echo "aborted." echo "Version value '$version' cannot contain spaces." >&2 exit 1 fi if [ "$packages" == "" ]; then - echo "aborted." + echo "aborted." echo "Packages argument cannot be empty." >&2 exit 2 fi @@ -50,7 +50,7 @@ echo "- Normalized package list is '$normalized_list'." value=$(echo $normalized_list @ $version) echo "- Value to hash is '$value'." -key=$(echo $value | md5sum | /bin/cut -f1 -d' ') +key=$(echo $value | md5sum | cut -f1 -d' ') echo "- Value hashed as '$key'." echo "done." From 1ca2ac1a4cbfe99f76f137054e3a5792b593356b Mon Sep 17 00:00:00 2001 From: awalsh128 Date: Thu, 30 Jun 2022 00:30:31 -0700 Subject: [PATCH 05/30] Standardize syntax and script sourcing. --- install_and_cache_pkgs.sh | 2 +- lib.sh | 4 ---- post_cache_action.sh | 12 ++++++------ pre_cache_action.sh | 18 +++++++++--------- restore_pkgs.sh | 24 ++++++++++++------------ 5 files changed, 28 insertions(+), 32 deletions(-) mode change 100644 => 100755 lib.sh diff --git a/install_and_cache_pkgs.sh b/install_and_cache_pkgs.sh index e4f6c2c..f3cdfe5 100755 --- a/install_and_cache_pkgs.sh +++ b/install_and_cache_pkgs.sh @@ -8,7 +8,7 @@ script_dir="$(dirname -- "$(realpath -- "${0}")")" source "${script_dir}/lib.sh" # Directory that holds the cached packages. -cache_dir=$1 +cache_dir="${1}" # List of the packages to use. input_packages="${@:2}" diff --git a/lib.sh b/lib.sh old mode 100644 new mode 100755 index 3802483..2a31aac --- a/lib.sh +++ b/lib.sh @@ -17,7 +17,3 @@ function get_package_name_ver { fi echo 'package_name="${name}"; package_ver="${ver}"' } - -function blah { - > /dev/null 2>&1 -} \ No newline at end of file diff --git a/post_cache_action.sh b/post_cache_action.sh index 5503013..56fda54 100755 --- a/post_cache_action.sh +++ b/post_cache_action.sh @@ -4,23 +4,23 @@ set -e # Directory that holds the cached packages. -cache_dir=$1 +cache_dir="${1}" # Root directory to untar the cached packages to. # Typically filesystem root '/' but can be changed for testing. -cache_restore_root=$2 +cache_restore_root="${2}" # Indicates that the cache was found. -cache_hit=$3 +cache_hit="${3}" # List of the packages to use. packages="${@:4}" -script_dir=$(dirname $0) +script_dir="$(dirname -- "$(realpath -- "${0}")")" if [ "$cache_hit" == true ]; then - $script_dir/restore_pkgs.sh ~/cache-apt-pkgs $cache_restore_root + ${script_dir}/restore_pkgs.sh ~/cache-apt-pkgs ${cache_restore_root} else - $script_dir/install_and_cache_pkgs.sh ~/cache-apt-pkgs $packages + ${script_dir}/install_and_cache_pkgs.sh ~/cache-apt-pkgs ${packages} fi echo "" diff --git a/pre_cache_action.sh b/pre_cache_action.sh index 05d2fbe..4bd63d4 100755 --- a/pre_cache_action.sh +++ b/pre_cache_action.sh @@ -5,10 +5,10 @@ script_dir="$(dirname -- "$(realpath -- "${0}")")" source "${script_dir}/lib.sh" # Directory that holds the cached packages. -cache_dir=$1 +cache_dir=${1} # Version of the cache to create or load. -version=$2 +version=${2} # List of the packages to use. input_packages="${@:3}" @@ -17,12 +17,12 @@ input_packages="${@:3}" packages="$(normalize_package_list "${input_packages}")" # 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}')..."; if grep -q " " <<< "${cache_version}"; then echo "aborted." - echo "Version value '$version' cannot contain spaces." >&2 + echo "Version value '${version}' cannot contain spaces." >&2 exit 1 fi @@ -39,7 +39,7 @@ echo -n "Verifying packages..." for package in ${packages}; do if test ! "$(apt show "${package}")"; then echo "aborted." - echo "Package '$package' not found." >&2 + echo "Package '${package}' not found." >&2 exit 3 fi get_package_name_ver "${package}" # -> package_name, package_ver @@ -64,6 +64,6 @@ echo "- Value hashed as '${key}'." echo "done." -key_filepath="$cache_dir/cache_key.md5" -echo $key > $key_filepath -echo "Hash value written to $key_filepath" +key_filepath="${cache_dir}/cache_key.md5" +echo ${key} > ${key_filepath} +echo "Hash value written to ${key_filepath}" diff --git a/restore_pkgs.sh b/restore_pkgs.sh index 4c710f3..3cee703 100755 --- a/restore_pkgs.sh +++ b/restore_pkgs.sh @@ -4,16 +4,16 @@ set -e # Directory that holds the cached packages. -cache_dir=$1 +cache_dir="${1}" # Root directory to untar the cached packages to. # Typically filesystem root '/' but can be changed for testing. -cache_restore_root=$2 +cache_restore_root="${2}" -cache_filepaths=$(ls -1 $cache_dir | sort) -echo "Found $(echo $cache_filepaths | wc -w) files in the cache." -for cache_filepath in $cache_filepaths; do - echo "- $(basename $cache_filepath)" +cache_filepaths=$(ls -1 ${cache_dir} | sort) +echo "Found $(echo ${cache_filepaths} | wc -w) files in the cache." +for cache_filepath in ${cache_filepaths}; do + echo "- $(basename ${cache_filepath})" done echo "Reading from manifest..." @@ -23,12 +23,12 @@ done echo "done." # Only search for archived results. Manifest and cache key also live here. -cache_pkg_filepaths=$(ls -1 $cache_dir/*.tar.gz | sort) -cache_pkg_filecount=$(echo $cache_pkg_filepaths | wc -w) -echo "Restoring $cache_pkg_filecount packages from cache..." -for cache_pkg_filepath in $cache_pkg_filepaths; do - echo -n "- $(basename $cache_pkg_filepath) restoring..." - sudo tar -xf $cache_pkg_filepath -C $cache_restore_root > /dev/null +cache_pkg_filepaths=$(ls -1 ${cache_dir}/*.tar.gz | sort) +cache_pkg_filecount=$(echo ${cache_pkg_filepaths} | wc -w) +echo "Restoring ${cache_pkg_filecount} packages from cache..." +for cache_pkg_filepath in ${cache_pkg_filepaths}; do + echo -n "- $(basename ${cache_pkg_filepath}) restoring..." + sudo tar -xf ${cache_pkg_filepath} -C ${cache_restore_root} > /dev/null echo "done." done echo "done." From c961be0ea58da1e5eb6b11e28e22fd1f83665613 Mon Sep 17 00:00:00 2001 From: awalsh128 Date: Thu, 30 Jun 2022 00:51:24 -0700 Subject: [PATCH 06/30] Fix lib arg ref and quoting behavior. --- lib.sh | 8 ++++---- pre_cache_action.sh | 4 ++-- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/lib.sh b/lib.sh index 2a31aac..a8011a2 100755 --- a/lib.sh +++ b/lib.sh @@ -2,15 +2,15 @@ # Sort these packages by name and split on commas. function normalize_package_list { - stripped="$(echo \"${0}\" | sed 's/,//g')" + stripped=$(echo "${1}" | 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)" + trimmed="$(echo "${stripped}" | sed 's/\s\+/ /g; s/^\s\+//g; s/\s\+$//g')" + echo "$(echo "${trimmed}" | sort)" } # Split fully qualified package into name and version function get_package_name_ver { - IFS=\= read name ver <<< "${0}" + IFS=\= read name ver <<< "${1}" # If version not found in the fully qualified package value. if test -z "${ver}"; then ver="$(grep "Version:" <<< "$(apt show ${name})" | awk '{print $2}')" diff --git a/pre_cache_action.sh b/pre_cache_action.sh index 4bd63d4..1b0c70c 100755 --- a/pre_cache_action.sh +++ b/pre_cache_action.sh @@ -5,10 +5,10 @@ script_dir="$(dirname -- "$(realpath -- "${0}")")" source "${script_dir}/lib.sh" # Directory that holds the cached packages. -cache_dir=${1} +cache_dir="${1}" # Version of the cache to create or load. -version=${2} +version="${2}" # List of the packages to use. input_packages="${@:3}" From 45b8861f3a7c4da4be7cdf6e950eab8ba54aeea6 Mon Sep 17 00:00:00 2001 From: awalsh128 Date: Thu, 30 Jun 2022 01:06:32 -0700 Subject: [PATCH 07/30] Fix package name ver function output. --- lib.sh | 2 +- pre_cache_action.sh | 5 ++--- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/lib.sh b/lib.sh index a8011a2..975fc3f 100755 --- a/lib.sh +++ b/lib.sh @@ -15,5 +15,5 @@ function get_package_name_ver { if test -z "${ver}"; then ver="$(grep "Version:" <<< "$(apt show ${name})" | awk '{print $2}')" fi - echo 'package_name="${name}"; package_ver="${ver}"' + echo ""${name}"="${ver}"" } diff --git a/pre_cache_action.sh b/pre_cache_action.sh index 1b0c70c..84d9868 100755 --- a/pre_cache_action.sh +++ b/pre_cache_action.sh @@ -41,9 +41,8 @@ for package in ${packages}; do echo "aborted." echo "Package '${package}' not found." >&2 exit 3 - fi - get_package_name_ver "${package}" # -> package_name, package_ver - versioned_packages="${versioned_packages} ${package_name}=${package_ver}" + fi + versioned_packages=""${versioned_packages}" "$(get_package_name_ver "${package}")"" done echo "done." From e6baec0bc6c0871eaee63b59fa4e0b64bf575c6b Mon Sep 17 00:00:00 2001 From: awalsh128 Date: Thu, 30 Jun 2022 01:17:13 -0700 Subject: [PATCH 08/30] Fix cache version ref and ver function output. --- lib.sh | 2 +- pre_cache_action.sh | 9 +++++---- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/lib.sh b/lib.sh index 975fc3f..0c9ddd1 100755 --- a/lib.sh +++ b/lib.sh @@ -15,5 +15,5 @@ function get_package_name_ver { if test -z "${ver}"; then ver="$(grep "Version:" <<< "$(apt show ${name})" | awk '{print $2}')" fi - echo ""${name}"="${ver}"" + echo "${name}" "${ver}" } diff --git a/pre_cache_action.sh b/pre_cache_action.sh index 84d9868..2f26954 100755 --- a/pre_cache_action.sh +++ b/pre_cache_action.sh @@ -20,7 +20,7 @@ packages="$(normalize_package_list "${input_packages}")" mkdir -p ${cache_dir} echo -n "Validating action arguments (version='${version}', packages='${packages}')..."; -if grep -q " " <<< "${cache_version}"; then +if grep -q " " <<< "${version}"; then echo "aborted." echo "Version value '${version}' cannot contain spaces." >&2 exit 1 @@ -41,8 +41,9 @@ for package in ${packages}; do echo "aborted." echo "Package '${package}' not found." >&2 exit 3 - fi - versioned_packages=""${versioned_packages}" "$(get_package_name_ver "${package}")"" + fi + read package_name package_ver < <(get_package_name_ver "${package}") # -> package_name, package_ver + versioned_packages=""${versioned_packages}" "${package_name}"="${package_ver}"" done echo "done." @@ -55,7 +56,7 @@ echo "Creating cache key..." normalized_versioned_packages="$(normalize_package_list "${versioned_packages}")" echo "- Normalized package list is '${normalized_versioned_packages}'." -value="$(echo "${normalized_versioned_packages} @ ${cache_version}")" +value="$(echo "${normalized_versioned_packages} @ ${version}")" echo "- Value to hash is '${value}'." key="$(echo "${value}" | md5sum | /bin/cut -f1 -d' ')" From 686ae5cbb1a074a8a2968d11b0c31fb16fc1c023 Mon Sep 17 00:00:00 2001 From: awalsh128 Date: Thu, 30 Jun 2022 01:27:20 -0700 Subject: [PATCH 09/30] Fix package enumeration. --- install_and_cache_pkgs.sh | 10 +++++----- pre_cache_action.sh | 2 +- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/install_and_cache_pkgs.sh b/install_and_cache_pkgs.sh index f3cdfe5..81096ed 100755 --- a/install_and_cache_pkgs.sh +++ b/install_and_cache_pkgs.sh @@ -14,12 +14,12 @@ cache_dir="${1}" input_packages="${@:2}" # Trim commas, excess spaces, and sort. -packages="$(normalize_package_list "${input_packages}")" +normalized_packages="$(normalize_package_list "${input_packages}")" -package_count=$(echo "${packages}" | wc -w) +package_count=$(echo "${normalized_packages}" | wc -w) echo "Clean installing and caching ${package_count} package(s)." echo "Package list:" -for package in "${packages}"; do +for package in ${normalized_packages}; do echo "- ${package}" done @@ -29,8 +29,8 @@ echo "done." manifest="" echo "Clean installing and caching ${package_count} packages..." -for package in "${packages}"; do - get_package_name_ver "${package}" # -> package_name, package_ver +for package in ${normalized_packages}; do + read package_name package_ver < <(get_package_name_ver "${package}") package_deps="$(apt-get install --dry-run --yes "${package_name}" | grep "^Inst" | awk '{print $2}')" echo "- ${package_name}" diff --git a/pre_cache_action.sh b/pre_cache_action.sh index 2f26954..ec33dae 100755 --- a/pre_cache_action.sh +++ b/pre_cache_action.sh @@ -42,7 +42,7 @@ for package in ${packages}; do echo "Package '${package}' not found." >&2 exit 3 fi - read package_name package_ver < <(get_package_name_ver "${package}") # -> package_name, package_ver + read package_name package_ver < <(get_package_name_ver "${package}") versioned_packages=""${versioned_packages}" "${package_name}"="${package_ver}"" done echo "done." From cb0990bfa293a145334e634cc744ccffc7364273 Mon Sep 17 00:00:00 2001 From: awalsh128 Date: Thu, 30 Jun 2022 01:35:13 -0700 Subject: [PATCH 10/30] Fix package enumeration. --- post_cache_action.sh | 2 +- restore_pkgs.sh | 12 ++++++------ 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/post_cache_action.sh b/post_cache_action.sh index 56fda54..8440aa3 100755 --- a/post_cache_action.sh +++ b/post_cache_action.sh @@ -19,7 +19,7 @@ packages="${@:4}" script_dir="$(dirname -- "$(realpath -- "${0}")")" if [ "$cache_hit" == true ]; then - ${script_dir}/restore_pkgs.sh ~/cache-apt-pkgs ${cache_restore_root} + ${script_dir}/restore_pkgs.sh ~/cache-apt-pkgs "${cache_restore_root}" else ${script_dir}/install_and_cache_pkgs.sh ~/cache-apt-pkgs ${packages} fi diff --git a/restore_pkgs.sh b/restore_pkgs.sh index 3cee703..e7b2e10 100755 --- a/restore_pkgs.sh +++ b/restore_pkgs.sh @@ -10,25 +10,25 @@ cache_dir="${1}" # Typically filesystem root '/' but can be changed for testing. cache_restore_root="${2}" -cache_filepaths=$(ls -1 ${cache_dir} | sort) +cache_filepaths="$(ls -1 "${cache_dir}" | sort)" echo "Found $(echo ${cache_filepaths} | wc -w) files in the cache." for cache_filepath in ${cache_filepaths}; do - echo "- $(basename ${cache_filepath})" + echo "- "$(basename ${cache_filepath})"" done 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 ':' ' ')" done echo "done." # Only search for archived results. Manifest and cache key also live here. -cache_pkg_filepaths=$(ls -1 ${cache_dir}/*.tar.gz | sort) +cache_pkg_filepaths=$(ls -1 "${cache_dir}"/*.tar.gz | sort) cache_pkg_filecount=$(echo ${cache_pkg_filepaths} | wc -w) echo "Restoring ${cache_pkg_filecount} packages from cache..." for cache_pkg_filepath in ${cache_pkg_filepaths}; do - echo -n "- $(basename ${cache_pkg_filepath}) restoring..." - sudo tar -xf ${cache_pkg_filepath} -C ${cache_restore_root} > /dev/null + echo -n "- $(basename "${cache_pkg_filepath}") restoring..." + sudo tar -xf "${cache_pkg_filepath}" -C "${cache_restore_root}" > /dev/null echo "done." done echo "done." From bc3229644bf719a4d335c8ba482683c248c74f65 Mon Sep 17 00:00:00 2001 From: awalsh128 Date: Thu, 30 Jun 2022 01:41:00 -0700 Subject: [PATCH 11/30] Fix another package enumeration. --- install_and_cache_pkgs.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/install_and_cache_pkgs.sh b/install_and_cache_pkgs.sh index 81096ed..21ba73e 100755 --- a/install_and_cache_pkgs.sh +++ b/install_and_cache_pkgs.sh @@ -41,7 +41,7 @@ for package in ${normalized_packages}; do sudo DEBIAN_FRONTEND=noninteractive apt-get --yes install "${package}" > /dev/null echo "done." - for cache_package in "${package_deps}"; do + for cache_package in ${package_deps}; do cache_filepath="${cache_dir}/${cache_package}.tar.gz" if test ! -f "${cache_filepath}"; then From 903a8a32ff59da642d5a1439c62b26f0ae6cb15d Mon Sep 17 00:00:00 2001 From: awalsh128 Date: Thu, 30 Jun 2022 02:10:23 -0700 Subject: [PATCH 12/30] Fix read package name and version call. --- install_and_cache_pkgs.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/install_and_cache_pkgs.sh b/install_and_cache_pkgs.sh index 21ba73e..6348403 100755 --- a/install_and_cache_pkgs.sh +++ b/install_and_cache_pkgs.sh @@ -45,7 +45,7 @@ for package in ${normalized_packages}; do cache_filepath="${cache_dir}/${cache_package}.tar.gz" if test ! -f "${cache_filepath}"; then - get_package_name_ver "${cache_package}" # -> package_name, package_ver + read package_name package_ver < <(get_package_name_ver "${cache_package}") echo -n " Caching ${package_name} to ${cache_filepath}..." # Pipe all package files (no folders) to Tar. dpkg -L "${package_name}" | From a91bf7b286265e7c644531f582c81bc0a1085bc9 Mon Sep 17 00:00:00 2001 From: awalsh128 Date: Thu, 30 Jun 2022 06:18:19 -0700 Subject: [PATCH 13/30] Create a new output for all packages including dependencies. --- README.md | 4 ++-- action.yml | 8 +++++-- install_and_cache_pkgs.sh | 50 ++++++++++++++++++++++++++------------- 3 files changed, 41 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index 927e93a..972ce8a 100644 --- a/README.md +++ b/README.md @@ -24,8 +24,8 @@ Create a workflow `.yml` file in your repositories `.github/workflows` directory ### Outputs * `cache-hit` - A boolean value to indicate a cache was found for the packages requested. -* `package-version-list` - The packages and versions that are installed as a comma delimited list with colon delimit on the package version (i.e. \:,\:\,...). - +* `package-version-list` - The main requested packages and versions that are installed. Represented as a comma delimited list with colon delimit on the package version (i.e. \:,\:\,...). +* `all-package-version-list` - All the pulled in packages and versions, including dependencies, that are installed. Represented as a comma delimited list with colon delimit on the package version (i.e. \:,\:\,...). ### Cache scopes diff --git a/action.yml b/action.yml index e717de5..c20f70a 100644 --- a/action.yml +++ b/action.yml @@ -26,7 +26,10 @@ outputs: # Need to output true and false instead of true and nothing. value: ${{ steps.load-cache.outputs.cache-hit || false }} package-version-list: - description: 'The packages and versions that are installed as a comma delimited list with colon delimit on the package version (i.e. ::).' + description: 'The main requested packages and versions that are installed. Represented as a comma delimited list with colon delimit on the package version (i.e. ::).' + value: ${{ steps.post-cache.outputs.package-version-list }} + all-package-version-list: + description: 'All the pulled in packages and versions, including dependencies, that are installed. Represented as a comma delimited list with colon delimit on the package version (i.e. ::).' value: ${{ steps.post-cache.outputs.package-version-list }} runs: @@ -54,5 +57,6 @@ runs: / \ "${{ steps.load-cache.outputs.cache-hit }}" \ ${{ inputs.packages }} - echo "::set-output name=package-version-list::$(cat ~/cache-apt-pkgs/manifest.log)" + echo "::set-output name=package-version-list::$(cat ~/cache-apt-pkgs/manifest_main.log)" + echo "::set-output name=all-package-version-list::$(cat ~/cache-apt-pkgs/manifest_all.log)" shell: bash diff --git a/install_and_cache_pkgs.sh b/install_and_cache_pkgs.sh index 6348403..0d741d9 100755 --- a/install_and_cache_pkgs.sh +++ b/install_and_cache_pkgs.sh @@ -16,7 +16,7 @@ input_packages="${@:2}" # Trim commas, excess spaces, and sort. normalized_packages="$(normalize_package_list "${input_packages}")" -package_count=$(echo "${normalized_packages}" | wc -w) +package_count=$(wc -w <<< "${normalized_packages}") echo "Clean installing and caching ${package_count} package(s)." echo "Package list:" for package in ${normalized_packages}; do @@ -27,42 +27,58 @@ echo -n "Updating APT package list..." sudo apt-get update > /dev/null echo "done." -manifest="" +# Strictly contains the requested packages. +manifest_main="" +# Contains all packages including dependencies. +manifest_all="" + echo "Clean installing and caching ${package_count} packages..." for package in ${normalized_packages}; do - read package_name package_ver < <(get_package_name_ver "${package}") - package_deps="$(apt-get install --dry-run --yes "${package_name}" | grep "^Inst" | awk '{print $2}')" + read package_name package_ver < <(get_package_name_ver "${package}") + + # Comma delimited name:ver pairs in the main requested packages manifest. + manifest_main="${manifest_main}${package_name}:${package_ver}," + + all_packages="$(apt-get install --dry-run --yes "${package_name}" | grep "^Inst" | awk '{print $2}')" + dep_packages="$(grep -v "${package_name}" <<< ${all_packages})" echo "- ${package_name}" echo " * Version: ${package_ver}" - echo " * Dependencies: ${package_deps}" + echo " * Dependencies: ${dep_packages}" 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." - for cache_package in ${package_deps}; do + for cache_package in ${all_packages}; do cache_filepath="${cache_dir}/${cache_package}.tar.gz" if test ! -f "${cache_filepath}"; then - read package_name package_ver < <(get_package_name_ver "${cache_package}") - echo -n " Caching ${package_name} to ${cache_filepath}..." + read cache_package_name cache_package_ver < <(get_package_name_ver "${cache_package}") + echo -n " Caching ${cache_package_name} to ${cache_filepath}..." # Pipe all package files (no folders) to Tar. - dpkg -L "${package_name}" | + dpkg -L "${cache_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}')," + echo "done." fi - done + + # Comma delimited name:ver pairs in the all packages manifest. + manifest_all="${manifest_all}${cache_package_name}:${cache_package_ver}," + done done echo "done." -manifest_filepath="${cache_dir}/manifest.log" -echo -n "Writing package manifest to ${manifest_filepath}..." -# Remove trailing comma and write to manifest file. -echo "${manifest:0:-1}" > "${manifest_filepath}" +manifest_all_filepath="${cache_dir}/manifest_all.log" +echo -n "Writing all packages manifest to ${manifest_all_filepath}..." +# Remove trailing comma and write to manifest_all file. +echo "${manifest_all:0:-1}" > "${manifest_all_filepath}" +echo "done." + +manifest_main_filepath="${cache_dir}/manifest_main.log" +echo -n "Writing main requested packages manifest to ${manifest_main_filepath}..." +# Remove trailing comma and write to manifest_main file. +echo "${manifest_main:0:-1}" > "${manifest_main_filepath}" echo "done." \ No newline at end of file From f9b0016d025185cfddbff87dbb6645eb6ced7963 Mon Sep 17 00:00:00 2001 From: awalsh128 Date: Thu, 30 Jun 2022 06:28:52 -0700 Subject: [PATCH 14/30] Inline dependency reporting in log. --- install_and_cache_pkgs.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/install_and_cache_pkgs.sh b/install_and_cache_pkgs.sh index 0d741d9..1b49407 100755 --- a/install_and_cache_pkgs.sh +++ b/install_and_cache_pkgs.sh @@ -40,11 +40,11 @@ for package in ${normalized_packages}; do manifest_main="${manifest_main}${package_name}:${package_ver}," all_packages="$(apt-get install --dry-run --yes "${package_name}" | grep "^Inst" | awk '{print $2}')" - dep_packages="$(grep -v "${package_name}" <<< ${all_packages})" + dep_packages="$(echo ${dep_packages} | grep -v "${package_name}" | tr '\n' ,)" echo "- ${package_name}" echo " * Version: ${package_ver}" - echo " * Dependencies: ${dep_packages}" + echo " * Dependencies: ${dep_packages:0:-1}" echo -n " * Installing..." # Zero interaction while installing or upgrading the system via apt. sudo DEBIAN_FRONTEND=noninteractive apt-get --yes install "${package}" > /dev/null From f5249e37b07a3284b58eb02d06988df145ed30be Mon Sep 17 00:00:00 2001 From: awalsh128 Date: Thu, 30 Jun 2022 06:29:47 -0700 Subject: [PATCH 15/30] Update caching log lines. --- install_and_cache_pkgs.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/install_and_cache_pkgs.sh b/install_and_cache_pkgs.sh index 1b49407..e6533a1 100755 --- a/install_and_cache_pkgs.sh +++ b/install_and_cache_pkgs.sh @@ -55,7 +55,7 @@ for package in ${normalized_packages}; do if test ! -f "${cache_filepath}"; then read cache_package_name cache_package_ver < <(get_package_name_ver "${cache_package}") - echo -n " Caching ${cache_package_name} to ${cache_filepath}..." + echo -n " * Caching ${cache_package_name} to ${cache_filepath}..." # Pipe all package files (no folders) to Tar. dpkg -L "${cache_package_name}" | while IFS= read -r f; do From 843a78dbf3681d50a16477ca2eea8556a077104a Mon Sep 17 00:00:00 2001 From: awalsh128 Date: Thu, 30 Jun 2022 06:37:31 -0700 Subject: [PATCH 16/30] Fix manifest output on restore. --- restore_pkgs.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/restore_pkgs.sh b/restore_pkgs.sh index e7b2e10..655490b 100755 --- a/restore_pkgs.sh +++ b/restore_pkgs.sh @@ -16,8 +16,8 @@ for cache_filepath in ${cache_filepaths}; do echo "- "$(basename ${cache_filepath})"" done -echo "Reading from manifest..." -for logline in $(cat "${cache_dir}/manifest.log" | tr ',' '\n' ); do +echo "Reading from main requested packages manifest..." +for logline in $(cat "${cache_dir}/manifest_main.log" | tr ',' '\n' ); do echo "- $(echo "${logline}" | tr ':' ' ')" done echo "done." From eb177a2a2b2cc7c73fac60fdecce070222db12b9 Mon Sep 17 00:00:00 2001 From: awalsh128 Date: Thu, 30 Jun 2022 06:43:45 -0700 Subject: [PATCH 17/30] Report file size of cached packages. --- install_and_cache_pkgs.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/install_and_cache_pkgs.sh b/install_and_cache_pkgs.sh index e6533a1..e4fef7f 100755 --- a/install_and_cache_pkgs.sh +++ b/install_and_cache_pkgs.sh @@ -61,8 +61,8 @@ for package in ${normalized_packages}; do 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." + xargs tar -czf "${cache_filepath}" -C / + echo "done (compressed size $(du -k "${cache_filepath}" | cut -f1))." fi # Comma delimited name:ver pairs in the all packages manifest. From f6ea1022c2cb59d180889e04fca5de1516b711ee Mon Sep 17 00:00:00 2001 From: awalsh128 Date: Thu, 30 Jun 2022 07:13:58 -0700 Subject: [PATCH 18/30] Add log timings for perf debugging. --- install_and_cache_pkgs.sh | 28 ++++++++++++++-------------- lib.sh | 10 ++++++++++ pre_cache_action.sh | 28 ++++++++++++++-------------- restore_pkgs.sh | 16 ++++++++-------- 4 files changed, 46 insertions(+), 36 deletions(-) diff --git a/install_and_cache_pkgs.sh b/install_and_cache_pkgs.sh index e4fef7f..f8e9ce0 100755 --- a/install_and_cache_pkgs.sh +++ b/install_and_cache_pkgs.sh @@ -17,22 +17,22 @@ input_packages="${@:2}" normalized_packages="$(normalize_package_list "${input_packages}")" package_count=$(wc -w <<< "${normalized_packages}") -echo "Clean installing and caching ${package_count} package(s)." -echo "Package list:" +log "Clean installing and caching ${package_count} package(s)." +log "Package list:" for package in ${normalized_packages}; do - echo "- ${package}" + log "- ${package}" done -echo -n "Updating APT package list..." +log -n "Updating APT package list..." sudo apt-get update > /dev/null -echo "done." +"done." # Strictly contains the requested packages. manifest_main="" # Contains all packages including dependencies. manifest_all="" -echo "Clean installing and caching ${package_count} packages..." +log "Clean installing and caching ${package_count} packages..." for package in ${normalized_packages}; do read package_name package_ver < <(get_package_name_ver "${package}") @@ -42,10 +42,10 @@ for package in ${normalized_packages}; do all_packages="$(apt-get install --dry-run --yes "${package_name}" | grep "^Inst" | awk '{print $2}')" dep_packages="$(echo ${dep_packages} | grep -v "${package_name}" | tr '\n' ,)" - echo "- ${package_name}" - echo " * Version: ${package_ver}" - echo " * Dependencies: ${dep_packages:0:-1}" - echo -n " * Installing..." + log "- ${package_name}" + log " * Version: ${package_ver}" + log " * Dependencies: ${dep_packages:0:-1}" + log -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." @@ -55,7 +55,7 @@ for package in ${normalized_packages}; do if test ! -f "${cache_filepath}"; then read cache_package_name cache_package_ver < <(get_package_name_ver "${cache_package}") - echo -n " * Caching ${cache_package_name} to ${cache_filepath}..." + log -n " * Caching ${cache_package_name} to ${cache_filepath}..." # Pipe all package files (no folders) to Tar. dpkg -L "${cache_package_name}" | while IFS= read -r f; do @@ -69,16 +69,16 @@ for package in ${normalized_packages}; do manifest_all="${manifest_all}${cache_package_name}:${cache_package_ver}," done done -echo "done." +log "done." manifest_all_filepath="${cache_dir}/manifest_all.log" -echo -n "Writing all packages manifest to ${manifest_all_filepath}..." +log -n "Writing all packages manifest to ${manifest_all_filepath}..." # Remove trailing comma and write to manifest_all file. echo "${manifest_all:0:-1}" > "${manifest_all_filepath}" echo "done." manifest_main_filepath="${cache_dir}/manifest_main.log" -echo -n "Writing main requested packages manifest to ${manifest_main_filepath}..." +log -n "Writing main requested packages manifest to ${manifest_main_filepath}..." # Remove trailing comma and write to manifest_main file. echo "${manifest_main:0:-1}" > "${manifest_main_filepath}" echo "done." \ No newline at end of file diff --git a/lib.sh b/lib.sh index 0c9ddd1..2c216be 100755 --- a/lib.sh +++ b/lib.sh @@ -17,3 +17,13 @@ function get_package_name_ver { fi echo "${name}" "${ver}" } + +function log { + timestamp="$(echo -n "$(date +%H:%M:%S)")" + line=""${timestamp}" "$(echo ${@})"" + if [[ "${1}" == "-n" ]]; then + echo -n "${line}" + else + echo "${line}" + fi +} \ No newline at end of file diff --git a/pre_cache_action.sh b/pre_cache_action.sh index ec33dae..19f6594 100755 --- a/pre_cache_action.sh +++ b/pre_cache_action.sh @@ -19,27 +19,27 @@ packages="$(normalize_package_list "${input_packages}")" # Create cache directory so artifacts can be saved. mkdir -p ${cache_dir} -echo -n "Validating action arguments (version='${version}', packages='${packages}')..."; +log -n "Validating action arguments (version='${version}', packages='${packages}')..."; if grep -q " " <<< "${version}"; then - echo "aborted." - echo "Version value '${version}' cannot contain spaces." >&2 + log "aborted." + log "Version value '${version}' cannot contain spaces." >&2 exit 1 fi # Is length of string zero? if test -z "${packages}"; then - echo "aborted." - echo "Packages argument cannot be empty." >&2 + log "aborted." + log "Packages argument cannot be empty." >&2 exit 2 fi -echo "done." +log "done." versioned_packages="" -echo -n "Verifying packages..." +log -n "Verifying packages..." for package in ${packages}; do if test ! "$(apt show "${package}")"; then echo "aborted." - echo "Package '${package}' not found." >&2 + log "Package '${package}' not found." >&2 exit 3 fi read package_name package_ver < <(get_package_name_ver "${package}") @@ -50,20 +50,20 @@ echo "done." # Abort on any failure at this point. set -e -echo "Creating cache key..." +log "Creating cache key..." # TODO Can we prove this will happen again? normalized_versioned_packages="$(normalize_package_list "${versioned_packages}")" -echo "- Normalized package list is '${normalized_versioned_packages}'." +log "- Normalized package list is '${normalized_versioned_packages}'." value="$(echo "${normalized_versioned_packages} @ ${version}")" -echo "- Value to hash is '${value}'." +log "- Value to hash is '${value}'." key="$(echo "${value}" | md5sum | /bin/cut -f1 -d' ')" -echo "- Value hashed as '${key}'." +log "- Value hashed as '${key}'." -echo "done." +log "done." key_filepath="${cache_dir}/cache_key.md5" echo ${key} > ${key_filepath} -echo "Hash value written to ${key_filepath}" +log "Hash value written to ${key_filepath}" diff --git a/restore_pkgs.sh b/restore_pkgs.sh index 655490b..647a2aa 100755 --- a/restore_pkgs.sh +++ b/restore_pkgs.sh @@ -11,24 +11,24 @@ cache_dir="${1}" cache_restore_root="${2}" cache_filepaths="$(ls -1 "${cache_dir}" | sort)" -echo "Found $(echo ${cache_filepaths} | wc -w) files in the cache." +log "Found $(echo ${cache_filepaths} | wc -w) files in the cache." for cache_filepath in ${cache_filepaths}; do - echo "- "$(basename ${cache_filepath})"" + log "- "$(basename ${cache_filepath})"" done -echo "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 - echo "- $(echo "${logline}" | tr ':' ' ')" + log "- $(echo "${logline}" | tr ':' ' ')" done -echo "done." +log "done." # Only search for archived results. Manifest and cache key also live here. cache_pkg_filepaths=$(ls -1 "${cache_dir}"/*.tar.gz | sort) cache_pkg_filecount=$(echo ${cache_pkg_filepaths} | wc -w) -echo "Restoring ${cache_pkg_filecount} packages from cache..." +log "Restoring ${cache_pkg_filecount} packages from cache..." for cache_pkg_filepath in ${cache_pkg_filepaths}; do - echo -n "- $(basename "${cache_pkg_filepath}") restoring..." + log -n "- $(basename "${cache_pkg_filepath}") restoring..." sudo tar -xf "${cache_pkg_filepath}" -C "${cache_restore_root}" > /dev/null echo "done." done -echo "done." +log "done." From 7274c01428dc0e8a01a993528db00af76a217b6c Mon Sep 17 00:00:00 2001 From: awalsh128 Date: Thu, 30 Jun 2022 08:24:18 -0700 Subject: [PATCH 19/30] Fix minor bug. --- install_and_cache_pkgs.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/install_and_cache_pkgs.sh b/install_and_cache_pkgs.sh index f8e9ce0..e22cabd 100755 --- a/install_and_cache_pkgs.sh +++ b/install_and_cache_pkgs.sh @@ -25,7 +25,7 @@ done log -n "Updating APT package list..." sudo apt-get update > /dev/null -"done." +echo "done." # Strictly contains the requested packages. manifest_main="" From 60c15ce2f095e46d08d546a6c8d22274083a2148 Mon Sep 17 00:00:00 2001 From: awalsh128 Date: Thu, 30 Jun 2022 08:28:26 -0700 Subject: [PATCH 20/30] Include library in all scripts. --- post_cache_action.sh | 4 ++++ restore_pkgs.sh | 4 ++++ 2 files changed, 8 insertions(+) diff --git a/post_cache_action.sh b/post_cache_action.sh index 8440aa3..144fe76 100755 --- a/post_cache_action.sh +++ b/post_cache_action.sh @@ -3,6 +3,10 @@ # Fail on any error. set -e +# Include library. +script_dir="$(dirname -- "$(realpath -- "${0}")")" +source "${script_dir}/lib.sh" + # Directory that holds the cached packages. cache_dir="${1}" diff --git a/restore_pkgs.sh b/restore_pkgs.sh index 647a2aa..d056630 100755 --- a/restore_pkgs.sh +++ b/restore_pkgs.sh @@ -3,6 +3,10 @@ # Fail on any error. set -e +# Include library. +script_dir="$(dirname -- "$(realpath -- "${0}")")" +source "${script_dir}/lib.sh" + # Directory that holds the cached packages. cache_dir="${1}" From 768417e4ec4577eba696a089c7d63892db492d16 Mon Sep 17 00:00:00 2001 From: awalsh128 Date: Thu, 30 Jun 2022 08:41:19 -0700 Subject: [PATCH 21/30] Fix dependency listing. --- install_and_cache_pkgs.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/install_and_cache_pkgs.sh b/install_and_cache_pkgs.sh index e22cabd..206acb0 100755 --- a/install_and_cache_pkgs.sh +++ b/install_and_cache_pkgs.sh @@ -40,7 +40,7 @@ for package in ${normalized_packages}; do manifest_main="${manifest_main}${package_name}:${package_ver}," all_packages="$(apt-get install --dry-run --yes "${package_name}" | grep "^Inst" | awk '{print $2}')" - dep_packages="$(echo ${dep_packages} | grep -v "${package_name}" | tr '\n' ,)" + dep_packages="$(echo ${all_packages} | grep -v "${package_name}" | tr '\n' ,)" log "- ${package_name}" log " * Version: ${package_ver}" From 39faaf9bee7f289b21cf9f43ea4a6750c82619cc Mon Sep 17 00:00:00 2001 From: Andrew Walsh Date: Thu, 14 Jul 2022 21:19:19 -0700 Subject: [PATCH 22/30] Update installation to consider symlinks as well. Addresses problem raised in #25 --- install_and_cache_pkgs.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/install_and_cache_pkgs.sh b/install_and_cache_pkgs.sh index 206acb0..4411e24 100755 --- a/install_and_cache_pkgs.sh +++ b/install_and_cache_pkgs.sh @@ -59,7 +59,7 @@ for package in ${normalized_packages}; do # Pipe all package files (no folders) to Tar. dpkg -L "${cache_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 + if test -f $f || test -L $f; then echo "${f:1}"; fi; #${f:1} removes the leading slash that Tar disallows done | xargs tar -czf "${cache_filepath}" -C / echo "done (compressed size $(du -k "${cache_filepath}" | cut -f1))." @@ -81,4 +81,4 @@ manifest_main_filepath="${cache_dir}/manifest_main.log" log -n "Writing main requested packages manifest to ${manifest_main_filepath}..." # Remove trailing comma and write to manifest_main file. echo "${manifest_main:0:-1}" > "${manifest_main_filepath}" -echo "done." \ No newline at end of file +echo "done." From c21469a518328262c15cf717ef2ce3fddf522048 Mon Sep 17 00:00:00 2001 From: Andrew Walsh Date: Thu, 14 Jul 2022 21:23:42 -0700 Subject: [PATCH 23/30] Update installation to consider symlinks as well. Addresses problem raised in #25 --- install_and_cache_pkgs.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/install_and_cache_pkgs.sh b/install_and_cache_pkgs.sh index bbf54dc..4de889d 100755 --- a/install_and_cache_pkgs.sh +++ b/install_and_cache_pkgs.sh @@ -33,7 +33,7 @@ for package in $packages; do # Pipe all package files (no folders) to Tar. dpkg -L $package | while IFS= read -r f; do - if test -f $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 | xargs tar -czf $cache_filepath -C / echo "done." @@ -48,4 +48,4 @@ for package in $packages; do done # Remove trailing comma. echo ${manifest:0:-1} > $manifest_filepath -echo "done." \ No newline at end of file +echo "done." From 63f7fccab9c795693ca5a4bfeab0da91271d9478 Mon Sep 17 00:00:00 2001 From: awalsh128 Date: Thu, 14 Jul 2022 21:44:35 -0700 Subject: [PATCH 24/30] Simplify logging calls. --- install_and_cache_pkgs.sh | 21 ++++++++++++--------- lib.sh | 16 ++++------------ pre_cache_action.sh | 2 +- restore_pkgs.sh | 4 ++-- 4 files changed, 19 insertions(+), 24 deletions(-) diff --git a/install_and_cache_pkgs.sh b/install_and_cache_pkgs.sh index 206acb0..23580e0 100755 --- a/install_and_cache_pkgs.sh +++ b/install_and_cache_pkgs.sh @@ -23,7 +23,7 @@ for package in ${normalized_packages}; do log "- ${package}" done -log -n "Updating APT package list..." +log "Updating APT package list..." sudo apt-get update > /dev/null echo "done." @@ -41,11 +41,14 @@ for package in ${normalized_packages}; do all_packages="$(apt-get install --dry-run --yes "${package_name}" | grep "^Inst" | awk '{print $2}')" dep_packages="$(echo ${all_packages} | grep -v "${package_name}" | tr '\n' ,)" + if "${dep_packages}" == ","; then + dep_packages="none"; + fi log "- ${package_name}" log " * Version: ${package_ver}" - log " * Dependencies: ${dep_packages:0:-1}" - log -n " * Installing..." + log " * Dependencies: ${dep_packages}" + log " * Installing..." # Zero interaction while installing or upgrading the system via apt. sudo DEBIAN_FRONTEND=noninteractive apt-get --yes install "${package}" > /dev/null echo "done." @@ -55,14 +58,14 @@ for package in ${normalized_packages}; do if test ! -f "${cache_filepath}"; then read cache_package_name cache_package_ver < <(get_package_name_ver "${cache_package}") - log -n " * Caching ${cache_package_name} to ${cache_filepath}..." + log " * Caching ${cache_package_name} to ${cache_filepath}..." # Pipe all package files (no folders) to Tar. dpkg -L "${cache_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 (compressed size $(du -k "${cache_filepath}" | cut -f1))." + log "done (compressed size $(du -k "${cache_filepath}" | cut -f1))." fi # Comma delimited name:ver pairs in the all packages manifest. @@ -72,13 +75,13 @@ done log "done." manifest_all_filepath="${cache_dir}/manifest_all.log" -log -n "Writing all packages manifest to ${manifest_all_filepath}..." +log "Writing all packages manifest to ${manifest_all_filepath}..." # Remove trailing comma and write to manifest_all file. echo "${manifest_all:0:-1}" > "${manifest_all_filepath}" -echo "done." +log "done." manifest_main_filepath="${cache_dir}/manifest_main.log" -log -n "Writing main requested packages manifest to ${manifest_main_filepath}..." +log "Writing main requested packages manifest to ${manifest_main_filepath}..." # Remove trailing comma and write to manifest_main file. echo "${manifest_main:0:-1}" > "${manifest_main_filepath}" -echo "done." \ No newline at end of file +log "done." \ No newline at end of file diff --git a/lib.sh b/lib.sh index 2c216be..0a9c580 100755 --- a/lib.sh +++ b/lib.sh @@ -2,10 +2,10 @@ # Sort these packages by name and split on commas. function normalize_package_list { - stripped=$(echo "${1}" | sed 's/,//g') + local stripped=$(echo "${1}" | 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 "$(echo "${trimmed}" | sort)" + local trimmed="$(echo "${stripped}" | sed 's/\s\+/ /g; s/^\s\+//g; s/\s\+$//g')" + echo "${trimmed}" | sort } # Split fully qualified package into name and version @@ -18,12 +18,4 @@ function get_package_name_ver { echo "${name}" "${ver}" } -function log { - timestamp="$(echo -n "$(date +%H:%M:%S)")" - line=""${timestamp}" "$(echo ${@})"" - if [[ "${1}" == "-n" ]]; then - echo -n "${line}" - else - echo "${line}" - fi -} \ No newline at end of file +function log { echo "$(date +%H:%M:%S)" "${@}"; } \ No newline at end of file diff --git a/pre_cache_action.sh b/pre_cache_action.sh index 19f6594..7921adb 100755 --- a/pre_cache_action.sh +++ b/pre_cache_action.sh @@ -56,7 +56,7 @@ log "Creating cache key..." normalized_versioned_packages="$(normalize_package_list "${versioned_packages}")" log "- Normalized package list is '${normalized_versioned_packages}'." -value="$(echo "${normalized_versioned_packages} @ ${version}")" +value="${normalized_versioned_packages} @ ${version}" log "- Value to hash is '${value}'." key="$(echo "${value}" | md5sum | /bin/cut -f1 -d' ')" diff --git a/restore_pkgs.sh b/restore_pkgs.sh index d056630..ec6c475 100755 --- a/restore_pkgs.sh +++ b/restore_pkgs.sh @@ -31,8 +31,8 @@ cache_pkg_filepaths=$(ls -1 "${cache_dir}"/*.tar.gz | sort) cache_pkg_filecount=$(echo ${cache_pkg_filepaths} | wc -w) log "Restoring ${cache_pkg_filecount} packages from cache..." for cache_pkg_filepath in ${cache_pkg_filepaths}; do - log -n "- $(basename "${cache_pkg_filepath}") restoring..." + log "- $(basename "${cache_pkg_filepath}") restoring..." sudo tar -xf "${cache_pkg_filepath}" -C "${cache_restore_root}" > /dev/null - echo "done." + log "done." done log "done." From fa7091e9a8c98a3c44faf1a7df8339f90cdf291b Mon Sep 17 00:00:00 2001 From: awalsh128 Date: Thu, 14 Jul 2022 21:57:20 -0700 Subject: [PATCH 25/30] Fix sorting when normalizing package list. --- lib.sh | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lib.sh b/lib.sh index 0a9c580..f3757c9 100755 --- a/lib.sh +++ b/lib.sh @@ -4,8 +4,9 @@ function normalize_package_list { local stripped=$(echo "${1}" | sed 's/,//g') # Remove extraneous spaces at the middle, beginning, and end. - local trimmed="$(echo "${stripped}" | sed 's/\s\+/ /g; s/^\s\+//g; s/\s\+$//g')" - echo "${trimmed}" | sort + local trimmed="$(echo "${stripped}" | sed 's/\s\+/ /g; s/^\s\+//g; s/\s\+$//g')" + local sorted="$(echo ${trimmed} | tr ' ' '\n' | sort | tr '\n' ' ')" + echo "${sorted}" } # Split fully qualified package into name and version From cda771820dacfe61a44891f26fc5f7445ad08f76 Mon Sep 17 00:00:00 2001 From: Andrew Walsh Date: Fri, 15 Jul 2022 12:55:37 -0700 Subject: [PATCH 26/30] Update to v3 cache action. --- action.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/action.yml b/action.yml index e717de5..3ef2f6a 100644 --- a/action.yml +++ b/action.yml @@ -42,7 +42,7 @@ runs: shell: bash - id: load-cache - uses: actions/cache@v2 + uses: actions/cache@v3 with: path: ~/cache-apt-pkgs key: cache-apt-pkgs_${{ env.CACHE_KEY }} From d5b9449cd38fc926b605bde918a404c876fc66c4 Mon Sep 17 00:00:00 2001 From: awalsh128 Date: Fri, 15 Jul 2022 15:38:51 -0700 Subject: [PATCH 27/30] Merge script include snippet. --- install_and_cache_pkgs.sh | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/install_and_cache_pkgs.sh b/install_and_cache_pkgs.sh index 467f980..2c22fb2 100755 --- a/install_and_cache_pkgs.sh +++ b/install_and_cache_pkgs.sh @@ -3,8 +3,12 @@ # Fail on any error. set -e +# Include library. +script_dir="$(dirname -- "$(realpath -- "${0}")")" +source "${script_dir}/lib.sh" + # Directory that holds the cached packages. -cache_dir=$1 +cache_dir="${1}" # List of the packages to use. input_packages="${@:2}" @@ -81,4 +85,3 @@ log "Writing main requested packages manifest to ${manifest_main_filepath}..." # Remove trailing comma and write to manifest_main file. echo "${manifest_main:0:-1}" > "${manifest_main_filepath}" log "done." - From 053557ecd1faef084fb18e80f6847038dd0f5925 Mon Sep 17 00:00:00 2001 From: awalsh128 Date: Fri, 15 Jul 2022 16:11:14 -0700 Subject: [PATCH 28/30] Fix all packages reporting. --- install_and_cache_pkgs.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/install_and_cache_pkgs.sh b/install_and_cache_pkgs.sh index 2c22fb2..79389dd 100755 --- a/install_and_cache_pkgs.sh +++ b/install_and_cache_pkgs.sh @@ -39,8 +39,8 @@ for package in ${normalized_packages}; do # Comma delimited name:ver pairs in the main requested packages manifest. manifest_main="${manifest_main}${package_name}:${package_ver}," - all_packages="$(apt-get install --dry-run --yes "${package_name}" | grep "^Inst" | awk '{print $2}')" - dep_packages="$(echo ${all_packages} | grep -v "${package_name}" | tr '\n' ,)" + all_packages="$(apt-get install --dry-run --yes "${package_name}" | grep "^Inst" | sort | awk '{print $2 $3}' | tr '(' ':'))" + dep_packages="$(echo "${all_packages}" | grep -v "${package_name}" | tr '\n' ,)" if "${dep_packages}" == ","; then dep_packages="none"; fi From f20c69935b0e8fe6274e57c6ab57a93ceb2c44fa Mon Sep 17 00:00:00 2001 From: awalsh128 Date: Fri, 15 Jul 2022 16:14:30 -0700 Subject: [PATCH 29/30] Minor bug. --- install_and_cache_pkgs.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/install_and_cache_pkgs.sh b/install_and_cache_pkgs.sh index 79389dd..61f08a1 100755 --- a/install_and_cache_pkgs.sh +++ b/install_and_cache_pkgs.sh @@ -39,7 +39,7 @@ for package in ${normalized_packages}; do # Comma delimited name:ver pairs in the main requested packages manifest. manifest_main="${manifest_main}${package_name}:${package_ver}," - all_packages="$(apt-get install --dry-run --yes "${package_name}" | grep "^Inst" | sort | awk '{print $2 $3}' | tr '(' ':'))" + all_packages="$(apt-get install --dry-run --yes "${package_name}" | grep "^Inst" | sort | awk '{print $2 $3}' | tr '(' ':')" dep_packages="$(echo "${all_packages}" | grep -v "${package_name}" | tr '\n' ,)" if "${dep_packages}" == ","; then dep_packages="none"; From 0735dbdc2248bbd7190763af7f5ef88bbb8db3f9 Mon Sep 17 00:00:00 2001 From: awalsh128 Date: Tue, 19 Jul 2022 19:53:44 -0700 Subject: [PATCH 30/30] Update package gathering. --- install_and_cache_pkgs.sh | 17 +++++++++-------- lib.sh | 10 +++++++++- 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/install_and_cache_pkgs.sh b/install_and_cache_pkgs.sh index 61f08a1..1d420b2 100755 --- a/install_and_cache_pkgs.sh +++ b/install_and_cache_pkgs.sh @@ -39,21 +39,22 @@ for package in ${normalized_packages}; do # Comma delimited name:ver pairs in the main requested packages manifest. manifest_main="${manifest_main}${package_name}:${package_ver}," - all_packages="$(apt-get install --dry-run --yes "${package_name}" | grep "^Inst" | sort | awk '{print $2 $3}' | tr '(' ':')" - dep_packages="$(echo "${all_packages}" | grep -v "${package_name}" | tr '\n' ,)" - if "${dep_packages}" == ","; then - dep_packages="none"; + read dep_packages < <(get_dep_packages "${package_name}") + if test -z "${dep_packages}"; then + dep_packages_text="none"; + else + dep_packages_text="${dep_packages}" fi log "- ${package_name}" log " * Version: ${package_ver}" - log " * Dependencies: ${dep_packages}" + log " * Dependencies: ${dep_packages_text}" log " * 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_name}" > /dev/null echo "done." - for cache_package in ${all_packages}; do + for cache_package in ${package_name}:${package_ver} ${dep_packages}; do cache_filepath="${cache_dir}/${cache_package}.tar.gz" if test ! -f "${cache_filepath}"; then @@ -65,7 +66,7 @@ for package in ${normalized_packages}; do if test -f $f || test -L $f; then echo "${f:1}"; fi; #${f:1} removes the leading slash that Tar disallows done | xargs tar -czf "${cache_filepath}" -C / - log "done (compressed size $(du -k "${cache_filepath}" | cut -f1))." + log "done (compressed size $(du -h "${cache_filepath}" | cut -f1))." fi # Comma delimited name:ver pairs in the all packages manifest. diff --git a/lib.sh b/lib.sh index f3757c9..fc8eb1e 100755 --- a/lib.sh +++ b/lib.sh @@ -9,9 +9,17 @@ function normalize_package_list { echo "${sorted}" } +# Gets a package list of dependencies as common delimited pairs +# :,... +function get_dep_packages { + echo $(apt-get install --dry-run --yes "${1}" | \ + grep "^Inst" | sort | awk '{print $2 $3}' | \ + tr '(' ':' | grep -v "${1}:") +} + # Split fully qualified package into name and version 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 test -z "${ver}"; then ver="$(grep "Version:" <<< "$(apt show ${name})" | awk '{print $2}')"