Created
January 21, 2011 20:21
-
-
Save gschueler/790341 to your computer and use it in GitHub Desktop.
Dispatch execution to the RunDeck server using curl
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 | |
#usage: rd-dispatch.sh <server URL> <project> [command [args ...]] | |
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 | |
shift | |
project=$1 | |
shift | |
args="$@" | |
if [ -z "$args" ] ; then | |
errorMsg "no args specified" | |
exit 2 | |
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 | |
#escape the string for xml | |
xmlargs=$($XMLSTARLET esc "$args") | |
xmlproj=$($XMLSTARLET esc "$project") | |
#produce job.xml content corresponding to the dispatch request | |
cat > $DIR/temp.out <<END | |
<joblist> | |
<job> | |
<name>cli job</name> | |
<description></description> | |
<loglevel>INFO</loglevel> | |
<context> | |
<project>$xmlproj</project> | |
</context> | |
<dispatch> | |
<threadcount>1</threadcount> | |
<keepgoing>true</keepgoing> | |
</dispatch> | |
<!-- | |
<nodefilters> | |
</nodefilters> | |
--> | |
<sequence> | |
<command> | |
<exec>$xmlargs</exec> | |
</command> | |
</sequence> | |
</job> | |
</joblist> | |
END | |
# now submit req | |
runurl="${url}/scheduledExecution/uploadAndExecute.xml" | |
# xmlreq=true is required to receive xml response | |
params="xmlreq=true" | |
echo "Submit job dispatch for $project..." | |
# specify the file for upload with curl, named "xmlBatch" | |
ulopts="-F xmlBatch=@$DIR/temp.out" | |
# make curl request | |
$CURL $ulopts ${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 /result element | |
$XMLSTARLET el $DIR/curl.out | grep -e '^result' -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 | |
#result will contain list of failed and succeeded jobs, in this | |
#case there should only be 1 failed or 1 succeeded since we submit only 1 | |
failedcount=$($XMLSTARLET sel -T -t -v "/result/failed/@count" $DIR/curl.out) | |
succount=$($XMLSTARLET sel -T -t -v "/result/succeeded/@count" $DIR/curl.out) | |
echoExecutionDetail () { | |
#echo all on one line | |
$XMLSTARLET sel -T -t -m $2 -o "[" -v "id" -o "] " -v "name" -o ' <' -v "url" -o '>' -n $1 | |
} | |
if [ "0" != "$failedcount" ] ; then | |
echo "The dispatch was not executed due to errors:" | |
echoExecutionDetail $DIR/curl.out "/result/failed/execution" | |
else | |
echo "The dispatch was submitted succesfully" | |
echoExecutionDetail $DIR/curl.out "/result/succeeded/execution" | |
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