Skip to content
This repository
Browse code

exec command

  • Loading branch information...
commit 765499300c5e3955a3e2086177c2154d98512d20 1 parent 40d0ce0
James Halliday authored March 06, 2012
73  bin/exec.js
... ...
@@ -0,0 +1,73 @@
  1
+#!/usr/bin/env node
  2
+var argv = require('../lib/argv');
  3
+var propagit = require('propagit');
  4
+var git = require('../lib/git');
  5
+
  6
+var EventEmitter = require('events').EventEmitter;
  7
+
  8
+var p = propagit(argv);
  9
+p.on('error', function (err) {
  10
+    console.dir(err);
  11
+});
  12
+
  13
+p.hub(function (hub) {
  14
+    var opts = {
  15
+        drone : argv.drone,
  16
+        drones : argv.drones,
  17
+        repo : argv.repo || git.repoName(),
  18
+        commit : argv.commit,
  19
+        command : argv._,
  20
+        env : argv.env || {},
  21
+        cwd : argv.cwd,
  22
+        once : true
  23
+    };
  24
+    if (!opts.repo) {
  25
+        console.error('specify --repo or navigate to a git repo');
  26
+        return;
  27
+    }
  28
+    if (!opts.commit) git.commit(function (err, commit) {
  29
+        if (err) {
  30
+            console.error(err);
  31
+            p.hub.close();
  32
+        }
  33
+        else {
  34
+            opts.commit = commit;
  35
+            spawn(hub, opts);
  36
+        }
  37
+    })
  38
+    else spawn(hub, opts);
  39
+});
  40
+
  41
+function spawn (hub, opts) {
  42
+    var em = new EventEmitter;
  43
+    hub.subscribe(em.emit.bind(em));
  44
+    em.on('ready', function () {
  45
+        hub.spawn(opts, function (err, procs) {
  46
+            if (err) {
  47
+                console.error(err)
  48
+                p.hub.close();
  49
+            }
  50
+            else {
  51
+                em.on('stdout', function (buf, proc) {
  52
+                    if (procs[proc.drone] !== proc.id) return;
  53
+                    console.log(
  54
+                        '[' + proc.drone + '#' + proc.id + '] '
  55
+                        + buf.replace(/\n$/, '')
  56
+                    );
  57
+                });
  58
+                em.on('stderr', function (buf, proc) {
  59
+                    if (procs[proc.drone] !== proc.id) return;
  60
+                    console.log(
  61
+                        '[' + proc.drone + '#' + proc.id + '] '
  62
+                        + buf.replace(/\n$/, '')
  63
+                    );
  64
+                });
  65
+                em.on('exit', function (code, sig, proc) {
  66
+                    if (procs[proc.drone] !== proc.id) return;
  67
+                    console.log('(' + proc.drone + '#' + proc.id + ' exited)');
  68
+                    p.hub.close();
  69
+                });
  70
+            }
  71
+        });
  72
+    });
  73
+}
1  bin/hub.js
@@ -22,6 +22,7 @@ prop.use(function (service, conn) {
22 22
                 delete subs[conn.id];
23 23
             });
24 24
             subs[conn.id] = emit;
  25
+            emit('ready');
25 26
         }
26 27
     };
27 28
 });
4  bin/monitor.js
@@ -15,7 +15,9 @@ p.hub.on('up', function (hub) {
15 15
     });
16 16
     
17 17
     em.on('stdout', function (buf, opts) {
18  
-        console.dir([ 'stdout', buf, opts ]);
  18
+        if (opts) {
  19
+            console.dir([ 'stdout', buf, opts ]);
  20
+        }
19 21
     });
20 22
     
21 23
     hub.subscribe(em.emit.bind(em));
7  package.json
... ...
@@ -1,7 +1,7 @@
1 1
 {
2 2
     "name" : "fleet",
3 3
     "description" : "command a cluster of processes ",
4  
-    "version" : "0.0.1",
  4
+    "version" : "0.0.2",
5 5
     "repository" : {
6 6
         "type" : "git",
7 7
         "url" : "git://github.com/substack/fleet.git"
@@ -16,7 +16,8 @@
16 16
         "fleet-ps" : "bin/ps.js",
17 17
         "fleet-monitor" : "bin/monitor.js",
18 18
         "fleet-spawn" : "bin/spawn.js",
19  
-        "fleet-stop" : "bin/stop.js"
  19
+        "fleet-stop" : "bin/stop.js",
  20
+        "fleet-exec" : "bin/exec.js"
20 21
     },
21 22
     "keywords" : [
22 23
         "cluster",
@@ -32,7 +33,7 @@
32 33
         "test" : "tap test/*.js"
33 34
     },
34 35
     "dependencies" : {
35  
-        "propagit" : "~0.4.10",
  36
+        "propagit" : "~0.5.0",
36 37
         "optimist" : "~0.3.1",
37 38
         "mkdirp" : "~0.3.0"
38 39
     },

0 notes on commit 7654993

Please sign in to comment.
Something went wrong with that request. Please try again.