Skip to content

Instantly share code, notes, and snippets.

@gschueler
Created January 18, 2011 20:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gschueler/785084 to your computer and use it in GitHub Desktop.
Save gschueler/785084 to your computer and use it in GitHub Desktop.
Query rundeck jobs list for a project, optionally save the xml file
#!/bin/bash
#usage: rdjobslist.sh <server URL> <project> [output.xml]
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
runurl="${url}/menu/workflows.xml"
project=$2
echo "Query Jobs for project $project..."
params="projFilter=$project"
# get nodes page
$CURL ${runurl}?${params} > $DIR/curl.out
if [ 0 != $? ] ; then
errorMsg "ERROR: failed query 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 /joblist element
$XMLSTARLET el $DIR/curl.out | grep -e '^joblist' -q
if [ 0 != $? ] ; then
errorMsg "ERROR: Response did not contain expected result"
exit 2
fi
# job list query doesn't wrap result in common result wrapper
#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
#Check count of jobs in the list
itemcount=$($XMLSTARLET sel -T -t -m "/joblist" -v "count(job)" $DIR/curl.out)
echo "$itemcount Jobs in result"
if [ "0" != "$itemcount" ] ; then
#display summary of jobs
$XMLSTARLET sel -T -t -m "/joblist/job" -o " " -o " [" -v "id" -o "] " -v "group" -o "/" -v "name" -n $DIR/curl.out
#if $3 is set, move file to location
outfile=$3
if [ ! -z "$outfile" ] ; then
mv $DIR/curl.out $outfile
echo "Saved xml to file $outfile"
fi
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