Skip to content

Commit

Permalink
fix: don't add function if we don't need it, fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jahzielv committed Jan 9, 2025
1 parent 280ddb1 commit 65b4bfd
Show file tree
Hide file tree
Showing 13 changed files with 458 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ WHERE fla.token IN (?)

// This is the list of Fleet-maintained apps we want to update ("token" is an ID found on the brew
// metadata)
appTokens := []string{"1password", "brave-browser", "docker", "figma", "google-chrome", "visual-studio-code", "firefox", "notion", "slack", "whatsapp"}
appTokens := []string{"1password", "brave-browser", "docker", "figma", "google-chrome", "visual-studio-code", "firefox", "notion", "slack", "whatsapp", "postman"}

stmt, args, err := sqlx.In(selectStmt, appTokens)
if err != nil {
Expand Down
7 changes: 6 additions & 1 deletion server/mdm/maintainedapps/scripts.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,17 @@ func installScriptForApp(app maintainedApp, cask *brewCask) (string, error) {

sb.AddVariable("TMPDIR", `$(dirname "$(realpath $INSTALLER_PATH)")`)
sb.AddVariable("APPDIR", `"/Applications/"`)
sb.AddFunction("quit_application", quitApplicationFunc)

formats := strings.Split(app.InstallerFormat, ":")
sb.Extract(formats[0])

var includeQuitFunc bool
for _, artifact := range cask.Artifacts {
switch {
case len(artifact.App) > 0:
sb.Write("# copy to the applications folder")
sb.Writef("quit_application '%s'", app.BundleIdentifier)
includeQuitFunc = true
for _, appPath := range artifact.App {
sb.Writef(`sudo [ -d "$APPDIR/%[1]s" ] && sudo mv "$APPDIR/%[1]s" "$TMPDIR/%[1]s.bkp"`, appPath)
sb.Copy(appPath, "$APPDIR")
Expand Down Expand Up @@ -58,6 +59,10 @@ func installScriptForApp(app maintainedApp, cask *brewCask) (string, error) {
}
}

if includeQuitFunc {
sb.AddFunction("quit_application", quitApplicationFunc)
}

return sb.String(), nil
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,49 @@
# variables
APPDIR="/Applications/"
TMPDIR=$(dirname "$(realpath $INSTALLER_PATH)")
# functions

quit_application() {
local bundle_id="$1"
local timeout_duration=10

# check if the application is running
if ! osascript -e "application id \"$bundle_id\" is running" 2>/dev/null; then
return
fi

local console_user
console_user=$(stat -f "%Su" /dev/console)
if [[ $EUID -eq 0 && "$console_user" == "root" ]]; then
echo "Not logged into a non-root GUI; skipping quitting application ID '$bundle_id'."
return
fi

echo "Quitting application '$bundle_id'..."

# try to quit the application within the timeout period
local quit_success=false
SECONDS=0
while (( SECONDS < timeout_duration )); do
if osascript -e "tell application id \"$bundle_id\" to quit" >/dev/null 2>&1; then
if ! pgrep -f "$bundle_id" >/dev/null 2>&1; then
echo "Application '$bundle_id' quit successfully."
quit_success=true
break
fi
fi
sleep 1
done

if [[ "$quit_success" = false ]]; then
echo "Application '$bundle_id' did not quit."
fi
}


# extract contents
unzip "$INSTALLER_PATH" -d "$TMPDIR"
# copy to the applications folder
quit_application 'com.1password.1password'
sudo [ -d "$APPDIR/1Password.app" ] && sudo mv "$APPDIR/1Password.app" "$TMPDIR/1Password.app.bkp"
sudo cp -R "$TMPDIR/1Password.app" "$APPDIR"
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,52 @@
# variables
APPDIR="/Applications/"
TMPDIR=$(dirname "$(realpath $INSTALLER_PATH)")
# functions

quit_application() {
local bundle_id="$1"
local timeout_duration=10

# check if the application is running
if ! osascript -e "application id \"$bundle_id\" is running" 2>/dev/null; then
return
fi

local console_user
console_user=$(stat -f "%Su" /dev/console)
if [[ $EUID -eq 0 && "$console_user" == "root" ]]; then
echo "Not logged into a non-root GUI; skipping quitting application ID '$bundle_id'."
return
fi

echo "Quitting application '$bundle_id'..."

# try to quit the application within the timeout period
local quit_success=false
SECONDS=0
while (( SECONDS < timeout_duration )); do
if osascript -e "tell application id \"$bundle_id\" to quit" >/dev/null 2>&1; then
if ! pgrep -f "$bundle_id" >/dev/null 2>&1; then
echo "Application '$bundle_id' quit successfully."
quit_success=true
break
fi
fi
sleep 1
done

if [[ "$quit_success" = false ]]; then
echo "Application '$bundle_id' did not quit."
fi
}


# extract contents
MOUNT_POINT=$(mktemp -d /tmp/dmg_mount_XXXXXX)
hdiutil attach -plist -nobrowse -readonly -mountpoint "$MOUNT_POINT" "$INSTALLER_PATH"
sudo cp -R "$MOUNT_POINT"/* "$TMPDIR"
hdiutil detach "$MOUNT_POINT"
# copy to the applications folder
quit_application 'com.brave.Browser'
sudo [ -d "$APPDIR/Brave Browser.app" ] && sudo mv "$APPDIR/Brave Browser.app" "$TMPDIR/Brave Browser.app.bkp"
sudo cp -R "$TMPDIR/Brave Browser.app" "$APPDIR"
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,54 @@
# variables
APPDIR="/Applications/"
TMPDIR=$(dirname "$(realpath $INSTALLER_PATH)")
# functions

quit_application() {
local bundle_id="$1"
local timeout_duration=10

# check if the application is running
if ! osascript -e "application id \"$bundle_id\" is running" 2>/dev/null; then
return
fi

local console_user
console_user=$(stat -f "%Su" /dev/console)
if [[ $EUID -eq 0 && "$console_user" == "root" ]]; then
echo "Not logged into a non-root GUI; skipping quitting application ID '$bundle_id'."
return
fi

echo "Quitting application '$bundle_id'..."

# try to quit the application within the timeout period
local quit_success=false
SECONDS=0
while (( SECONDS < timeout_duration )); do
if osascript -e "tell application id \"$bundle_id\" to quit" >/dev/null 2>&1; then
if ! pgrep -f "$bundle_id" >/dev/null 2>&1; then
echo "Application '$bundle_id' quit successfully."
quit_success=true
break
fi
fi
sleep 1
done

if [[ "$quit_success" = false ]]; then
echo "Application '$bundle_id' did not quit."
fi
}


# extract contents
MOUNT_POINT=$(mktemp -d /tmp/dmg_mount_XXXXXX)
hdiutil attach -plist -nobrowse -readonly -mountpoint "$MOUNT_POINT" "$INSTALLER_PATH"
sudo cp -R "$MOUNT_POINT"/* "$TMPDIR"
hdiutil detach "$MOUNT_POINT"
# copy to the applications folder
quit_application 'com.docker.docker'
sudo [ -d "$APPDIR/Docker.app" ] && sudo mv "$APPDIR/Docker.app" "$TMPDIR/Docker.app.bkp"
sudo cp -R "$TMPDIR/Docker.app" "$APPDIR"
/bin/ln -h -f -s -- "$APPDIR/Docker.app/Contents/Resources/bin/docker" "/usr/local/bin/docker"
/bin/ln -h -f -s -- "$APPDIR/Docker.app/Contents/Resources/bin/docker-credential-desktop" "/usr/local/bin/docker-credential-desktop"
Expand Down
41 changes: 41 additions & 0 deletions server/mdm/maintainedapps/testdata/scripts/figma_install.golden.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,49 @@
# variables
APPDIR="/Applications/"
TMPDIR=$(dirname "$(realpath $INSTALLER_PATH)")
# functions

quit_application() {
local bundle_id="$1"
local timeout_duration=10

# check if the application is running
if ! osascript -e "application id \"$bundle_id\" is running" 2>/dev/null; then
return
fi

local console_user
console_user=$(stat -f "%Su" /dev/console)
if [[ $EUID -eq 0 && "$console_user" == "root" ]]; then
echo "Not logged into a non-root GUI; skipping quitting application ID '$bundle_id'."
return
fi

echo "Quitting application '$bundle_id'..."

# try to quit the application within the timeout period
local quit_success=false
SECONDS=0
while (( SECONDS < timeout_duration )); do
if osascript -e "tell application id \"$bundle_id\" to quit" >/dev/null 2>&1; then
if ! pgrep -f "$bundle_id" >/dev/null 2>&1; then
echo "Application '$bundle_id' quit successfully."
quit_success=true
break
fi
fi
sleep 1
done

if [[ "$quit_success" = false ]]; then
echo "Application '$bundle_id' did not quit."
fi
}


# extract contents
unzip "$INSTALLER_PATH" -d "$TMPDIR"
# copy to the applications folder
quit_application 'com.figma.Desktop'
sudo [ -d "$APPDIR/Figma.app" ] && sudo mv "$APPDIR/Figma.app" "$TMPDIR/Figma.app.bkp"
sudo cp -R "$TMPDIR/Figma.app" "$APPDIR"
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,52 @@
# variables
APPDIR="/Applications/"
TMPDIR=$(dirname "$(realpath $INSTALLER_PATH)")
# functions

quit_application() {
local bundle_id="$1"
local timeout_duration=10

# check if the application is running
if ! osascript -e "application id \"$bundle_id\" is running" 2>/dev/null; then
return
fi

local console_user
console_user=$(stat -f "%Su" /dev/console)
if [[ $EUID -eq 0 && "$console_user" == "root" ]]; then
echo "Not logged into a non-root GUI; skipping quitting application ID '$bundle_id'."
return
fi

echo "Quitting application '$bundle_id'..."

# try to quit the application within the timeout period
local quit_success=false
SECONDS=0
while (( SECONDS < timeout_duration )); do
if osascript -e "tell application id \"$bundle_id\" to quit" >/dev/null 2>&1; then
if ! pgrep -f "$bundle_id" >/dev/null 2>&1; then
echo "Application '$bundle_id' quit successfully."
quit_success=true
break
fi
fi
sleep 1
done

if [[ "$quit_success" = false ]]; then
echo "Application '$bundle_id' did not quit."
fi
}


# extract contents
MOUNT_POINT=$(mktemp -d /tmp/dmg_mount_XXXXXX)
hdiutil attach -plist -nobrowse -readonly -mountpoint "$MOUNT_POINT" "$INSTALLER_PATH"
sudo cp -R "$MOUNT_POINT"/* "$TMPDIR"
hdiutil detach "$MOUNT_POINT"
# copy to the applications folder
quit_application 'org.mozilla.firefox'
sudo [ -d "$APPDIR/Firefox.app" ] && sudo mv "$APPDIR/Firefox.app" "$TMPDIR/Firefox.app.bkp"
sudo cp -R "$TMPDIR/Firefox.app" "$APPDIR"
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,52 @@
# variables
APPDIR="/Applications/"
TMPDIR=$(dirname "$(realpath $INSTALLER_PATH)")
# functions

quit_application() {
local bundle_id="$1"
local timeout_duration=10

# check if the application is running
if ! osascript -e "application id \"$bundle_id\" is running" 2>/dev/null; then
return
fi

local console_user
console_user=$(stat -f "%Su" /dev/console)
if [[ $EUID -eq 0 && "$console_user" == "root" ]]; then
echo "Not logged into a non-root GUI; skipping quitting application ID '$bundle_id'."
return
fi

echo "Quitting application '$bundle_id'..."

# try to quit the application within the timeout period
local quit_success=false
SECONDS=0
while (( SECONDS < timeout_duration )); do
if osascript -e "tell application id \"$bundle_id\" to quit" >/dev/null 2>&1; then
if ! pgrep -f "$bundle_id" >/dev/null 2>&1; then
echo "Application '$bundle_id' quit successfully."
quit_success=true
break
fi
fi
sleep 1
done

if [[ "$quit_success" = false ]]; then
echo "Application '$bundle_id' did not quit."
fi
}


# extract contents
MOUNT_POINT=$(mktemp -d /tmp/dmg_mount_XXXXXX)
hdiutil attach -plist -nobrowse -readonly -mountpoint "$MOUNT_POINT" "$INSTALLER_PATH"
sudo cp -R "$MOUNT_POINT"/* "$TMPDIR"
hdiutil detach "$MOUNT_POINT"
# copy to the applications folder
quit_application 'com.google.Chrome'
sudo [ -d "$APPDIR/Google Chrome.app" ] && sudo mv "$APPDIR/Google Chrome.app" "$TMPDIR/Google Chrome.app.bkp"
sudo cp -R "$TMPDIR/Google Chrome.app" "$APPDIR"
Loading

0 comments on commit 65b4bfd

Please sign in to comment.