1.3 Custom JavaScript Code

Code tab JavaScript

  1. JavaScript Code Layer
  2. Places where you can use JavaScript Code:
    • a) Main Code Tab
    • b) Code Parameter Fields (inside actions)
    • c) Conditional Code
    • d) Pre and Post-Execution Functions
    • e) Functions
    • f) Inputs
  1. Kaholo SDK
  2. Other code capabilities: Python Language & Shell scripts  

 

  1. JavaScript Code Layer 

JavaScript Code Layer gives you substantial flexibility to handle complex Pipelines. Typical reasons to use custom code in your Pipeline include:

  • To conditionally run an action
  • To trigger functions before and after actions are executed (Hooks)
  • To pass data between actions
  • To transform data before passing them between actions

Note: The Code Tab enables you to add short snippets of JavaScript code to extend Pipelines in complex scenarios. The Code Tab is not a direct translation of the Design page. 

 

2. Places where you can use JavaScript Code:

There are several places where Kaholo allows to use JavaScript code:

 

a) Main Code Page

The Code Page allows you to add larger complex functions, initialize variables, and data manipulations when needed.

With built-in context-aware code completion (IntelliSense), you can see everything we provide in our SDK under the Kaholo and Actions objects. For example, declaring and assigning a variable which would then be plugged into an Action Parameter Field.

 

var awsRegion;

var awsRegion = kaholo.execution.configuration.Region;

 

b) Code Parameter Fields (inside actions)

When configuring an Action, you can activate the code feature by turning on the toggle switch above a parameter field. You can use this to set values dynamically when the Pipeline is being executed. You can either access the Kaholo SDK to directly insert values, invoke a function, or get a variable defined in the Code page. Here’s an example for accessing the results of a previously executed Action:

 

c) Conditional Code

The Conditional Code field determines whether an Action should execute. You can define this in the action’s Advanced Configuration tab. For example, basing it on a previous action’s status: actions.httprequest.status == ‘success‘. Another example of using the Condition field is to set a condition on a loop: deployImageLoopIndex < 5

 

 actions.httprequest.status == ‘success’

 

 

d) Pre and Post-Execution Functions

Pre and Post Execution functions are the functions before or after an action is executed. A common example is to increment a variable in a Post-execution Hook in conjunction with using the Loop and Conditional code features: deployImageLoopIndex++

 

e) Functions

If you need to process or transform your output from actions, you can do it from within functions that can be defined in the Code tab. There, you can use JavaScript functions to call results from actions and return the values.

Here’s a simple example of a function that returns the output from an Action – and prepares the information to send to Slack:

 

function getSlackMessage(){ return `New Instance ID: ${actions.result.Instances[0].InstanceId}`}

 

To get the result of this function, you can call it by turning on the toggle switch above a parameter field.

getSlackMessage()

 

You can also access Vault items using functions. You can do this by using the async function.

async function getCmd(){    return `echo ${await kaholo.vault.getValueByKey(“Vault_name”}` }

 

To get the result of this function, you can call it by turning on the toggle switch above a parameter field. Keep in mind that example function returns whole bash command (with “echo” word)

getCmd()

 

f) Inputs

Execution inputs is an abstraction layer where you can control execution for users. 

Using custom Javascript fields you create fields that will be populated like in the inputs tab. To create this inside custom javascript code you need to write in the field:

    kaholo.execution.inputs.<input-field-ID> 

  1. Kaholo SDK

Kaholo provides an SDK, which is exposed in the JavaScript Code Layer as the `kaholo` namespace object.

In general, but especially when creating loops, the action’s results will be updated after each iteration. In case you need to get and manipulate the results to use them at a later point in the pipeline, it’s best to store the results in a variable first. You can define and initialize variables in the Code Page.

Note: To quickly see what’s available in the Kaholo SDK, go to the Code Page in the UI and start by typing “kaholo”. Then you will see all the available objects in our context-aware code completion features. The most useful objects are tabulated below.

 

Kaholo SDK Table:

https://kaholo.io/docs/kaholo-getting-started/custom-js-code/kaholo-sdk-table

Accessing Action Results

When accessing Action results, you need to reference the Action’s ID. This is a user-defined field. Let’s say you’ve created an Action and set its ID to ‘gcceReserveIp’. Now you want to access its result from a different Action in the Pipeline. In this case, you would get the results as follows:

Actions.gcceReserveIp.result

If the Action result returned an object that contained an “ID” field that you want to access, you would access it as follows:

Actions.gcceReserveIp.result.id

Tip: If you’re unsure of a particular Action’s result data structure, you should execute the Pipeline once and then see what was returned by the Action. Simply navigate to the Execution Results page, click on the particular Action, and view the data in the “Final Results” panel.

Accessing Configuration Data

If you want to access the current execution’s configuration data, and let’s say the configuration data is {“MachineType”: “e2-medium”}, you can access it as follows:

kaholo.execution.configuration.MachineType

To access all configurations, not just the one currently being used for the execution:

kaholo.execution.configurations["medium web server"]

Action Statuses

If you want to get the status of an Action and use it in the Conditional Code for another Action, you can access it as follows:

Actions.gcceReserveIp.status=='skipped'

Access Trigger Payload

When a Kaholo Pipeline is executed with one of our Trigger plugins, it arrives with a payload that you can access via the Code layer as follows:

kaholo.execution.trigger.payload

If you want to see the actual payload of a particular execution, go to the Execution Results screen and click on the Trigger hyperlink.

 

 

  1. Other code capabilities: Python Language & Shell scripts 

a) Python

Python is at this moment not natively supported by the platform and SDK but still, you can run Python script on the agent. You can make any operation using python, using Python or CMD plugin.


b) Shell Scripts

Standard Kaholo Agent is Linux based so it means that you can use shell scripts/commands directly on the agent. You can use this while creating your pipeline