Fix postinst bugs and add docs to lib.

This commit is contained in:
awalsh128 2022-09-12 19:28:55 -07:00
parent e32a1a4ea0
commit 673318d804
3 changed files with 54 additions and 21 deletions

View file

@ -64,10 +64,8 @@ done
log_empty_line log_empty_line
# Post install script install location. # Post install script install location {package name}.postinst
postinst_filepath="/tmp/deb-ctrl-data/postinst" postinst_dirpath="/var/lib/dpkg/info/"
postinst_dirpath=$(dirname ${postinst_filepath})
mkdir "${postinst_dirpath}"
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..."
@ -87,12 +85,9 @@ for installed_package in ${installed_packages}; do
sudo xargs tar -cf "${cache_filepath}" -C / sudo xargs tar -cf "${cache_filepath}" -C /
# Append post install scripts if enabled and available. # Append post install scripts if enabled and available.
if test "${execute_postinst}" == "true"; then postinst_filepath=$(get_postinst_filepath "${package_name}")
dpkg -e pkg "${postinst_dirpath}" if test "${execute_postinst}" == "true" && test ! -z "${postinst_filepath}"; then
if test -f "${postinst_filepath}"; then
tar -caf "${cache_filepath}" "${postinst_filepath}" tar -caf "${cache_filepath}" "${postinst_filepath}"
rm "${postinst_filepath}"
fi
fi fi
log " done (compressed size $(du -h "${cache_filepath}" | cut -f1))." log " done (compressed size $(du -h "${cache_filepath}" | cut -f1))."

47
lib.sh
View file

@ -1,6 +1,13 @@
#!/bin/bash #!/bin/bash
# Sort these packages by name and split on commas. # 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 { 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.
@ -9,8 +16,14 @@ function normalize_package_list {
echo "${sorted}" 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.
# <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)?\(([^ )]+)"
@ -30,7 +43,13 @@ function get_installed_packages {
fi 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 { 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.
@ -40,12 +59,36 @@ function get_package_name_ver {
echo "${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 { 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:
# 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 { 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

@ -37,21 +37,16 @@ log_empty_line
cached_pkg_filepaths=$(ls -1 "${cache_dir}"/*.tar | sort) cached_pkg_filepaths=$(ls -1 "${cache_dir}"/*.tar | sort)
cached_pkg_filecount=$(echo ${cached_pkg_filepaths} | wc -w) 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..." 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
if test "${execute_postinst}" == "true"; then
# Execute post install script if available. # Execute post install script if available.
if test -f "${postint_filepath}"; then postinst_filepath=$(get_postinst_filepath "${package_name}")
if test "${execute_postinst}" == "true" && test ! -z "${postinst_filepath}"; then
sh -x ${postint_filepath} sh -x ${postint_filepath}
rm -fr ${postint_filepath}
fi
fi fi
log " done" log " done"