Skip to content

Instantly share code, notes, and snippets.

@gschueler
Created January 18, 2011 20:14
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save gschueler/785055 to your computer and use it in GitHub Desktop.
Script to query rundeck queue using curl/xmlstarlet
#!/bin/bash
#usage: runjob.sh <server URL> <job ID>
errorMsg() {
echo "$*" 1>&2
}
DIR=$(cd `dirname $0` && pwd)
# accept url argument on commandline, if '-' use default
url=$1
if [ "-" == "$url" ] ; then
url='http://localhost:4440'
fi
loginurl="${url}/j_security_check"
# curl opts to use a cookie jar, and follow redirects, showing only errors
CURLOPTS="-s -S -L -c $DIR/cookies -b $DIR/cookies"
CURL="curl $CURLOPTS"
# get main page for login
RDUSER=default
RDPASS=default
echo "Login..."
$CURL -d j_username=$RDUSER -d j_password=$RDPASS $loginurl > $DIR/curl.out
if [ 0 != $? ] ; then
errorMsg "failed login request to ${loginurl}"
exit 2
fi
grep 'j_security_check' -q $DIR/curl.out
if [ 0 == $? ] ; then
errorMsg "login was not successful"
exit 2
fi
echo "Login OK"
XMLSTARLET=xml
# now submit req
jobid=$3
params="id=${jobid}"
runurl="${url}/menu/queueList.xml"
echo "Query queue..."
# get nodes page
$CURL ${runurl}?${params} > $DIR/curl.out
if [ 0 != $? ] ; then
errorMsg "failed nodes request"
exit 2
fi
#test curl.out for valid xml
$XMLSTARLET val -w $DIR/curl.out > /dev/null 2>&1
if [ 0 != $? ] ; then
errorMsg "ERROR: Response was not valid xml"
exit 2
fi
#test for expected /result element
$XMLSTARLET el $DIR/curl.out | grep -e '^result/items' -q
if [ 0 != $? ] ; then
errorMsg "ERROR: Response did not contain expected result"
exit 2
fi
#If <result error="true"> then an error occured.
waserror=$($XMLSTARLET sel -T -t -v "/result/@error" $DIR/curl.out)
if [ "true" == "$waserror" ] ; then
errorMsg "Server reported an error: "
$XMLSTARLET sel -T -t -v "/result/error/message" -n $DIR/curl.out
exit 2
fi
#If <response success='true'> then request succeeded.
itemcount=$($XMLSTARLET sel -T -t -v "/result/items/@count" $DIR/curl.out)
echo "$itemcount Execution(s) Running"
if [ "0" != "$itemcount" ] ; then
#echo all on one line
$XMLSTARLET sel -T -t -m "/result/items/item" -o " " -o " [" -v "id" -o "] " -v "name" -o " &lt;" -v "url" -o '&gt;' -n $DIR/curl.out
fi
#rm $DIR/curl.out
rm $DIR/cookies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment