Shell functions for MarkLogic packaging
Use shell functions to simplify complex command line invocations.
MarkLogic 7 ships with a new REST API to work with packages. Packages allow you to move database and application server settings between clusters.
For example, if you've been developing a new version of an application on your development machine, you can make a package that contains all of the databases and application servers related to that application. Download that package as a ZIP file, move it to your staging cluster, and install it. Now your staging cluster has all the correct settings, no tedious poking about in the Admin UI required.
The easiest way to create and install packages is with the Configuration Manager UI.
If you're a software developer, you can easily imagine that while I was developing and testing the packaging functionality, I had to create and install a lot of packages. Way more than I wanted to do in the browser UI.
Enter the REST API. The REST API lets you script interactions with the packaging system. You could write your own front end, for example. Or you could just use curl:
curl -s -X POST --digest -u admin:admin --data-ascii @/dev/null \
-H "Accept: application/xml" -H "Content-Type: application/xml" \
http://localhost:8002/manage/v2/packages?pkgname=myNewPackage
After typing about four curl commands, I thought there must be a better way.
That lead me to re-learning how to extend bash by writing shell functions. This function:
function pkg_create {
# [-xml|-json] pkgname [filename]
DEBUG=0
if [ "$1" = "-d" ]; then
DEBUG=1
shift
fi
FMT=xml
if [ "$1" = "-json" -o "$1" = "-xml" ]; then
FMT=xml
if [ $1 = "-json" ]; then FMT=json; fi
shift
fi
if [ $# -lt 1 -o $# -gt 2 ]; then
echo "Usage: $FUNCNAME [-xml|-json] pkgname [filename]"
else
PKG=$1
CT="application/xml"
DFLAG="--data-ascii"
FN=${2-/dev/null}
if [[ $FN == *json* ]]; then
CT="application/json"
fi
if [[ $FN == *zip* ]]; then
DFLAG="--data-binary"
CT="application/zip"
fi
ACCEPT="application/$FMT"
if [ $DEBUG = 1 ]; then
echo curl $COPT -X POST $AUTH $DFLAG @$FN -H "Accept: $ACCEPT" -H "Content-Type: $CT" "$BASE/v2/packages?pkgname=$PKG&format=$FMT" 1>&2
fi
curl $COPT -X POST $AUTH $DFLAG @$FN -H "Accept: $ACCEPT" -H "Content-Type: $CT" "$BASE/v2/packages?pkgname=$PKG&format=$FMT"
fi
}
turns that complex curl command into this:
pkg_create myNewPackage
I win.
And now that MarkLogic 7 has been officially released, you can win too.
Share and enjoy.