Skip to content

Instantly share code, notes, and snippets.

@gregalia
Created February 5, 2021 17:45
Show Gist options
  • Save gregalia/8a7815efcdd2205bf8d7732e2bb72c01 to your computer and use it in GitHub Desktop.
Save gregalia/8a7815efcdd2205bf8d7732e2bb72c01 to your computer and use it in GitHub Desktop.
Jamf Pro extension attribute for creating arbitrary groups of arbitrary size
#!/bin/bash
####################################################################
# \\\/// #
# / _ _ \ #
# (| (.)(.) |) #
# .-----------------------.OOOo--()--oOOO.-----------------------. #
# | | #
# | Test whether the machine has assigned itself a random number | #
# | for rolling out policies in staggered groups and assign one | #
# | if not | #
# | | #
# | Version 1.0 | #
# | | #
# '-----------------------.oooO----------------------------------' #
# ( ) Oooo. #
# \ ( ( ) #
# \_) ) / #
# (_/ #
# #
# https://boxes.thomasjensen.com/ #
####################################################################
export PATH=/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin
readonly RANGE_BOTTOM=1
readonly RANGE_TOP=8176
readonly DATA_FILE="/Library/Application Support/JamfAdmin/clientData.plist"
readonly PLB="/usr/libexec/PlistBuddy"
function get_staggered_rollout_number() {
local silent="false"
while getopts "s" flag; do
case "$flag" in
s)
silent="true"
;;
*) ;;
esac
done
result="$(
"$PLB" -c \
"print :StaggeredRolloutNumber" \
"$DATA_FILE" \
2>/dev/null
)"
# test whether result is a nonzero positive integer - in testing, some
# clients had blank values and PListBuddy writes them as zeroes when the type
# is 'integer'
if [[ "$result" -eq "$result" && "$result" -ge 1 ]] 2>/dev/null; then
# added -s flag to be able to use this function for control flow
if [[ "$silent" == "false" ]]; then
echo "$result"
fi
return 0
fi
return 1
}
function add_staggered_rollout_number() {
local entry="StaggeredRolloutNumber"
local type="integer"
local value
read -r value
"$PLB" -c \
"add :$entry $type $value" \
"$DATA_FILE" \
&>/dev/null
}
function set_staggered_rollout_number() {
local entry="StaggeredRolloutNumber"
local value
read -r value
"$PLB" -c \
"set :$entry $value" \
"$DATA_FILE" \
&>/dev/null
}
function generate_random_number_within_range() {
awk \
-v RANGE_TOP="$RANGE_TOP" \
-v RANGE_BOTTOM="$RANGE_BOTTOM" \
'BEGIN {
srand()
print int(b + rand() * (RANGE_TOP - RANGE_BOTTOM + 1))
}'
# not sure if this is what produced the zeros mentioned in the
# get_staggered_rollout_number comment but it looks to be working more
# consistently with this pause
sleep .25
}
function print_enclosed_in_result_tags() {
read -r result
echo "<result>$result</result>"
}
function main() {
mkdir -p "$(dirname "$DATA_FILE")"
while ! get_staggered_rollout_number -s; do
num=$(generate_random_number_within_range)
add_staggered_rollout_number <<<"$num" ||
set_staggered_rollout_number <<<"$num"
done
get_staggered_rollout_number |
print_enclosed_in_result_tags
}
main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment