#!/bin/bash ################## # Constants {{{1 # ################## PROG_NAME=$(basename $0) VERSION=1.0 YES=yes ######################### # Global variables {{{1 # ######################### DEBUG=0 CONTAINER_IMAGE= CMDS_FILE=test_cmds.txt DOCKERFILE= ################### # Print help {{{1 # ################### function print_help { echo "Usage: $PROG_NAME [options] container_image" echo echo " -g, --debug Debug mode." echo " -h, --help Print this help message." } ############ # Msg {{{1 # ############ function msg { local tag=$1 shift local code_level=1 is_numeric=$(echo $1 | grep '^[0-9]*$') if [ -n "$is_numeric" ] ; then code_level=$1 shift fi local msg=$1 # Check tag if [ -z "$tag" -o \( "$tag" != INFO -a "$tag" != DEBUG -a "$tag" != ERROR \) ] ; then echo "ERROR: Unvalid message tag \"$tag\"." >&2 exit 999 fi # Print message [ "$tag" = DEBUG -a "$DEBUG" -lt "$code_level" ] || echo "$tag: $msg" >&2 # Exit [ $tag = ERROR ] && exit $code_level } ################# # Requires {{{1 # ################# function requires { local prg=$1 [ -n "$(which $prg)" ] || msg ERROR "Cannot find $prg. Please install required package." } ################## # Read args {{{1 # ################## function read_args { local args="$*" # save arguments for debugging purpose # Read options while true ; do shift_count=1 case $1 in -g|--debug) DEBUG=$((DEBUG + 1)) ;; -h|--help) print_help ; exit 0 ;; -|--|--*) msg ERROR "Illegal option $1." ;; -?) msg ERROR "Unknown option $1." ;; -[^-]*) split_opt=$(echo $1 | sed 's/^-//' | sed 's/\([a-zA-Z]\)/ -\1/g') ; set -- $1$split_opt "${@:2}" ;; *) break esac shift $shift_count done shift $((OPTIND - 1)) # Read remaining arguments [ $# -eq 1 ] || msg ERROR "You must set one, and only one, container image." CONTAINER_IMAGE=$1 # Set dockerfile if [[ -f Dockerfile ]] ; then DOCKERFILE=Dockerfile fi # Debug msg DEBUG 1 "Arguments are : $args" } # Test container {{{1 ################################################################ function test_container { local entrypoint=$1 local args=$2 local entrypoint_arg= [ -z "$entrypoint" ] || entrypoint_arg="--entrypoint=$1" if docker run $entrypoint_arg $CONTAINER_IMAGE $args ; then msg INFO "Container $CONTAINER_IMAGE tested successfully." else msg INFO "Failure when testing container $CONTAINER_IMAGE." exit 1 fi return 0 } # Test R package loading {{{1 ################################################################ function test_r_pkg_loading { [[ -n $DOCKERFILE ]] || return 0 # Try to find package names in Dockerfile r_packages=$(egrep 'install.packages|biocLite' $DOCKERFILE | sed -E 's/^.*(install.packages|biocLite)\((c\(.*\)|[^c][^,]*)[,)].*$/\2/' | sed -E 's/^c\((.*)\)$/\1/' | tr ',' "\n" | sed "s/['\" ]//g") # Loop on all R packages for pkg in $r_packages ; do msg INFO "Try loading R package $pkg" if docker run --entrypoint=R $CONTAINER_IMAGE -e "library('$pkg')" ; then msg INFO "R package $pkg loaded successfully." else msg INFO "Failure while loading R package $pkg." exit 2 fi done } # MAIN {{{1 ################################################################ # Requirements requires docker # Read arguments read_args "$@" # Test loading of R packages if any test_r_pkg_loading # Test commands listed in file if [[ -f $CMDS_FILE && -s $CMDS_FILE ]] ; then while read line ; do has_entrypoint=$(echo $line | grep '^[^-]') if [ -n "$has_entrypoint" ] ; then entrypoint=$(echo $line | sed 's/^\([^ ]*\).*$/\1/') args=$(echo $line | sed 's/^[^ ]*\(.*\)$/\1/') else entrypoint= args=$line fi test_container "$entrypoint" "$args" done <"$CMDS_FILE" # Test help option else test_container "" -h fi