#!/bin/bash

# diffuse command on each nodes
# Script is run under uid of oar who is sudo
# argv[1] is the file which contains the list of nodes used
# argv[2...] is the command to run on each node

#exit

#Exemple for mono processor cluster
#SENTINELLE=/usr/bin/sentinelle
SSH=ssh
NODEFILE=$1
shift
CMD=$@

if [ "a$SENTINELLE" != "a" ]
then
    echo "Launch $CMD on each nodes with sentinelle"
    for i in `sort -u $NODEFILE`
    do
        NODESENTINELLE="$NODESENTINELLE -m $i"
    done
    echo "$SENTINELLE -cconnect=$SSH,timeout=30000 $NODESENTINELLE -- $CMD"
    $SENTINELLE -cconnect=$SSH,timeout=30000 $NODESENTINELLE -- "$CMD"
else
    echo "Launch $CMD on each nodes with ssh"
    for i in `sort -u $NODEFILE`
    do
        ($SSH $i "$CMD" || echo "/!\\ SOMETHING GOES WRONG WITH $i, exit_code = $?") &
    done
    wait
fi

