cache-apt-pkgs-action/restore_pkgs.sh
George Joseph 041d4c3864 Preserve apt package install order on restore.
* The manifest_all.log file is now kept in the order apt installed the packages.
* restore_pkgs.sh now restores packages in the order they are in the
manifest_all.log file instead of the order of the tarballs in the filesystem.

Resolves: #196
2026-06-14 08:30:42 -06:00

66 lines
2 KiB
Bash
Executable file

#!/bin/bash
# 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"
# Directory that holds the cached packages.
cache_dir="${1}"
# Root directory to untar the cached packages to.
# Typically filesystem root '/' but can be changed for testing.
cache_restore_root="${2}"
test -d "${cache_restore_root}" || mkdir "${cache_restore_root}"
# Cache and execute post install scripts on restore.
execute_install_scripts="${3}"
# Use find instead of ls to better handle non-alphanumeric filenames
cache_filepaths="$(find "${cache_dir}" -mindepth 1 -maxdepth 1 -type f -o -type d | sort)"
file_count=$(echo "${cache_filepaths}" | wc -w)
log "Found ${file_count} files in the cache."
for cache_filepath in ${cache_filepaths}; do
log "- $(basename "${cache_filepath}")"
done
log_empty_line
log "Reading from main requested packages manifest..."
while IFS= read -r logline; do
log "- $(echo "${logline}" | tr ':' ' ')"
done < <(tr ',' '\n' < "${cache_dir}/manifest_main.log")
log "done"
log_empty_line
# Only search for archived results. Manifest and cache key also live here.
manifest_all="${cache_dir}/manifest_all.log"
mapfile -t packages <"${manifest_all}"
cached_filecount="${#packages[@]}"
log "Restoring ${cached_filecount} packages from cache..."
for package in "${packages[@]}"; do
cached_filepath="${cache_dir}/${package}.tar"
log "- ${package} restoring..."
sudo tar -xf "${cached_filepath}" -C "${cache_restore_root}" > /dev/null
log " done"
# Execute install scripts if available.
if test "${execute_install_scripts}" == "true"; then
# May have to add more handling for extracting pre-install script before extracting all files.
# Keeping it simple for now.
execute_install_script "${cache_restore_root}" "${cached_filepath}" preinst install
execute_install_script "${cache_restore_root}" "${cached_filepath}" postinst configure
fi
done
log "done"