Module: util

Miscellaneous utility functions that don't have a better home

Methods

(static) callInSerial(thisArg, promises, delayopt)

Calls an array of promises in serial.

Parameters:
Name Type Attributes Description
thisArg Object

The 'this' argument to pass to the called function

promises Array.<Object>

An array of promise definitions. Each definition should be: { promise: A function that returns a promise arguments: Array of arguments to pass to the function, message: An optional message to display at the start of this promise }

delay Integer <optional>

Delay in milliseconds to use between calls.

(static) collect(val, collecction) → {Array.<String>}

Adds value to an array

Typically used by the option parser for collecting multiple values for a command line option

Parameters:
Name Type Description
val String

The comma separated value string

collecction Array.<String>

The array into which to put the value

Returns:

The updated collection

Type
Array.<String>

(static) createBufferFrom(data, encoding)

Create a buffer from a data string. Buffer.from() is preferred, and should be used in versions of NodeJS that support it. Buffer.from() was introduced in Node 4.5.0 and in 5.10.0

Parameters:
Name Type Description
data String

data to create a buffer from

encoding String

data encoding. Default is 'utf8'

(static) csv(val, collecction) → {Array.<Array.<String>>}

Parses a comma-separated value

Typically used by the option parser for collecting multiple values which are comma separated values.

Leading and trailing spaces are removed from keys and values

Parameters:
Name Type Description
val String

The comma separated value string

collecction Array.<Array.<String>>

The array into which to put a new array conataining the values

Returns:

The updated collection

Type
Array.<Array.<String>>

(static) deleteArgs(id)

Deletes the arguments previously saved with saveArgs

This should be called when a script successfully completes.

Parameters:
Name Type Description
id String

Some unique id for the process. This will be used as the file name in which to store the files.

(static) download(url) → {Promise}

Downloads a file from a URL

Parameters:
Name Type Description
url String

URL to download from

Returns:

A promise which is resolved with the file name the file was downloaded to or rejected if an error occurs.

Type
Promise

(static) getArgsToStripDuringForcedReboot(options, argsToKeepopt)

Filters options arguments provided based on a list of arguments to keep

Parameters:
Name Type Attributes Description
options Object

Options object to pull arguments from

argsToKeep Object <optional>

Arguments to keep. Default ARGS_TO_KEEP.

(static) getDataFromUrl(url, optionsopt) → {String}

Gets data from a URL.

Only file, http, https URLs are supported for now.

Parameters:
Name Type Attributes Description
url String

URL from which to get the data. Only file, http, https URLs are supported for now.

options Object <optional>

http/https request options

Returns:

A promise which will be resolved with the data or rejected if an error occurs.

Type
String

(static) getProcessCount(grepCommand) → {Promise}

Returns the count of running processes, given the provided grep command example: getProcessCount('grep autoscale.js')

Parameters:
Name Type Description
grepCommand String

grep command to execute.

Returns:

A Promise that is resolved with the output of the shell command or rejected if an error occurs.

Type
Promise

(static) getProductString()

Returns the product type (BigIP/BigIQ) from the bigip_base.conf file

(static) localReady() → {Promise}

Performs a local ready check

Returns:

A promise which is resolved upon completion of the script or rejected if an error occurs.

Type
Promise

(static) logAndExit(message, levelopt, codeopt)

Log a message and exit.

Makes sure that message is logged before exiting.

Parameters:
Name Type Attributes Description
message String

Message to log

level String <optional>

Level at which to log the message. Default info.

code Number <optional>

Exit code. Default 0.

(static) logError(message, options)

Log a message to the error log file.

Parameters:
Name Type Description
message String

Message to log

options Object

Logger options

(static) lowerCaseKeys(obj) → {Object}

Lower cases all the keys in an object, including nested keys.

Typically used when working with JSON input.

If a parameter that is not of type object is provided, the original parameter will be returned.

Parameters:
Name Type Description
obj Object

Object in which to lowercase keys

Returns:
  • An object resembling the provided obj, but with lowercased keys.
Type
Object

(static) map(mapString, container) → {Object}

Parses a string of keys and values into a single map

Keys are separated from values by a ':' Key value pairs are separated from each other by a ',' Example: user:JoeBob,password:iamcool

Parameters:
Name Type Description
mapString String

String form of map. See example above.

container Objcect

Container to hold the map

Returns:

A map of all of key value pairs.

Type
Object

(static) mapArray(mapString, container) → {Array.<Object>}

Parses a string of keys and values. Each call is one element in the array.

Keys are separated from values by a ':' Key value pairs are separated from each other by a ',' Example: user:JoeBob,password:iamcool

Parameters:
Name Type Description
mapString String

String form of map. See example above.

container Array.<Objcect>

Container into which to push the map object

Returns:

An array containing one map per call with the same container

Type
Array.<Object>

(static) pair(pair, container)

Parses a ':' deliminated key-value pair and stores them in a container.

Leading and trailing spaces are removed from keys and values

Typically used by the option parser for collecting multiple key-value pairs for a command line option

Parameters:
Name Type Description
pair String

String in the format of key:value

container Object

Object into which to put the key:value

(static) parseTmshResponse(response) → {Promise}

Parse a tmsh response into an object

Parameters:
Name Type Description
response String

tmsh response data to parse

Returns:

A promise containing the parsed response data, as an object.

Type
Promise

(static) readData(dataOrUri, dataIsUri, optionsopt)

Disambiguates data that is either raw data or in a URI.

Parameters:
Name Type Attributes Description
dataOrUri String

Data URI (file, http, https, AWS arn) to location containing data.

dataIsUri Boolean

Indicates that password is a URI for the password

options Object <optional>

Optional parameters.

Properties
Name Type Attributes Description
clOptions Object <optional>

Command line options if called from a script. Required for Azure Storage URIs

logger Object <optional>

Logger to use. Or, pass loggerOptions to get your own logger.

loggerOptions Object <optional>

Options for the logger. See module:logger.getLogger for details.

(static) readDataFromFile(file)

Reads data from a file

Parameters:
Name Type Description
file String

The file to read from

Returns:

A promise which will be resolved with the contents of the file or rejected if an error occurs

(static) reboot(bigIp, optionsopt) → {Promise}

Reboots the device

First save arguments from running scripts so that they are started again on startup

Parameters:
Name Type Attributes Description
bigIp Object

The BigIp instance to reboot.

options Object <optional>

Optional parameters.

Properties
Name Type Attributes Description
signalOnly Boolean <optional>

Indicates that we should not actually reboot, just prepare args and signal that reboot is required

Returns:

A Promise that is resolved when the reboot command has been sent or rejected if an error occurs.

Type
Promise

(static) removeDirectorySync(dir)

Synchronously removes a directory and all files in the directory

Parameters:
Name Type Description
dir String

Directory to remove

(static) runInBackgroundAndExit(process, logFileName)

Spawns a new process in the background and exits current process

Parameters:
Name Type Description
process Object

Node.js process

logFileName String

Name to pass for output log file

(static) runShellCommand(commands) → {Promise}

Runs a shell command and returns the output

Parameters:
Name Type Description
commands String

Command to run

Returns:

A promise which is resolved with the results of the command or rejected if an error occurs.

Type
Promise

(static) runTmshCommand(commands) → {Promise}

Runs a tmsh command and returns the output

Parameters:
Name Type Description
commands String

Command to run ('list ltm pool', for example)

Returns:

A promise which is resolved with the results of the command or rejected if an error occurs.

Type
Promise

(static) saveArgs(args, id, argsToStripopt)

Saves arguments that started a script so that they can be re-used in the event we get stuck and need to reboot.

Parameters:
Name Type Attributes Description
args Array.<String>

Array of arguments that can be used to re-run the process (i.e. process.argv)

id String

Some unique id for the process. This will be used as the file name in which to store the files.

argsToStrip Object <optional>

Array of arguments to strip. Default none.

(static) tryUntil(thisArg, retryOptions, funcToTry, args) → {Promise}

Tries a method until it succeeds or reaches a maximum number of retries.

Parameters:
Name Type Description
thisArg Object

The 'this' argument to pass to the called function

retryOptions Object

Options for retrying the request.

Properties
Name Type Attributes Description
maxRetries Integer

Number of times to retry if first try fails. 0 to not retry. Default 60.

retryIntervalMs Integer

Milliseconds between retries. Default 10000.

continueOnError Boolean <optional>

Continue even if we get an HTTP BAD_REQUEST code. Default false.

continueOnErrorMessage String | RegExp <optional>

Continue on error if the 400 error message matches this regex

funcToTry function

Function to try. Function should return a Promise which is later resolved or rejected.

args Array.<Object>

Array of arguments to pass to funcToTry

Returns:

A promise which is resolved with the return from funcToTry if funcToTry is resolved within maxRetries.

Type
Promise

(static) versionCompare(v1, v2, optionsopt) → {number}

Compares two software version numbers (e.g. "1.7.1" or "1.2b").

This function is based on https://gist.github.com/TheDistantSea/8021359

Parameters:
Name Type Attributes Description
v1 string

The first version to be compared.

v2 string

The second version to be compared.

options object <optional>

Optional flags that affect comparison behavior:

  • zeroExtend: true changes the result if one version string has less parts than the other. In this case the shorter string will be padded with "zero" parts instead of being considered smaller.
License:
  • This function is in the public domain. Do what you want with it, no strings attached.
Returns:
  • 0 if the versions are equal
  • a negative integer iff v1 < v2
  • a positive integer iff v1 > v2
Type
number

(static) writeDataToFile(data, file)

Writes data to a file

Parameters:
Name Type Description
data String

The data to write

file String

The file to write to

Returns:

A promise which will be resolved when the file is written or rejected if an error occurs

(static) writeDataToUrl(data, url)

Writes data to a URL.

Only file URLs are supported for now.

Parameters:
Name Type Description
data String

The data to write

url String

URL to which to write. Only file URLs are supported

(inner) prepareArgsForReboot()

Copies all the saved arguments (from saveArgs) to the startup file so that when the box reboots, the arguments are executed.