diff --git a/ssh_tools/multicopy.sh b/ssh_tools/multicopy.sh index dca19cc..c7179f9 100755 --- a/ssh_tools/multicopy.sh +++ b/ssh_tools/multicopy.sh @@ -1,6 +1,6 @@ #!/bin/sh # -# multicopy.sh, Copyright © 2013 Matteo Cypriani +# multicopy.sh, Copyright © 2013, 2019 Matteo Cypriani # (Formerly named cluster-deploy.sh) # # This program is free software. It comes without any warranty, to @@ -12,6 +12,15 @@ # Deploy files on a number of remote hosts using Parallel SCP. #set -x +set -u + +# Exit codes +readonly EXIT_SUCCESS=0 +readonly EXIT_USAGE=1 +readonly EXIT_DEPENDENCY=4 +readonly EXIT_HOSTS_LIST=6 +readonly EXIT_HOSTS_DEAD=7 +readonly EXIT_CONNECTION=8 print_usage() { @@ -29,6 +38,10 @@ print_usage() # Parse the optional arguments while [ $# -gt 2 ] ; do case $1 in + "-h") + print_usage + exit $EXIT_SUCCESS + ;; "-l") LOGIN="$2" shift @@ -58,7 +71,7 @@ done # "-P" as hosts' list and "my_file" as the file to be transfered. if [ $# -lt 2 ] ; then print_usage - exit 1 + exit $EXIT_USAGE fi # Check the usage of -r and -R @@ -67,7 +80,7 @@ if [ "$DELETE" = "1" ] ; then echo "Use either -r or -R, but not both!" echo print_usage - exit 2 + exit $EXIT_USAGE fi RSYNC=1 DELETE="-X --delete" @@ -85,14 +98,16 @@ PRSYNC=$(command -v parallel-rsync || command -v prsync) MULTIPING=$(command -v multiping) if [ -z "$RSYNC" ] && [ -z "$PSCP" ] ; then echo "Parallel SSH (pssh) is required for this script to work." - exit 4 -elif [ -z "$PRSYNC" ] ; then + exit $EXIT_DEPENDENCY +fi +if [ -z "$PRSYNC" ] ; then echo "Parallel rsync (prsync) is required for this script to work." - exit 5 -elif [ -z "$MULTIPING" ] ; then + exit $EXIT_DEPENDENCY +fi +if [ -z "$MULTIPING" ] ; then echo "multiping (which should have been provided along with this" echo "script) is required for this script to work." - exit 6 + exit $EXIT_DEPENDENCY fi # Hosts' list file @@ -105,7 +120,7 @@ shift echo "Using file '$HOSTS' as hosts' list." if [ ! -f "$HOSTS" ] ; then echo "The hosts' list file doesn't exist or is not a regular file." - exit 3 + exit $EXIT_HOSTS_LIST fi # Login @@ -115,19 +130,19 @@ if [ -n "$LOGIN" ] ; then LOGIN="-l $LOGIN" fi -# Test the connection to the first host and get the destination +# Test the connection to the first alive host and get the destination # directory (home directory of the remote user) FIRST_HOST=$($MULTIPING "$HOSTS_LIST_NAME" 2>/dev/null \ | sed -n "s/ is alive$//p" | head -n1) if [ -z "$FIRST_HOST" ] ; then echo "None of the remote hosts is alive." - exit 7 + exit $EXIT_HOSTS_DEAD fi echo "Testing connection to $FIRST_HOST..." DEST_DIR="$(ssh "${SSH_LOGIN}${FIRST_HOST}" 'echo $HOME' 2>/dev/null)" if [ -z "$DEST_DIR" ] ; then echo "Cannot connect to the first alive host. Aborting." - exit 7 + exit $EXIT_CONNECTION fi echo "Destination directory: $DEST_DIR"