################ USER OPTIONS ##################
numNodes=4 # how many nodes in your cluster
machineType=n1-standard-8 # what kind of virtual hardware for your nodes?
zone=us-central2-a  # what geographic zone

################################################

imageID=projects/google/global/images/gcel-12-04-v20121106

nodes=$(eval echo vm{0..$(($numNodes-1))})

echo $nodes # this should just confirm that you have a shell variable containing node names: vm0 vm1 vm2 ...

gcutil addinstance --image=$imageID --machine_type=$machineType --zone=$zone --wait_until_running $nodes

# create install.sh
echo 'sudo apt-get update' > install.sh
echo 'sudo apt-get install -y libopenblas-base openmpi-bin libopenmpi-dev r-base octave3.2 openjdk-7-jre openjdk-7-jdk' >> install.sh
echo 'echo "DONE with Ubuntu Linux package installation on $(hostname -s)." ' >> install.sh
# done creating install.sh

for node in $nodes; do 
    gcutil push $node install.sh .
    gcutil ssh $node "sudo /bin/bash ./install.sh >& install.log.$node" &
done

# for checking that installation is complete:
for node in $nodes; do
	gcutil ssh $node "grep DONE install.log.$node"
done

### IMPORTANT: wait to proceed until Ubuntu package 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

for node in $nodes; do 
	gcutil push $node installRpkgs.R . &
done

for node in $nodes; do 
	gcutil ssh $node "sudo R CMD BATCH --no-save installRpkgs.R install.Rpkgs.log.$node" &
done

for node in $nodes; do 
	gcutil ssh $node "sudo R CMD BATCH --no-save installRpkgs.R install.Rpkgs.log.$node" &
done

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