#!/bin/bash # crash: Report a problem and exit crash() { MSG=$1 echo ${MSG} >&2 exit 1 } # get_value: Return the right hand side of an equals # statment in swift.properties format: #site sitename option=value # will return bar get_value() { for last; do true; done eval echo $last |cut -d "=" -f2 } # Verify an argument is not null verify_not_null() { argname=$1; shift if [ _$1 != _ ]; then return 0; else echo $0: value for $argname can not be null exit 1 fi } # Print usage information and exit usage() { cat << END usage: gensites template [-p properties.file] [-L template_directory] [-h] [-T] [-l] template Name of template to use -p properties.file Specify a swift.properties to use -L template_directory Specify a non-standard template directory -T List all templates available -h Help / usage information -l List the contents of a specific template Examples: Create a site configuration file for sites.xml using default properties.file in current directory $ gensites pads > sites.xml Use a specific properties file for a site $ gensites -p sites.properties pads > sites.xml Specify a non-standard directory where templates are located $ gensites -L template.dir pads > sites.xml END exit 0; } # Parse command line arguments while [ $# -gt 0 ] do case "$1" in -p) PROPERTIES_FILE=$2; verify_not_null properties_file $PROPERTIES_FILE; shift ;; -L) TEMPLATE_DIRECTORY=$2; verify_not_null template_directory $TEMPLATE_DIRECTORY; shift ;; -l) LIST_SPECIFIC_TEMPLATE=1 ;; -T) LIST_TEMPLATES=1 ;; -h|-help|-?) usage ;; *) TEMPLATE=$1 ;; esac shift done # Determine SWIFT_HOME if [ -z "$SWIFT_HOME" ]; then SWIFT_BIN_DIR=`which swift` if [ ! -d "$SWIFT_HOME" ]; then SWIFT_HOME=`dirname $SWIFT_BIN_DIR`"/../etc/sites" fi # Can't find swift path, give up if [ ! -d "$SWIFT_HOME" ]; then crash "Unable to determine SWIFT_HOME. Please set manually." fi fi # List all templates if [ ! -z $LIST_TEMPLATES ]; then # Templates in etc/sites for file in `ls -1 $SWIFT_HOME/* 2>/dev/null` do basename $file done # Templates in .swift/sites for file in `ls -1 $HOME/.swift/sites/* 2>/dev/null` do basename $file done exit 0 fi # Require template name to be specified by user if [ -z "$TEMPLATE" ]; then crash "Not specified: TEMPLATE" fi # Use custom template directory if asked if [ -d "$TEMPLATE_DIRECTORY" ]; then TEMPLATE_PATH="$TEMPLATE_DIRECTORY/$TEMPLATE" fi # Locate template if [ ! -f "$TEMPLATE_PATH" ]; then if [ -f "$TEMPLATE" ]; then $TEMPLATE_PATH = $TEMPLATE elif [ -f "$SWIFT_HOME/$TEMPLATE" ]; then TEMPLATE_PATH=$SWIFT_HOME/$TEMPLATE elif [ -f "$HOME/.swift/sites/$TEMPLATE" ]; then TEMPLATE_PATH=$HOME/.swift/sites/$TEMPLATE fi if [ ! -f "$TEMPLATE_PATH" ]; then crash "Cannot find template for $TEMPLATE" fi fi # List a specific template if [ ! -z $LIST_SPECIFIC_TEMPLATE ]; then cat $TEMPLATE_PATH exit 0 fi # Ensure a properties file exists if [ ! -f "$PROPERTIES_FILE" ]; then PROPERTIES_FILE="swift.properties" if [ ! -f "$PROPERTIES_FILE" ]; then PROPERTIES_FILE="$HOME/.swift/swift.properties" if [ ! -f "$PROPERTIES_FILE" ]; then crash "Unable to find a valid properties file! Please specify a valid file with the -p option" fi fi fi # Parse values into variables for later sed processing WORK=`pwd`"/work" while read line do case "$line" in "#site $TEMPLATE work="*|'#site work='*) WORK=`get_value $line` ;; "#site $TEMPLATE globus_hostname="*|'#site globus_hostname=') GLOBUS_HOSTNAME=`get_value $line` ;; "#site $TEMPLATE nodes="*|'#site nodes='*) NODES=`get_value $line` ;; "#site $TEMPLATE project="*|'#site project='*) PROJECT=`get_value $line` ;; "#site $TEMPLATE queue="*|'#site queue='*) QUEUE=`get_value $line` ;; "#site $TEMPLATE n_gran="*|'#site n_gran='*) N_GRAN=`get_value $line` ;; "#site $TEMPLATE n_max="*|'#site n_max='*) MAXTIME=`get_value $line` ;; "#site $TEMPLATE slots="*|'#site slots='*) SLOTS=`get_value $line` ;; "#site $TEMPLATE execution_url="*|'#site slots='*) EXECUTION_URL=`get_value $line` ;; esac done < $PROPERTIES_FILE # Test for mandatory variables [[ ${WORK} == "" ]] && crash "Not specified: WORK" # Verify that the variables by the template are defined for TOKEN in PROJECT QUEUE N_GRAN N_MAX SLOTS do # Need but can't find, due if grep _${TOKEN}_ $TEMPLATE_PATH > /dev/null; then if [ -z "${!TOKEN}" ]; then crash "Not specified: ${TOKEN}" fi fi done # Replace values SEDFILE=`mktemp` { echo "s/_NODES_/${NODES}/" echo "s/_HOST_/${GLOBUS_HOSTNAME}/" echo "s@_WORK_@${WORK}@" echo "s/_PROJECT_/${PROJECT}/" echo "s/_QUEUE_/${QUEUE}/" echo "s/_N_GRAN_/${N_GRAN}/" echo "s/_N_MAX_/${N_MAX}/" echo "s/_SLOTS_/${SLOTS}/" echo "s/_MAXTIME_/${MAXTIME}/" echo "s@_EXECUTION_URL_@${EXECUTION_URL}@" echo "s@_SERVICE_COASTERS_@${SERVICE_COASTERS:-NO_URL_GIVEN}@" echo "s@_SERVICE_PORT_@${SERVICE_PORT:-NO_PORT_GIVEN}@" } > $SEDFILE sed -f $SEDFILE < $TEMPLATE_PATH rm $SEDFILE