Skip to content

Commit 66f8e5d

Browse files
committed
Automated Push
1 parent 93dd45b commit 66f8e5d

File tree

1 file changed

+172
-0
lines changed

1 file changed

+172
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
#!/bin/zsh
2+
###################################################################################################
3+
# Created by Noah Anderson | [email protected] | Kandji, Inc. | Systems Engineering
4+
###################################################################################################
5+
# Created on 06/07/2024
6+
###################################################################################################
7+
# Software Information
8+
###################################################################################################
9+
#
10+
# Version 1.0.0
11+
#
12+
# Script to migrate existing Insomnia 2020-2023 installs to Insomnia 8/9+ via Kandji
13+
# An upgrade cannot occur without intervention due to a violation in semantic versioning practices
14+
# Runs on macOS and detects/modifies an existing Insomnia bundle, allowing an upgrade in-place
15+
# Once the .app is altered, script calls Kandji binary to trigger install of Kong Insomnia Auto App
16+
# NOTE: Ensure Auto App 'Kong Insomnia' is scoped to Kandji devices before running
17+
#
18+
###################################################################################################
19+
# License Information
20+
###################################################################################################
21+
# Copyright 2024 Kandji, Inc.
22+
#
23+
# Permission is hereby granted, free of charge, to any person obtaining a copy of this
24+
# software and associated documentation files (the "Software"), to deal in the Software
25+
# without restriction, including without limitation the rights to use, copy, modify, merge,
26+
# publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
27+
# to whom the Software is furnished to do so, subject to the following conditions:
28+
#
29+
# The above copyright notice and this permission notice shall be included in all copies or
30+
# substantial portions of the Software.
31+
#
32+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
33+
# INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
34+
# PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
35+
# FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
36+
# OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
37+
# DEALINGS IN THE SOFTWARE.
38+
###################################################################################################
39+
40+
###################################################################################################
41+
########################################## DO NOT MODIFY ##########################################
42+
###################################################################################################
43+
44+
##############################
45+
########## VARIABLES #########
46+
##############################
47+
48+
kandji_bin="/usr/local/bin/kandji"
49+
insomnia_path="/Applications/Insomnia.app"
50+
insomnia_info_plist="${insomnia_path}/Contents/Info.plist"
51+
insomnia_old_floor="2020.0.0"
52+
insomnia_new_floor="7.0.0"
53+
insomnia_new_aa_name="Kong Insomnia"
54+
55+
##############################
56+
########## FUNCTIONS #########
57+
##############################
58+
59+
##############################################
60+
# Overwrites CFBundleShortVersionString value
61+
# in Info.plist to new major version floor to
62+
# allow in-place upgrade of Kong Insomnia
63+
# Fixes permissions on Info.plist, re-signs
64+
# ad hoc, and strips xattrs that could prevent
65+
# app launch post-upgrade
66+
# Globals:
67+
# insomnia_info_plist
68+
# insomnia_new_floor
69+
# insomnia_path
70+
# Outputs:
71+
# Writes new version to Info.plist
72+
##############################################
73+
function downgrade_vers_fix_perms() {
74+
# Overwrite version to be less than new major for app
75+
/usr/libexec/PlistBuddy -c "Set :CFBundleShortVersionString ${insomnia_new_floor}" "${insomnia_info_plist}"
76+
# Fix perms
77+
/bin/chmod 644 "${insomnia_info_plist}"
78+
# Re-sign ad hoc
79+
/usr/bin/codesign --force --deep --sign - "${insomnia_path}"
80+
# Remove xattrs (if present) that could prevent app launch
81+
# -r flag got removed around 14.3, so if it fails, run find to clear recursively
82+
/usr/bin/xattr -rc "${insomnia_path}" >/dev/null 2>&1 || /usr/bin/find "${insomnia_path}" -exec /usr/bin/xattr -c "{}" \;
83+
}
84+
85+
##############################################
86+
# Invokes Kandji binary to trigger install of
87+
# Kong Insomnia; gets stdin from and redirects
88+
# to /dev/null in bg to allow parallel
89+
# execution without hanging Kandji binary
90+
# Outputs:
91+
# Triggers Kong Insomnia install to disk
92+
##############################################
93+
function kandji_self_call() {
94+
95+
echo "Triggering Auto App install of Kong Insomnia..."
96+
97+
# Redirecting stderr/out to /dev/null and bg'ing the Kandji proc
98+
# This allows the agent to end its run without waiting for our script exec
99+
# We also provide stdin from /dev/null as well, allowing us to detach from any active TTY connections
100+
# Serves to inform our program any input will not be coming from a terminal session
101+
"${kandji_bin}" library --item "${insomnia_new_aa_name}" -F < /dev/null > /dev/null 2>&1 &
102+
}
103+
104+
##############################################
105+
# Main function to handle script execution
106+
# Validates appropriate run permissions (sudo)
107+
# Checks for existing Insomnia app bundle; if
108+
# missing, proceeds to Kong Insomnia install
109+
# If found, ensures Kong Insomnia is scoped
110+
# Checks existing version; if higher than old
111+
# major version floor, updates to new floor
112+
# Calls Kandji binary to install new Kong
113+
# Insomnia in-place over existing Auto App
114+
# Globals:
115+
# EUID
116+
# insomnia_info_plist
117+
# insomnia_new_aa_name
118+
# insomnia_new_floor
119+
# insomnia_old_floor
120+
# insomnia_path
121+
# kandji_bin
122+
# Returns:
123+
# Exit 0 on successful completion
124+
# Exit 1 if non-root exec or AA not in scope
125+
##############################################
126+
function main() {
127+
# Check invocation perms
128+
if [[ "${EUID}" -ne 0 ]]; then
129+
echo "Script must be run with sudo or as root"
130+
exit 1
131+
fi
132+
133+
# Only proceed with version mod if Insomnia exists at expected path
134+
if [[ -d "${insomnia_path}" ]]; then
135+
echo "Insomnia installed... checking version"
136+
137+
insomnia_vers=$(/usr/libexec/PlistBuddy -c "Print :CFBundleShortVersionString" "${insomnia_info_plist}")
138+
139+
# Compare minimum enforced version to installed version via zsh builtin is-at-least
140+
autoload is-at-least
141+
is-at-least "${insomnia_old_floor}" "${insomnia_vers}" && upgrade_needed=true || upgrade_needed=false
142+
143+
if ${upgrade_needed}; then
144+
echo "Insomnia version ${insomnia_vers} is greater than ${insomnia_old_floor}... Proceeding with downgrade"
145+
kandji_lib_items=$(${kandji_bin} library --list)
146+
if [[ -z $(grep -o "${insomnia_new_aa_name}" <<< "${kandji_lib_items}") ]]; then
147+
echo "${insomnia_new_aa_name} not scoped to Mac! Cannot complete cutover..."
148+
exit 1
149+
fi
150+
echo "Downgrading Insomnia CFBundleShortVersionString from ${insomnia_vers} to ${insomnia_new_floor}"
151+
downgrade_vers_fix_perms
152+
kandji_self_call
153+
else
154+
echo "Insomnia already on new major version ${insomnia_vers}... Nothing to do"
155+
fi
156+
else
157+
echo "Insomnia not installed..."
158+
kandji_lib_items=$(${kandji_bin} library --list)
159+
if [[ -z $(grep -o "${insomnia_new_aa_name}" <<< "${kandji_lib_items}") ]]; then
160+
echo "${insomnia_new_aa_name} not scoped to Mac! Cannot complete install..."
161+
exit 1
162+
fi
163+
kandji_self_call
164+
fi
165+
exit 0
166+
}
167+
168+
169+
###############
170+
##### MAIN ####
171+
###############
172+
main

0 commit comments

Comments
 (0)