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