renamed ScriptLogging to printlog, will only write to log file when running as root

This commit is contained in:
Armin Briegel
2020-05-29 16:04:11 +02:00
parent 8c7c58b7d9
commit e4e9aec0e2

View File

@@ -9,7 +9,7 @@
# with additional ideas and contribution from Isaac Ordonez, Mann consulting # with additional ideas and contribution from Isaac Ordonez, Mann consulting
VERSION='0.2' VERSION='0.2'
VERSIONDATE='20200518' VERSIONDATE='20200529'
export PATH=/usr/bin:/bin:/usr/sbin:/sbin export PATH=/usr/bin:/bin:/usr/sbin:/sbin
@@ -115,14 +115,17 @@ BLOCKING_PROCESS_ACTION=prompt_user
# functions to help with getting info # functions to help with getting info
# Logging # Logging
log_location="/var/log/Installomator.log" log_location="/private/var/log/Installomator.log"
ScriptLogging(){ printlog(){
DATE=$(date +%Y-%m-%d\ %H:%M:%S) timestamp=$(date +%F\ %T)
LOG="$log_location"
if [[ "$(whoami)" == "root" ]]; then
echo "$DATE" " $1" 2>&1 | tee -a $LOG echo "$timestamp" "$1" | tee -a $log_location
else
echo "$timestamp" "$1"
fi
} }
# will get the latest release download from a github repo # will get the latest release download from a github repo
@@ -149,26 +152,26 @@ downloadURLFromGit() { # $1 git user name, $2 git repo name
echo "could not retrieve download URL for $gitusername/$gitreponame" echo "could not retrieve download URL for $gitusername/$gitreponame"
exit 9 exit 9
else else
ScriptLogging "$downloadURL" printlog "$downloadURL"
return 0 return 0
fi fi
} }
ScriptLogging "################## Start Installomator" printlog "################## Start Installomator"
# get the label # get the label
if [[ $# -eq 0 ]]; then if [[ $# -eq 0 ]]; then
ScriptLogging "no label provided" printlog "no label provided"
exit 1 exit 1
elif [[ $# -gt 3 ]]; then elif [[ $# -gt 3 ]]; then
# jamf uses $4 for the first custom parameter # jamf uses $4 for the first custom parameter
ScriptLogging "shifting arguments for Jamf" printlog "shifting arguments for Jamf"
shift 3 shift 3
fi fi
label=${1:?"no label provided"} label=${1:?"no label provided"}
ScriptLogging "################## $label" printlog "################## $label"
# lowercase the label # lowercase the label
label=${label:l} label=${label:l}
@@ -181,12 +184,12 @@ currentUser=$(scutil <<< "show State:/Users/ConsoleUser" | awk '/Name :/ { print
case $label in case $label in
version) version)
# print the script VERSION # print the script VERSION
ScriptLogging "$VERSION" printlog "$VERSION"
exit 0 exit 0
;; ;;
longversion) longversion)
# print the script version # print the script version
ScriptLogging "Installomater: version $VERSION ($VERSIONDATE)" printlog "Installomater: version $VERSION ($VERSIONDATE)"
exit 0 exit 0
;; ;;
@@ -786,7 +789,7 @@ case $label in
;; ;;
*) *)
# unknown label # unknown label
ScriptLogging "unknown label $label" printlog "unknown label $label"
exit 1 exit 1
;; ;;
esac esac
@@ -794,20 +797,20 @@ esac
# functions # functions
cleanupAndExit() { # $1 = exit code, $2 message cleanupAndExit() { # $1 = exit code, $2 message
if [[ -n $2 && $1 -ne 0 ]]; then if [[ -n $2 && $1 -ne 0 ]]; then
ScriptLogging "ERROR: $2" printlog "ERROR: $2"
fi fi
if [ "$DEBUG" -eq 0 ]; then if [ "$DEBUG" -eq 0 ]; then
# remove the temporary working directory when done # remove the temporary working directory when done
ScriptLogging "Deleting $tmpDir" printlog "Deleting $tmpDir"
rm -Rf "$tmpDir" rm -Rf "$tmpDir"
fi fi
if [ -n "$dmgmount" ]; then if [ -n "$dmgmount" ]; then
# unmount disk image # unmount disk image
ScriptLogging "Unmounting $dmgmount" printlog "Unmounting $dmgmount"
hdiutil detach "$dmgmount" hdiutil detach "$dmgmount"
fi fi
ScriptLogging "################## End Installomator \n\n" printlog "################## End Installomator \n\n"
exit "$1" exit "$1"
} }
@@ -846,19 +849,19 @@ getAppVersion() {
if [[ ${#filteredAppPaths} -eq 1 ]]; then if [[ ${#filteredAppPaths} -eq 1 ]]; then
installedAppPath=$filteredAppPaths[1] installedAppPath=$filteredAppPaths[1]
appversion=$(mdls -name kMDItemVersion -raw $installedAppPath ) appversion=$(mdls -name kMDItemVersion -raw $installedAppPath )
ScriptLogging "found app at $installedAppPath, version $appversion" printlog "found app at $installedAppPath, version $appversion"
else else
ScriptLogging "could not determine location of $appName" printlog "could not determine location of $appName"
fi fi
else else
ScriptLogging "could not find $appName" printlog "could not find $appName"
fi fi
} }
checkRunningProcesses() { checkRunningProcesses() {
# don't check in DEBUG mode # don't check in DEBUG mode
if [[ $DEBUG -ne 0 ]]; then if [[ $DEBUG -ne 0 ]]; then
ScriptLogging "DEBUG mode, not checking for blocking processes" printlog "DEBUG mode, not checking for blocking processes"
return return
fi fi
@@ -871,7 +874,7 @@ checkRunningProcesses() {
case $BLOCKING_PROCESS_ACTION in case $BLOCKING_PROCESS_ACTION in
kill) kill)
ScriptLogging "killing process $x" printlog "killing process $x"
pkill $x pkill $x
;; ;;
prompt_user) prompt_user)
@@ -896,7 +899,7 @@ checkRunningProcesses() {
break break
else else
# give the user a bit of time to quit apps # give the user a bit of time to quit apps
ScriptLogging "waiting 30 seconds for processes to quit" printlog "waiting 30 seconds for processes to quit"
sleep 30 sleep 30
fi fi
done done
@@ -905,7 +908,7 @@ checkRunningProcesses() {
cleanupAndExit 11 "could not quit all processes, aborting..." cleanupAndExit 11 "could not quit all processes, aborting..."
fi fi
ScriptLogging "no more blocking processes, continue with update" printlog "no more blocking processes, continue with update"
} }
installAppWithPath() { # $1: path to app to install in $targetDir installAppWithPath() { # $1: path to app to install in $targetDir
@@ -917,12 +920,12 @@ installAppWithPath() { # $1: path to app to install in $targetDir
fi fi
# verify with spctl # verify with spctl
ScriptLogging "Verifying: $appPath" printlog "Verifying: $appPath"
if ! teamID=$(spctl -a -vv "$appPath" 2>&1 | awk '/origin=/ {print $NF }' | tr -d '()' ); then if ! teamID=$(spctl -a -vv "$appPath" 2>&1 | awk '/origin=/ {print $NF }' | tr -d '()' ); then
cleanupAndExit 4 "Error verifying $appPath" cleanupAndExit 4 "Error verifying $appPath"
fi fi
ScriptLogging "Team ID: $teamID (expected: $expectedTeamID )" printlog "Team ID: $teamID (expected: $expectedTeamID )"
if [ "$expectedTeamID" != "$teamID" ]; then if [ "$expectedTeamID" != "$teamID" ]; then
cleanupAndExit 5 "Team IDs do not match" cleanupAndExit 5 "Team IDs do not match"
@@ -941,12 +944,12 @@ installAppWithPath() { # $1: path to app to install in $targetDir
# remove existing application # remove existing application
if [ -e "$targetDir/$appName" ]; then if [ -e "$targetDir/$appName" ]; then
ScriptLogging "Removing existing $targetDir/$appName" printlog "Removing existing $targetDir/$appName"
rm -Rf "$targetDir/$appName" rm -Rf "$targetDir/$appName"
fi fi
# copy app to /Applications # copy app to /Applications
ScriptLogging "Copy $appPath to $targetDir" printlog "Copy $appPath to $targetDir"
if ! ditto "$appPath" "$targetDir/$appName"; then if ! ditto "$appPath" "$targetDir/$appName"; then
cleanupAndExit 7 "Error while copying" cleanupAndExit 7 "Error while copying"
fi fi
@@ -957,21 +960,21 @@ installAppWithPath() { # $1: path to app to install in $targetDir
echo "Changing owner to $currentUser" echo "Changing owner to $currentUser"
chown -R "$currentUser" "$targetDir/$appName" chown -R "$currentUser" "$targetDir/$appName"
else else
ScriptLogging "No user logged in, not changing user" printlog "No user logged in, not changing user"
fi fi
} }
mountDMG() { mountDMG() {
# mount the dmg # mount the dmg
ScriptLogging "Mounting $tmpDir/$archiveName" printlog "Mounting $tmpDir/$archiveName"
# always pipe 'Y\n' in case the dmg requires an agreement # always pipe 'Y\n' in case the dmg requires an agreement
if ! dmgmount=$(ScriptLogging 'Y'$'\n' | hdiutil attach "$tmpDir/$archiveName" -nobrowse -readonly | tail -n 1 | cut -c 54- ); then if ! dmgmount=$(printlog 'Y'$'\n' | hdiutil attach "$tmpDir/$archiveName" -nobrowse -readonly | tail -n 1 | cut -c 54- ); then
cleanupAndExit 3 "Error mounting $tmpDir/$archiveName" cleanupAndExit 3 "Error mounting $tmpDir/$archiveName"
fi fi
if [[ ! -e $dmgmount ]]; then if [[ ! -e $dmgmount ]]; then
ScriptLogging "Error mounting $tmpDir/$archiveName" printlog "Error mounting $tmpDir/$archiveName"
cleanupAndExit 3 cleanupAndExit 3
fi fi
@@ -986,22 +989,22 @@ installFromDMG() {
installFromPKG() { installFromPKG() {
# verify with spctl # verify with spctl
ScriptLogging "Verifying: $archiveName" printlog "Verifying: $archiveName"
if ! teamID=$(spctl -a -vv -t install "$archiveName" 2>&1 | awk '/origin=/ {print $NF }' | tr -d '()' ); then if ! teamID=$(spctl -a -vv -t install "$archiveName" 2>&1 | awk '/origin=/ {print $NF }' | tr -d '()' ); then
ScriptLogging "Error verifying $archiveName" printlog "Error verifying $archiveName"
cleanupAndExit 4 cleanupAndExit 4
fi fi
ScriptLogging "Team ID: $teamID (expected: $expectedTeamID )" printlog "Team ID: $teamID (expected: $expectedTeamID )"
if [ "$expectedTeamID" != "$teamID" ]; then if [ "$expectedTeamID" != "$teamID" ]; then
ScriptLogging "Team IDs do not match!" printlog "Team IDs do not match!"
cleanupAndExit 5 cleanupAndExit 5
fi fi
# skip install for DEBUG # skip install for DEBUG
if [ "$DEBUG" -ne 0 ]; then if [ "$DEBUG" -ne 0 ]; then
ScriptLogging "DEBUG enabled, skipping installation" printlog "DEBUG enabled, skipping installation"
return 0 return 0
fi fi
@@ -1013,16 +1016,16 @@ installFromPKG() {
fi fi
# install pkg # install pkg
ScriptLogging "Installing $archiveName to $targetDir" printlog "Installing $archiveName to $targetDir"
if ! installer -pkg "$archiveName" -tgt "$targetDir" ; then if ! installer -pkg "$archiveName" -tgt "$targetDir" ; then
ScriptLogging "error installing $archiveName" printlog "error installing $archiveName"
cleanupAndExit 9 cleanupAndExit 9
fi fi
} }
installFromZIP() { installFromZIP() {
# unzip the archive # unzip the archive
ScriptLogging "Unzipping $archiveName" printlog "Unzipping $archiveName"
tar -xf "$archiveName" tar -xf "$archiveName"
installAppWithPath "$tmpDir/$appName" installAppWithPath "$tmpDir/$appName"
@@ -1051,7 +1054,7 @@ installPkgInDmg() {
installPkgInZip() { installPkgInZip() {
# unzip the archive # unzip the archive
ScriptLogging "Unzipping $archiveName" printlog "Unzipping $archiveName"
tar -xf "$archiveName" tar -xf "$archiveName"
# locate pkg in zip # locate pkg in zip
@@ -1076,7 +1079,7 @@ installPkgInZip() {
runUpdateTool() { runUpdateTool() {
if [[ -x $updateTool ]]; then if [[ -x $updateTool ]]; then
ScriptLogging "running $updateTool $updateToolArguments" printlog "running $updateTool $updateToolArguments"
if [[ -n $updateToolRunAsCurrentUser ]]; then if [[ -n $updateToolRunAsCurrentUser ]]; then
runAsUser $updateTool ${updateToolArguments} runAsUser $updateTool ${updateToolArguments}
else else
@@ -1086,7 +1089,7 @@ runUpdateTool() {
cleanupAndExit 15 "Error running $updateTool" cleanupAndExit 15 "Error running $updateTool"
fi fi
else else
ScriptLogging "couldn't find $updateTool, continuing normally" printlog "couldn't find $updateTool, continuing normally"
return 1 return 1
fi fi
return 0 return 0
@@ -1111,7 +1114,7 @@ if [ -z "$archiveName" ]; then
archiveName="${name}.zip" archiveName="${name}.zip"
;; ;;
*) *)
ScriptLogging "Cannot handle type $type" printlog "Cannot handle type $type"
cleanupAndExit 99 cleanupAndExit 99
;; ;;
esac esac
@@ -1131,14 +1134,14 @@ if [ -z "$targetDir" ]; then
targetDir="/" targetDir="/"
;; ;;
*) *)
ScriptLogging "Cannot handle type $type" printlog "Cannot handle type $type"
cleanupAndExit 99 cleanupAndExit 99
;; ;;
esac esac
fi fi
if [[ -z $blockingProcesses ]]; then if [[ -z $blockingProcesses ]]; then
ScriptLogging "no blocking processes defined, using $name as default" printlog "no blocking processes defined, using $name as default"
blockingProcesses=( $name ) blockingProcesses=( $name )
fi fi
@@ -1152,9 +1155,9 @@ else
fi fi
# change directory to temporary working directory # change directory to temporary working directory
ScriptLogging "Changing directory to $tmpDir" printlog "Changing directory to $tmpDir"
if ! cd "$tmpDir"; then if ! cd "$tmpDir"; then
ScriptLogging "error changing directory $tmpDir" printlog "error changing directory $tmpDir"
#rm -Rf "$tmpDir" #rm -Rf "$tmpDir"
cleanupAndExit 1 cleanupAndExit 1
fi fi
@@ -1167,14 +1170,14 @@ if [[ -n $appVersion ]]; then
cleanupAndExit 0 cleanupAndExit 0
fi # otherwise continue fi # otherwise continue
else else
ScriptLogging "DEBUG mode enabled, not running update tool" printlog "DEBUG mode enabled, not running update tool"
fi fi
fi fi
# when user is logged in, and app is running, prompt user to quit app # when user is logged in, and app is running, prompt user to quit app
if [[ $BLOCKING_PROCESS_ACTION == "ignore" ]]; then if [[ $BLOCKING_PROCESS_ACTION == "ignore" ]]; then
ScriptLogging "ignoring blocking processes" printlog "ignoring blocking processes"
else else
if [[ $currentUser != "loginwindow" ]]; then if [[ $currentUser != "loginwindow" ]]; then
if [[ ${#blockingProcesses} -gt 0 ]]; then if [[ ${#blockingProcesses} -gt 0 ]]; then
@@ -1188,12 +1191,12 @@ fi
# download the archive # download the archive
if [ -f "$archiveName" ] && [ "$DEBUG" -ne 0 ]; then if [ -f "$archiveName" ] && [ "$DEBUG" -ne 0 ]; then
ScriptLogging "$archiveName exists and DEBUG enabled, skipping download" printlog "$archiveName exists and DEBUG enabled, skipping download"
else else
# download the dmg # download the dmg
ScriptLogging "Downloading $downloadURL to $archiveName" printlog "Downloading $downloadURL to $archiveName"
if ! curl --location --fail --silent "$downloadURL" -o "$archiveName"; then if ! curl --location --fail --silent "$downloadURL" -o "$archiveName"; then
ScriptLogging "error downloading $downloadURL" printlog "error downloading $downloadURL"
cleanupAndExit 2 cleanupAndExit 2
fi fi
fi fi
@@ -1215,18 +1218,18 @@ case $type in
installPkgInZip installPkgInZip
;; ;;
*) *)
ScriptLogging "Cannot handle type $type" printlog "Cannot handle type $type"
cleanupAndExit 99 cleanupAndExit 99
;; ;;
esac esac
# print installed application location and version # print installed application location and version
sleep 10 sleep 10 # wait a moment to let spotlight catch up
getAppVersion getAppVersion
# TODO: notify when done # TODO: notify when done
if [[ $currentUser != "loginwindow" ]]; then if [[ $currentUser != "loginwindow" ]]; then
ScriptLogging "notifying" printlog "notifying"
displaynotification "Installed $name, version $appversion" "Installation complete!" displaynotification "Installed $name, version $appversion" "Installation complete!"
fi fi