Created
November 26, 2015 16:25
-
-
Save davidrapin/65a68060cd08b5743531 to your computer and use it in GitHub Desktop.
Start/stop/status on multiple Neo4j instances
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * Start, stop or get the status of all Neo4j installations in current folder. | |
| * | |
| * Usage: | |
| * >node neo4js status | |
| * >node neo4js start | |
| * >node neo4js stop | |
| * Author: David Rapin | |
| * Date: 2015-11-26. | |
| * licence: MIT | |
| */ | |
| 'use strict'; | |
| var fs = require('fs'); | |
| var path = require('path'); | |
| var child = require('child_process'); | |
| // main | |
| var command = process.argv.length === 2 ? 'status' : process.argv[2]; | |
| getNeo4jCommand('./', command, function(err, states) { | |
| if (err) { | |
| return console.log(err); | |
| } | |
| var maxLen = 0; | |
| states.forEach(function(state) { | |
| maxLen = Math.max(maxLen, state.folder.length); | |
| }); | |
| states.forEach(function(state) { | |
| console.log(pad(state.folder, maxLen) + ': ' + state.value); | |
| }); | |
| }); | |
| // private functions follow | |
| function getNeo4jCommand(base, option, cb) { | |
| var root = path.resolve(base); | |
| fs.readdir(root, function (err, folders) { | |
| if (err) { return cb(err); } | |
| var expectedResults = folders.length; | |
| var results = []; | |
| folders.forEach(function(folder) { | |
| var folderPath = path.resolve(root, folder); | |
| fs.stat(folderPath, function (err, stat) { | |
| if (err) { return cb(err); } | |
| if (!stat.isDirectory()) { | |
| // not a folder | |
| expectedResults--; | |
| return; | |
| } | |
| fs.readdir(folderPath, function (err, subFiles) { | |
| if (!containsAll(subFiles, ['bin', 'conf', 'data', 'lib', 'plugins', 'system'])) { | |
| // not a neo4j folder | |
| expectedResults--; | |
| return; | |
| } | |
| var command = path.resolve(folderPath, 'bin'); | |
| child.exec('./neo4j ' + option, {cwd: command}, function (err, stdout, stderr) { | |
| // keeping only last line of output | |
| var outlines = stdout.trim().split('\n'); | |
| var state = outlines[outlines.length - 1]; | |
| results.push({folder:folder, value:state}); | |
| // we are done collecting all results | |
| if (results.length === expectedResults) { | |
| // sort results by folder name | |
| results.sort(function(r1, r2) { | |
| return r1.folder.localeCompare(r2.folder); | |
| }); | |
| cb(null, results); | |
| } | |
| }); | |
| }); | |
| }); | |
| }); | |
| }); | |
| } | |
| function containsAll(checked, references) { | |
| var ok = true; | |
| references.forEach(function(reference) { | |
| if (checked.indexOf(reference) < 0) { | |
| ok = false; | |
| } | |
| }); | |
| return ok; | |
| } | |
| function pad(str, len) { | |
| while (str.length < len) { str += ' '; } | |
| return str; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment