From b00b2eb16bea6d5bb1a9889421de494bf30a142e Mon Sep 17 00:00:00 2001 From: Matteo Cypriani Date: Wed, 11 Apr 2018 22:56:01 +0200 Subject: [PATCH] [cluster] minor improvements & shellcheck --- cluster/cluster-deploy.sh | 24 ++++++++++++++---------- cluster/cluster-ping.sh | 3 ++- cluster/cluster-run.sh | 9 +++++---- cluster/cluster-ssh.sh | 15 ++++++++------- 4 files changed, 29 insertions(+), 22 deletions(-) diff --git a/cluster/cluster-deploy.sh b/cluster/cluster-deploy.sh index 961edc0..fb640ae 100755 --- a/cluster/cluster-deploy.sh +++ b/cluster/cluster-deploy.sh @@ -73,7 +73,7 @@ if [ "$DELETE" = "1" ] ; then fi # Ignore -P if rsync is going to be used -if [ "$RSYNC" = "1" -a "$PARALLEL" = "1" ] ; then +if [ "$RSYNC" = "1" ] && [ "$PARALLEL" = "1" ] ; then echo "Cannot transfer in parallel when using rsync: ignoring -P." PARALLEL="" fi @@ -82,13 +82,13 @@ fi PSCP=$(command -v parallel-scp || command -v pscp.pssh || command -v pscp) PRSYNC=$(command -v parallel-rsync || command -v prsync) CLUSTERPING=$(command -v cluster-ping) -if [ "$RSYNC" = "" -a "$PSCP" = "" ] ; then +if [ -z "$RSYNC" ] && [ -z "$PSCP" ] ; then echo "Parallel SSH (pssh) is required for this script to work." exit 4 -elif [ "$PRSYNC" = "" ] ; then +elif [ -z "$PRSYNC" ] ; then echo "Parallel rsync (prsync) is required for this script to work." exit 5 -elif [ "$CLUSTERPING" = "" ] ; then +elif [ -z "$CLUSTERPING" ] ; then echo "cluster-ping (which should have been provided along with this" echo "script) is required for this script to work." exit 6 @@ -96,7 +96,7 @@ fi # Hosts' list file HOSTS_LIST_NAME="$1" -if [ "$XDG_CONFIG_HOME" = "" ] ; then +if [ -z "$XDG_CONFIG_HOME" ] ; then XDG_CONFIG_HOME="$HOME/.config" fi HOSTS="$XDG_CONFIG_HOME/cluster/${HOSTS_LIST_NAME}.lst" @@ -108,7 +108,7 @@ if [ ! -f "$HOSTS" ] ; then fi # Login -if [ "$LOGIN" != "" ] ; then +if [ -n "$LOGIN" ] ; then echo "Login: $LOGIN" SSH_LOGIN="${LOGIN}@" LOGIN="-l $LOGIN" @@ -116,14 +116,15 @@ fi # Test the connection to the first host and get the destination # directory (home directory of the remote user) -FIRST_HOST=$($CLUSTERPING "$HOSTS_LIST_NAME" 2>/dev/null | sed -n "s/ is alive$//p" | head -n1) -if [ "$FIRST_HOST" = "" ] ; then +FIRST_HOST=$($CLUSTERPING "$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 fi echo "Testing connection to $FIRST_HOST..." -DEST_DIR="$(ssh ${SSH_LOGIN}${FIRST_HOST} 'echo $HOME' 2>/dev/null)" -if [ "$DEST_DIR" = "" ] ; then +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 fi @@ -131,6 +132,7 @@ echo "Destination directory: $DEST_DIR" # Transfer the files in parallel... if [ "$PARALLEL" = "1" ] ; then + # shellcheck disable=SC2086 exec $PSCP -r $LOGIN -h "$HOSTS" -- "$@" "$DEST_DIR" fi @@ -142,8 +144,10 @@ for FILE in "$@" ; do continue fi if [ "$RSYNC" = "1" ] ; then + # shellcheck disable=SC2086 $PRSYNC -a $DELETE $LOGIN -h "$HOSTS" -- "$FILE" "$DEST_DIR" else + # shellcheck disable=SC2086 $PSCP -r $LOGIN -h "$HOSTS" -- "$FILE" "$DEST_DIR" fi done diff --git a/cluster/cluster-ping.sh b/cluster/cluster-ping.sh index 894d0d2..c4df121 100755 --- a/cluster/cluster-ping.sh +++ b/cluster/cluster-ping.sh @@ -17,7 +17,7 @@ if [ $# -ne 1 ] ; then fi # Hosts' list file -if [ "$XDG_CONFIG_HOME" = "" ] ; then +if [ -z "$XDG_CONFIG_HOME" ] ; then XDG_CONFIG_HOME="$HOME/.config" fi HOSTS="$XDG_CONFIG_HOME/cluster/$1.lst" @@ -28,4 +28,5 @@ if [ ! -f "$HOSTS" ] ; then fi # Go! +# shellcheck disable=SC2046 exec fping $(cat "$HOSTS") 2>/dev/null diff --git a/cluster/cluster-run.sh b/cluster/cluster-run.sh index 5e12a41..023c71c 100755 --- a/cluster/cluster-run.sh +++ b/cluster/cluster-run.sh @@ -19,7 +19,7 @@ print_usage() # Do we have at least a host list and a command? if [ $# -lt 2 ] ; then - print_usage + print_usage >&2 exit 1 fi @@ -38,13 +38,13 @@ fi # Check dependencies PSSH=$(command -v parallel-ssh || command -v pssh) -if [ "$PSSH" = "" ] ; then +if [ -z "$PSSH" ] ; then echo "Parallel SSH (pssh) is required for this script to work." exit 4 fi # Hosts' list file -if [ "$XDG_CONFIG_HOME" = "" ] ; then +if [ -z "$XDG_CONFIG_HOME" ] ; then XDG_CONFIG_HOME="$HOME/.config" fi HOSTS="$XDG_CONFIG_HOME/cluster/$1.lst" @@ -56,11 +56,12 @@ if [ ! -f "$HOSTS" ] ; then fi # Login -if [ "$LOGIN" != "" ] ; then +if [ -n "$LOGIN" ] ; then echo "Login: $LOGIN" LOGIN="-l $LOGIN" fi echo "Command:" "$@" +# shellcheck disable=SC2086 exec $PSSH --print $LOGIN -h "$HOSTS" -- "$@" diff --git a/cluster/cluster-ssh.sh b/cluster/cluster-ssh.sh index 6cb6d16..d6b81bb 100755 --- a/cluster/cluster-ssh.sh +++ b/cluster/cluster-ssh.sh @@ -35,25 +35,26 @@ elif [ $# -ne 1 ] ; then fi # Hosts' list file -if [ "$XDG_CONFIG_HOME" = "" ] ; then +if [ -z "$XDG_CONFIG_HOME" ] ; then XDG_CONFIG_HOME="$HOME/.config" fi -HOSTS="$XDG_CONFIG_HOME/cluster/$1.lst" +HOSTS_FILE="$XDG_CONFIG_HOME/cluster/$1.lst" shift -echo "Using file '$HOSTS' as hosts' list." -if [ ! -f "$HOSTS" ] ; then +echo "Using file '$HOSTS_FILE' as hosts' list." +if [ ! -f "$HOSTS_FILE" ] ; then echo "The hosts' list file doesn't exist or is not a regular file." exit 2 fi # Login -if [ "$LOGIN" != "" ] ; then +if [ -n "$LOGIN" ] ; then echo "Login: $LOGIN" LOGIN="${LOGIN}@" fi # Create the screen tabs -for HOST in $(cat "$HOSTS") ; do +while read -r HOST ; do SSH="ssh ${LOGIN}${HOST}" + # shellcheck disable=SC2086 screen -t "$SSH" $SSH -done +done <"$HOSTS_FILE"