Created
October 18, 2020 02:47
-
-
Save apizz/2ccf69863bcd98ccf468fa60444df8cb to your computer and use it in GitHub Desktop.
Jamf script template to install or remove a specified homebrew formula as part of a policy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Script template to install or remove an installed homebrew | |
# formula. | |
# Variable to supply as part of the assigned Jamf policy to | |
# install or remove the specified brew formula | |
BREW_ITEM="$4" | |
# Comment out the undesired BREW_ACTION | |
BREW_ACTION="install" | |
#BREW_ACTION="remove" | |
# Custom trigger to install Homebrew if not installed | |
BREW_INSTALL_TRIGGER="<your custom trigger>" | |
# Get logged-in user | |
CURRENT_USER=$(echo "show State:/Users/ConsoleUser" | scutil | awk '/Name :/ { print $3 }') | |
brewCheck() { | |
BREW_PATH=$(which brew) | |
} | |
brewInstall() { | |
/bin/echo "Brew not installed. Installing..." | |
# Install brew | |
jamf policy -event "$BREW_INSTALL_TRIGGER" | |
} | |
brewProcessFormula() { | |
# Process the brew item | |
/usr/bin/su - "$CURRENT_USER" -c "\"${BREW_PATH}\" ${BREW_ACTION} ${BREW_ITEM}" | |
exitcode=$(/bin/echo $?) | |
if [ "$exitcode" = 0 ]; then | |
if [ "$BREW_ACTION" = "install" ]; then | |
ACTION="installed" | |
elif [ "$BREW_ACTION" = "remove" ]; then | |
ACTION="removed" | |
fi | |
/bin/echo "Successfully ${ACTION} ${BREW_ITEM} for user ${CURRENT_USER}!" | |
exit 0 | |
else | |
/bin/echo "Failed to ${BREW_ACTION} ${BREW_ITEM} for user ${CURRENT_USER}." | |
exit 1 | |
fi | |
} | |
brewUpgradeFormula () { | |
# Update the brew item | |
/bin/echo "Brew formula ${BREW_ITEM} already installed. Attempting to upgrade..." | |
/usr/bin/su - "$CURRENT_USER" -c "\"${BREW_PATH}\" upgrade ${BREW_ITEM}" | |
} | |
# Make sure we have a brew item to process | |
if [ -z "$BREW_ITEM" ]; then | |
/bin/echo "'$4' is empty. Need to specify an item to ${BREW_ACTION}." | |
fi | |
brewCheck | |
# Make sure brew is installed | |
if [ -z "$BREW_PATH" ]; then | |
brewInstall | |
brewCheck | |
# Make sure we have brew now | |
if [ ! -z "$BREW_PATH" ]; then | |
/bin/echo "Successfully installed brew. Continuing with ${BREW_ACTION} of ${BREW_ITEM}..." | |
else | |
/bin/echo "Failed to install brew. Exiting..." | |
exit 1 | |
fi | |
fi | |
# Make sure not already installed, grep exact match | |
BREW_ITEM_CHECK=$(/usr/bin/su - "$CURRENT_USER" -c "\"${BREW_PATH}\" list | /usr/bin/grep -w \"^${BREW_ITEM}$\"") | |
if [ "$BREW_ACTION" = "install" ] && [ -z "$BREW_ITEM_CHECK" ]; then | |
brewProcessFormula | |
elif [ "$BREW_ACTION" = "install" ] && [ ! -z "$BREW_ITEM_CHECK" ] ; then | |
brewUpgradeFormula | |
elif [ "$BREW_ACTION" = "remove" ] && [ ! -z "$BREW_ITEM_CHECK" ]; then | |
brewProcessFormula | |
else | |
/bin/echo "ERROR: Unaccounted for logic or unique issue." | |
exit 1 | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment