Browse Source

[cluster] minor improvements & shellcheck

master
Matteo Cypriani 1 year ago
parent
commit
b00b2eb16b
4 changed files with 29 additions and 22 deletions
  1. 14
    10
      cluster/cluster-deploy.sh
  2. 2
    1
      cluster/cluster-ping.sh
  3. 5
    4
      cluster/cluster-run.sh
  4. 8
    7
      cluster/cluster-ssh.sh

+ 14
- 10
cluster/cluster-deploy.sh View File

@@ -73,7 +73,7 @@ if [ "$DELETE" = "1" ] ; then
73 73
 fi
74 74
 
75 75
 # Ignore -P if rsync is going to be used
76
-if [ "$RSYNC" = "1" -a "$PARALLEL" = "1" ] ; then
76
+if [ "$RSYNC" = "1" ] && [ "$PARALLEL" = "1" ] ; then
77 77
   echo "Cannot transfer in parallel when using rsync: ignoring -P."
78 78
   PARALLEL=""
79 79
 fi
@@ -82,13 +82,13 @@ fi
82 82
 PSCP=$(command -v parallel-scp || command -v pscp.pssh || command -v pscp)
83 83
 PRSYNC=$(command -v parallel-rsync || command -v prsync)
84 84
 CLUSTERPING=$(command -v cluster-ping)
85
-if [ "$RSYNC" = "" -a "$PSCP" = "" ] ; then
85
+if [ -z "$RSYNC" ] && [ -z "$PSCP" ] ; then
86 86
   echo "Parallel SSH (pssh) is required for this script to work."
87 87
   exit 4
88
-elif [ "$PRSYNC" = "" ] ; then
88
+elif [ -z "$PRSYNC" ] ; then
89 89
   echo "Parallel rsync (prsync) is required for this script to work."
90 90
   exit 5
91
-elif [ "$CLUSTERPING" = "" ] ; then
91
+elif [ -z "$CLUSTERPING" ] ; then
92 92
   echo "cluster-ping (which should have been provided along with this"
93 93
   echo "script) is required for this script to work."
94 94
   exit 6
@@ -96,7 +96,7 @@ fi
96 96
 
97 97
 # Hosts' list file
98 98
 HOSTS_LIST_NAME="$1"
99
-if [ "$XDG_CONFIG_HOME" = "" ] ; then
99
+if [ -z "$XDG_CONFIG_HOME" ] ; then
100 100
   XDG_CONFIG_HOME="$HOME/.config"
101 101
 fi
102 102
 HOSTS="$XDG_CONFIG_HOME/cluster/${HOSTS_LIST_NAME}.lst"
@@ -108,7 +108,7 @@ if [ ! -f "$HOSTS" ] ; then
108 108
 fi
109 109
 
110 110
 # Login
111
-if [ "$LOGIN" != "" ] ; then
111
+if [ -n "$LOGIN" ] ; then
112 112
   echo "Login: $LOGIN"
113 113
   SSH_LOGIN="${LOGIN}@"
114 114
   LOGIN="-l $LOGIN"
@@ -116,14 +116,15 @@ fi
116 116
 
117 117
 # Test the connection to the first host and get the destination
118 118
 # directory (home directory of the remote user)
119
-FIRST_HOST=$($CLUSTERPING "$HOSTS_LIST_NAME" 2>/dev/null | sed -n "s/ is alive$//p" | head -n1)
120
-if [ "$FIRST_HOST" = "" ] ; then
119
+FIRST_HOST=$($CLUSTERPING "$HOSTS_LIST_NAME" 2>/dev/null \
120
+           | sed -n "s/ is alive$//p" | head -n1)
121
+if [ -z "$FIRST_HOST" ] ; then
121 122
   echo "None of the remote hosts is alive."
122 123
   exit 7
123 124
 fi
124 125
 echo "Testing connection to $FIRST_HOST..."
125
-DEST_DIR="$(ssh ${SSH_LOGIN}${FIRST_HOST} 'echo $HOME' 2>/dev/null)"
126
-if [ "$DEST_DIR" = "" ] ; then
126
+DEST_DIR="$(ssh "${SSH_LOGIN}${FIRST_HOST}" 'echo $HOME' 2>/dev/null)"
127
+if [ -z "$DEST_DIR" ] ; then
127 128
   echo "Cannot connect to the first alive host. Aborting."
128 129
   exit 7
129 130
 fi
@@ -131,6 +132,7 @@ echo "Destination directory: $DEST_DIR"
131 132
 
132 133
 # Transfer the files in parallel...
133 134
 if [ "$PARALLEL" = "1" ] ; then
135
+  # shellcheck disable=SC2086
134 136
   exec $PSCP -r $LOGIN -h "$HOSTS" -- "$@" "$DEST_DIR"
135 137
 fi
136 138
 
