[ssh_tools] multicopy: variables for exit codes

Also enable set -u.
This commit is contained in:
Matteo Cypriani 2019-10-21 13:30:04 +02:00
parent 1a0c6183c4
commit f3a51827f2
1 changed files with 27 additions and 12 deletions

View File

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