From 63736e2a815e5b1208397795c27aac7649bd1ca1 Mon Sep 17 00:00:00 2001 From: awalsh128 Date: Sat, 19 Nov 2022 18:53:52 -0800 Subject: [PATCH] Debug feature. --- action.yml | 4 +++- install_and_cache_pkgs.sh | 16 +++++++++++++--- lib.sh | 17 +++++++++++++++++ post_cache_action.sh | 12 +++++++----- pre_cache_action.sh | 23 ++++++++++++++--------- restore_pkgs.sh | 5 +++++ 6 files changed, 59 insertions(+), 18 deletions(-) diff --git a/action.yml b/action.yml index 46d3d19..8d85199 100644 --- a/action.yml +++ b/action.yml @@ -47,6 +47,7 @@ runs: ~/cache-apt-pkgs \ "${{ inputs.version }}" \ "${{ inputs.execute_install_scripts }}" \ + "${{ inputs.debug }}" \ ${{ inputs.packages }} echo "CACHE_KEY=$(cat ~/cache-apt-pkgs/cache_key.md5)" >> $GITHUB_ENV shell: bash @@ -64,6 +65,7 @@ runs: / \ "${{ steps.load-cache.outputs.cache-hit }}" \ "${{ inputs.execute_install_scripts }}" \ + "${{ inputs.debug }}" \ ${{ inputs.packages }} function create_list { local list=$(cat ~/cache-apt-pkgs/manifest_${1}.log | tr '\n' ','); echo ${list:0:-1}; }; echo "name=package-version-list::$(create_list main)" >> $GITHUB_OUTPUT @@ -74,5 +76,5 @@ runs: if: ${{ inputs.debug }} == "true" uses: actions/upload-artifact@v3 with: - name: ${{ inputs.packages }}%${{ inputs.version }} + name: cache-apt-pkgs-logs%${{ inputs.packages }}%${{ inputs.version }} path: ~/cache-apt-pkgs/*.log diff --git a/install_and_cache_pkgs.sh b/install_and_cache_pkgs.sh index ff0f356..e2dd914 100755 --- a/install_and_cache_pkgs.sh +++ b/install_and_cache_pkgs.sh @@ -3,6 +3,11 @@ # Fail on any error. set -e +# Debug mode for diagnosing issues. +# Setup first before other operations. +debug="${2}" +test ${debug} == "true" && set -x + # Include library. script_dir="$(dirname -- "$(realpath -- "${0}")")" source "${script_dir}/lib.sh" @@ -11,7 +16,7 @@ source "${script_dir}/lib.sh" cache_dir="${1}" # List of the packages to use. -input_packages="${@:2}" +input_packages="${@:3}" # Trim commas, excess spaces, and sort. normalized_packages="$(normalize_package_list "${input_packages}")" @@ -33,8 +38,13 @@ write_manifest "main" "${manifest_main}" "${cache_dir}/manifest_main.log" log_empty_line log "Updating APT package list..." -sudo apt-fast update > /dev/null -log "done" +last_update_delta_s=$(($(date +%s) - $(date +%s -r /var/cache/apt/pkgcache.bin))) +if test $last_update_delta_s -gt 300; then + sudo apt-fast update > /dev/null + log "done" +else + log "skipped (fresh by ${last_update_delta_s} seconds)" +fi log_empty_line diff --git a/lib.sh b/lib.sh index fa11143..c2f1ec9 100755 --- a/lib.sh +++ b/lib.sh @@ -134,6 +134,23 @@ function normalize_package_list { echo "${sorted}" } +############################################################################### +# Validates an argument to be of a boolean value. +# Arguments: +# Argument to validate. +# Variable name of the argument. +# Exit code if validation fails. +# Returns: +# Sorted list of space delimited packages. +############################################################################### +function validate_bool { + if test "${1}" != "true" -a "${1}" != "false"; then + log "aborted" + log "${2} value '${1}' must be either true or false (case sensitive)." + exit ${3} + fi +} + ############################################################################### # Writes the manifest to a specified file. # Arguments: diff --git a/post_cache_action.sh b/post_cache_action.sh index 55d45d0..25e7da3 100755 --- a/post_cache_action.sh +++ b/post_cache_action.sh @@ -21,15 +21,17 @@ cache_hit="${3}" # Cache and execute post install scripts on restore. execute_install_scripts="${4}" -# List of the packages to use. -packages="${@:5}" +# Debug mode for diagnosing issues. +debug="${5}" +test ${debug} == "true" && set -x -script_dir="$(dirname -- "$(realpath -- "${0}")")" +# List of the packages to use. +packages="${@:6}" if [ "$cache_hit" == true ]; then - ${script_dir}/restore_pkgs.sh "${cache_dir}" "${cache_restore_root}" "${execute_install_scripts}" + ${script_dir}/restore_pkgs.sh "${cache_dir}" "${cache_restore_root}" "${execute_install_scripts}" "${debug}" else - ${script_dir}/install_and_cache_pkgs.sh "${cache_dir}" ${packages} + ${script_dir}/install_and_cache_pkgs.sh "${cache_dir}" "${debug}" ${packages} fi log_empty_line diff --git a/pre_cache_action.sh b/pre_cache_action.sh index 2801257..1612731 100755 --- a/pre_cache_action.sh +++ b/pre_cache_action.sh @@ -1,5 +1,11 @@ #!/bin/bash +# Debug mode for diagnosing issues. +# Setup first before other operations. +debug="${4}" +validate_bool "${debug}" debug 1 +test ${debug} == "true" && set -x + # Include library. script_dir="$(dirname -- "$(realpath -- "${0}")")" source "${script_dir}/lib.sh" @@ -13,8 +19,11 @@ version="${2}" # Execute post-installation script. execute_install_scripts="${3}" +# Debug mode for diagnosing issues. +debug="${4}" + # List of the packages to use. -input_packages="${@:4}" +input_packages="${@:5}" # Trim commas, excess spaces, and sort. packages="$(normalize_package_list "${input_packages}")" @@ -26,22 +35,18 @@ log "Validating action arguments (version='${version}', packages='${packages}'). if grep -q " " <<< "${version}"; then log "aborted" log "Version value '${version}' cannot contain spaces." >&2 - exit 1 + exit 2 fi # Is length of string zero? if test -z "${packages}"; then log "aborted" log "Packages argument cannot be empty." >&2 - exit 2 -fi - -if test "${execute_install_scripts}" != "true" -a "${execute_install_scripts}" != "false"; then - log "aborted" - log "execute_install_scripts value '${execute_install_scripts}' must be either true or false (case sensitive)." exit 3 fi +validate_bool "${execute_install_scripts}" execute_install_scripts 4 + log "done" log_empty_line @@ -59,7 +64,7 @@ for package in ${packages}; do if test ! "$(apt-cache show "${package}")"; then echo "aborted" log "Package '${package}' not found." >&2 - exit 4 + exit 5 fi read package_name package_ver < <(get_package_name_ver "${package}") versioned_packages=""${versioned_packages}" "${package_name}"="${package_ver}"" diff --git a/restore_pkgs.sh b/restore_pkgs.sh index 7826146..fec5b29 100755 --- a/restore_pkgs.sh +++ b/restore_pkgs.sh @@ -3,6 +3,11 @@ # Fail on any error. set -e +# Debug mode for diagnosing issues. +# Setup first before other operations. +debug="${4}" +test ${debug} == "true" && set -x + # Include library. script_dir="$(dirname -- "$(realpath -- "${0}")")" source "${script_dir}/lib.sh"