Improved logging levels

Also fixes #340
This commit is contained in:
Søren Theilgaard
2022-02-08 12:36:16 +01:00
parent 071586e43c
commit 0432eb62d3
4 changed files with 40 additions and 40 deletions

View File

@@ -1,5 +1,6 @@
## v10 ## v10
- Changed and improved logging levels on many messages throughout the script. Should be more polished.
- -
## v9 ## v9

View File

@@ -10,21 +10,21 @@ fi
# MARK: argument parsing # MARK: argument parsing
if [[ $# -eq 0 ]]; then if [[ $# -eq 0 ]]; then
if [[ -z $label ]]; then # check if label is set inside script if [[ -z $label ]]; then # check if label is set inside script
printlog "no label provided, printing labels" printlog "no label provided, printing labels" REQ
grep -E '^[a-z0-9\_-]*(\)|\|\\)$' "$0" | tr -d ')|\' | grep -v -E '^(broken.*|longversion|version|valuesfromarguments)$' | sort grep -E '^[a-z0-9\_-]*(\)|\|\\)$' "$0" | tr -d ')|\' | grep -v -E '^(broken.*|longversion|version|valuesfromarguments)$' | sort
#grep -E '^[a-z0-9\_-]*(\)|\|\\)$' "${labelFile}" | tr -d ')|\' | grep -v -E '^(broken.*|longversion|version|valuesfromarguments)$' | sort #grep -E '^[a-z0-9\_-]*(\)|\|\\)$' "${labelFile}" | tr -d ')|\' | grep -v -E '^(broken.*|longversion|version|valuesfromarguments)$' | sort
exit 0 exit 0
fi fi
elif [[ $1 == "/" ]]; then elif [[ $1 == "/" ]]; then
# jamf uses sends '/' as the first argument # jamf uses sends '/' as the first argument
printlog "shifting arguments for Jamf" printlog "shifting arguments for Jamf" REQ
shift 3 shift 3
fi fi
while [[ -n $1 ]]; do while [[ -n $1 ]]; do
if [[ $1 =~ ".*\=.*" ]]; then if [[ $1 =~ ".*\=.*" ]]; then
# if an argument contains an = character, send it to eval # if an argument contains an = character, send it to eval
printlog "setting variable from argument $1" printlog "setting variable from argument $1" REQ
eval $1 eval $1
else else
# assume it's a label # assume it's a label
@@ -96,31 +96,31 @@ currentUser=$(scutil <<< "show State:/Users/ConsoleUser" | awk '/Name :/ { print
# MARK: check for root # MARK: check for root
if [[ "$(whoami)" != "root" && "$DEBUG" -eq 0 ]]; then if [[ "$(whoami)" != "root" && "$DEBUG" -eq 0 ]]; then
# not running as root # not running as root
cleanupAndExit 6 "not running as root, exiting" cleanupAndExit 6 "not running as root, exiting" ERROR
fi fi
# MARK: labels in case statement # MARK: labels in case statement
case $label in case $label in
longversion) longversion)
# print the script version # print the script version
printlog "Installomater: version $VERSION ($VERSIONDATE)" printlog "Installomater: version $VERSION ($VERSIONDATE)" REQ
exit 0 exit 0
;; ;;
valuesfromarguments) valuesfromarguments)
if [[ -z $name ]]; then if [[ -z $name ]]; then
printlog "need to provide 'name'" printlog "need to provide 'name'" ERROR
exit 1 exit 1
fi fi
if [[ -z $type ]]; then if [[ -z $type ]]; then
printlog "need to provide 'type'" printlog "need to provide 'type'" ERROR
exit 1 exit 1
fi fi
if [[ -z $downloadURL ]]; then if [[ -z $downloadURL ]]; then
printlog "need to provide 'downloadURL'" printlog "need to provide 'downloadURL'" ERROR
exit 1 exit 1
fi fi
if [[ -z $expectedTeamID ]]; then if [[ -z $expectedTeamID ]]; then
printlog "need to provide 'expectedTeamID'" printlog "need to provide 'expectedTeamID'" ERROR
exit 1 exit 1
fi fi
;; ;;

View File

