sugo-module-shell

4.0.4 • Public • Published

sugo-module-shell

Build Status npm Version JS Standard

Shell module for SUGOS

Requirements

Node.js NPM

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.

Param Type Description
command string The command to run
args array List of string arguments
options Object Optional settings

.exec(command, options) -> object

Execute a command and returns io as object.

Param Type Description
command string The command to run, with space-separated arguments
options Object Optional settings

.ping(pong) -> string

Test the reachability of a module.

Param Type Description
pong string Pong message to return

.assert() -> boolean

Test if the actor fulfills system requirements

Events

The following events my be emitted from the module.

Param Description
"stdout" Standard out from spawned process.
"stderr" Standard error from spawned process.

License

This software is released under the Apache-2.0 License.

Links

Readme

Keywords

Package Sidebar

Install

npm i sugo-module-shell

Weekly Downloads

1

Version

4.0.4

License

Apache-2.0

Last publish

Collaborators

  • realglobe