Quantcast
Channel: rtrouton – Der Flounder
Viewing all articles
Browse latest Browse all 764

autopkg-conductor updated to support both JamfUploaderSlacker and Slacker AutoPkg processors

$
0
0

As part of my preparations for Jamf’s planned authentication changes to the Classic API, I’ve been working more with the JamfUploader AutoPkg processors for Jamf Upload. These processors have emerged as a successor to JSSImporter, the original tool available to upload installer packages and other components to Jamf Pro using AutoPkg.

As part of my work with Jamf Upload, I’ve also updated my autopkg-conductor script to allow the use of either Jamf Upload’s JamfUploaderSlacker AutoPkg processor or JSSImporter’s Slacker AutoPkg processors. For more details, please see below the jump.

JamfUploaderSlacker and Slacker both allow uploading information to a Slack channel using a webhook. The message which appears in Slack should look similar to what is shown below:

JamfUploaderSlacker

Screen Shot 2022 02 11 at 4 54 22 PM

Slacker

Screen Shot 2022 02 11 at 5 12 12 PM

To accommodate using either the JamfUploaderSlacker or Slacker processor, I’ve added a new slack_autopkg_processor variable to autopkg-conductor. If this variable is set correctly with the values specified in the script, it’ll enable autopkg-conductor to use the correct processor when reporting to a Slack channel.

Screen Shot 2022 02 11 at 5 25 41 PM

If the slack_autopkg_processor variable is set, but isn’t one of the two expected values, the use of the processors is bypassed. Instead, all logged output would be sent to Slack if a Slack webhook is set up.

Screen Shot 2018 07 05 at 9 14 08 PM

Another change to autopkg-conductor is to error reporting. In the previous version of autopkg-conductor, the error log was sent to Slack even if no errors were detected.

Screen Shot 2022 02 11 at 5 28 57 PM

This behavior has been changed. If no errors have been detected, the error log will no longer be sent to Slack. Instead, the information that the error log was empty will be logged to the logs stored on the Mac which is running autopkg-conductor.

The autopkg-conductor script is available below. It’s also available from GitHub using the following link:

https://github.com/rtrouton/autopkg-conductor


This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters


