Permalink
Browse files

restructured cli with runAction, prep for start

  • Loading branch information...
1 parent 9863e7e commit af2aaf2f40f1b983acbff58a453e4e6bf68b72ad @substack substack committed Oct 11, 2010
Showing with 67 additions and 51 deletions.
  1. +29 −23 bin/cli.js
  2. +5 −4 { → bin}/server.js
  3. +26 −19 lib/setup/config.js
  4. +2 −2 lib/setup/deploy.js
  5. +2 −0 lib/setup/mkdir_p.js
  6. +3 −3 package.json
View
@@ -1,6 +1,7 @@
#!/usr/bin/env node
var fs = require('fs');
var sys = require('sys');
+var spawn = require('child_process').spawn;
var Deployer = require('../lib/setup/deploy');
var Hash = require('traverse/hash');
@@ -24,44 +25,49 @@ var action = {
throw 'Usage: deploy [name] [directory] {options}';
}
- sys.print('Deploying StackVM to ' + argv._[1] + '... ');
- Deployer.deploy(Hash.merge(argv, {
- name : argv._[0],
- base : argv._[1],
- done : function (err) {
- if (err) {
- console.log('failed');
- console.error('\n !!! '
- + (err.stack ? err.stack : err) + '\n'
- );
- }
- else console.log('ok');
- },
- }));
+ runAction('Deploying StackVM to ' + argv._[1], function (cb) {
+ Deployer.deploy(Hash.merge(argv, {
+ name : argv._[0],
+ base : argv._[1],
+ done : cb,
+ }));
+ });
},
undeploy : function () {
if (argv._.length == 0) {
throw 'Usage: undeploy [name] {options}';
}
- sys.print('Undeploying StackVM at ' + argv._[0] + '... ');
- Deployer.undeploy(argv._[0], function (err) {
- if (err) {
- console.log('failed');
- console.error('\n !!! '
- + (err.stack ? err.stack : err) + '\n'
- );
- }
- else console.log('ok');
+ runAction('Undeploying StackVM at ' + argv._[0], function (cb) {
+ Deployer.undeploy(argv._[0], cb);
});
},
start : function () {
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 () {
},
}[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) {
console.error('Undefined command ' + sys.inspect(cmd));
}
@@ -10,14 +10,15 @@ var User = require('./lib/models/user');
var Web = require('./lib/web');
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();
-app.use(express.staticProvider(__dirname + '/static'));
+app.use(express.staticProvider(__dirname + '/../static'));
app.use(express.cookieDecoder());
app.use(express.bodyDecoder());
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'
}));
@@ -35,7 +36,7 @@ app.configure('production', function () {
app.get('/js/dnode.js', require('dnode/web').route());
var users = User.fromHashes(
- JSON.parse(fs.readFileSync(__dirname + '/data/users.json'))
+ JSON.parse(fs.readFileSync(__dirname + '/../data/users.json'))
);
Web(app, users);
View
@@ -8,44 +8,51 @@ var mkdirP = require('./mkdir_p');
module.exports = function (cb) {
var configDir = process.env.HOME + '/.config/stackvm';
- var localFile = configDir + '/local.json';
- var remoteFile = configDir + '/remote.json';
+ var files = [ 'local', 'remote' ].reduce(function (acc, x) {
+ acc[x] = configDir + '/' + x + '.json';
+ return acc;
+ }, {});
Step(
function () { mkdirP(configDir, 0700, this) },
function (err) {
- path.exists(localFile, this.parallel().bind(this, null));
- path.exists(remoteFile, this.parallel().bind(this, null));
+ Hash(files).forEach((function (file) {
+ path.exists(file, this.parallel().bind(this, null));
+ }).bind(this));
},
- function (err, lex, rex) {
+ function (err) {
if (err) { cb(err); return }
- var ex = Hash([ localFile, remoteFile ], [ lex, rex ])
- .filter(function (x) { return !x });
+ this.parallel()(null);
- ex.forEach((function (x, file) {
- var ws = fs.createWriteStream(file, { mode : 0600 });
- ws.on('close', this.parallel().bind(this, null));
- ws.write(JSON.stringify({}));
- ws.end();
- }).bind(this));
-
- if (ex.length == 0) this();
+ Hash(Object.keys(files), [].slice.call(arguments, 1))
+ .filter(function (x) { return !x })
+ .forEach((function (x, file) {
+ var ws = fs.createWriteStream(file, { mode : 0600 });
+ ws.on('close', this.parallel().bind(this, null));
+ ws.write(JSON.stringify({}));
+ ws.end();
+ }).bind(this))
+ ;
},
function (err) {
if (err) cb(err)
else {
- ConfigFile(localFile, this.parallel());
- ConfigFile(remoteFile, this.parallel());
+ Hash(files).forEach((function (file) {
+ ConfigFile(file, this.parallel());
+ }).bind(this));
}
},
- function (err, local, remote) {
+ function (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) {
var self = new EventEmitter;
self.update = function (f) {
View
@@ -24,8 +24,8 @@ exports.deploy = function (opts) {
}
},
function (err) {
- if (err) { cb(err); return }
- Config(function (err, config) {
+ if (err) cb(err)
+ else Config(function (err, config) {
if (err) cb(err)
else config.local
.update(function (data) { data[opts.name] = opts.base })
View
@@ -3,6 +3,8 @@ var fs = require('fs');
module.exports = function mkdirP (p, mode, f) {
var cb = f || function () {};
+ if (p.charAt(0) != '/') { cb('Relative path: ' + p); return }
+
var ps = path.normalize(p).split('/');
path.exists(p, function (exists) {
if (exists) cb(null);
View
@@ -1,6 +1,6 @@
{
"name" : "stackvm",
- "version" : "0.0.4",
+ "version" : "0.0.5",
"descrption" : "Control virtual machines in a browser",
"repository" : {
"type" : "git",
@@ -20,6 +20,6 @@
"cart" : ">=1.0.6",
"step" : ">=0.0.3"
},
- "engine" : ["node >=0.2.0"],
- "bin" : { "stackvm" : "./server.js" }
+ "engine" : [ "node >=0.2.0" ],
+ "bin" : { "stackvm" : "./bin/cli.js" }
}

0 comments on commit af2aaf2

Please sign in to comment.