4.0.4 • Published 7 years ago

sugo-module-shell v4.0.4

Weekly downloads
3
License
Apache-2.0
Repository
github
Last release
7 years ago

sugo-module-shell

Build Status npm Version JS Standard

Shell module for SUGOS

Requirements

Installation

$ npm install sugo-module-shell --save

Usage

Register module to SUGO-Actor

#!/usr/bin/env node

/**
 * Example usage to register module on actor
 * @see https://github.com/realglobe-Inc/sugo-actor
 */
'use strict'

const sugoModuleShell = require('sugo-module-shell')

const sugoActor = require('sugo-actor')
const co = require('co')

co(function * () {
  let actor = sugoActor('http://my-sugo-cloud.example.com/actors', {
    key: 'my-actor-01',
    modules: {
      // Register the module on actor as "shell"
      shell: sugoModuleShell({})
    }
  })
  yield actor.connect()
}).catch((err) => console.error(err))

Then, call the module from remote caller.

#!/usr/bin/env node

/**
 * Example to call from caller
 * @see https://github.com/realglobe-Inc/sugo-caller
 */
'use strict'

const co = require('co')
const assert = require('assert')
const sugoCaller = require('sugo-caller')

co(function * () {
  let caller = sugoCaller('http://my-sugo-cloud.example.com/callers', {})
  let actor = caller.connect('my-actor-01')

  // Access to the module
  let shell = actor.shell()

  // Send ping
  let pong = yield shell.ping()
  assert.ok(pong)

  // Spawn command
  {
    const out = (chunk) => process.stdout.write(chunk)
    const err = (chunk) => process.stderr.write(chunk)

    shell.on('stdout', out)
    shell.on('stderr', err)

    shell.spawn('tail', [ '-f', '/var/log/app.log' ])
    yield new Promise((resolve) => setTimeout(resolve, 3000))

    shell.off('stdout', out)
    shell.off('stderr', err)
  }

  // Execute a command
  {
    let ls = yield shell.exec('ls -la ~') // yield to wait result
    console.log(ls)
  }
}).catch((err) => console.error(err))

Methods

The following methods are available from remote callers for the module.

.spawn(command, args, options) -> number

Spawn a command and pipe io to event emitting.

ParamTypeDescription
commandstringThe command to run
argsarrayList of string arguments
optionsObjectOptional settings

.exec(command, options) -> object

Execute a command and returns io as object.

ParamTypeDescription
commandstringThe command to run, with space-separated arguments
optionsObjectOptional settings

.ping(pong) -> string

Test the reachability of a module.

ParamTypeDescription
pongstringPong message to return

.assert() -> boolean

Test if the actor fulfills system requirements

Events

The following events my be emitted from the module.

ParamDescription
"stdout"Standard out from spawned process.
"stderr"Standard error from spawned process.

License

This software is released under the Apache-2.0 License.

Links