#!/bin/bash
# AutoPkg automation script
# Adjust the following variables for your particular configuration.
#
# autopkg_user_account – This should be the user account you're running AutoPkg in.
# autopkg_user_account_home – This should be the home folder location of the AutoPkg user account
#
# Note: The home folder location is currently set to be automatically discovered
# using the autopkg_user_account variable.
#
# recipe_list – This is the location of the plain text file being used to store
# your list of AutoPkg recipes. For more information about this list, please see
# the link below:
#
# https://github.com/autopkg/autopkg/wiki/Running-Multiple-Recipes
#
# log_location – This should be the location and name of the AutoPkg run logs.
#
# Note: The location is currently set to be automatically discovered
# using the autopkg_user_account_home variable.
autopkg_user_account="username_goes_here"
autopkg_user_account_home=$(/usr/bin/dscl . -read /Users/"$autopkg_user_account" NFSHomeDirectory | awk '{print $2}')
recipe_list="/path/to/recipe_list.txt"
log_location="$autopkg_user_account_home/Library/Logs/autopkg-run-for-$(date +%Y-%m-%d-%H%M%S).log"
# If you're using JSSImporter or Jamf Upload, the URL of your Jamf Pro server should be populated
# into the jamfpro_server variable automatically.
#
# If you're not using JSSImporter or Jamf Upload, this variable will return nothing and that's OK.
jamfpro_server=$(/usr/bin/defaults read "$autopkg_user_account_home"/Library/Preferences/com.github.autopkg JSS_URL)
# Optional variables
# This script supports using either Jamf Upload's JamfUploaderSlacker or JSSImporter's Slacker processors
# JamfUploaderSlacker – used with Jamf Upload
#
# To use the JamfUploaderSlacker post-processor, you'll need to use add Graham Pugh's
# Autopkg repo by running the command below:
#
# autopkg repo-add grahampugh-recipes
#
# The slack_post_processor variable should look like this:
# slack_post_processor="com.github.grahampugh.jamf-upload.processors/JamfUploaderSlacker"
#
# Slacker – used with JSSImporter
#
# To use the Slacker post-processor, you'll need to use either Graham Pugh's or my
# fork of Graham's. For information on Graham's, please see the following post:
#
# http://grahampugh.github.io/2017/12/22/slack-for-autopkg-jssimporter.html
#
# To use mine, please add my AutoPkg repo by running the following command:
#
# autopkg repo-add rtrouton-recipes
#
# If using Graham's, the slack_post_processor variable should look like this:
# slack_post_processor="com.github.grahampugh.recipes.postprocessors/Slacker"
#
# If using mine, the slack_post_processor variable should look like this:
# slack_post_processor="com.github.rtrouton.recipes.postprocessors/Slacker"
slack_post_processor=""
# The key used by the JamfUploaderSlacker and Slacker AutoPkg processors is slightly different
# so the right one needs to be used when running AutoPkg.
#
# JamfUploaderSlacker: slack_webhook_url
#
# Slacker: webhook_url
#
# Setting the slack_autopkg_processor variable will enable the script to use the correct key for the processor.
#
# If using JamfUploaderSlacker, the slack_autopkg_processor processor should be set as shown below:
#
# slack_autopkg_processor="JamfUploaderSlacker"
#
# If using Slacker, the slack_autopkg_processor processor should be set as shown below:
#
# slack_autopkg_processor="Slacker"
slack_autopkg_processor=""
# If you're sending the results of your AutoPkg run to Slack, you'll need to set up
# a Slack webhook to receive the information being sent by the script.
# If you need help with configuring a Slack webhook, please see the links below:
#
# https://api.slack.com/incoming-webhooks
# https://get.slack.help/hc/en-us/articles/115005265063-Incoming-WebHooks-for-Slack
#
# Once a Slack webhook is available, the slack_webhook variable should look similar
# to this:
# slack_webhook="https://hooks.slack.com/services/XXXXXXXXX/YYYYYYYYY/ZZZZZZZZZZ"
slack_webhook=""
# don't change anything below this line
# Set script exit status
exit_error=0
# Define logger behavior
ScriptLogging(){
DATE=$(date +%Y-%m-%d\ %H:%M:%S)
LOG="$log_location"
echo "$DATE" " $1" >> $LOG
}
# Function for sending multi-line output to a Slack webhook. Original script from here:
#
# http://blog.getpostman.com/2015/12/23/stream-any-log-file-to-slack-using-curl/
SendToSlack(){
cat "$1" | while read LINE; do
(echo "$LINE" | grep -e "$3") && curl -X POST –silent –data-urlencode "payload={\"text\": \"$(echo $LINE | sed "s/\"/'/g")\"}" "$2";
done
}
# If the AutoPkg run's log file is not available, create it
if [[ ! -r "$log_location" ]]; then
touch "$log_location"
fi
# If the AutoPkg recipe list is missing or unreadable, stop the script with an error.
if [[ ! -r "$recipe_list" ]]; then
ScriptLogging "Error Detected. Unable to start AutoPkg run."
echo "" > /tmp/autopkg_error.out
if [[ "$jamfpro_server" = "" ]]; then
echo "AutoPkg run failed" >> /tmp/autopkg_error.out
else
echo "AutoPkg run for $jamfpro_server failed" >> /tmp/autopkg_error.out
fi
echo "$recipe_list is missing or unreadable. Fix immediately." >> /tmp/autopkg_error.out
echo "" > /tmp/autopkg.out
# If a Slack webhook is configured, send the error log to Slack.
if [[ ! -z "$slack_webhook" ]]; then
SendToSlack /tmp/autopkg_error.out ${slack_webhook}
fi
cat /tmp/autopkg_error.out >> "$log_location"
ScriptLogging "Finished AutoPkg run"
exit_error=1
fi
# If the the AutoPkg recipe list is readable and AutoPkg is installed,
# run the recipes stored in the recipe list.
if [[ -x /usr/local/bin/autopkg ]] && [[ -r "$recipe_list" ]]; then
ScriptLogging "AutoPkg installed at $(which autopkg)"
ScriptLogging "Recipe list located at $recipe_list and is readable."
echo "" > /tmp/autopkg.out
if [[ "$jamfpro_server" = "" ]]; then
echo "Starting AutoPkg run" >> /tmp/autopkg_error.out
else
echo "Starting AutoPkg run for $jamfpro_server" >> /tmp/autopkg.out
fi
echo "" >> /tmp/autopkg.out
echo "" > /tmp/autopkg_error.out
echo "Error log for AutoPkg run" >> /tmp/autopkg_error.out
echo "" >> /tmp/autopkg_error.out
/usr/local/bin/autopkg repo-update all 2>&1 >> /tmp/autopkg.out 2>>/tmp/autopkg_error.out
cat /tmp/autopkg.out >> "$log_location" && cat /tmp/autopkg_error.out >> "$log_location"
if [[ ! -z "$slack_webhook" ]]; then
if [[ ! -z "$slack_post_processor" ]] && [[ ! -z "$slack_autopkg_processor" ]]; then
if [[ ${slack_autopkg_processor} = "Slacker" ]] || [[ ${slack_autopkg_processor} = "JamfUploaderSlacker" ]]; then
# If both a post-processor to post to Slack and a Slack webhook are configured, the JSSImporter
# and Jamf Upload recipes should have their outputs posted to Slack using the post-processor, while
# all other standard output should go to /tmp/autopkg.out. All standard error output
# should go to /tmp/autopkg_error.out
if [[ ${slack_autopkg_processor} = "Slacker" ]]; then
slack_autopkg_processor_key="webhook_url"
elif [[ ${slack_autopkg_processor} = "JamfUploaderSlacker" ]]; then
slack_autopkg_processor_key="slack_webhook_url"
fi
/usr/local/bin/autopkg run –recipe-list=${recipe_list} –post=${slack_post_processor} –key ${slack_autopkg_processor_key}=${slack_webhook} >> /tmp/autopkg.out 2>>/tmp/autopkg_error.out
else
# If for some reason the slack_autopkg_processor variable is configured with an unknown value,
# neither processor is called and all standard output should go to /tmp/autopkg.out.
# All standard error output should go to /tmp/autopkg_error.out.
/usr/local/bin/autopkg run –recipe-list=${recipe_list} >> /tmp/autopkg.out 2>>/tmp/autopkg_error.out
fi
else
# If only using a Slack webhook, all standard output should go to /tmp/autopkg.out.
# All standard error output should go to /tmp/autopkg_error.out.
/usr/local/bin/autopkg run –recipe-list=${recipe_list} >> /tmp/autopkg.out 2>>/tmp/autopkg_error.out
fi
else
# If a Slack webhook is not configured, all standard output should go to /tmp/autopkg.out.
# All standard error output should go to /tmp/autopkg_error.out.
/usr/local/bin/autopkg run –recipe-list="$recipe_list" >> /tmp/autopkg.out 2>>/tmp/autopkg_error.out
fi
if [[ "$jamfpro_server" = "" ]]; then
echo "Finished with AutoPkg run" >> /tmp/autopkg.out
else
echo "Finished with AutoPkg run for $jamfpro_server" >> /tmp/autopkg.out
fi
echo "" >> /tmp/autopkg.out && echo "" >> /tmp/autopkg_error.out
cat /tmp/autopkg.out >> "$log_location"
cat /tmp/autopkg_error.out >> "$log_location"
ScriptLogging "Finished AutoPkg run"
echo "" >> /tmp/autopkg_error.out
echo "End of error log for AutoPkg run" >> /tmp/autopkg_error.out
echo "" >> /tmp/autopkg_error.out
if [[ -z "$slack_post_processor" ]] && [[ ! -z "$slack_webhook" ]]; then
# If the AutoPkg post-processor for posting to Slack is not
# configured but we do have a Slack webhook set up, all
# standard output should be sent to Slack.
ScriptLogging "Sending AutoPkg output log to Slack"
SendToSlack /tmp/autopkg.out ${slack_webhook}
ScriptLogging "Sent AutoPkg output log to $slack_webhook."
fi
if [[ ! -z "$slack_post_processor" ]] && [[ ! -z "$slack_webhook" ]] && [[ ${slack_autopkg_processor} != "Slacker" ]] && [[ ${slack_autopkg_processor} != "JamfUploaderSlacker" ]]; then
# If the AutoPkg post-processor for posting to Slack is
# misconfigured but we do have a Slack webhook set up,
# all standard output should be sent to Slack.
ScriptLogging "Sending AutoPkg output log to Slack"
SendToSlack /tmp/autopkg.out ${slack_webhook}
ScriptLogging "Sent AutoPkg output log to $slack_webhook."
fi
if [[ ! -z "$slack_webhook" ]]; then
# If using a Slack webhook, at the end of the AutoPkg run all standard
# error output logged to /tmp/autopkg_error.out should be output to Slack,
# using the SendToSlack function.
if [[ $(wc -l </tmp/autopkg_error.out) -gt 7 ]]; then
ScriptLogging "Sending AutoPkg error log to Slack"
SendToSlack /tmp/autopkg_error.out ${slack_webhook}
ScriptLogging "Sent autopkg log to $slack_webhook. Ending run."
else
ScriptLogging "Error log was empty. Nothing to send to Slack."
fi
fi
fi
exit "$exit_error"

Viewing all articles
Browse latest Browse all 764

Trending Articles