Skip to content

Commit

Permalink
Initial Creation
Browse files Browse the repository at this point in the history
  • Loading branch information
Mark Cavage committed Jul 8, 2011
0 parents commit eb4ac6a
Show file tree
Hide file tree
Showing 40 changed files with 3,997 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -0,0 +1 @@
node_modules
19 changes: 19 additions & 0 deletions LICENSE
@@ -0,0 +1,19 @@
Copyright (c) 2011 Joyent, Inc., All rights reserved.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE
94 changes: 94 additions & 0 deletions README.md
@@ -0,0 +1,94 @@
node-smartdc is a node.js client library for interacting with the Joyent
SmartDataCenter API. This package additionally contains a CLI you can use
to write scripts encapsulating most common tasks.

## Installation

You probably want to install this package globally, so the CLI commands are
always in your path.

npm install smartdc -g

## Usage

### CLI

There are CLI commands corresponding to almost every action available in the
SmartDataCenter API; see the
[SmartDataCenter documentation][http://apidocs.joyent.com/napi/cloudapi/] for
complete information, but to get started, you'll want to run the following:

sdc-setup

The `sdc-setup` command will prompt you for your username and password, and
upload your SSH key. All the rest of the CLI commands use your RSA private
key for signing requests to the API, rather than sending your password to the
Joyent API. Once you've run `sdc-setup` (and set the environment variables
it indicates), you can provision a machine, and check it's status. For example,
here's an example that creates a new node.js machine and tags it as a
'test' machine, then you can grab the status a few times until it's `running`.

Note this assumes you've also got [jsontool][https://github.com/trentm/json]
installed:

sdc-createmachine -e nodejs -n demo -t group=test
...
sdc-listmachines | json 0.state
provisioning
sdc-listmachines | json 0.state
provisioning
sdc-listmachines | json 0.state
running

At that point, you can ssh into the machine; try this:

ssh-add
ssh -A admin@`./sdc-listmachines | json 0.ips[0]`

Note that we added your keys to the SSH agent, so that you can use the CLI
seamlessly on your new SmartMachine. Once you've played around and are done,
you can dispose of it; shut it down, then poll until it's `stopped`.

sdc-listmachines | json 0.id | xargs sdc-stopmachine
sdc-listmachines | json 0.state
stopped
sdc-listmachines | json 0.id | xargs sdc-deletemachine

There's a lot more you can do, like manage snapshots, analytics, keys, tags,
etc.

### Programmatic Usage

var fs = require('fs');
var smartdc = require('smartdc');

// Read in the SSH private key
var home = process.env.HOME;
var key = fs.readFileSync(home + '/.ssh/id_rsa', 'ascii');

var client = smartdc.createClient({
url: 'https://api.no.de',
key: key,
keyId: '/<your login here>/keys/id_rsa'
});

client.listMachines(function(err, machines) {
if (err) {
console.log('Unable to list machines: ' + e);
return;
}

machines.forEach(function(m) {
console.log('Machine: ' + JSON.stringify(m, null, 2));
});
});

Check out the source documentation for JSDocs on the API.

## License

MIT.

## Bugs

See <https://github.com/joyent/node-smartdc/issues>.
65 changes: 65 additions & 0 deletions bin/sdc-addmachinetags
@@ -0,0 +1,65 @@
#!/usr/bin/env node
// -*- mode: js -*-
// Copyright 2011 Joyent, Inc. All rights reserved.

var fs = require('fs');
var path = require('path');
var url = require('url');

var common = require('../lib/cli_common');



///--- Globals

var Options = {
"account": String,
"debug": Boolean,
"help": Boolean,
"identity": path,
"keyId": String,
"tag": [String, Array],
"url": url
};

var ShortOptions = {
"a": ["--account"],
"d": ["--debug"],
"h": ["--help"],
"?": ["--help"],
"i": ["--identity"],
"k": ["--keyId"],
"t": ["--tag"],
"u": ["--url"]
};

var usageStr = common.buildUsageString(Options);
usageStr += ' machine';


///--- Mainline

common.parseArguments(Options, ShortOptions, function(parsed) {

if (parsed.argv.remain.length < 1)
common.usage(usageStr, 1, 'machine required');

var opts = {};
if (parsed.name)
opts.name = parsed.name;
if (!parsed.tag)
common.usage(usageStr, 1, '--tag required');

var tags = {};
for (var i = 0; i < parsed.tag.length; i++) {
var tmp = parsed.tag[i].split('=');
if (!tmp || tmp.length !== 2) {
console.error(parsed.tag[i] + ' is an invalid tag; try foo=bar');
process.exit(1);
}
tags[tmp[0]] = tmp[1];
}

var client = common.newClient(parsed);
client.addMachineTags(parsed.argv.remain[0], tags, common.callback);
}, usageStr);
70 changes: 70 additions & 0 deletions bin/sdc-createinstrumentation
@@ -0,0 +1,70 @@
#!/usr/bin/env node
// -*- mode: js -*-
// Copyright 2011 Joyent, Inc. All rights reserved.

var fs = require('fs');
var path = require('path');
var url = require('url');

var common = require('../lib/cli_common');



///--- Globals

var Options = {
"account": String,
"clone": Number,
"debug": Boolean,
"decomposition": String,
"help": Boolean,
"identity": path,
"keyId": String,
"module": String,
"predicate": String,
"stat": String,
"url": url
};

var ShortOptions = {
"a": ["--account"],
"c": ["--clone"],
"d": ["--debug"],
"h": ["--help"],
"?": ["--help"],
"i": ["--identity"],
"k": ["--keyId"],
"m": ["--module"],
"n": ["--decomposition"],
"p": ["--predicate"],
"s": ["--stat"],
"u": ["--url"]
};


///--- Mainline

common.parseArguments(Options, ShortOptions, function(parsed) {
var opts = {};
if (parsed.module)
opts.module = parsed.module;

if (parsed.stat)
opts.stat = parsed.stat;

if (parsed.decomposition)
opts.decomposition = parsed.decomposition;

if (parsed.predicate)
opts.predicate = parsed.predicate;

if (parsed.clone)
opts.clone = parsed.clone;

var client = common.newClient(parsed);
client.createInstrumentation(opts, common.callback);
});




71 changes: 71 additions & 0 deletions bin/sdc-createkey
@@ -0,0 +1,71 @@
#!/usr/bin/env node
// -*- mode: js -*-
// Copyright 2011 Joyent, Inc. All rights reserved.

var fs = require('fs');
var path = require('path');
var url = require('url');

var common = require('../lib/cli_common');



///--- Globals

var Options = {
"account": String,
"debug": Boolean,
"help": Boolean,
"identity": path,
"keyId": String,
"name": String,
"url": url
};

var ShortOptions = {
"a": ["--account"],
"d": ["--debug"],
"h": ["--help"],
"?": ["--help"],
"i": ["--identity"],
"k": ["--keyId"],
"n": ["--name"],
"u": ["--url"]
};

var usageStr = common.buildUsageString(Options);
usageStr += ' public_ssh_key';


///--- Internal Functions

function loadNewKey(key) {
try {
return fs.readFileSync(key, 'ascii');
} catch(e) {
common.usage(usageStr, 2, 'Unable to load key ' + identity + ': ' + e);
}
}



///--- Mainline

common.parseArguments(Options, ShortOptions, function(parsed) {

if (parsed.argv.remain.length < 1)
common.usage(usageStr, 1, 'ssh_key required');

var opts = {
key: loadNewKey(parsed.argv.remain[0])
};
if (parsed.name) {
opts.name = parsed.name;
} else {
var name = parsed.argv.remain[0].split('/');
opts.name = name[name.length - 1];
}

var client = common.newClient(parsed);
client.createKey(opts, common.callback);
}, usageStr);
63 changes: 63 additions & 0 deletions bin/sdc-createmachine
@@ -0,0 +1,63 @@
#!/usr/bin/env node
// -*- mode: js -*-
// Copyright 2011 Joyent, Inc. All rights reserved.

var https = require('https');
var path = require('path');
var url = require('url');

var common = require('../lib/cli_common');



///--- Globals

var Options = {
"account": String,
"debug": Boolean,
"dataset": String,
"help": Boolean,
"identity": path,
"keyId": String,
"name": String,
"package": String,
"tag": [String, Array],
"url": url
};

var ShortOptions = {
"a": ["--account"],
"d": ["--debug"],
"e": ["--dataset"],
"h": ["--help"],
"?": ["--help"],
"i": ["--identity"],
"k": ["--keyId"],
"n": ["--name"],
"p": ["--package"],
"t": ["--tag"],
"u": ["--url"]
};


///--- Mainline

common.parseArguments(Options, ShortOptions, function(parsed) {
var opts = {}
if (parsed.dataset) opts.dataset = parsed.dataet;
if (parsed.name) opts.name = parsed.name;
if (parsed['package']) opts['package'] = parsed['package'];
if (parsed.tag) {
for (var i = 0; i < parsed.tag.length; i++) {
var tmp = parsed.tag[i].split('=');
if (!tmp || tmp.length !== 2) {
console.error(parsed.tag[i] + ' is an invalid tag; try foo=bar');
process.exit(1);
}
opts['tag.' + tmp[0]] = tmp[1];
}
}

var client = common.newClient(parsed);
client.createMachine(opts, common.callback);
});

0 comments on commit eb4ac6a

Please sign in to comment.