@@ -142,8 +144,10 @@ for FILE in "$@" ; do
142 144
     continue
143 145
   fi
144 146
   if [ "$RSYNC" = "1" ] ; then
147
+    # shellcheck disable=SC2086
145 148
     $PRSYNC -a $DELETE $LOGIN -h "$HOSTS" -- "$FILE" "$DEST_DIR"
146 149
   else
150
+    # shellcheck disable=SC2086
147 151
     $PSCP -r $LOGIN -h "$HOSTS" -- "$FILE" "$DEST_DIR"
148 152
   fi
149 153
 done

+ 2
- 1
cluster/cluster-ping.sh View File

@@ -17,7 +17,7 @@ if [ $# -ne 1 ] ; then
17 17
 fi
18 18
 
19 19
 # Hosts' list file
20
-if [ "$XDG_CONFIG_HOME" = "" ] ; then
20
+if [ -z "$XDG_CONFIG_HOME" ] ; then
21 21
   XDG_CONFIG_HOME="$HOME/.config"
22 22
 fi
23 23
 HOSTS="$XDG_CONFIG_HOME/cluster/$1.lst"
@@ -28,4 +28,5 @@ if [ ! -f "$HOSTS" ] ; then
28 28
 fi
29 29
 
30 30
 # Go!
31
+# shellcheck disable=SC2046
31 32
 exec fping $(cat "$HOSTS") 2>/dev/null

+ 5
- 4
cluster/cluster-run.sh View File

@@ -19,7 +19,7 @@ print_usage()
19 19
 
20 20
 # Do we have at least a host list and a command?
21 21
 if [ $# -lt 2 ] ; then
22
-  print_usage
22
+  print_usage >&2
23 23
   exit 1
24 24
 fi
25 25
 
@@ -38,13 +38,13 @@ fi
38 38
 
39 39
 # Check dependencies
40 40
 PSSH=$(command -v parallel-ssh || command -v pssh)
41
-if [ "$PSSH" = "" ] ; then
41
+if [ -z "$PSSH" ] ; then
42 42
   echo "Parallel SSH (pssh) is required for this script to work."
43 43
   exit 4
44 44
 fi
45 45
 
46 46
 # Hosts' list file
47
-if [ "$XDG_CONFIG_HOME" = "" ] ; then
47
+if [ -z "$XDG_CONFIG_HOME" ] ; then
48 48
   XDG_CONFIG_HOME="$HOME/.config"
49 49
 fi
50 50
 HOSTS="$XDG_CONFIG_HOME/cluster/$1.lst"
@@ -56,11 +56,12 @@ if [ ! -f "$HOSTS" ] ; then
56 56
 fi
57 57
 
58 58
 # Login
59
-if [ "$LOGIN" != "" ] ; then
59
+if [ -n "$LOGIN" ] ; then
60 60
   echo "Login: $LOGIN"
61 61
   LOGIN="-l $LOGIN"
62 62
 fi
63 63
 
64 64
 echo "Command:" "$@"
65 65
 
66
+# shellcheck disable=SC2086
66 67
 exec $PSSH --print $LOGIN -h "$HOSTS" -- "$@"

+ 8
- 7
cluster/cluster-ssh.sh View File

@@ -35,25 +35,26 @@ elif [ $# -ne 1 ] ; then
35 35
 fi
36 36
 
37 37
 # Hosts' list file
38
-if [ "$XDG_CONFIG_HOME" = "" ] ; then
38
+if [ -z "$XDG_CONFIG_HOME" ] ; then
39 39
   XDG_CONFIG_HOME="$HOME/.config"
40 40
 fi
41
-HOSTS="$XDG_CONFIG_HOME/cluster/$1.lst"
41
+HOSTS_FILE="$XDG_CONFIG_HOME/cluster/$1.lst"
42 42
 shift
43
-echo "Using file '$HOSTS' as hosts' list."
44
-if [ ! -f "$HOSTS" ] ; then
43
+echo "Using file '$HOSTS_FILE' as hosts' list."
44
+if [ ! -f "$HOSTS_FILE" ] ; then
45 45
   echo "The hosts' list file doesn't exist or is not a regular file."
46 46
   exit 2
47 47
 fi
48 48
 
49 49
 # Login
50
-if [ "$LOGIN" != "" ] ; then
50
+if [ -n "$LOGIN" ] ; then
51 51
   echo "Login: $LOGIN"
52 52
   LOGIN="${LOGIN}@"
53 53
 fi
54 54
 
55 55
 # Create the screen tabs
56
-for HOST in $(cat "$HOSTS") ; do
56
+while read -r HOST ; do
57 57
   SSH="ssh ${LOGIN}${HOST}"
58
+  # shellcheck disable=SC2086
58 59
   screen -t "$SSH" $SSH
59
-done
60
+done <"$HOSTS_FILE"

Loading…
Cancel
Save