#!/bin/bash ########################################################################## # Shellscript : Downloade and install Applikation # Autor : Andreas Vogel, NEXT Enterprise GmbH ########################################################################## set -x ########################################################################## #################### Testing ############################################# app="Firefox.app" appname=${app%.*} processpath="/Applications/Firefox.app/Contents/MacOS/firefox" universallink=yes type="pkg" #################### Executing ########################################### #app="$4" #appname=${app%.*} #processpath="$5" #universallink="$6" #type="$7" ########################################################################## #################### Testing ############################################# if [[ $universallink == "no" ]] then if [[ $(arch) == "arm64" ]]; then downloadURL="https://download.mozilla.org/?product=firefox-esr-pkg-latest-ssl&os=osx" elif [[ $(arch) == "i386" ]]; then downloadURL="https://download.mozilla.org/?product=firefox-esr-pkg-latest-ssl&os=osx" fi else downloadURL="https://download.mozilla.org/?product=firefox-esr-pkg-latest-ssl&os=osx" fi #################### Executing ########################################### #if [[ $universallink == "no" ]] #then # if [[ $(arch) == "arm64" ]]; then # downloadURL="${8}" # elif [[ $(arch) == "i386" ]]; then # downloadURL="${9}" # fi #else # downloadURL="${9}" #fi ########################################################################## logandmetadir="/private/var/log/" logfileproces="Patch Management" #logfileproces="${10}" terminateprocess="true" tempdir=$(/usr/bin/mktemp -d "/private/tmp/tmp.XXXXXX") log="$logandmetadir$logfileproces.log" ########################################################################## # free_1="${11}" ##################################################################################################### 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 "$tempdir" # curl -f -s --connect-timeout 30 --retry 5 --retry-delay 60 -L -J -O "$downloadURL" /usr/bin/curl "$downloadURL" --silent --location --output "Firefox.pkg" if [ $? == 0 ] then echo "$(date) | Downloaded [$app] to [$tempdir]" else echo "$(date) | Failure to download [$downloadURL] to [$tempdir]" exit 1 fi tempSearchPath="$tempdir/*" for f in $tempSearchPath; do tempfile=$f done } ##################################################################################################### installPKG () { waitForProcess "$processpath" "300" "$terminateprocess" echo "$(date) | Installing $appname" if [[ -d "/Applications/$app" ]]; then rm -rf "/Applications/$app" fi installer -pkg "$tempfile" -target /Applications if [ "$?" = "0" ] then echo "$(date) | $appname Installed" echo "$(date) | Cleaning Up" #rm -rf "$tempdir" echo "$(date) | Application [$appname] succesfully installed" exit 0 else echo "$(date) | Failed to install $appname" #rm -rf "$tempdir" exit 1 fi } ##################################################################################################### installDMG () { waitForProcess "$processpath" "300" "$terminateprocess" echo "$(date) | Installing [$appname]" volume="$tempdir/$appname" echo "$(date) | Mounting Image" hdiutil attach -quiet -nobrowse -mountpoint "$volume" "$tempfile" if [[ -d "/Applications/$app" ]]; then echo "$(date) | Removing existing files" rm -rf "/Applications/$app" fi echo "$(date) | Copying app files to /Applications/$app" rsync -a "$volume"/*.app/ "/Applications/$app" echo "$(date) | Un-mounting [$volume]" hdiutil detach -quiet "$volume" if [[ -a "/Applications/$app" ]] then echo "$(date) | [$appname] Installed" echo "$(date) | Cleaning Up" rm -rf "$tempdir" echo "$(date) | Fixing up permissions" sudo chown -R root:wheel "/Applications/$app" echo "$(date) | Application [$appname] succesfully installed" exit 0 else echo "$(date) | Failed to install [$appname]" rm -rf "$tempdir" exit 1 fi } ##################################################################################################### installPkgInDmg() { archiveName=$(find "$tempfile" -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=$(find "$dmgmount" -iname "*.pkg" ) if [[ $findfiles != "" ]] then echo "found pkg in dmg $archiveName" archiveName="$findfiles" else echo "couldn't find pkg in dmg $archiveName" rm -rf "$tempdir" exit 1 fi 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 "$tempdir" 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 "$tempdir" exit 1 fi } ##################################################################################################### installZIP () { waitForProcess "$processpath" "300" "$terminateprocess" echo "$(date) | Installing $appname" # Change into temp dir cd "$tempdir" if [ "$?" = "0" ] then echo "$(date) | Changed current directory to $tempdir" else echo "$(date) | failed to change to $tempfile" if [ -d "$tempdir" ]; then rm -rf $tempdir; fi exit 1 fi unzip -qq -o "$tempfile" if [ "$?" = "0" ] then echo "$(date) | $tempfile unzipped" else echo "$(date) | failed to unzip $tempfile" if [ -d "$tempdir" ]; then rm -rf $tempdir; fi exit 1 fi if [[ -a "/Applications/$app" ]]; then echo "$(date) | Removing old installation at /Applications/$app" rm -rf "/Applications/$app" fi rsync -a "$app/" "/Applications/$app" if [ "$?" = "0" ]; then echo "$(date) | $appname moved into /Applications" else echo "$(date) | failed to move $appname to /Applications" if [ -d "$tempdir" ]; then rm -rf $tempdir; fi exit 1 fi echo "$(date) | Fix up permissions" sudo chown -R root:wheel "/Applications/$app" if [ "$?" = "0" ] then echo "$(date) | correctly applied permissions to $appname" else echo "$(date) | failed to apply permissions to $appname" if [ -d "$tempdir" ]; then rm -rf $tempdir; fi exit 1 fi if [ "$?" = "0" ] then if [[ -a "/Applications/$app" ]] then echo "$(date) | $appname Installed" echo "$(date) | Cleaning Up" rm -rf "$tempdir" echo "$(date) | Fixing up permissions" sudo chown -R root:wheel "/Applications/$app" echo "$(date) | Application [$appname] succesfully installed" exit 0 else echo "$(date) | Failed to install $appname" exit 1 fi else echo "$(date) | Failed to install $appname" if [ -d "$tempdir" ]; then rm -rf $tempdir; fi exit 1 fi } ##################################################################################################### installFromTBZ () { waitForProcess "$processpath" "300" "$terminateprocess" echo "$(date) | Installing $appname" # Change into temp dir cd "$tempdir" if [ "$?" = "0" ] then echo "$(date) | Changed current directory to $tempdir" else echo "$(date) | failed to change to $tempfile" if [ -d "$tempdir" ]; then rm -rf $tempdir; fi exit 1 fi tar -xf "$tempfile" if [ "$?" = "0" ] then echo "$(date) | $tempfile unzipped" else echo "$(date) | failed to unzip $tempfile" if [ -d "$tempdir" ]; then rm -rf $tempdir; fi exit 1 fi if [[ -a "/Applications/$app" ]]; then echo "$(date) | Removing old installation at /Applications/$app" rm -rf "/Applications/$app" fi rsync -a "$app/" "/Applications/$app" if [ "$?" = "0" ]; then echo "$(date) | $appname moved into /Applications" else echo "$(date) | failed to move $appname to /Applications" if [ -d "$tempdir" ]; then rm -rf $tempdir; fi exit 1 fi echo "$(date) | Fix up permissions" sudo chown -R root:wheel "/Applications/$app" if [ "$?" = "0" ] then echo "$(date) | correctly applied permissions to $appname" else echo "$(date) | failed to apply permissions to $appname" if [ -d "$tempdir" ]; then rm -rf $tempdir; fi exit 1 fi if [ "$?" = "0" ] then if [[ -a "/Applications/$app" ]] then echo "$(date) | $appname Installed" echo "$(date) | Cleaning Up" rm -rf "$tempdir" echo "$(date) | Fixing up permissions" sudo chown -R root:wheel "/Applications/$app" echo "$(date) | Application [$appname] succesfully installed" exit 0 else echo "$(date) | Failed to install $appname" exit 1 fi else echo "$(date) | Failed to install $appname" if [ -d "$tempdir" ]; then rm -rf $tempdir; fi 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 ############################## start Install ################################################# case $type in pkg) installPKG ;; dmg) installDMG ;; pkgInDmg) installPkgInDmg ;; zip) installZIP ;; tbz) installFromTBZ ;; *) echo "Cannot handle type $type" ;; esac