Simplify logging calls.

This commit is contained in:
awalsh128 2022-07-14 21:44:35 -07:00
parent 768417e4ec
commit 63f7fccab9
4 changed files with 19 additions and 24 deletions

View file

@ -23,7 +23,7 @@ for package in ${normalized_packages}; do
log "- ${package}" log "- ${package}"
done done
log -n "Updating APT package list..." log "Updating APT package list..."
sudo apt-get update > /dev/null sudo apt-get update > /dev/null
echo "done." echo "done."
@ -41,11 +41,14 @@ for package in ${normalized_packages}; do
all_packages="$(apt-get install --dry-run --yes "${package_name}" | grep "^Inst" | awk '{print $2}')" all_packages="$(apt-get install --dry-run --yes "${package_name}" | grep "^Inst" | awk '{print $2}')"
dep_packages="$(echo ${all_packages} | grep -v "${package_name}" | tr '\n' ,)" dep_packages="$(echo ${all_packages} | grep -v "${package_name}" | tr '\n' ,)"
if "${dep_packages}" == ","; then
dep_packages="none";
fi
log "- ${package_name}" log "- ${package_name}"
log " * Version: ${package_ver}" log " * Version: ${package_ver}"
log " * Dependencies: ${dep_packages:0:-1}" log " * Dependencies: ${dep_packages}"
log -n " * Installing..." log " * Installing..."
# Zero interaction while installing or upgrading the system via apt. # Zero interaction while installing or upgrading the system via apt.
sudo DEBIAN_FRONTEND=noninteractive apt-get --yes install "${package}" > /dev/null sudo DEBIAN_FRONTEND=noninteractive apt-get --yes install "${package}" > /dev/null
echo "done." echo "done."
@ -55,14 +58,14 @@ for package in ${normalized_packages}; do
if test ! -f "${cache_filepath}"; then if test ! -f "${cache_filepath}"; then
read cache_package_name cache_package_ver < <(get_package_name_ver "${cache_package}") read cache_package_name cache_package_ver < <(get_package_name_ver "${cache_package}")
log -n " * Caching ${cache_package_name} to ${cache_filepath}..." log " * Caching ${cache_package_name} to ${cache_filepath}..."
# Pipe all package files (no folders) to Tar. # Pipe all package files (no folders) to Tar.
dpkg -L "${cache_package_name}" | dpkg -L "${cache_package_name}" |
while IFS= read -r f; do while IFS= read -r f; do
if test -f $f; then echo "${f:1}"; fi; #${f:1} removes the leading slash that Tar disallows if test -f $f; then echo "${f:1}"; fi; #${f:1} removes the leading slash that Tar disallows
done | done |
xargs tar -czf "${cache_filepath}" -C / xargs tar -czf "${cache_filepath}" -C /
echo "done (compressed size $(du -k "${cache_filepath}" | cut -f1))." log "done (compressed size $(du -k "${cache_filepath}" | cut -f1))."
fi fi
# Comma delimited name:ver pairs in the all packages manifest. # Comma delimited name:ver pairs in the all packages manifest.
@ -72,13 +75,13 @@ done
log "done." log "done."
manifest_all_filepath="${cache_dir}/manifest_all.log" manifest_all_filepath="${cache_dir}/manifest_all.log"
log -n "Writing all packages manifest to ${manifest_all_filepath}..." log "Writing all packages manifest to ${manifest_all_filepath}..."
# Remove trailing comma and write to manifest_all file. # Remove trailing comma and write to manifest_all file.
echo "${manifest_all:0:-1}" > "${manifest_all_filepath}" echo "${manifest_all:0:-1}" > "${manifest_all_filepath}"
echo "done." log "done."
manifest_main_filepath="${cache_dir}/manifest_main.log" manifest_main_filepath="${cache_dir}/manifest_main.log"
log -n "Writing main requested packages manifest to ${manifest_main_filepath}..." log "Writing main requested packages manifest to ${manifest_main_filepath}..."
# Remove trailing comma and write to manifest_main file. # Remove trailing comma and write to manifest_main file.
echo "${manifest_main:0:-1}" > "${manifest_main_filepath}" echo "${manifest_main:0:-1}" > "${manifest_main_filepath}"
echo "done." log "done."

16
lib.sh
View file

@ -2,10 +2,10 @@
# Sort these packages by name and split on commas. # Sort these packages by name and split on commas.
function normalize_package_list { function normalize_package_list {
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.
trimmed="$(echo "${stripped}" | sed 's/\s\+/ /g; s/^\s\+//g; s/\s\+$//g')" local trimmed="$(echo "${stripped}" | sed 's/\s\+/ /g; s/^\s\+//g; s/\s\+$//g')"
echo "$(echo "${trimmed}" | sort)" echo "${trimmed}" | sort
} }
# Split fully qualified package into name and version # Split fully qualified package into name and version
@ -18,12 +18,4 @@ function get_package_name_ver {
echo "${name}" "${ver}" echo "${name}" "${ver}"
} }
function log { function log { echo "$(date +%H:%M:%S)" "${@}"; }
timestamp="$(echo -n "$(date +%H:%M:%S)")"
line=""${timestamp}" "$(echo ${@})""
if [[ "${1}" == "-n" ]]; then
echo -n "${line}"
else
echo "${line}"
fi
}

View file

@ -56,7 +56,7 @@ log "Creating cache key..."
normalized_versioned_packages="$(normalize_package_list "${versioned_packages}")" normalized_versioned_packages="$(normalize_package_list "${versioned_packages}")"
log "- Normalized package list is '${normalized_versioned_packages}'." log "- Normalized package list is '${normalized_versioned_packages}'."
value="$(echo "${normalized_versioned_packages} @ ${version}")" value="${normalized_versioned_packages} @ ${version}"
log "- Value to hash is '${value}'." log "- Value to hash is '${value}'."
key="$(echo "${value}" | md5sum | /bin/cut -f1 -d' ')" key="$(echo "${value}" | md5sum | /bin/cut -f1 -d' ')"

View file

@ -31,8 +31,8 @@ cache_pkg_filepaths=$(ls -1 "${cache_dir}"/*.tar.gz | sort)
cache_pkg_filecount=$(echo ${cache_pkg_filepaths} | wc -w) cache_pkg_filecount=$(echo ${cache_pkg_filepaths} | wc -w)
log "Restoring ${cache_pkg_filecount} packages from cache..." log "Restoring ${cache_pkg_filecount} packages from cache..."
for cache_pkg_filepath in ${cache_pkg_filepaths}; do for cache_pkg_filepath in ${cache_pkg_filepaths}; do
log -n "- $(basename "${cache_pkg_filepath}") restoring..." log "- $(basename "${cache_pkg_filepath}") restoring..."
sudo tar -xf "${cache_pkg_filepath}" -C "${cache_restore_root}" > /dev/null sudo tar -xf "${cache_pkg_filepath}" -C "${cache_restore_root}" > /dev/null
echo "done." log "done."
done done
log "done." log "done."