Made cache directory variable and more refinements to postinst.

This commit is contained in:
awalsh128 2022-10-15 21:58:58 -07:00
parent 673318d804
commit 86a813eaa1
4 changed files with 60 additions and 37 deletions

View file

@ -64,9 +64,6 @@ done
log_empty_line log_empty_line
# Post install script install location {package name}.postinst
postinst_dirpath="/var/lib/dpkg/info/"
installed_package_count=$(wc -w <<< "${installed_packages}") installed_package_count=$(wc -w <<< "${installed_packages}")
log "Caching ${installed_package_count} installed packages..." log "Caching ${installed_package_count} installed packages..."
for installed_package in ${installed_packages}; do for installed_package in ${installed_packages}; do
@ -77,19 +74,11 @@ for installed_package in ${installed_packages}; do
read installed_package_name installed_package_ver < <(get_package_name_ver "${installed_package}") read installed_package_name installed_package_ver < <(get_package_name_ver "${installed_package}")
log " * Caching ${installed_package_name} to ${cache_filepath}..." log " * Caching ${installed_package_name} to ${cache_filepath}..."
# Pipe all package files (no folders) to Tar. # Pipe all package files (no folders) and postinst control data to Tar.
dpkg -L "${installed_package_name}" | { dpkg -L "${installed_package_name}" & get_postinst_filepath "${package_name}"; } |
while IFS= read -r f; do while IFS= read -r f; do test -f "${f}" -o -L "${f}" && get_tar_relpath "${f}"; done |
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 / sudo xargs tar -cf "${cache_filepath}" -C /
# Append post install scripts if enabled and available.
postinst_filepath=$(get_postinst_filepath "${package_name}")
if test "${execute_postinst}" == "true" && test ! -z "${postinst_filepath}"; then
tar -caf "${cache_filepath}" "${postinst_filepath}"
fi
log " done (compressed size $(du -h "${cache_filepath}" | cut -f1))." log " done (compressed size $(du -h "${cache_filepath}" | cut -f1))."
fi fi

54
lib.sh
View file

