diff --git a/install_and_cache_pkgs.sh b/install_and_cache_pkgs.sh index a755019..bb3ae67 100755 --- a/install_and_cache_pkgs.sh +++ b/install_and_cache_pkgs.sh @@ -64,10 +64,8 @@ done log_empty_line -# Post install script install location. -postinst_filepath="/tmp/deb-ctrl-data/postinst" -postinst_dirpath=$(dirname ${postinst_filepath}) -mkdir "${postinst_dirpath}" +# Post install script install location {package name}.postinst +postinst_dirpath="/var/lib/dpkg/info/" installed_package_count=$(wc -w <<< "${installed_packages}") log "Caching ${installed_package_count} installed packages..." @@ -87,12 +85,9 @@ for installed_package in ${installed_packages}; do 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 + 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))." diff --git a/lib.sh b/lib.sh index 4169ae4..86101cd 100755 --- a/lib.sh +++ b/lib.sh @@ -1,6 +1,13 @@ #!/bin/bash # Sort these packages by name and split on commas. +####################################### +# Sorts given packages by name and split on commas. +# Arguments: +# The comma delimited list of packages. +# Returns: +# Sorted list of space delimited packages. +####################################### function normalize_package_list { local stripped=$(echo "${1}" | sed 's/,//g') # Remove extraneous spaces at the middle, beginning, and end. @@ -9,8 +16,14 @@ function normalize_package_list { echo "${sorted}" } -# Gets a list of installed packages as space delimited pairs with each pair colon delimited. +####################################### +# Gets a list of installed packages from a Debian package installation log. +# Arguments: +# The filepath of the Debian install log. +# Returns: +# The list of space delimited pairs with each pair colon delimited. # : ... +####################################### function get_installed_packages { install_log_filepath="${1}" local regex="^Unpacking ([^ :]+)([^ ]+)? (\[[^ ]+\]\s)?\(([^ )]+)" @@ -30,7 +43,13 @@ function get_installed_packages { fi } -# Split fully qualified package into name and version. +####################################### +# Splits a fully qualified package into name and version. +# Arguments: +# The colon delimited package pair or just the package name. +# Returns: +# The package name and version pair. +####################################### function get_package_name_ver { IFS=\: read name ver <<< "${1}" # If version not found in the fully qualified package value. @@ -40,12 +59,36 @@ function get_package_name_ver { echo "${name}" "${ver}" } +####################################### +# Gets the Debian postinst file location. +# Arguments: +# Name of the unqualified package to search for. +# Returns: +# Filepath of the postinst file, otherwise an empty string. +####################################### +function get_postinst_filepath { + filepath="/var/lib/dpkg/info/${1}" + if test -f "${filepath}"; then + echo "${filepath}" + else + echo "" + fi +} + function log { echo "$(date +%H:%M:%S)" "${@}"; } function log_err { >&2 echo "$(date +%H:%M:%S)" "${@}"; } function log_empty_line { echo ""; } +####################################### # Writes the manifest to a specified file. +# Arguments: +# Type of manifest being written. +# List of packages being written to the file. +# File path of the manifest being written. +# Returns: +# Log lines from write. +####################################### function write_manifest { if [ ${#2} -eq 0 ]; then log "Skipped ${1} manifest write. No packages to install." diff --git a/restore_pkgs.sh b/restore_pkgs.sh index 6a4a8eb..1cb7b4e 100755 --- a/restore_pkgs.sh +++ b/restore_pkgs.sh @@ -37,21 +37,16 @@ log_empty_line 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 + # Execute post install script if available. + postinst_filepath=$(get_postinst_filepath "${package_name}") + if test "${execute_postinst}" == "true" && test ! -z "${postinst_filepath}"; then + sh -x ${postint_filepath} fi log " done"