336 lines
8.5 KiB
Bash
Executable File
336 lines
8.5 KiB
Bash
Executable File
#!/bin/bash
|
|
##########################################################################
|
|
# Shellscript : Install Slack
|
|
# Autor : Andreas Vogel, nextenterprise gmbh,
|
|
##########################################################################
|
|
|
|
weburl="https://slack.com/ssb/download-osx-universal"
|
|
appname="Slack"
|
|
app="Slack.app"
|
|
processpath="/Applications/Slack.app/Contents/MacOS/Slack"
|
|
|
|
|
|
logandmetadir="/private/var/log/"
|
|
terminateprocess="true"
|
|
tempdir=$(mktemp -d)
|
|
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
|
|
|
|
# If we've been passed a delay we should use it, otherwise we'll create a random delay each run
|
|
if [[ ! $fixedDelay ]]; then
|
|
delay=$(( $RANDOM % 50 + 10 ))
|
|
else
|
|
delay=$fixedDelay
|
|
fi
|
|
|
|
echo "$(date) | + Another instance of $processName is running, waiting [$delay] seconds"
|
|
sleep $delay
|
|
done
|
|
|
|
echo "$(date) | No instances of [$processName] found, safe to proceed"
|
|
|
|
}
|
|
|
|
|
|
function downloadApp () {
|
|
## $appname = Description of the App we are installing
|
|
## $weburl = URL of download location
|
|
## $tempfile = location of temporary DMG file downloaded
|
|
|
|
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 "$weburl"
|
|
if [ $? == 0 ]; then
|
|
|
|
|
|
tempSearchPath="$tempdir/*"
|
|
for f in $tempSearchPath; do
|
|
tempfile=$f
|
|
done
|
|
|
|
case $tempfile in
|
|
|
|
*.pkg|*.PKG)
|
|
packageType="PKG"
|
|
;;
|
|
|
|
*.zip|*.ZIP)
|
|
packageType="ZIP"
|
|
;;
|
|
|
|
*.dmg|*.DMG)
|
|
packageType="DMG"
|
|
;;
|
|
|
|
*)
|
|
# We can't tell what this is by the file name, lets look at the metadata
|
|
echo "$(date) | Unknown file type [$f], analysing metadata"
|
|
metadata=$(file "$tempfile")
|
|
if [[ "$metadata" == *"Zip archive data"* ]]; then
|
|
packageType="ZIP"
|
|
mv "$tempfile" "$tempdir/install.zip"
|
|
tempfile="$tempdir/install.zip"
|
|
fi
|
|
|
|
if [[ "$metadata" == *"xar archive"* ]]; then
|
|
packageType="PKG"
|
|
mv "$tempfile" "$tempdir/install.pkg"
|
|
tempfile="$tempdir/install.pkg"
|
|
fi
|
|
|
|
if [[ "$metadata" == *"bzip2 compressed data"* ]] || [[ "$metadata" == *"zlib compressed data"* ]] ; then
|
|
packageType="DMG"
|
|
mv "$tempfile" "$tempdir/install.dmg"
|
|
tempfile="$tempdir/install.dmg"
|
|
fi
|
|
;;
|
|
esac
|
|
|
|
if [[ ! $packageType ]]; then
|
|
echo "Failed to determine temp file type [$metadata]"
|
|
rm -rf "$tempdir"
|
|
else
|
|
echo "$(date) | Downloaded [$app] to [$tempfile]"
|
|
echo "$(date) | Detected install type as [$packageType]"
|
|
fi
|
|
|
|
else
|
|
|
|
echo "$(date) | Failure to download [$weburl] to [$tempfile]"
|
|
exit 1
|
|
fi
|
|
|
|
}
|
|
|
|
function 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
|
|
|
|
}
|
|
|
|
|
|
function installDMG () {
|
|
## $appname = Description of the App we are installing
|
|
## $tempfile = location of temporary DMG file downloaded
|
|
## $volume = name of volume mount point
|
|
## $app = name of Application directory under /Applications
|
|
|
|
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 "$tempfile"
|
|
|
|
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
|
|
|
|
}
|
|
|
|
function installZIP () {
|
|
## $appname = Description of the App we are installing ##
|
|
## $tempfile = location of temporary DMG file downloaded ##
|
|
## $volume = name of volume mount point ##
|
|
## $app = name of Application directory under /Applications ##
|
|
|
|
|
|
# Check if app is running, if it is we need to wait.
|
|
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
|
|
|
|
# Copy over new files
|
|
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
|
|
|
|
# Make sure permissions are correct
|
|
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 "$tempfile"
|
|
|
|
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
|
|
}
|
|
|
|
|
|
function startLog() {
|
|
|
|
if [[ ! -d "$logandmetadir" ]]; then
|
|
|
|
echo "$(date) | Creating [$logandmetadir] to store logs"
|
|
mkdir -p "$logandmetadir"
|
|
fi
|
|
|
|
exec &> >(tee -a "$log")
|
|
|
|
}
|
|
|
|
|
|
waitForDesktop () {
|
|
until ps aux | grep /System/Library/CoreServices/Dock.app/Contents/MacOS/Dock | grep -v grep &>/dev/null; do
|
|
delay=$(( $RANDOM % 50 + 10 ))
|
|
echo "$(date) | + Dock not running, waiting [$delay] seconds"
|
|
sleep $delay
|
|
done
|
|
echo "$(date) | Dock is here, lets carry on"
|
|
}
|
|
|
|
###################################################################################
|
|
startLog
|
|
|
|
echo ""
|
|
echo "##############################################################"
|
|
echo "# $(date) | Logging install of [$appname] to [$log]"
|
|
echo "############################################################"
|
|
echo ""
|
|
|
|
|
|
# Wait for Desktop
|
|
waitForDesktop
|
|
|
|
# Download app
|
|
downloadApp
|
|
|
|
# Install PKG file
|
|
if [[ $packageType == "PKG" ]]; then
|
|
installPKG
|
|
fi
|
|
|
|
# Install PKG file
|
|
if [[ $packageType == "ZIP" ]]; then
|
|
installZIP
|
|
fi
|
|
|
|
# Install PKG file
|
|
if [[ $packageType == "DMG" ]]; then
|
|
installDMG
|
|
fi |