@ -1,13 +1,12 @@
#!/bin/bash #!/bin/bash
# Sort these packages by name and split on commas. ###############################################################################
#######################################
# Sorts given packages by name and split on commas. # Sorts given packages by name and split on commas.
# Arguments: # Arguments:
# The comma delimited list of packages. # The comma delimited list of packages.
# Returns: # Returns:
# Sorted list of space delimited packages. # Sorted list of space delimited packages.
####################################### ###############################################################################
function normalize_package_list { function normalize_package_list {
local stripped=$(echo "${1}" | sed 's/,//g') local stripped=$(echo "${1}" | sed 's/,//g')
# Remove extraneous spaces at the middle, beginning, and end. # Remove extraneous spaces at the middle, beginning, and end.
@ -16,14 +15,14 @@ function normalize_package_list {
echo "${sorted}" echo "${sorted}"
} }
####################################### ###############################################################################
# Gets a list of installed packages from a Debian package installation log. # Gets a list of installed packages from a Debian package installation log.
# Arguments: # Arguments:
# The filepath of the Debian install log. # The filepath of the Debian install log.
# Returns: # Returns:
# The list of space delimited pairs with each pair colon delimited. # The list of space delimited pairs with each pair colon delimited.
# <name>:<version> <name:version>... # <name>:<version> <name:version>...
####################################### ###############################################################################
function get_installed_packages { function get_installed_packages {
install_log_filepath="${1}" install_log_filepath="${1}"
local regex="^Unpacking ([^ :]+)([^ ]+)? (\[[^ ]+\]\s)?\(([^ )]+)" local regex="^Unpacking ([^ :]+)([^ ]+)? (\[[^ ]+\]\s)?\(([^ )]+)"
@ -43,13 +42,13 @@ function get_installed_packages {
fi fi
} }
####################################### ###############################################################################
# Splits a fully qualified package into name and version. # Splits a fully qualified package into name and version.
# Arguments: # Arguments:
# The colon delimited package pair or just the package name. # The colon delimited package pair or just the package name.
# Returns: # Returns:
# The package name and version pair. # The package name and version pair.
####################################### ###############################################################################
function get_package_name_ver { 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.
@ -59,15 +58,28 @@ function get_package_name_ver {
echo "${name}" "${ver}" echo "${name}" "${ver}"
} }
####################################### ###############################################################################
# Gets the package name from the cached package filepath in the
# path/to/cache/dir/<name>:<version>.tar format.
# Arguments:
# Filepath to the cached packaged.
# Returns:
# The package name.
###############################################################################
function get_package_name_from_cached_filepath {
basename ${cached_pkg_filepath} | awk -F\: '{print $1}'
}
###############################################################################
# Gets the Debian postinst file location. # Gets the Debian postinst file location.
# Arguments: # Arguments:
# Root directory to search from.
# Name of the unqualified package to search for. # Name of the unqualified package to search for.
# Returns: # Returns:
# Filepath of the postinst file, otherwise an empty string. # Filepath of the postinst file, otherwise an empty string.
####################################### ###############################################################################
function get_postinst_filepath { function get_postinst_filepath {
filepath="/var/lib/dpkg/info/${1}" filepath="${1}/var/lib/dpkg/info/${2}.postinst"
if test -f "${filepath}"; then if test -f "${filepath}"; then
echo "${filepath}" echo "${filepath}"
else else
@ -75,12 +87,30 @@ function get_postinst_filepath {
fi fi
} }
###############################################################################
# Gets the relative filepath acceptable by Tar. Just removes the leading slash
# that Tar disallows.
# Arguments:
# Absolute filepath to archive.
# Returns:
# The relative filepath to archive.
###############################################################################
function get_tar_relpath {
filepath=${1}
if test ${filepath:0:1} = "/"; then
echo "${filepath:1}"
else
echo "${filepath}"
fi
}
function log { echo "$(date +%H:%M:%S)" "${@}"; } function log { echo "$(date +%H:%M:%S)" "${@}"; }
function log_err { >&2 echo "$(date +%H:%M:%S)" "${@}"; } function log_err { >&2 echo "$(date +%H:%M:%S)" "${@}"; }
function log_empty_line { echo ""; } function log_empty_line { echo ""; }
####################################### ###############################################################################
# Writes the manifest to a specified file. # Writes the manifest to a specified file.
# Arguments: # Arguments:
# Type of manifest being written. # Type of manifest being written.
@ -88,7 +118,7 @@ function log_empty_line { echo ""; }
# File path of the manifest being written. # File path of the manifest being written.
# Returns: # Returns:
# Log lines from write. # Log lines from write.
####################################### ###############################################################################
function write_manifest { function write_manifest {
if [ ${#2} -eq 0 ]; then if [ ${#2} -eq 0 ]; then
log "Skipped ${1} manifest write. No packages to install." log "Skipped ${1} manifest write. No packages to install."

View file

@ -26,9 +26,9 @@ packages="${@:5}"
script_dir="$(dirname -- "$(realpath -- "${0}")")" script_dir="$(dirname -- "$(realpath -- "${0}")")"
if [ "$cache_hit" == true ]; then if [ "$cache_hit" == true ]; then
${script_dir}/restore_pkgs.sh ~/cache-apt-pkgs "${cache_restore_root}" "${execute_postinst}" ${script_dir}/restore_pkgs.sh "${cache_dir}" "${cache_restore_root}" "${execute_postinst}"
else else
${script_dir}/install_and_cache_pkgs.sh ~/cache-apt-pkgs "${execute_postinst}" ${packages} ${script_dir}/install_and_cache_pkgs.sh "${cache_dir}" "${execute_postinst}" ${packages}
fi fi
log_empty_line log_empty_line

View file

@ -39,16 +39,20 @@ cached_pkg_filecount=$(echo ${cached_pkg_filepaths} | wc -w)
log "Restoring ${cached_pkg_filecount} packages from cache..." log "Restoring ${cached_pkg_filecount} packages from cache..."
for cached_pkg_filepath in ${cached_pkg_filepaths}; do for cached_pkg_filepath in ${cached_pkg_filepaths}; do
log "- $(basename "${cached_pkg_filepath}") restoring..."
log "- $(basename "${cached_pkg_filepath}") restoring..."
sudo tar -xf "${cached_pkg_filepath}" -C "${cache_restore_root}" > /dev/null sudo tar -xf "${cached_pkg_filepath}" -C "${cache_restore_root}" > /dev/null
log " done"
# Execute post install script if available. # Execute post install script if available.
postinst_filepath=$(get_postinst_filepath "${package_name}") if test "${execute_postinst}" == "true"; then
if test "${execute_postinst}" == "true" && test ! -z "${postinst_filepath}"; then package_name=$(get_package_name_from_cached_filepath ${package_name})
sh -x ${postint_filepath} postinst_filepath=$(get_postinst_filepath "${cache_restore_root}" "${package_name}")
if test ! -z "${postinst_filepath}"; then
log "- Executing ${postinst_filepath}..."
sudo sh -x ${postinst_filepath}
log " done"
fi
fi fi
log " done"
done done
log "done" log "done"