Keeping Your autopkg Cache So Fresh and So Clean, Clean

It’s been a minute since I felt I had something worthwhile to write about. In the past, the autopkg systems I had configured either had plenty of internal storage or external storage available to more or less “set it and forget it”. The consequence of this approach is over time if you have many software titles your autopkg cache folder will become quite large. Depending on the recipes you use, say you have many that involve naming downloaded or packaged software with the version, this can also greatly increase the size because new software version downloads and packages don’t overwrite the previous ones. So if you find yourself like I do where free space is precious on your autopkg system, you need an automated way to clean it.

While I haven’t yet taken the leap to implement autopkg in a CI workflow that makes this whole issue moot, I wrote a script to periodically clean out old downloads, packages, as well as the tmp files that can result from incomplete downloads.

As written, running this on the command line will remove any .dmg, .pkg, .tar.gz, and .zip files older than 7 days. It also removes those pesky tmp files that can get leftover from incomplete software downloads. This can also be triggered to be run as part of a LaunchDaemon.

If you wish, you can tweak the threshold for removing old files by entering your own day value for DAYS_TO_DELETE. Additionally, if you wish to exclude any items that would normally be deleted, uncomment the EXCLUSIONS variable and enter any desired search terms.

Happy cleaning!

#!/bin/bash
# Script to find and delete .dmg, .pkg, .tar.gz, and .zip files from
# your autopkg cache
# Failed / incomplete downloads (prefixed with tmp) are also removed
# Number of days after which to delete matching files
readonly DAYS_TO_DELETE=7
# Path to autopkg Cache folder for current user
readonly SEARCH_PATH="/Users/${USER}/Library/AutoPkg/Cache"
# List of search terms to exclude from cleanup, separated by spaces
#
# readonly EXCLUSIONS="Slack Firefox"
# Path to autopkg cleanup log. For CLI use, otherwise comment out
readonly LOG="/Library/Logs/autopkg_cleanup.log"
#######################################
# Output errors with timestamp to STDERR
# Globals:
# CLEANUP_LIST
# DAYS_TO_DELETE
# SEARCH_PATH
# EXCLUSIONS
# Arguments:
# None
# Outputs:
# None
#######################################
get_files_to_clean() {
CLEANUP_LIST="$(/usr/bin/find ${SEARCH_PATH} \( -iname "*.pkg" -o -iname "*.dmg" -o -iname "*.zip" -o -iname "tmp*" -o -iname "*.tar.gz" \) -mtime +${DAYS_TO_DELETE} -maxdepth 5 -print | sort)"
# Remove any excluded items from the list
for i in $EXCLUSIONS; do
CLEANUP_LIST=$(echo "$CLEANUP_LIST" | grep -v ${i})
done
}
#######################################
# Output results
# Globals:
# CLEANUP_LIST
# DAYS_TO_DELETE
# Arguments:
# None
# Outputs:
# Prints & writes to log if no files found older
# than specified DAYS_TO_DELETE, or prints & writes
# all found files to clean and indicates if it failed
#######################################
clean_files() {
# Output results
if [ -z "$CLEANUP_LIST" ]; then
# Indicate no files found to delete
writelog "No files found to clean older than ${DAYS_TO_DELETE} days."
else
# Indicate found files to delete
writelog "Files found to clean older than ${DAYS_TO_DELETE} days:"
for i in ${CLEANUP_LIST}; do
writelog "${i}"
done
# Remove found files and output any failures
for f in $CLEANUP_LIST; do
writelog "Removing $(basename ${f})"
/bin/rm -f "$f" || writelog "Failed to remove ${f}."
done
fi
}
start_run() {
writelog "####### START $(/bin/date +'%Y-%m-%d') #######"
}
end_run() {
writelog "####### END $(/bin/date +'%Y-%m-%d') #######"
}
writelog() {
# For LaunchDaemon use
#echo "$(/bin/date +'%Y-%m-%dT%H:%M:%S') – $*" #>&1
# For CLI use
echo "$(/bin/date +'%Y-%m-%dT%H:%M:%S')$*" >> "$LOG"
}
main() {
start_run
get_files_to_clean
clean_files
end_run
}
main "@"

Automating Manual munki Imports with autopkg – Part 3: Pro Tools

In previous posts (macOS erase & install, Autodesk Maya), I discussed how I created recipes for importing different software titles that I’d previously processed manually.

In this third part, my focus is on Pro Tools. The challenge with automating this task is that any given release includes in the downloaded DMG the Pro Tools install as a PKG as well as both the required Codecs and HD Driver PKG installers. These supporting installs are tied to the specific Pro Tools release and which must be installed before Pro Tools. The core question then was using the available processors, could I create the necessary Pro Tools munki item while simultaneous making the applicable Codecs and HD Driver versions a part of the item’s requires array?

TL;DR – you can reassign any collected autopkg output variable to any other desired variable for later use.

See more info past the jump.

Read More

Automating Manual munki Imports with autopkg – Part 2: Autodesk Maya

In a previous post, I discussed how I created a .munki recipe for importing macOS installers into a munki_repo with the necessary settings to complete an erase and install.

In this second part, my focus is on Autodesk Maya, which has long been a challenge for admins. As an education institution, we have a standalone license, so while this post does not address network licensing it may still prove helpful.

TL;DR – you can create a .munki recipe that imports a DMG with an app inside but which ultimately removes the normally created installs array and instead creates an artificial package receipts array!

See more info past the jump.

Read More