0.0.1 • Published 8 years ago
digital-navy v0.0.1
digital-navy
Run lots of fully isolated commands on digital ocean
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:
name | default | description |
---|---|---|
name | required | The name for the snapshot and instances. The snapshot is rebuilt whenever this name changes, so use it to encode all versioning info. |
prepare | required | A function to be called with an ssh connection to the droplet that will be used to generate a snapshot. Use it to install dependencies. |
token | required | A digital ocean token, used to create the droplets 😊 |
keypair | required | An object with {public, private} keys. Generated with keypair or rsa-json |
maxPoolSize | 2 | The 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. |
minPoolSize | 0 | The 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 | = workerSize | The 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:
name | default | description |
---|---|---|
debug | false | Set 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
.
name | default | description |
---|---|---|
debug | = parentOptions.debug | Set this to true to override the default value set in digitalNavy.run |
stdin | null | Set this to a Buffer or string to provide it as the stdin for the command being run |
License
MIT
0.0.1
8 years ago