diff --git a/cluster/README b/cluster/README index 209a075..b08874d 100644 --- a/cluster/README +++ b/cluster/README @@ -1,7 +1,8 @@ The cluster utilities are script allowing to work in parallel with a number of remote hosts, using utilities such as Parallel SSH (pssh) or -fping. They all take as first and mandatory argument a host list name -“NAME”; the list will be searched according to the following pattern: +fping. They all take as first and mandatory positional argument a host +list name “NAME”; the list will be searched according to the following +pattern: ``` $HOME/.config/cluster/NAME.lst @@ -20,7 +21,13 @@ for example: priam ``` -The scripts provided are detailed in the sequel. +The scrits based on SSH use the local user's login name as remote login; +this can be changed using the ``-l`` option, that must appear before the +hosts' list on the command line, for example: + +``` cluster-run -l root openwrt_machines opkg install screen + +The provided scripts are detailed in the sequel. = cluster-ping.sh = diff --git a/cluster/cluster-deploy.sh b/cluster/cluster-deploy.sh index 7fd1bf6..689f8a3 100755 --- a/cluster/cluster-deploy.sh +++ b/cluster/cluster-deploy.sh @@ -10,22 +10,63 @@ # # Deploy files on a number of remote hosts using Parallel SCP. -if [ $# -lt 2 ] ; then - echo "Usage: $0 [file2 [...]]" +print_usage() +{ + echo "Usage: $0 [-l login] [file2 [...]]" +} + +# Parse the optional arguments with getopt +export POSIXLY_CORRECT=yes +export GETOPT_COMPATIBLE=yes +OPTSTRING="l:" +ARGS=$(getopt $OPTSTRING $*) +if [ $? -ne 0 ] ; then + print_usage exit 1 fi +set -- $ARGS +while [ $# -ne 0 ] ; do + case "$1" in + -l) + LOGIN="$2" + shift + shift + ;; + --) + shift + break + ;; + *) + echo "Unknown argument:" $1 + exit 2 + esac +done +# Do we have at least a host list and a command? +if [ $# -lt 2 ] ; then + print_usage + exit 3 +fi + +# Check dependencies PSCP=$(command -v parallel-scp || command -v pscp.pssh || command -v pscp) if [ "$PSCP" = "" ] ; then echo "Parallel SSH (pssh) is required for this script to work." - exit 2 + exit 4 fi +# Hosts' list file HOSTS="$HOME/.config/cluster/$1.lst" shift echo "Using file '$HOSTS' as hosts' list." +# Login +if [ "$LOGIN" != "" ] ; then + echo "Login: $LOGIN" + LOGIN="-l $LOGIN" +fi + for FILE in $@ ; do echo "Deploying '$FILE'..." - $PSCP -r -l root -h "$HOSTS" -- "$FILE" /root + $PSCP -r $LOGIN -h "$HOSTS" -- "$FILE" /root done diff --git a/cluster/cluster-run.sh b/cluster/cluster-run.sh index 3e967cf..602a403 100755 --- a/cluster/cluster-run.sh +++ b/cluster/cluster-run.sh @@ -10,20 +10,64 @@ # # Run a command on a list of remote hosts using Parallel SSH. -if [ $# -lt 2 ] ; then - echo "Usage: $0 " +#set -x + +print_usage() +{ + echo "Usage: $0 [-l login] " +} + +# Parse the optional arguments with getopt +export POSIXLY_CORRECT=yes +export GETOPT_COMPATIBLE=yes +OPTSTRING="l:" +ARGS=$(getopt $OPTSTRING $*) +if [ $? -ne 0 ] ; then + print_usage exit 1 fi +set -- $ARGS +while [ $# -ne 0 ] ; do + case "$1" in + -l) + LOGIN="$2" + shift + shift + ;; + --) + shift + break + ;; + *) + echo "Unknown argument:" $1 + exit 2 + esac +done +# Do we have at least a host list and a command? +if [ $# -lt 2 ] ; then + print_usage + exit 3 +fi + +# Check dependencies PSSH=$(command -v parallel-ssh || command -v pssh) if [ "$PSSH" = "" ] ; then echo "Parallel SSH (pssh) is required for this script to work." - exit 2 + exit 4 fi +# Hosts' list file HOSTS="$HOME/.config/cluster/$1.lst" shift echo "Using file '$HOSTS' as hosts' list." -echo "Command: $@" -exec $PSSH --print -l root -h "$HOSTS" -- $@ +# Login +if [ "$LOGIN" != "" ] ; then + echo "Login: $LOGIN" + LOGIN="-l $LOGIN" +fi + +echo "Command:" "$@" + +exec $PSSH --print $LOGIN -h "$HOSTS" -- "$@" diff --git a/cluster/cluster-ssh.sh b/cluster/cluster-ssh.sh index 5f54a14..7300168 100755 --- a/cluster/cluster-ssh.sh +++ b/cluster/cluster-ssh.sh @@ -11,15 +11,57 @@ # Open an SSH interactive shell on a list of remote hosts, opening one # GNU Screen tab per host. -if [ $# -ne 1 ] ; then - echo "Usage: $0 " +print_usage() +{ + echo "Usage: $0 [-l login] " +} + +# Parse the optional arguments with getopt +export POSIXLY_CORRECT=yes +export GETOPT_COMPATIBLE=yes +OPTSTRING="l:" +ARGS=$(getopt $OPTSTRING $*) +if [ $? -ne 0 ] ; then + print_usage exit 1 fi +set -- $ARGS +while [ $# -ne 0 ] ; do + case "$1" in + -l) + LOGIN="$2" + shift + shift + ;; + --) + shift + break + ;; + *) + echo "Unknown argument:" $1 + exit 2 + esac +done +# Do we have only one argument left? +if [ $# -ne 1 ] ; then + print_usage + exit 3 +fi + +# Hosts' list file HOSTS="$HOME/.config/cluster/$1.lst" shift echo "Using file '$HOSTS' as hosts' list." +# Login +if [ "$LOGIN" != "" ] ; then + echo "Login: $LOGIN" + LOGIN="${LOGIN}@" +fi + +# Create the screen tabs for HOST in $(cat "$HOSTS") ; do - screen -t $HOST ssh root@$HOST + SSH="ssh ${LOGIN}${HOST}" + screen -t "$SSH" $SSH done