[ssh_tools] multicopy: improve message handling

This commit is contained in:
Matteo Cypriani 2019-10-21 13:51:39 +02:00
parent f3a51827f2
commit 5e6359cf8d
1 changed files with 42 additions and 25 deletions

View File

@ -16,7 +16,7 @@ set -u
# Exit codes
readonly EXIT_SUCCESS=0
readonly EXIT_USAGE=1
readonly EXIT_USAGE=127
readonly EXIT_DEPENDENCY=4
readonly EXIT_HOSTS_LIST=6
readonly EXIT_HOSTS_DEAD=7
@ -24,15 +24,36 @@ readonly EXIT_CONNECTION=8
print_usage()
{
echo "Usage:"
echo " $0 [-P] [-r|-R] [-l login] <hosts_list> <file1> [file2 [...]]"
echo
echo "Options:"
echo " -P: transfer files in parallel instead of one by one if possible"
echo " (ignored when -r or -R is used)."
echo " -r: use prsync instead of pscp."
echo " -R: use prsync with option --delete (delete remote files that are"
echo " not in the local copy)."
cat <<EOF
Usage:
$0 [-P] [-r|-R] [-l login] <hosts_list> <file1> [file2 [...]]
Options:
-h Print this help message.
-P Transfer files in parallel instead of one by one whenever possible
(ignored when -r or -R is used).
-r Use prsync instead of pscp.
-R Use prsync's --delete option (delete remote files that are not in
the local copy).
EOF
}
bad_usage()
{
err "$@"
echo >&2
print_usage >&2
exit $EXIT_USAGE
}
err()
{
printf 'Error! %s\n' "$@" >&2
}
warn()
{
printf 'Warning! %s\n' "$@" >&2
}
# Parse the optional arguments
@ -70,17 +91,13 @@ done
# example with "multicopy -l root -P my_file", it will try to use
# "-P" as hosts' list and "my_file" as the file to be transfered.
if [ $# -lt 2 ] ; then
print_usage
exit $EXIT_USAGE
bad_usage "Wrong number of arguments."
fi
# Check the usage of -r and -R
if [ "$DELETE" = "1" ] ; then
if [ "$RSYNC" = "1" ] ; then
echo "Use either -r or -R, but not both!"
echo
print_usage
exit $EXIT_USAGE
bad_usage "Use either -r or -R, but not both."
fi
RSYNC=1
DELETE="-X --delete"
@ -88,7 +105,7 @@ fi
# Ignore -P if rsync is going to be used
if [ "$RSYNC" = "1" ] && [ "$PARALLEL" = "1" ] ; then
echo "Cannot transfer in parallel when using rsync: ignoring -P."
warn "Cannot transfer in parallel when using rsync: ignoring -P."
PARALLEL=""
fi
@ -97,16 +114,16 @@ PSCP=$(command -v parallel-scp || command -v pscp.pssh || command -v pscp)
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."
err "Parallel SSH (pssh) is required for this script to work."
exit $EXIT_DEPENDENCY
fi
if [ -z "$PRSYNC" ] ; then
echo "Parallel rsync (prsync) is required for this script to work."
err "Parallel rsync (prsync) is required for this script to work."
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."
err "multiping (which should have been provided along with this" \
"script) is required for this script to work."
exit $EXIT_DEPENDENCY
fi
@ -119,7 +136,7 @@ HOSTS="$XDG_CONFIG_HOME/ssh_tools/${HOSTS_LIST_NAME}.lst"
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."
err "The hosts' list file doesn't exist or is not a regular file."
exit $EXIT_HOSTS_LIST
fi
@ -135,13 +152,13 @@ fi
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."
err "None of the remote hosts is alive."
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."
err "Cannot connect to the first alive host. Aborting."
exit $EXIT_CONNECTION
fi
echo "Destination directory: $DEST_DIR"
@ -156,7 +173,7 @@ fi
for FILE in "$@" ; do
echo "Deploying '$FILE'..."
if [ ! -e "$FILE" ] ; then
echo "This file doesn't exist, skipping."
warn "This file doesn't exist, skipping."
continue
fi
if [ "$RSYNC" = "1" ] ; then