[[ucc_scripting]]
Scripting
---------

UCC can execute Groovy scripts. Groovy (http://groovy.codehaus.org) is a dynamic
scripting language similar to Python or Ruby, but very closely integrated with Java.
The scripting facility can be used for automation tasks or implementation of 
custom commands, but it needs a bit of insight into how UNICORE 6 and UCC work.

Script context
~~~~~~~~~~~~~~

Your Groovy scripts can access some predefined variables that are summarized in the following table

.Variables accessible for scripts
[options="header"]
|=========================
|variable | description | Java type
| registry | A preconfigured client for accessing the registry | de.fzj.unicore.uas.client.IRegistryQuery
| securityProperties | Security configuration (keystore, etc) | eu.unicore.util.httpclient.IClientConfiguration
| registryURL | the URL of the registry | java.lang.String 
| messageWriter | for writing messages to the user | de.fzj.unicore.ucc.MessageWriter
| commandLine | the command line | org.apache.commons.cli.CommandLine
| properties | defaults from the user's properties file | java.util.Properties
|=========================

  
Examples  
~~~~~~~~
  
Some example Groovy scripts can be found in the samples/ directory of the UCC distribution.
  
Here is a script that will delete all your jobs (use at your own risk):

.Groovy example: delete all your jobs
-------------
/*
* remove all jobs 
*/

//import UNICORE/X client classes
import de.fzj.unicore.uas.client.*;

//iterate over TSSs and remove all jobs

def lister = new de.fzj.unicore.ucc.helpers.TargetSystemLister(registry,securityProperties,messageWriter)

lister.each { 
    it.jobs.each{
   	 messageWriter.message "Job at "+it.address.stringValue
         new JobClient(it.address.stringValue, it, securityProperties).destroy()
    }
}
-------------

.Groovy example: list available storages
-------------
/*
* list available storages 
*/
import de.fzj.unicore.uas.client.*
import javax.xml.namespace.QName

//porttype of storage service 
def SMSPORT=new QName("http://unigrids.org/2006/04/services/sms","StorageManagement")

//method to extract storage name from a storage client
def findName(epr){
  sms=new StorageClient(epr.address.stringValue, epr, securityProperties)
  return sms.resourcePropertiesDocument.storageProperties.fileSystem.name
}

//list storages from registry
registry.listAccessibleServices(SMSPORT).each { 
    name=findName(it)
    messageWriter.message "Storage <"+name+"> at "+it.address.stringValue			    
}

//list storages attached to target systems
def lister = new de.fzj.unicore.ucc.helpers.TargetSystemLister(registry,securityProperties,messageWriter)

lister.each { 
    it.storages.each{
        name=findName(it)
   	    messageWriter.message "Storage <"+name+"> at "+it.address.stringValue
    }
}

-------------

  
  
