Repository: couchdb-nmo
Updated Branches:
  refs/heads/master 7b9bf5e5e -> 0d89e6389


Check if a cluster is online

Add support to do `nmo isonline mycluster` and it will check whether all
nodes are online.


Project: http://git-wip-us.apache.org/repos/asf/couchdb-nmo/repo
Commit: http://git-wip-us.apache.org/repos/asf/couchdb-nmo/commit/0d89e638
Tree: http://git-wip-us.apache.org/repos/asf/couchdb-nmo/tree/0d89e638
Diff: http://git-wip-us.apache.org/repos/asf/couchdb-nmo/diff/0d89e638

Branch: refs/heads/master
Commit: 0d89e6389a14a1c0477b60532c792f4fef5e8e43
Parents: 7b9bf5e
Author: Garren Smith <[email protected]>
Authored: Thu Sep 3 17:32:50 2015 +0200
Committer: Garren Smith <[email protected]>
Committed: Mon Sep 7 14:58:38 2015 +0200

----------------------------------------------------------------------
 doc/api/nmo-isonline.md |  6 ++++
 doc/cli/nmo-isonline.md |  6 ++++
 src/isonline.js         | 13 +++++++
 test/cluster.js         |  6 ++--
 test/config.js          | 13 ++++---
 test/isonline.js        | 80 +++++++++++++++++++++++++++++++++++++++++---
 6 files changed, 110 insertions(+), 14 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/couchdb-nmo/blob/0d89e638/doc/api/nmo-isonline.md
----------------------------------------------------------------------
diff --git a/doc/api/nmo-isonline.md b/doc/api/nmo-isonline.md
index f896a34..03f1e36 100644
--- a/doc/api/nmo-isonline.md
+++ b/doc/api/nmo-isonline.md
@@ -4,16 +4,22 @@ nmo-isonline(3) -- check if a cluster node is online
 ## SYNOPSIS
 
     nmo.commands.isonline(url, [url], ...)
+    nmo.commands.isonline(cluster,...)
 
 
 ## DESCRIPTION
 
 Check if nodes are online / available on the current network.
 
+url:
 The command takes multiple url arguments for checking multiple nodes
 at once. The url must be a `String`. The last argument must be an
 `Object` providing options.
 
+cluster:
+The command looks up the nodes defined in the config file for a given cluster
+and will check whether is node is online
+
 The command returns a promise which will return an `Object` where the
 keys are the provided urls and the values have the type `Boolean`.
 `true` indicates an online, available node.

http://git-wip-us.apache.org/repos/asf/couchdb-nmo/blob/0d89e638/doc/cli/nmo-isonline.md
----------------------------------------------------------------------
diff --git a/doc/cli/nmo-isonline.md b/doc/cli/nmo-isonline.md
index 89d97e5..9088cf4 100644
--- a/doc/cli/nmo-isonline.md
+++ b/doc/cli/nmo-isonline.md
@@ -4,11 +4,17 @@ nmo-isonline(1) -- check if a cluster node is online
 ## SYNOPSIS
 
     nmo isonline <url> [<url>, <url> ...] [--json]
+    nmo isonline <cluster> [--json]
 
 
 ## DESCRIPTION
 
+  <url>:
 Check if one or several nodes are currently online or available.
 
+  <cluster>:
+If a cluster is defined in the nmo config file it will then check that each 
node
+in that cluster is online
+
 It will print the result as colored output. JSON output is also
 supported by passing the `--json` flag.

http://git-wip-us.apache.org/repos/asf/couchdb-nmo/blob/0d89e638/src/isonline.js
----------------------------------------------------------------------
diff --git a/src/isonline.js b/src/isonline.js
index 804087b..a3a88aa 100644
--- a/src/isonline.js
+++ b/src/isonline.js
@@ -15,6 +15,10 @@ function isOnlineCli (...urls) {
       return reject(err);
     }
 
+    if (urls.length === 1 && !utils.isUri(urls[0])) {
+      urls = getClusterUrls(urls[0]);
+    }
+
     isonline.apply(isonline, urls)
       .then((results) => {
         const jsonOut = nmo.config.get('json');
@@ -37,6 +41,15 @@ function isOnlineCli (...urls) {
   });
 }
 
+export function getClusterUrls (clusterName) {
+  const nodes = nmo.config.get(clusterName);
+  if (!nodes) {
+    const err = new Error('Cluster does not exist');
+    err.type = 'EUSAGE';
+    throw err;
+  }
+  return Object.keys(nodes).map(key => nodes[key]);
+}
 
 export default isonline;
 function isonline (...args) {

http://git-wip-us.apache.org/repos/asf/couchdb-nmo/blob/0d89e638/test/cluster.js
----------------------------------------------------------------------
diff --git a/test/cluster.js b/test/cluster.js
index e583c29..950b412 100644
--- a/test/cluster.js
+++ b/test/cluster.js
@@ -10,8 +10,8 @@ import * as common from './common.js';
 import fs from 'fs';
 
 const data = `[clusterone]
-node0=127.0.0.1
-node1=192.168.0.1
+node0=http://127.0.0.1
+node1=http://192.168.0.1
 
 [onenodecluster]
 node1=iamalonelylnode
@@ -55,7 +55,7 @@ lab.experiment('cluster - get', () => {
       cluster
         .get('clusterone', 'node0')
         .then((res) => {
-          assert.equal(res, '127.0.0.1');
+          assert.equal(res, 'http://127.0.0.1');
           done();
         });
     });

http://git-wip-us.apache.org/repos/asf/couchdb-nmo/blob/0d89e638/test/config.js
----------------------------------------------------------------------
diff --git a/test/config.js b/test/config.js
index a1e6a8f..19df52d 100644
--- a/test/config.js
+++ b/test/config.js
@@ -11,8 +11,8 @@ import fs from 'fs';
 
 
 const data = `[clusterone]
-node0=127.0.0.1
-node1=192.168.0.1
+node0=http://127.0.0.1
+node1=http://192.168.0.1
 
 [gang]
 rocko=artischocko
@@ -29,8 +29,8 @@ const jsonData = {
     "mussman": "dermussmaen"
   },
   "clusterone": {
-    "node0": "127.0.0.1",
-    "node1": "192.168.0.1"
+    "node0": "http://127.0.0.1";,
+    "node1": "http://192.168.0.1";
   }
 };
 
@@ -74,7 +74,7 @@ lab.experiment('config', () => {
         .then((res) => {
           config.set('cluster1337', 'node1337', '192.168.133.7').then((e) => {
             const c = ini.parse(fs.readFileSync(__dirname + 
'/fixtures/randomini', 'utf-8'));
-            assert.equal(c.clusterone.node1, '192.168.0.1');
+            assert.equal(c.clusterone.node1, 'http://192.168.0.1');
             assert.equal(c.gang.rocko, 'artischocko');
             assert.equal(c.cluster1337.node1337, '192.168.133.7');
             done();
@@ -287,7 +287,7 @@ lab.experiment('config', () => {
         .then((res) => {
           config.cli('set', 'cluster1337', 'node1337', 
'192.168.133.7').then((e) => {
             const c = ini.parse(fs.readFileSync(__dirname + 
'/fixtures/randomini', 'utf-8'));
-            assert.equal(c.clusterone.node1, '192.168.0.1');
+            assert.equal(c.clusterone.node1, 'http://192.168.0.1');
             assert.equal(c.gang.rocko, 'artischocko');
             assert.equal(c.cluster1337.node1337, '192.168.133.7');
             done();
@@ -310,4 +310,3 @@ lab.experiment('config', () => {
   });
 
 });
-

http://git-wip-us.apache.org/repos/asf/couchdb-nmo/blob/0d89e638/test/isonline.js
----------------------------------------------------------------------
diff --git a/test/isonline.js b/test/isonline.js
index 14fac85..aa9cc5a 100644
--- a/test/isonline.js
+++ b/test/isonline.js
@@ -2,12 +2,12 @@ import assert from 'assert';
 import Lab from 'lab';
 
 import isonline from '../src/isonline.js';
-import {cli} from '../src/isonline.js';
+import {cli, getClusterUrls } from '../src/isonline.js';
 
 import * as common from './common.js';
-import log from 'npmlog';
 import nmo from '../src/nmo.js';
 
+import nock from 'nock';
 
 export let lab = Lab.script();
 const oldConsole = console.log;
@@ -41,8 +41,60 @@ lab.experiment('isonline', () => {
           done();
         });
     });
-  });
 
+    lab.test('executes correct url for cluster name', (done) => {
+      nock('http://127.0.0.1')
+        .get('/')
+        .reply(200);
+
+      nock('http://192.168.0.1')
+        .get('/')
+        .reply(200);
+
+      nmo
+        .load({nmoconf: __dirname + '/fixtures/randomini', json: true})
+        .then(() => {
+          return cli('clusterone');
+        }).then(res => {
+          assert.deepEqual(res, {'http://127.0.0.1': true, 
'http://192.168.0.1': true });
+          done();
+        });
+    });
+
+    lab.test('still executes for urls', (done) => {
+     nock('http://127.0.0.1')
+       .get('/')
+       .reply(200);
+
+     nmo
+       .load({nmoconf: __dirname + '/fixtures/randomini', json: true})
+       .then(() => {
+         return cli('http://127.0.0.1');
+       }).then(res => {
+         assert.deepEqual(res, {'http://127.0.0.1': true });
+         done();
+       });
+     });
+
+     lab.test('executes correct for multiple urls', (done) => {
+       nock('http://127.0.0.1')
+         .get('/')
+         .reply(200);
+
+       nock('http://192.168.0.1')
+         .get('/')
+         .reply(200);
+
+       nmo
+         .load({nmoconf: __dirname + '/fixtures/randomini', json: true})
+         .then(() => {
+           return cli('http://127.0.0.1', 'http://192.168.0.1');
+         }).then(res => {
+           assert.deepEqual(res, {'http://127.0.0.1': true, 
'http://192.168.0.1': true });
+           done();
+         });
+     });
+  });
 
   lab.experiment('api', () => {
     lab.beforeEach((done) => {
@@ -62,6 +114,26 @@ lab.experiment('isonline', () => {
       });
     });
 
+    lab.test('getClustersUrl returns correct urls', (done) => {
+      nmo
+        .load({nmoconf: __dirname + '/fixtures/randomini'})
+        .then(() => {
+          const urls = getClusterUrls('clusterone');
+          assert.deepEqual(['http://127.0.0.1', 'http://192.168.0.1'], urls);
+          done();
+        });
+    });
+
+    lab.test("getClustersUrl throws an error if the cluster doesn't exist", 
(done) => {
+        try {
+          getClusterUrls('doesnt-exist');
+        } catch(e) {
+          assert.ok(/Cluster does not exist/.test(e.message));
+          done();
+        }
+
+    });
+
     lab.test('returns error for all other errors', (done) => {
       isonline({})
         .catch((err) => {
@@ -138,7 +210,7 @@ lab.experiment('isonline', () => {
         assert.ok(/online/.test(args[1]), 'returns online for online nodes');
         done();
       };
-      nmo.load({nmoconf: __dirname + '/fixtures/randomini'})
+      nmo.load({nmoconf: __dirname + '/fixtures/randomini', json: false})
         .then(() => {
           cli(common.NODE);
         });

Reply via email to