Skip to content

Commit

Permalink
restructured cli with runAction, prep for start
Browse files Browse the repository at this point in the history
  • Loading branch information
James Halliday committed Oct 11, 2010
1 parent 9863e7e commit af2aaf2
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 51 deletions.
52 changes: 29 additions & 23 deletions bin/cli.js
Original file line number Original file line Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/env node #!/usr/bin/env node
var fs = require('fs'); var fs = require('fs');
var sys = require('sys'); var sys = require('sys');
var spawn = require('child_process').spawn;
var Deployer = require('../lib/setup/deploy'); var Deployer = require('../lib/setup/deploy');
var Hash = require('traverse/hash'); var Hash = require('traverse/hash');


Expand All @@ -24,44 +25,49 @@ var action = {
throw 'Usage: deploy [name] [directory] {options}'; throw 'Usage: deploy [name] [directory] {options}';
} }


sys.print('Deploying StackVM to ' + argv._[1] + '... '); runAction('Deploying StackVM to ' + argv._[1], function (cb) {
Deployer.deploy(Hash.merge(argv, { Deployer.deploy(Hash.merge(argv, {
name : argv._[0], name : argv._[0],
base : argv._[1], base : argv._[1],
done : function (err) { done : cb,
if (err) { }));
console.log('failed'); });
console.error('\n !!! '
+ (err.stack ? err.stack : err) + '\n'
);
}
else console.log('ok');
},
}));
}, },
undeploy : function () { undeploy : function () {
if (argv._.length == 0) { if (argv._.length == 0) {
throw 'Usage: undeploy [name] {options}'; throw 'Usage: undeploy [name] {options}';
} }


sys.print('Undeploying StackVM at ' + argv._[0] + '... '); runAction('Undeploying StackVM at ' + argv._[0], function (cb) {
Deployer.undeploy(argv._[0], function (err) { Deployer.undeploy(argv._[0], cb);
if (err) {
console.log('failed');
console.error('\n !!! '
+ (err.stack ? err.stack : err) + '\n'
);
}
else console.log('ok');
}); });
}, },
start : function () { start : function () {
var name = argv._.length ? argv._[0] : 'main'; var name = argv._.length ? argv._[0] : 'main';
runAction('Starting StackVM:' + name, function (cb) {
Config(function (err, config) {
if (err) { cb(err); return }
var dir = config.local[name];
console.log(' *** dir = ' + dir);
cb(null);
});
});
}, },
stop : function () { stop : function () {
}, },
}[cmd]; }[cmd];


function runAction (msg, cb) {
sys.print(msg + '... ');
cb(function (err) {
if (err) {
console.log('failed');
console.error('\n !!! ' + (err.stack ? err.stack : err) + '\n');
}
else console.log('ok')
});
}

if (action === undefined) { if (action === undefined) {
console.error('Undefined command ' + sys.inspect(cmd)); console.error('Undefined command ' + sys.inspect(cmd));
} }
Expand Down
9 changes: 5 additions & 4 deletions server.js → bin/server.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@ var User = require('./lib/models/user');
var Web = require('./lib/web'); var Web = require('./lib/web');
var Service = require('./lib/service'); var Service = require('./lib/service');


var port = Number(process.argv[2]) || 9000; var argv = require('argv').argv;
var port = parseInt(argv._[0], 10) || 9000;


var app = express.createServer(); var app = express.createServer();
app.use(express.staticProvider(__dirname + '/static')); app.use(express.staticProvider(__dirname + '/../static'));
app.use(express.cookieDecoder()); app.use(express.cookieDecoder());
app.use(express.bodyDecoder()); app.use(express.bodyDecoder());
app.use(express.session({ app.use(express.session({
store : new Cart({ dbFile : __dirname + '/data/sessions.db' }), store : new Cart({ dbFile : __dirname + '/../data/sessions.db' }),
secret : 'todo: set this in the stackvm site config with cli.js' secret : 'todo: set this in the stackvm site config with cli.js'
})); }));


