0.0.1 • Published 8 years ago

digital-navy v0.0.1

Weekly downloads
3
License
MIT
Repository
github
Last release
8 years ago

digital-navy

Run lots of fully isolated commands on digital ocean

Build Status Dependency Status NPM version

Installation

npm install digital-navy --save

Usage

var fs = require('fs');
var DigitalNavy = require('digital-navy');

var script = fs.readFileSync(__dirname + '/example-script.js');

var navy = new DigitalNavy({
  // Note that the base image is only re-built when the name changes,
  // so we include a shasum of the script and the node version
  name: 'node-' + process.version + '-' + shasum(script),
  // the prepare function is used to run ssh commands before creating
  // the snapshot.  Use it to install all your dependencies
  prepare(ssh) {
    console.log('installing node');
    // we install the same version of node as we're using locally
    return DigitalNavy.installNode(ssh, process.version).then(
      () => {
        console.log('installed node');
        // copy some JavaScript script to the server
        return ssh.exec('cat > ~/example-script.js', {stdin: script});
      }
    );
  },
  token: '<DIGITAL OCEAN KEY>',
  // keypair generated by either rsa-json or keypair
  keypair: keypair(),

  // You can use functions for minPoolSize and maxPoolSize if you want to have these change over time
  // changes in the return values of those functions will only be taken into
  // account when droplets start or stop executing
  minPoolSize: 3,
  maxPoolSize: 4,
});

// Call this function each time you want to run some commands in their own isolated sandbox
navy.run(
  ssh => {
    console.log('got droplet');
    return ssh.exec('node ~/example-script.js').then(
      result => {
        console.log('finished script');
        console.dir(result);
        assert(result === 'Hello World\n');
        return result;
      },
    );
  }
);

DigitalNavy constructor

options:

namedefaultdescription
namerequiredThe name for the snapshot and instances. The snapshot is rebuilt whenever this name changes, so use it to encode all versioning info.
preparerequiredA function to be called with an ssh connection to the droplet that will be used to generate a snapshot. Use it to install dependencies.
tokenrequiredA digital ocean token, used to create the droplets 😊
keypairrequiredAn object with {public, private} keys. Generated with keypair or rsa-json
maxPoolSize2The maximum number of isolated virtual machines to allocate at a time. By default this is kept low to prevent you accidentally spending lots of money.
minPoolSize0The minimum number of workers to keep running at a time. Keeping servers running even when there's no work to do helps a lot with latency.
workerSize'512mb'The size of the droplet allocated for performing the work. Increase this if you need more memory or CPU for each worker.
builderSize= workerSizeThe size of the droplet used to build the initial sandbox. Use a larger droplet if you need to build big assets up front, but the actual worker droplets can be much smaller.

digitalNavy.run(options, fn) (instance method)

Run a sequence of ssh commands in an isolated droplet. The function fn is given an instance of SshConnection.

options:

namedefaultdescription
debugfalseSet this to true to emit the stdout & stderr or remote commands on the local terminal

DigitalNavy.installNode(ssh, version) (static method)

A helper method to install a chosen version of node.js via an SshConnection. You probably want to use this in your prepare function.

SshConnection#exec(commands, options) (instance method)

execute a command or sequence of commands and return a Promise for the string value of stdout.

namedefaultdescription
debug= parentOptions.debugSet this to true to override the default value set in digitalNavy.run
stdinnullSet this to a Buffer or string to provide it as the stdin for the command being run

License

MIT