From 00e34cb96c39a7e26bbc4322257dd3d4b978c08a Mon Sep 17 00:00:00 2001 From: awalsh128 Date: Fri, 9 Sep 2022 20:35:48 -0700 Subject: [PATCH] Draft of postinst support from issue #44. --- action.yml | 7 ++++++- install_and_cache_pkgs.sh | 21 ++++++++++++++++++++- post_cache_action.sh | 9 ++++++--- restore_pkgs.sh | 17 +++++++++++++++++ 4 files changed, 49 insertions(+), 5 deletions(-) diff --git a/action.yml b/action.yml index 9a5ce3d..fe3b7e4 100644 --- a/action.yml +++ b/action.yml @@ -13,7 +13,11 @@ inputs: version: description: 'Version will create a new cache and install packages.' required: false - default: '' + default: '' + execute_postinst: + description: 'Execute Debian package postinst script upon restore. Required by some packages.' + required: false + default: 'false' refresh: description: 'Option to refresh / upgrade the packages in the same cache.' required: false @@ -56,6 +60,7 @@ runs: ~/cache-apt-pkgs \ / \ "${{ steps.load-cache.outputs.cache-hit }}" \ + "${{ inputs.execute_postinst }}" \ ${{ inputs.packages }} function create_list { local list=$(cat ~/cache-apt-pkgs/manifest_${1}.log | tr '\n' ','); echo ${list:0:-1}; }; echo "::set-output name=package-version-list::$(create_list main)" diff --git a/install_and_cache_pkgs.sh b/install_and_cache_pkgs.sh index 2f05a4c..4b81281 100755 --- a/install_and_cache_pkgs.sh +++ b/install_and_cache_pkgs.sh @@ -10,8 +10,11 @@ source "${script_dir}/lib.sh" # Directory that holds the cached packages. cache_dir="${1}" +# Cache and execute post install scripts on restore. +execute_postinst="${2}" + # 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}")" @@ -61,6 +64,11 @@ done log_empty_line +# Post install script install location. +postinst_filepath="/tmp/deb-ctrl-data/postinst" +postinst_dirpath=$(dirname ${postinst_filepath}) +mkdir -r "${postinst_dirpath}" + installed_package_count=$(wc -w <<< "${installed_packages}") log "Caching ${installed_package_count} installed packages..." for installed_package in ${installed_packages}; do @@ -70,12 +78,23 @@ for installed_package in ${installed_packages}; do if test ! -f "${cache_filepath}"; then read installed_package_name installed_package_ver < <(get_package_name_ver "${installed_package}") log " * Caching ${installed_package_name} to ${cache_filepath}..." + # Pipe all package files (no folders) to Tar. dpkg -L "${installed_package_name}" | 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 done | sudo xargs tar -cf "${cache_filepath}" -C / + + # Append post install scripts if enabled and available. + if test "${execute_postinst}" == "true"; then + dpkg -e pkg "${postinst_dirpath}" + if test -f "${postinst_filepath}"; then + tar -caf "${cache_filepath}" "${postinst_filepath}" + rm "${postinst_filepath}" + fi + fi + log " done (compressed size $(du -h "${cache_filepath}" | cut -f1))." fi diff --git a/post_cache_action.sh b/post_cache_action.sh index eaae06d..9ebdb69 100755 --- a/post_cache_action.sh +++ b/post_cache_action.sh @@ -17,15 +17,18 @@ cache_restore_root="${2}" # Indicates that the cache was found. cache_hit="${3}" +# Cache and execute post install scripts on restore. +execute_postinst="${4}" + # List of the packages to use. -packages="${@:4}" +packages="${@:5}" 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}" "${execute_postinst}" else - ${script_dir}/install_and_cache_pkgs.sh ~/cache-apt-pkgs ${packages} + ${script_dir}/install_and_cache_pkgs.sh ~/cache-apt-pkgs "${execute_postinst}" ${packages} fi log_empty_line diff --git a/restore_pkgs.sh b/restore_pkgs.sh index e398a3e..6a4a8eb 100755 --- a/restore_pkgs.sh +++ b/restore_pkgs.sh @@ -14,6 +14,9 @@ cache_dir="${1}" # Typically filesystem root '/' but can be changed for testing. cache_restore_root="${2}" +# Cache and execute post install scripts on restore. +execute_postinst="${3}" + cache_filepaths="$(ls -1 "${cache_dir}" | sort)" log "Found $(echo ${cache_filepaths} | wc -w) files in the cache." for cache_filepath in ${cache_filepaths}; do @@ -33,10 +36,24 @@ log_empty_line # Only search for archived results. Manifest and cache key also live here. cached_pkg_filepaths=$(ls -1 "${cache_dir}"/*.tar | sort) cached_pkg_filecount=$(echo ${cached_pkg_filepaths} | wc -w) + +# Post install script restore location. +postint_filepath="/tmp/deb-ctrl-data/postinst" + log "Restoring ${cached_pkg_filecount} packages from cache..." for cached_pkg_filepath in ${cached_pkg_filepaths}; do log "- $(basename "${cached_pkg_filepath}") restoring..." + sudo tar -xf "${cached_pkg_filepath}" -C "${cache_restore_root}" > /dev/null + + if test "${execute_postinst}" == "true"; then + # Execute post install script if available. + if test -f "${postint_filepath}"; then + sh -x ${postint_filepath} + rm -fr ${postint_filepath} + fi + fi + log " done" done log "done"