#
# This is a script example, which shows how to install and run an application on a set 
# of nodes
#
# The scenario of this experiment involves one set of nodes: 'worker'. We will install
# the application "do_some_work.sh" in the directory "/usr/bin/" of all the nodes within 
# that set. We will then execute this application on all the nodes.
#
# The application to install must be contained into a TAR archive on the local 
# machine/console where this experiment script is being executed.
#
# It is the duty of this application to decide/implement what to do with its output (e.g.
# send then to STDOUT, or into a text file, or to the OML Collection framework as explained
# in other tutorials). 
#
# In this example we:
# 1) Define a new Application, which will be a wrapper around the "do_some_work.sh" application
# 2) Define a set of nodes which contains 1 node: [1,1] that will run the application
# 3) Run the application 
#

# 1) 
# Define a new application
# The following declaration defines a new application which as a URI 'myAppWrapper'
# and the name 'someWorkToDo'
#
# To use this Application definition in multiple experiment scripts, place it
# into a separate stand-alone file with the name 'myAppWrapper.rb' in the same 
# directory as the experiment scripts that would use it. These experiment scripts
# will then NOT require this step 1) declaration.
#
defApplication('myAppWrapper', 'someWorkToDo') {|app|
  app.shortDescription = "This is a simple wrapper application around my application"
  
  # We tell the experiment script where is the TAR archive which contains the application 
  # that we would like to install.
  # The path that we give here is a local path to the TAR archive on the local machine/console
  # on which we are running this experiment script.
  # This TAR archive will be extracted at the ROOT "/" of the filesystem of each node. Therefore,
  # you must layout the directory structure in your TAR archive in the exact way you would like 
  # its content to be placed within each node's filesystem.
  # For example, here our TAR archive contains the following directory layout (using: 'tar -tf'):
  #    usr/
  #    usr/bin/
  #    usr/bin/do_some_work.sh
  #
  app.binaryRepository = "/home/myUsername/myArchive.tar"

  # Here we tell the node where it can find the installed application
  app.path = "/usr/bin/do_some_work.sh"
  
}

# 2)
# Define a set of node that would use the above application
#
defGroup('worker', [1,1]) {|node|

   # Add the 'myAppWrapper' application to the node(s) in this set
   node.addApplication('myAppWrapper', 'someWorkToDo', nil, nil)

   # Note: the signature of addApplication() is
   #
   #   addApplication(app, vName, bindings, env)
   #
   # 'app' - The application URI or instance
   # 'vName' - Virtual name given to this app
   # 'paramBindings' - Parameter bindings for this application
   # 'env' - Environment to set before starting application
}

# 3)
# When all the nodes are UP, execute the application on them
#
whenAllUp() {|node|

   # Wait 10 sec to make sure that the applications are all 
   # installed
   wait 5
   
   # Start all the applications
   allGroups.startApplications

   # Wait 10 sec that will be the application duration time
   wait 10 
   
   # Stop the experiment
   Experiment.done
}

