Skip to content

JavaScript in Orchestrator: the Rhino engine

Every JavaScript script you write in Orchestrator runs in Rhino, a JavaScript engine written in Java by the Mozilla project. That includes scriptable tasks, actions, scripted decisions, and the actions behind input forms. Rhino runs inside the Orchestrator server’s Java process, so your scripts can call Java objects as if they were JavaScript, and plug-ins can hand you objects such as a vCenter VM without any translation layer.

It also means the JavaScript you know from browsers and Node.js only partly applies. There’s no window, no setTimeout, no require, and no fetch, and some newer syntax may or may not be accepted. In this recipe you write a small action that asks the engine which features it supports, then you learn to look things up in the scripting API instead of guessing.

Getting ready

  • A login with a role that can create actions, such as Workflow designer or administrator.
  • An action module for this chapter, com.vcfguide.chapter4. You create it in step 1.

How to do it

Ask the engine what it accepts

  1. Go to Library > Actions and click New Action. Enter the name probeLanguage and the module com.vcfguide.chapter4, and click Create.
  2. On the Script tab, leave the inputs empty and set the Return type to Properties.
  3. Enter this script and click Save. Each test is compiled separately by eval inside try, so a feature the engine doesn’t know fails only its own test:
com.vcfguide.chapter4/probeLanguage
// Reports which newer JavaScript features this Orchestrator's Rhino engine accepts.
// The script itself is plain ES5.1, so it runs on any version.
var tests = [
["let", "let a = 1; a"],
["const", "const b = 2; b"],
["arrow function", "(function () { var f = (x) => x * 2; return f(2); })()"],
["template literal", "var n = 3; `n is ${n}`"],
["default parameter", "(function (x = 5) { return x; })()"],
["for...of", "var s = 0; for (var v of [1, 2, 3]) { s += v; } s"],
["destructuring", "var o = { p: 1 }; var { p } = o; p"],
["spread", "Math.max(...[1, 2, 3])"],
["class", "(function () { class A {} return typeof A; })()"],
["Array.prototype.includes", "[1, 2, 3].includes(2)"],
["Object.assign", "Object.assign({}, { a: 1 }).a"],
["String.prototype.padStart", "'7'.padStart(3, '0')"]
];
var result = new Properties();
for (var i = 0; i < tests.length; i++) {
var name = tests[i][0];
try {
var value = eval(tests[i][1]);
result.put(name, "yes");
System.log("yes " + name + " -> " + value);
} catch (e) {
result.put(name, "no");
System.warn("no " + name + " (" + e + ")");
}
}
return result;
  1. Click Run. The action has no inputs, so it starts at once, with no dialog. Open the Logs tab under the script: each feature has a yes line or a no warning with the error the engine gave. The result shows the same answers as a Properties object.
The probeLanguage action in the Orchestrator Client after a run. The Logs pane lists one INFO line per feature that compiled and two WARNING lines, for spread and class, with the SyntaxError each one raised.
Figure 4.1. The probe's log on Orchestrator 9.1.1: one line per feature. Only spread and class fail.

On Orchestrator 9.1.1 (build 25643527) the probe returns:

Feature 9.1.1
let, const yes
Arrow functions, template literals yes
Default parameters, for...of, destructuring yes
Array.prototype.includes, Object.assign, String.prototype.padStart yes
Spread (...) no: SyntaxError: syntax error
class no: SyntaxError: identifier is a reserved word: class

The Run button runs an action with no inputs straight away, without a dialog. The editor then shows Result / Inputs and Logs tabs under the script, and a Completed badge next to the action’s name.

Look things up in the API Explorer

  1. Open API Explorer, near the bottom of the navigation pane, below Deleted Items. Its tree has three branches: System (the built-in Objects, Enums and Types), Plugins (one entry per installed plug-in, such as VC or REST) and Actions (one entry per action module).
  2. Type System in the Filter objects box, expand System > Objects and click System. The right side lists its methods, such as log, warn, error, getModule, sleep and nextUUID, each with a one-line description, its parameters and its return type. The browser address now ends in explorer?section=s&type=o&name=System, so you can bookmark the page.
  3. Filter for VcVirtualMachine, the scripting object behind the VC:VirtualMachine type. It sits under Plugins > VC. Its properties include name, runtime, and summary, which recipe 3.1 used to build a VM summary.
