### IMPORTANT: modify the StarCluster config file (~/.starcluster/config) to set various options for your cluster ###

scuser=$USER

starcluster start mycluster

numNodes=`starcluster listclusters mycluster | grep "Total nodes" | cut -d’ ’ -f3`
nodes="master `eval echo $(seq -f node%03g 1 $(($numNodes-1)))`"

# create install.sh
echo 'apt-get update' > install.sh
echo 'apt-get install -y r-base octave3.2' >> install.sh
echo 'echo "DONE with Ubuntu Linux package installation on $(hostname -s)." ' >> install.sh
# done creating install.sh

starcluster put -u $scuser mycluster install.sh .
# first we'll install on the master
starcluster sshmaster mycluster "source /home/$scuser/install.sh >& /home/$scuser/install.log.master"
# answer 'yes', then hit Ctrl-Z to put the installation in the
# background, then do the following to install on the nodes
for node in $nodes; do
    cmd="source /home/$scuser/install.sh >& /home/$scuser/install.log.$node"
    if [ "$node" != "master" ]; then
        starcluster sshmaster mycluster "ssh $node $cmd" &
    fi
done

watch starcluster sshmaster mycluster "grep 'DONE' /home/$scuser/install.log.*"

### IMPORTANT: wait to proceed until installation done on all nodes

echo "install.packages(c('Rmpi', 'foreach', 'doMC', 'doParallel', 'doMPI'), repos = 'http://cran.cnr.berkeley.edu'); print(paste('DONE with R package installation on ', system('hostname -s', intern = TRUE), '.')" > installRpkgs.R
starcluster put -u $scuser mycluster installRpkgs.R .
for node in $nodes; do
    cmd="R CMD BATCH --no-save /home/$scuser/Rpkgs.R /home/$scuser/install.Rpkgs.log.$node"
    starcluster sshmaster mycluster "ssh $node $cmd" &
done

watch starcluster sshmaster mycluster "grep ’DONE’ /home/$scuser/install.Rpkgs.log.*"

### IMPORTANT: wait to proceed until R package installation done on all nodes




