Files
2026-02-16 15:05:15 +01:00

154 lines
4.1 KiB
Bash
Executable File

#!/bin/bash
##########################################################################
# Shellscript : Install JamfConnect
# Autor : Andreas Vogel, nextenterprise gmbh,
##########################################################################
set -x
app="Jamf Connect.app"
appname=${app%.*}
processpath="/Applications/Jamf Connect.app/Contents/MacOS/Jamf Connect"
tmpDir=$(/usr/bin/mktemp -d "/private/tmp/tmp.XXXXXX")
downloadURL="https://files.jamfconnect.com/JamfConnect.dmg"
logandmetadir="/private/var/log/"
terminateprocess="true"
tmpDir=$(/usr/bin/mktemp -d "/private/tmp/tmp.XXXXXX")
log="$logandmetadir$appname.log"
#####################################################################################################
waitForProcess () {
## $1 = name of process to check for
## $2 = length of delay (if missing, function to generate random delay between 10 and 60s)
## $3 = true/false if = "true" terminate process, if "false" wait for it to close
processName=$1
fixedDelay=$2
terminate=$3
echo "$(date) | Waiting for other [$processName] processes to end"
while ps aux | grep "$processName" | grep -v grep &>/dev/null; do
if [[ $terminate == "true" ]]; then
echo "$(date) | + [$appname] running, terminating [$processpath]..."
pkill -f "$processName"
return
fi
done
echo "$(date) | No instances of [$processName] found, safe to proceed"
}
#####################################################################################################
downloadApp () {
echo "$(date) | Starting downlading of [$appname]"
waitForProcess "curl -f"
echo "$(date) | Downloading $appname"
cd "$tmpDir"
curl -f -s --connect-timeout 30 --retry 5 --retry-delay 60 -L -J -O "$downloadURL"
if [ $? == 0 ]
then
echo "download ok"
else
echo "faild to download"
exit 1
fi
}
#####################################################################################################
installPkgInDmg() {
archiveName=$(find "$tmpDir" -iname "*.dmg" -maxdepth 1 )
echo $archiveName
echo "Mounting $archiveName"
# always pipe 'Y\n' in case the dmg requires an agreement
if ! dmgmount=$(echo 'Y'$'\n' | hdiutil attach "$archiveName" -nobrowse -readonly | tail -n 1 | cut -c 54- ); then
echo "Error mounting $archiveName"
fi
if [[ ! -e $dmgmount ]]; then
echo "Error mounting $archiveName"
exit 1
fi
echo "Mounted: $dmgmount"
findfiles="$dmgmount/JamfConnect.pkg"
if [[ $findfiles != "" ]]
then
echo "found pkg in dmg $archiveName"
archiveName="$findfiles"
else
echo "couldn't find pkg in dmg $archiveName"
rm -rf "$tmpDir"
exit 1
fi
installPKG
}
#####################################################################################################
installPKG () {
waitForProcess "$processpath" "300" "$terminateprocess"
echo "$(date) | Installing $appname"
if [[ -d "/Applications/$app" ]]; then
rm -rf "/Applications/$app"
fi
installer -pkg "$archiveName" -target /Applications
if [ "$?" = "0" ]
then
echo "$(date) | $appname Installed"
echo "$(date) | Cleaning Up"
rm -rf "$tmpDir"
echo "$(date) | Application [$appname] succesfully installed"
echo "$(date) | Un-mounting [$dmgmount]"
hdiutil detach -quiet "$dmgmount"
exit 0
else
echo "$(date) | Failed to install $appname"
echo "$(date) | Un-mounting [$dmgmount]"
hdiutil detach -quiet "$dmgmount"
rm -rf "$tmpDir"
exit 1
fi
}
#####################################################################################################
startLog() {
if [[ ! -d "$logandmetadir" ]]; then
echo "$(date) | Creating [$logandmetadir] to store logs"
mkdir -p "$logandmetadir"
fi
exec &> >(tee -a "$log")
}
############################## start Log #####################################################
startLog
echo ""
echo "##############################################################"
echo "# $(date) | Logging install of [$appname] to [$log]"
echo "##############################################################"
echo ""
############################## downloadApp ###################################################
downloadApp
installPkgInDmg