Skip to content

Instantly share code, notes, and snippets.

@gschueler
Created December 5, 2012 21:37
Show Gist options
  • Save gschueler/4219733 to your computer and use it in GitHub Desktop.
Save gschueler/4219733 to your computer and use it in GitHub Desktop.
Example CGI script for receiving Rundeck Webhook calls
#!/bin/bash -e
#set to correct path to xmlstarlet
XMLSTARLET=/usr/local/bin/xml
# xmlstarlet select xpath
# usage: xmlsel XPATH file
xmlsel(){
xpath=$1
shift
$XMLSTARLET sel -T -t -v "$xpath" $*
}
# echo a human-readable crude duration from milliseconds
duration_display(){
dur=$1
displaytime="${dur}ms"
dur=$(( $dur / 1000 ))
if [ "0" -ne "$dur" ] ; then
displaytime="${dur}s"
fi
dur=$(( $dur / 60 ))
if [ "0" -ne "$dur" ] ; then
displaytime="${dur}m"
fi
dur=$(( $dur / 60 ))
if [ "0" -ne "$dur" ] ; then
displaytime="${dur}h"
fi
dur=$(( $dur / 24 ))
if [ "0" -ne "$dur" ] ; then
displaytime="${dur}d"
fi
echo $displaytime
}
CTYPE=$(echo $CONTENT_TYPE | cut -d \; -f 1)
if [ "POST" = "$REQUEST_METHOD" -a ! -z "$CONTENT_LENGTH" -a "${CTYPE}" = "text/xml" ] ; then
TEMPXML=/tmp/post.xml.$$
cat > $TEMPXML
$XMLSTARLET val -q -w 2>/dev/null $TEMPXML
if [ 0 = $? ] ; then
#valid xml
#select execution info
jstatus=$( xmlsel "/notification/@status" $TEMPXML )
eid=$( xmlsel "//execution/@id" $TEMPXML )
ehref=$( xmlsel "//execution/@href" $TEMPXML )
euser=$( xmlsel "//execution/user" $TEMPXML )
jname=$( xmlsel "//execution/job/name" $TEMPXML )
jgroup=$( xmlsel "//execution/job/group" $TEMPXML )
[ -n "$jgroup" ] && \
jgroup="$jgroup/"
jdstart=$( xmlsel "//execution/date-started/@unixtime" $TEMPXML )
jdend=$( xmlsel "//execution/date-ended/@unixtime" $TEMPXML )
jdur=$(( $jdend - $jdstart ))
durdisplay=$( duration_display $jdur )
#
#
# do something with the execution status data here
#
#
echo "Job $jstatus: $jgroup$jname by $euser ($durdisplay) $ehref" >> /tmp/status.txt
#
# examples:
#
# irccat
#
#
fi
rm $TEMPXML
echo "Content-type: text/plain"
echo ""
echo "OK"
else
echo "Content-type: text/plain"
echo ""
echo "GET"
fi
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment