Skip to content

Scripts

Overview

Scripts are used by distributions, to decide how application instances should be distributed. Here you can find more information about writing scripts.

Script types

There are three types of scripts:

This tutorial shows how to create all three types of scripts.

Write script

Script content must be written to file.

check-if-node-can-accept-new-application-instance-v1

function checkIfNodeCanAcceptNewApplicationInstance(environmentState, environmentProjection, node, application) {
    if (node.status != "available") {
        return false;
    }

    return true;
}

This script decides if node can accept new application instance, based on the node status. If status is set to "available", the function returns true, which means that node can accept new application instance.

select-node-for-new-application-instance-v1

function selectNodeForNewApplicationInstance(environmentState, environmentProjection, nodes, application) {
    const grouped = new Map();

    for (let node of nodes) {
        grouped.set(
            node.id,
            environmentProjection
                .getApplicationInstancesByNodeId(node.id).length
        );
    }

    const sorted = Array.from(grouped)
        .sort((a, b) => a[1] - b[1]);

    if (sorted.length == 0) {
        return null;
    }

    return sorted[0][0];
}

This script selects best node for new application instance, from nodes accepted in the script from previous step (select-node-for-new-application-instance-v1) . First it groups nodes and number of instances, and then selects node that has the least instances. Returned value is the id of node. If there are no candidates, you can return null value (application instance won't be started).

select-application-instance-to-remove-v1

function selectApplicationInstanceToRemove(environmentState, environmentProjection, applicationInstances, application) {
    const grouped = new Map();

    for (let node of environmentProjection.getNodes()) {
        grouped.set(
            node.id,
            environmentProjection
                .getApplicationInstancesByNodeId(node.id).length
        );
    }

    const sorted = Array.from(grouped)
        .sort((a, b) => b[1] - a[1]);

    if (sorted.length == 0) {
        return null;
    }

    for (let entry of sorted) {
        for (let applicationInstance of applicationInstances) {
            if (applicationInstance.nodeId == entry[0]) {
                return applicationInstance.id;
            }
        }
    }

    return null;
}

This script selects best application instance to remove. First it groups nodes and number of instances, and then sorts them starting from nodes with the most instances. Next, it searches for specific application instance in nodes, starting from node with the greatest number of all instances. Returned value is the id of application instance that will be removed. If there are no candidates, you can return null value (application instance won't be removed).

Create script

If you have your file with script content ready, you can create it by executing onteoncli script create <name> <scriptType> <scriptFile> where:

  • name is a unique name of script.
  • scriptType is one of the three script type.
  • scriptFile is a file with script content.

You can check if your script was created by executing onteoncli script list.

root@c50383c3990c:/opt/onteon# onteoncli script create acceptAvailableNodes check-if-node-can-accept-new-application-instance-v1 script.js 
id:         61767dd07d69d56d8d0081de
createdAT:  2021-10-25T09:50:08.639Z
updatedAt:  2021-10-25T09:50:08.639Z
name:       acceptAvailableNodes
scriptType: check-if-node-can-accept-new-application-instance-v1

root@c50383c3990c:/opt/onteon# onteoncli script list
id                       createdAt                updatedAt                name                                         scriptType
61767dd07d69d56d8d0081de 2021-10-25T09:50:08.639Z 2021-10-25T09:50:08.639Z acceptAvailableNodes                         check-if-node-can-accept-new-application-instance-v1
61765bbf23a4ed4397763e4c 2021-10-25T07:24:47.455Z 2021-10-25T07:24:47.455Z defaultAvailableNodeOnlyCINCANAIV1           check-if-node-can-accept-new-application-instance-v1
61765bbf23a4ed4397763e4a 2021-10-25T07:24:47.449Z 2021-10-25T07:24:47.449Z defaultApplicationInstancesCountOnlySAITRV1  select-application-instance-to-remove-v1
61765bbf23a4ed4397763e48 2021-10-25T07:24:47.439Z 2021-10-25T07:24:47.439Z defaultApplicationInstancesCountOnlySNFNAIV1 select-node-for-new-application-instance-v1

Now you can use your script in distribution. Here you can read more about creating and managing distributions.