Skip to content

Instantly share code, notes, and snippets.

@tsmith
Created December 29, 2010 13:04
Show Gist options
  • Save tsmith/758517 to your computer and use it in GitHub Desktop.
Save tsmith/758517 to your computer and use it in GitHub Desktop.
three minute cloud server illustration with node-control
/*global require */
// Illustration-only for a mailing list discussion - not a recommended pattern
// for production because of root usage. Untested.
// http://groups.google.com/group/nodejs/browse_thread/thread/b41ad5d727d8dff8
// usage example:
// node thisfile.js local server:create
// node thisfile.js local server:stop <IP>
// node thisfile.js local server:start <IP>
// or once cloudserver config is populated with one or more server IPs
// node thisfile.js cloudserver server:stop
// node thisfile.js cloudserver server:start
var cloudservers = require('cloudservers'),
control = require('control'),
task = control.task,
sys = require('sys');
function createServer(serverName, username, apiKey, callback) {
var options = {
name: serverName,
image: 49, // Ubuntu 10.04 (Lucid Lynx)
flavor: 1 // 256 server
};
cloudservers.setAuth({ username: username, apiKey: apiKey }, function () {
cloudservers.createServer(options, function (err, server) {
server.setWait({ status: 'ACTIVE' }, 5000, function () {
// Our server is now built and active, so we can install node.js on it
sys.puts('Your server ' + serverName + ' is now ready.');
sys.puts(' IP Address: ' + server.addresses.public);
sys.puts(' Root password: ' + server.adminPass);
if (callback) {
callback(server.addresses.public);
}
});
});
});
}
var commands = 'apt-get update' +
' && apt-get install git-core build-essential libssl-dev' +
' && cd /usr/src' +
' && git clone http://github.com/ry/node.git' +
' && cd node' +
' && ./configure' +
' && make' +
' && make install' +
' && curl http://npmjs.org/install.sh | sh' +
' && cd /usr/src' +
' && git clone git://github.com/Marak/hellonode.git' +
' && cd hellonode' +
' && nohup node server.js';
task('local', 'Configuration for local host', function () {
return control.hosts({}, [ '127.0.0.1' ]);
});
var config = {
user: 'root'
};
task('server:create', 'create a cloud server',
function (serverName, username, apiKey) {
var host;
username = username || 'your-username';
apiKey = apiKey || 'your-api-key';
createServer(serverName, username, apiKey, function (ip) {
host = control.hosts(config, [ip]).shift();
sys.puts('Running commands as ' + host.user + '@' + host.address);
host.ssh(commands);
});
});
task('server:start', 'start cloud server', function (host, ip) {
// Usually, after you procured a server, you would create a new config task
// with that ip (see cloudserver task following), but this allows passing
// in the ip of the remote host as an argument to the task.
if (ip) {
host = control.hosts(config, [ip]).shift();
}
host.ssh('cd /usr/src/hellonode && nohup node server.js');
});
// See note inside task above
task('cloudserver', 'Configuration for procured server(s)', function () {
return control.hosts(config, [ '<IP>' ]); // Enter actual IP(s) when known
});
task('server:stop', 'stop cloud server', function (host, ip) {
if (ip) {
host = control.hosts(config, [ip]).shift();
}
host.ssh('killall -9 node');
});
control.begin();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment