Friday, October 17, 2008

Bash feat. Ant runner

I like Ant. I made some maintenance tasks for my projects and here Bash script that launches them via crontab.

It uses native Ant XML build files that you can specify by absolute or relative path.

There is useful option -r your@email.com. When that is set and build is failed, you will recieve on specified e-mail address short message that build was broken, include it's name.

albass@cisco:~$ ./ant-runner.sh -t db/analyze
Will run ./db/analyze.xml build.

Use -d key if you want to debug it.


#!/bin/bash

# Description: ANT runner
# Author: Alexey Bass (albass)
# Version: 1.1.0, 16/10/2008
SCRIPT_VERSION="1.1.0, 16/10/2008"

JAVA_HOME=/usr
# ant location. home directory, not file
ANT_HOME=/var/family/java-ant/ant
ANT_SCRIPT=$ANT_HOME/bin/ant
PATH=$PATH:$ANT_HOME/bin

if [ ! -f $ANT_SCRIPT ]; then
echo "[ERROR] ANT not found at $ANT_SCRIPT"
echo "Please, check the location and correct ANT_HOME if need."
exit 1
fi

# Will be used for reporting to email without it.
# Example: albass => albass + DEFAULT_EMAIL_SUFFIX
# Example: admin@site.com => admin@site.com
DEFAULT_EMAIL_SUFFIX=@gmail.com

# Holds email for reports.
REPORT_EMAIL=
# need to send notice email if build failed
IS_SEND_EMAIL_ON_FAIL=0

# holds task name to run
TASK_TO_RUN=

# normally, we don't need debug. that's default.
IS_DEBUG=0

print_usage()
{
cat << EOF
Usage: ant.sh [options] [-r email] -t task

OPTIONS:
-h Show this message
-t Task to run. Absolute or relative (to PWD) path without ".xml"
-v Print version information and exit
-a Runs Ant diagnostics
-r Send fail notice to provided e-mail address if need
-d Enable debug mode, shows extra info while running

EOF
}

print_versions()
{
echo $0
echo ANT runner $SCRIPT_VERSION
echo

which java
java -version
echo

echo $ANT_SCRIPT
$ANT_SCRIPT -version

echo
}

run_ant_diagnostics()
{
echo $ANT_SCRIPT
$ANT_SCRIPT -diagnostics

echo
}

correct_report_email()
{
# check for full e-mail was specified
local TEST=`echo "$REPORT_EMAIL" | grep "@"`

if [ -z $TEST ]; then
REPORT_EMAIL=$REPORT_EMAIL$DEFAULT_EMAIL_SUFFIX
fi

if [ $IS_DEBUG -eq 1 ]; then
echo "[DEBUG] Fail notice will be sent to $REPORT_EMAIL"
fi
}

# no options specified
if [ $# -eq 0 ]; then
echo "Hey, try -h for help"
exit 1
fi

# get throught passed options
while getopts "hvr:t:da" OPTION
do
case $OPTION in
h)
print_usage
exit 0
;;
a)
run_ant_diagnostics
exit 0
;;
v)
print_versions
exit 0
;;
r)
IS_SEND_EMAIL_ON_FAIL=1
REPORT_EMAIL=$OPTARG
;;
t)
TASK_TO_RUN="$OPTARG.xml"
;;
d)
echo "[DEBUG] is on"
IS_DEBUG=1
ANT_SCRIPT="$ANT_SCRIPT -debug"
;;
?)
print_usage
exit 0
;;
esac
done

if [ $IS_SEND_EMAIL_ON_FAIL -eq 1 ]; then
correct_report_email
fi

# user has task?
if [ ${#TASK_TO_RUN} -eq 0 ]; then
echo "[ERROR] Please, specify task to run"
exit 1
fi

# if not absolute path, set relative
if [ ! -f $TASK_TO_RUN ]; then
TASK_TO_RUN="./$TASK_TO_RUN"
fi

if [ $IS_DEBUG -eq 1 ]; then
echo "[DEBUG] Task path: $TASK_TO_RUN"
fi

# does build exists?
if [ ! -f $TASK_TO_RUN ]; then
echo "[ERROR] Can not find task: $TASK_TO_RUN"
echo
exit 1
fi

if [ $IS_DEBUG -eq 1 ]; then
echo "[DEBUG] Starting ANT"
fi
$ANT_SCRIPT -buildfile $TASK_TO_RUN
if [ $? -ne 0 ]; then
if [ $IS_SEND_EMAIL_ON_FAIL -eq 1 ]; then

if [ $IS_DEBUG -eq 1 ]; then
echo "[DEBUG] Sending e-mail..."
fi

mailx -s "[ANT-RUNNER] FAILED $TASK_TO_RUN" $REPORT_EMAIL <<< "O_o ACHTUNG!!!"

if [ $IS_DEBUG -eq 1 ]; then
echo "[DEBUG] E-mail sent"
fi

echo
exit 1;
else
if [ $IS_DEBUG -eq 1 ]; then
echo "[DEBUG] Congratulations, Failed! E-mail will not sent, use -r for that"
fi
fi
fi
echo

exit 0


In case you wish to improve it and not so familiar with Bash, here is the manual.