@@ -176,7 +176,7 @@ versionFromGit() {
appNewVersion=$(curl --silent --fail "https://api.github.com/repos/$gitusername/$gitreponame/releases/latest" | grep tag_name | cut -d '"' -f 4 | sed 's/[^0-9\.]//g') appNewVersion=$(curl --silent --fail "https://api.github.com/repos/$gitusername/$gitreponame/releases/latest" | grep tag_name | cut -d '"' -f 4 | sed 's/[^0-9\.]//g')
if [ -z "$appNewVersion" ]; then if [ -z "$appNewVersion" ]; then
printlog "could not retrieve version number for $gitusername/$gitreponame" printlog "could not retrieve version number for $gitusername/$gitreponame" WARN
appNewVersion="" appNewVersion=""
else else
echo "$appNewVersion" echo "$appNewVersion"
@@ -229,9 +229,9 @@ getAppVersion() {
applist=$(mdfind "kind:application $appName" -0 ) applist=$(mdfind "kind:application $appName" -0 )
fi fi
if [[ -z applist ]]; then if [[ -z applist ]]; then
printlog "No previous app found" DEBUG printlog "No previous app found" INFO
else else
printlog "App(s) found: ${applist}" DEBUG printlog "App(s) found: ${applist}" INFO
fi fi
appPathArray=( ${(0)applist} ) appPathArray=( ${(0)applist} )
@@ -255,10 +255,10 @@ getAppVersion() {
fi fi
fi fi
else else
printlog "could not determine location of $appName" printlog "could not determine location of $appName" WARN
fi fi
else else
printlog "could not find $appName" printlog "could not find $appName" WARN
fi fi
} }
@@ -385,7 +385,7 @@ reopenClosedProcess() {
processuser=$(ps aux | grep -i "${appName}" | grep -vi "grep" | awk '{print $1}') processuser=$(ps aux | grep -i "${appName}" | grep -vi "grep" | awk '{print $1}')
printlog "Reopened ${appName} as $processuser" printlog "Reopened ${appName} as $processuser"
else else
printlog "App not closed, so no reopen." DEBUG printlog "App not closed, so no reopen." INFO
fi fi
} }
@@ -395,7 +395,7 @@ installAppWithPath() { # $1: path to app to install in $targetDir
# check if app exists # check if app exists
if [ ! -e "$appPath" ]; then if [ ! -e "$appPath" ]; then
cleanupAndExit 8 "could not find: $appPath" DEBUG cleanupAndExit 8 "could not find: $appPath" ERROR
fi fi
# verify with spctl # verify with spctl
@@ -427,10 +427,12 @@ installAppWithPath() { # $1: path to app to install in $targetDir
printlog "notifying" printlog "notifying"
displaynotification "$message" "No update for $name!" displaynotification "$message" "No update for $name!"
fi fi
cleanupAndExit 0 "No new version to install" INFO cleanupAndExit 0 "No new version to install" WARN
else else
printlog "Using force to install anyway." printlog "Using force to install anyway."
fi fi
elif [[ -z $appversion ]]; then
printlog "Installing $name version $appNewVersion on versionKey $versionKey."
else else
printlog "Downloaded version of $name is $appNewVersion on versionKey $versionKey (replacing version $appversion)." printlog "Downloaded version of $name is $appNewVersion on versionKey $versionKey (replacing version $appversion)."
fi fi
@@ -446,7 +448,7 @@ installAppWithPath() { # $1: path to app to install in $targetDir
printlog "notifying" printlog "notifying"
displaynotification "$message" "Error updating $name!" displaynotification "$message" "Error updating $name!"
fi fi
cleanupAndExit 6 "Installed macOS is too old for this app." INFO cleanupAndExit 6 "Installed macOS is too old for this app." ERROR
fi fi
fi fi
@@ -585,7 +587,7 @@ installFromPKG() {
printlog "notifying" printlog "notifying"
displaynotification "$message" "No update for $name!" displaynotification "$message" "No update for $name!"
fi fi
cleanupAndExit 0 "No new version to install" INFO cleanupAndExit 0 "No new version to install" WARN
else else
printlog "Using force to install anyway." printlog "Using force to install anyway."
fi fi
@@ -770,13 +772,13 @@ runUpdateTool() {
printlog "Error running $updateTool, Procceding with normal installation. Exit Status: $updateStatus Error:\n$logoutput" WARN printlog "Error running $updateTool, Procceding with normal installation. Exit Status: $updateStatus Error:\n$logoutput" WARN
return 1 return 1
if [[ $type == updateronly ]]; then if [[ $type == updateronly ]]; then
cleanupAndExit 77 "No Download URL Set, this is an update only application and the updater failed" WARN cleanupAndExit 77 "No Download URL Set, this is an update only application and the updater failed" ERROR
fi fi
elif [[ $updateStatus -eq 0 ]]; then elif [[ $updateStatus -eq 0 ]]; then
printlog "Debugging enabled, update tool output was:\n$logoutput" DEBUG printlog "Debugging enabled, update tool output was:\n$logoutput" DEBUG
fi fi
else else
printlog "couldn't find $updateTool, continuing normally" printlog "couldn't find $updateTool, continuing normally" WARN
return 1 return 1
fi fi
return 0 return 0

View File

@@ -1,13 +1,13 @@
*) *)
# unknown label # unknown label
#printlog "unknown label $label" #printlog "unknown label $label"
cleanupAndExit 1 "unknown label $label" cleanupAndExit 1 "unknown label $label" ERROR
;; ;;
esac esac
# Are we only asked to return label name # Are we only asked to return label name
if [[ $RETURN_LABEL_NAME -eq 1 ]]; then if [[ $RETURN_LABEL_NAME -eq 1 ]]; then
printlog "Only returning label name." printlog "Only returning label name." REQ
printlog "$name" printlog "$name"
echo "$name" echo "$name"
exit exit
@@ -18,7 +18,7 @@ fi
if [[ ${INTERRUPT_DND} = "no" ]]; then if [[ ${INTERRUPT_DND} = "no" ]]; then
# Check if a fullscreen app is active # Check if a fullscreen app is active
if hasDisplaySleepAssertion; then if hasDisplaySleepAssertion; then
cleanupAndExit 1 "active display sleep assertion detected, aborting" cleanupAndExit 1 "active display sleep assertion detected, aborting" ERROR
fi fi
fi fi
@@ -63,9 +63,9 @@ if [[ ! -a "${LOGO}" ]]; then
LOGO="/Applications/App Store.app/Contents/Resources/AppIcon.icns" LOGO="/Applications/App Store.app/Contents/Resources/AppIcon.icns"
fi fi
fi fi
printlog "LOGO=${LOGO}" printlog "LOGO=${LOGO}" INFO
printlog "Label type: $type" printlog "Label type: $type" INFO
# MARK: extract info from data # MARK: extract info from data
if [ -z "$archiveName" ]; then if [ -z "$archiveName" ]; then
@@ -87,7 +87,7 @@ if [ -z "$archiveName" ]; then
;; ;;
esac esac
fi fi
printlog "archiveName: $archiveName" DEBUG printlog "archiveName: $archiveName" INFO
if [ -z "$appName" ]; then if [ -z "$appName" ]; then
# when not given derive from name # when not given derive from name
@@ -105,14 +105,13 @@ if [ -z "$targetDir" ]; then
updateronly) updateronly)
;; ;;
*) *)
printlog "Cannot handle type $type" cleanupAndExit 99 "Cannot handle type $type" ERROR
cleanupAndExit 99
;; ;;
esac esac
fi fi
if [[ -z $blockingProcesses ]]; then if [[ -z $blockingProcesses ]]; then
printlog "no blocking processes defined, using $name as default" printlog "no blocking processes defined, using $name as default" INFO
blockingProcesses=( $name ) blockingProcesses=( $name )
fi fi
@@ -128,8 +127,7 @@ fi
# MARK: change directory to temporary working directory # MARK: change directory to temporary working directory
printlog "Changing directory to $tmpDir" DEBUG printlog "Changing directory to $tmpDir" DEBUG
if ! cd "$tmpDir"; then if ! cd "$tmpDir"; then
printlog "error changing directory $tmpDir" cleanupAndExit 1 "error changing directory $tmpDir" ERROR
cleanupAndExit 1
fi fi
# MARK: get installed version # MARK: get installed version
@@ -152,10 +150,10 @@ if [[ -n $appNewVersion ]]; then
printlog "notifying" printlog "notifying"
displaynotification "$message" "No update for $name!" displaynotification "$message" "No update for $name!"
fi fi
cleanupAndExit 0 "No newer version." cleanupAndExit 0 "No newer version." WARN
fi fi
else else
printlog "DEBUG mode 1 enabled, not exiting, but there is no new version of app." printlog "DEBUG mode 1 enabled, not exiting, but there is no new version of app." WARN
fi fi
fi fi
else else
@@ -174,7 +172,7 @@ if [[ (-n $appversion && -n "$updateTool") || "$type" == "updateronly" ]]; then
cleanupAndExit 0 cleanupAndExit 0
fi # otherwise continue fi # otherwise continue
else else
printlog "DEBUG mode 1 enabled, not running update tool" printlog "DEBUG mode 1 enabled, not running update tool" WARN
fi fi
fi fi
@@ -197,14 +195,14 @@ else
deduplicatelogs "$curlDownload" deduplicatelogs "$curlDownload"
if [[ $curlDownloadStatus -ne 0 ]]; then if [[ $curlDownloadStatus -ne 0 ]]; then
#if ! curl --location --fail --silent "$downloadURL" -o "$archiveName"; then #if ! curl --location --fail --silent "$downloadURL" -o "$archiveName"; then
printlog "error downloading $downloadURL" printlog "error downloading $downloadURL" ERROR
message="$name update/installation failed. This will be logged, so IT can follow up." message="$name update/installation failed. This will be logged, so IT can follow up."
if [[ $currentUser != "loginwindow" && $NOTIFY == "all" ]]; then if [[ $currentUser != "loginwindow" && $NOTIFY == "all" ]]; then
printlog "notifying" printlog "notifying"
if [[ $updateDetected == "YES" ]]; then if [[ $updateDetected == "YES" ]]; then
displaynotification "$message" "Error updating $name" ERROR displaynotification "$message" "Error updating $name"
else else
displaynotification "$message" "Error installing $name" ERROR displaynotification "$message" "Error installing $name"
fi fi
fi fi
printlog "File list: $(ls -lh "$archiveName")" ERROR printlog "File list: $(ls -lh "$archiveName")" ERROR
@@ -269,8 +267,7 @@ case $type in
installAppInDmgInZip installAppInDmgInZip
;; ;;
*) *)
printlog "Cannot handle type $type" cleanupAndExit 99 "Cannot handle type $type" ERROR
cleanupAndExit 99
;; ;;
esac esac