Skip to end of metadata
Go to start of metadata

Modules in CLI

Overview

RHQ now supports CommonJS modules in its CLI and scripted alert notifications on the server. Using the modules, it is easy to create reusable functional parts that you can easily compose together. This is great for RHQ, whose API is a little bit chatty so various utility functions will therefore come in handy for any task you might want to automate.

Quick CommonJS primer

CommonJS modules are simple javascript files with one special feature. Anything that you want to be "exported" from the module, i.e. available to the callers, needs to be assigned to a special variable called exports. All other functions and objects that are defined in the module file are private to the module and cannot be used outside of it. Modules can also load another modules.

Below is a simple example module, let's call it "printedMath.js":

Now to use the module in other scripts, you need to first "require" it and then use it:

Note that in the script that uses the printedMath module, you cannot access neither the privateVariable variable, nor the privateFunction. Because you can only access the module's exported features through a variable that the module has been assigned to, i.e. the privateMath variable in the example above, it is possible for two modules to require functions with the same names and they won't conflict in the script that would use both of those modules.

Module loading

The modules can be loaded from various locations including filesystem and RHQ repositories. Especially the ability load the scripts from the repositories on the RHQ server lends itself to a clever script management. If you place your scripts in the server, any update you make to them is automatically picked up by any existing or future remote CLI - if you have multiple CLI installations, you don't need to update them one by one with the latest version of your script - it just gets picked up at the point in time you use it.

The loading of modules is pluggable. You can read about the default module loaders and how to provide new module loaders (aka script source providers) on the Script Sources page.

Labels:
None
Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.