Giter Site home page Giter Site logo

spoonboy-io / morpheus-nodejs-task-plugin Goto Github PK

View Code? Open in Web Editor NEW
2.0 2.0 1.0 145 KB

Script Morpheus automation tasks in Node.js. Utilise the latest ECMA standards and NPM modules.

Groovy 100.00%
automation morpheus node nodejs plugin task

morpheus-nodejs-task-plugin's Introduction

Node.js Morpheus Task Plugin

Write Morpheus automation tasks in Node.js

  • Results of previous tasks are available in a results object variable as key:value
  • Node.js must be installed on the appliance
  • Modules must be installed on the appliance with the global flag npm install -g modulename
  • The Plugin needs to be configured with the absolute path to the Node installation in the plugin settings
  • Does not use Nashorn. Use modern Ecmascript syntax, dependent on the version of Node.js you install

Build

You should be able to build this locally using the included grade wrapper

./gradlew shadowJar     

Setup

  1. Install the plugin on Morpheus
  2. Install Node.js on the Morpheus Appliance. I used NVM to install Node.js v16.5.1 LTS
  3. Discover the absolute path of your Node.js installation (mine was /home/ollie/.nvm/versions/node/v16.15.1)
  4. Visit the plugin settings (Admin > Integrations > Plugins) and edit the plugin settings to add the absolute path as shown below:-

edit plugin settings

Example

In this example, we want to use a simple Node.js script in Morpheus. The code requires an external package, Axios, a HTTP request library.

This is the code:-

const axios = require('axios');

axios.get('https://jsonplaceholder.typicode.com/users')
  .then(res => {
    const headerDate = res.headers && res.headers.date ? res.headers.date : 'no response date';
    console.log('Status Code:', res.status);
    console.log('Date in Response header:', headerDate);

    const users = res.data;

    for(user of users) {
      console.log(`Got user with id: ${user.id}, name: ${user.name}`);
    }
  })
  .catch(err => {
    console.log('Error: ', err.message);
  });

External modules need to be installed on the Morpheus appliance globally. Once installed the module can be used by all Morpheus Node.js tasks.

To install Axios globally we run this command:-

npm install -g axios

We can now add the script to a Node.js task. The screenshot below shows it in Morpheus.

image

Note how we split the require statements from the main code? This simplifies processing the script on the Morpheus side.

Note also, how we prefix our call for the Axios package? See how require('axios'); becomes require('morpheus/axios');? This tells the task plugin to use the globally installed version of the module, and allows us to differentiate it from core Node.js modules such as http

Running this task in Morpheus we should see the following task output:-

image

TODO

  • Make the Morpheus vars available to the task

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.