API Explorer page: the filter box reads System; the tree shows System, Objects with AtomicSystem, LockingSystem, System (selected) and TimeZone, then Plugins VAPI, VC and VCO, then Actions modules. The right side lists methods of System: nextUUID returns string; sleep with parameter ms (number) returns boolean; getModule, Return an action module with the given name, parameter name (string), returns Module; getContext returns ExecutionContext; getAllModules.
Figure 4.2. The API Explorer, filtered for System, with the System object open.
  1. Back in the editor of probeLanguage, start a new line, type System., and wait. Autocomplete lists the same methods. You don’t have to leave the editor to look something up: the Script tab has its own API Explorer panel beside the code, with the same tree and filter. The schema editor has one too, as the API Explorer tab of a scriptable task.

How it works

Rhino reads your script, compiles it, and runs it inside the vco-app server’s Java virtual machine. Before it runs, Orchestrator puts three kinds of name into the script’s scope: the bound inputs and outputs of the element (recipe 3.1), the built-in scripting objects such as System, Server, and Properties, and the objects of each plug-in, such as VcPlugin. Everything else is standard JavaScript: Math, Date, JSON, RegExp, arrays, and objects.

Which JavaScript that is depends on the Rhino version inside Orchestrator. Broadcom doesn’t publish it. Community reports give the versions below for older releases, and Rhino’s own compatibility table says what each one supports in general:

Orchestrator Rhino What Rhino supports
7.x and 8.x 1.7R4 (reported) ECMAScript 5.1 with a few extras such as let
9.0 1.7.15 (reported) Adds arrow functions, template literals, Array.prototype.includes, and more
9.1.1 1.9.1 (measured in the lab) Adds default parameters, for...of and destructuring, among others

On the 9.1.1 lab, a test action logged Packages.org.mozilla.javascript.Context.getCurrentContext().getImplementationVersion() as Rhino 1.9.1, and getLanguageVersion() as 200, Rhino’s ES6 mode. The lab’s class shutter allowed the call; on a server with a stricter shutter (recipe 2.8), allow org.mozilla.javascript.Context first.

Needs a lab check. Confirm the Rhino version on 9.0 the same way.

This book keeps every JavaScript sample to plain ES5.1: var, function, string concatenation, and loops with an index. Content often moves between Orchestrator versions, in packages, in Git, or in a VCF Automation upgrade, and an action that uses an arrow function works on one server and fails to compile on the next. ES5.1 runs everywhere.

Java under the surface. Plug-in objects are Java objects that Rhino wraps so they look like JavaScript ones. Most of the time you can’t tell. Sometimes a value that comes from Java behaves like an object rather than a JavaScript string, so a strict comparison with === fails or JSON.stringify gives something odd. When that happens, convert it first with String(value), or a number with Number(value). Scripts can’t create arbitrary Java objects. Only the classes in the class shutter file are allowed, as recipe 2.8 shows.

Back in vRO 5.5–7Scripts run in Rhino 1.7R4, which Broadcom's vRO 7.6 documentation named. Browse the scripting API in the Java client's API Explorer, or in a separate HTML reference.
In Orchestrator 9.xScripts run in a newer Rhino in VCF 9, but Broadcom no longer names the version. The API Explorer is part of the browser client, and a script can also be Python, Node.js, or PowerShell (recipe 4.3).

There's more

Use System.log, not console.log. A script’s log lines go to the run’s Logs tab through System.log, System.warn, System.error, and System.debug. Recipe 3.2 introduced them, and recipe 4.6 covers logging in depth.

No timers, no promises. A script runs from top to bottom and then ends. There’s no event loop to call you back later, so setTimeout doesn’t exist, and a promise never settles after the script ends. To wait, use the elements in recipe 3.7. To call an HTTP service, use the HTTP-REST plug-in (Chapter 5).

Properties or an object? Properties is Orchestrator’s own key–value type, backed by Java. Use it for inputs, outputs, and variables, because only Orchestrator types can be bound and saved with a run. Inside a script, a plain JavaScript object is often easier. Convert between them at the edges:

Properties to a plain object and back
// props is a Properties variable
var obj = {};
var keys = props.keys;
for (var i = 0; i < keys.length; i++) {
obj[keys[i]] = props.get(keys[i]);
}
var back = new Properties();
for (var key in obj) {
if (obj.hasOwnProperty(key)) {
back.put(key, obj[key]);
}
}

Avoid Rhino’s own extensions. Rhino accepts some syntax that no other engine does, such as for each (var x in list). It works, but it ties the script to Rhino and confuses editors and linters. A plain for loop does the same job.

Write in an IDE, then import. If you’d rather write modern JavaScript or TypeScript in a code editor, VMware’s open-source Build Tools for VMware Aria compile it down to code Orchestrator accepts and package it for import. Chapter 6 covers moving content in and out of Orchestrator.

Test small pieces as actions. An action with no inputs, like probeLanguage, is the quickest place to try out a few lines. Action runs aren’t kept in the run history, so you can run them as often as you like without cluttering Activity > Workflow Runs.

See also