Expand All @@ -35,7 +36,7 @@ app.configure('production', function () {
app.get('/js/dnode.js', require('dnode/web').route()); app.get('/js/dnode.js', require('dnode/web').route());


var users = User.fromHashes( var users = User.fromHashes(
JSON.parse(fs.readFileSync(__dirname + '/data/users.json')) JSON.parse(fs.readFileSync(__dirname + '/../data/users.json'))
); );


Web(app, users); Web(app, users);
Expand Down
45 changes: 26 additions & 19 deletions lib/setup/config.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -8,44 +8,51 @@ var mkdirP = require('./mkdir_p');


module.exports = function (cb) { module.exports = function (cb) {
var configDir = process.env.HOME + '/.config/stackvm'; var configDir = process.env.HOME + '/.config/stackvm';
var localFile = configDir + '/local.json'; var files = [ 'local', 'remote' ].reduce(function (acc, x) {
var remoteFile = configDir + '/remote.json'; acc[x] = configDir + '/' + x + '.json';
return acc;
}, {});


Step( Step(
function () { mkdirP(configDir, 0700, this) }, function () { mkdirP(configDir, 0700, this) },
function (err) { function (err) {
path.exists(localFile, this.parallel().bind(this, null)); Hash(files).forEach((function (file) {
path.exists(remoteFile, this.parallel().bind(this, null)); path.exists(file, this.parallel().bind(this, null));
}).bind(this));
}, },
function (err, lex, rex) { function (err) {
if (err) { cb(err); return } if (err) { cb(err); return }


var ex = Hash([ localFile, remoteFile ], [ lex, rex ]) this.parallel()(null);
.filter(function (x) { return !x });


ex.forEach((function (x, file) { Hash(Object.keys(files), [].slice.call(arguments, 1))
var ws = fs.createWriteStream(file, { mode : 0600 }); .filter(function (x) { return !x })
ws.on('close', this.parallel().bind(this, null)); .forEach((function (x, file) {
ws.write(JSON.stringify({})); var ws = fs.createWriteStream(file, { mode : 0600 });
ws.end(); ws.on('close', this.parallel().bind(this, null));
}).bind(this)); ws.write(JSON.stringify({}));

ws.end();
if (ex.length == 0) this(); }).bind(this))
;
}, },
function (err) { function (err) {
if (err) cb(err) if (err) cb(err)
else { else {
ConfigFile(localFile, this.parallel()); Hash(files).forEach((function (file) {
ConfigFile(remoteFile, this.parallel()); ConfigFile(file, this.parallel());
}).bind(this));
} }
}, },
function (err, local, remote) { function (err) {
if (err) cb(err) if (err) cb(err)
cb(null, { local : local, remote : remote }); else cb(null, Hash.zip(
Object.keys(files), [].slice.call(arguments, 1)
));
} }
); );
}; };


exports.ConfigFile = ConfigFile;
function ConfigFile (filename, cb) { function ConfigFile (filename, cb) {
var self = new EventEmitter; var self = new EventEmitter;
self.update = function (f) { self.update = function (f) {
Expand Down
4 changes: 2 additions & 2 deletions lib/setup/deploy.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ exports.deploy = function (opts) {
} }
}, },
function (err) { function (err) {
if (err) { cb(err); return } if (err) cb(err)
Config(function (err, config) { else Config(function (err, config) {
if (err) cb(err) if (err) cb(err)
else config.local else config.local
.update(function (data) { data[opts.name] = opts.base }) .update(function (data) { data[opts.name] = opts.base })
Expand Down
2 changes: 2 additions & 0 deletions lib/setup/mkdir_p.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ var fs = require('fs');


module.exports = function mkdirP (p, mode, f) { module.exports = function mkdirP (p, mode, f) {
var cb = f || function () {}; var cb = f || function () {};
if (p.charAt(0) != '/') { cb('Relative path: ' + p); return }

var ps = path.normalize(p).split('/'); var ps = path.normalize(p).split('/');
path.exists(p, function (exists) { path.exists(p, function (exists) {
if (exists) cb(null); if (exists) cb(null);
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Original file line Diff line number Diff line change
@@ -1,6 +1,6 @@
{ {
"name" : "stackvm", "name" : "stackvm",
"version" : "0.0.4", "version" : "0.0.5",
"descrption" : "Control virtual machines in a browser", "descrption" : "Control virtual machines in a browser",
"repository" : { "repository" : {
"type" : "git", "type" : "git",
Expand All @@ -20,6 +20,6 @@
"cart" : ">=1.0.6", "cart" : ">=1.0.6",
"step" : ">=0.0.3" "step" : ">=0.0.3"
}, },
"engine" : ["node >=0.2.0"], "engine" : [ "node >=0.2.0" ],
"bin" : { "stackvm" : "./server.js" } "bin" : { "stackvm" : "./bin/cli.js" }
} }

0 comments on commit af2aaf2

Please sign in to comment.