oJob langs

(OpenAF version >= 20230828)

Quick examples, and reference, of the use of different languages as oJob’s jobs.

🪟 Powershell

In Powershell the variable “$args” is reserved so OpenAF’s oJob uses “$_args” instead.

jobs:
- name: Powershell example
  lang: powershell
  exec: |
    # For an existing property in args
    $_args.firstName = "Scott"

    # For a new property in args
    $_args | Add-Member -MemberType NoteProperty -Name "lastName" -Value "Tiger"


🐿️ Go

Each Go OpenAF’s job is already the main function.

- name: Go example
  lang: go
  exec: |
    args["name"] = fmt.Sprintf("%s %s", args["firstName"], args["lastName"])


💎 Ruby

The variable args is available to be use in Ruby.

- name: Ruby example
  lang: ruby
  exec: |
    args["name"] = args["firstName"] + " " + args["lastName"]


🐪 Perl

In order to access the variable $args in Perl it’s required to have the Perl module JSON. You can run the following perl command to understand if you have it installed or not:

perl -e 'if (eval { require JSON; 1 }) { print "Installed!"; } else { print "NOT installed!" };'

Example with the JSON module installed:

- name: Perl example
  lang: perl
  exec: |
    $args->{name} = $args->{firstName}.$args->{lastName};

NOTE: don’t forget to add ‘;’ on the end of each line.

Example without the JSON module installed:

- name: Perl example
  lang: perl
  exec: |
    $name = '{{firstName}} {{lastName}}''
    
    # return name

NOTE: Only string variables are supported at the moment when the JSON module is not installed.


💻 Shell/SSH

Keep in mind that shell scripting only supports string-based variables on one level. Each current args will be transformed into an environment variable (such as $firstName and $lastName). For more complex cases you can use OpenAF’s templating. To return values use the comment ‘# return ‘ followed by the shell script variables

# -------------------
- name: Shell example
  lang: shell
  exec: |
    name="$firstName $lastName"
    other={{someMap.someProperty}}
    
    # return name, other

# -----------------
- name: SSH example
  args:
    ssh:
      host : myhost
      port : 22
      login: user
      pass : badPassword1
      #id : myKeyFile
      #key:  
      #timeout:
  lang: ssh
  exec: |
    name="{{firstName}} {{lastName}}"
    other={{someMap.someProperty}}
    
    # return name, other
    

🐍 Python

Python runs in parallel with OpenAF’s oJob and provides the map args that can be used and changed with some additional functions:

- name: Python example
  lang: Python
  exec: |
    args["name"] = args["firstName"] + " " + args["lastName"]
    
    _s("paths", _("({ p: getOpenAFPath(), j: getOpenAFJar() })"))
    args["contentsOfPaths"] = _g("paths")
    args["stringifyOfArgs"] = _d(args)

Python function Description
_s(aKey, aObj) Equivalent to OpenAF’s $set using aKey string and a Python object aObj.
_g(aKey) Equivalent to OpenAF’s $get using aKey string.
_d(aObj) Returns a Python string with the JSON stringify of the Python map aObj.