Using Scripts

It is good practice to run with dynamic scripts disabled, but you can still use scripts. To use scripts with dynamic scripts disabled, you need to upload a bundle containing the scripts. A bundle is a zip file that you upload and associate with your cluster’s configuration.

The bundle needs to contain a folder named scripts/, with the scripts inside. Scripts in sub-folders must be referred to with _ as the delimiter, and not /. For example, the script scripts/foo/bar.grovy must be referred to as foo_bar.

Here’s an example of three scripts and how to invoke them:

$ tree scripts # Show files and their placement
scripts
├── module1
│   └── script_name.groovy
├── module2
│   └── other_script.groovy
└── top_level.groovy

$ cat scripts/top_level.groovy
123 * my_var

$ cat scripts/module1/script_name.groovy
42 + my_var

$ cat scripts/module2/other_script.groovy
42 * my_var

$ zip my-scripts.zip -r scripts # make a zip-bundle of the scripts
  adding: scripts/ (stored 0%)
  adding: scripts/module1/ (stored 0%)
  adding: scripts/module1/script_name.groovy (stored 0%)
  adding: scripts/module2/ (stored 0%)
  adding: scripts/module2/other_script.groovy (stored 0%)
  adding: scripts/top_level.groovy (stored 0%)

With these scripts, you can upload my-scripts.zip as a bundle and associate them with a cluster.

Then, you can use them like the following:

  • The script scripts/top_level.groovy can be referred to as top_level:
{
    "size": 1,
    "script_fields": {
        "foo": {
            "script": "top_level",
            "params": {
                "my_var": 2
            }
        }
    }
}
  • The script scripts/module1/script_name.groovy can be referred to as module1_script_name
{
    "size": 1,
    "script_fields": {
        "foo": {
            "script": "module1_script_name",
            "params": {
                "my_var": 2
            }
        }
    }
}
  • The script scripts/module2/other_script.groovy can be referred to as module2_other_script:
{
    "size": 1,
    "script_fields": {
        "sample_field": {
            "script": "module2_other_script",
            "params": {
                "my_var": 2
            }
        }
    }
}

Summary

  • Upload scripts as a zipped bundle, where scripts reside in a scripts/ folder.
  • Associate the bundle with your cluster, by enabling the bundle under Custom plugins / dictionaries / scripts for the cluster.
  • Refer to scripts with folder names as modules, for example scripts/module_name/script_name.groovy becomes module_name_script_name.