Skip to content

Commit

Permalink
Merge pull request #122 from kuzzleio/kuzzleCLI
Browse files Browse the repository at this point in the history
Kuzzle CLI
  • Loading branch information
scottinet committed Jan 12, 2016
2 parents f839eb6 + 2407d28 commit 299e016
Show file tree
Hide file tree
Showing 14 changed files with 304 additions and 655 deletions.
29 changes: 0 additions & 29 deletions app-perf.js

This file was deleted.

66 changes: 0 additions & 66 deletions app-start.js

This file was deleted.

7 changes: 7 additions & 0 deletions bin/README.md
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@

The Kuzzle service is launched by [kuzzle.js](kuzzle.js) command line.


```
$ kuzzle.js install
```

will install plugins declared in `config/defaultPlugins.json` and in `config/customPlugins.json`

```
$ kuzzle.js start
```
Expand Down
Empty file modified bin/kuzzle-disable.js
100644 → 100755
Empty file.
Empty file modified bin/kuzzle-enable.js
100644 → 100755
Empty file.
207 changes: 207 additions & 0 deletions bin/kuzzle-install.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,207 @@
#!/usr/bin/env node

var
fs = require('fs'),
path = require('path'),
childProcess = require('child_process'),
lockfile = require('proper-lockfile'),
_ = require('lodash'),
pathConfig = path.join(__dirname, '..', 'config');

var app = module.exports = function () {
var
pathDefaultPlugins = path.join(pathConfig, 'defaultPlugins.json'),
pathCustomPlugins = path.join(pathConfig, 'customPlugins.json'),
defaultPlugins,
customPlugins = {};

if (!childProcess.hasOwnProperty('execSync')) {
console.error('███ kuzzle-install: Make sure you\'re using Node version >= 0.12');
process.exit(1);
}

console.log('███ kuzzle-install: Starting plugins installation...');

/*
Prevents multiple plugin installations at the same time.
*/
lockfile.lock('./node_modules', {retries: 1000, minTimeout: 200, maxTimeout: 1000}, (err, release) => {
if (err) {
console.error('███ kuzzle-install: Unable to acquire lock: ', err);
process.exit(1);
}

try {
defaultPlugins = require(pathDefaultPlugins);
}
catch (err) {
console.error('███ kuzzle-install: Unable to load default plugin configuration: ', err);
process.exit(1);
}

if (fs.existsSync(pathCustomPlugins)) {
try {
customPlugins = require(pathCustomPlugins);
}
catch (err) {
console.error('███ kuzzle-install: Unable to load custom plugin configuration: ', err);
process.exit(1);
}
}

if (installPlugins.call(this, defaultPlugins)) {
try {
fs.writeFileSync(pathDefaultPlugins, JSON.stringify(defaultPlugins, null, 2));
}
catch (err) {
console.error('███ kuzzle-install: Unable to write the default plugin configuration file: ', err);
process.exit(1);
}
}

if (installPlugins.call(this, customPlugins, defaultPlugins)) {
try {
fs.writeFileSync(pathCustomPlugins, JSON.stringify(customPlugins, null, 2));
}
catch (err) {
console.error('███ kuzzle-install: Unable to write the custom plugin configuration file: ', err);
}
}

release();

console.log('███ kuzzle-install: Done');
});
};

/**
* Install given plugins
* @param plugins
* @param basePlugins
* @returns {boolean}
*/
function installPlugins(plugins, basePlugins) {
var
newInstalled = false,
pluginInstallId;

_.forEach(plugins, (plugin, name) => {
if (plugin.url) {
pluginInstallId = plugin.url;
}
else if (plugin.version) {
pluginInstallId = name + '@' + plugin.version;
}
else {
console.error('███ kuzzle-install: Plugin', name, 'has no version. The version is mandatory if there is no URL.');
process.exit(1);
}

if (!needInstall(name, pluginInstallId)) {
console.log('███ kuzzle-install: Plugin', name, 'is already installed. Skipping...');
return true;
}

console.log('███ kuzzle-install: Downloading plugin: ', name);
newInstalled = true;
npmInstall(pluginInstallId);
initConfig(plugin, name);
console.log('███ kuzzle-install: Plugin', name, 'downloaded');

// By default, when a new plugin is installed, the plugin is disabled
// If in customPlugins the `activated` flag is undefined, we get the `activated` flag from the default one
if (plugin.activated === undefined) {
plugin.activated = (basePlugins !== undefined && basePlugins[name] && basePlugins[name].activated === true);
}
});

return newInstalled;
}


/**
* Execute shell command
* @param command
*/
function sh(command) {
return childProcess.execSync(command).toString();
}

/**
* Install a plugin with NPM
* @param plugin
*/
function npmInstall(plugin) {
sh('npm install ' + plugin);
}

/**
* Initialize the config plugin
* @param plugin
* @param name
* @returns {boolean}
*/
function initConfig(plugin, name) {
var
pluginPackage;

try {
pluginPackage = require(path.join(getPathPlugin(name), 'package.json'));
}
catch (e) {
console.error(new InternalError('███ kuzzle-install: There is a problem with plugin ' + name + '. Check the plugin name'));
}

// If there is no information about plugin in the package.json
if (!pluginPackage.pluginInfo) {
return false;
}

plugin = _.extend(plugin, pluginPackage.pluginInfo);
}


/**
* Function for detect if the configured plugin must be installed
* If the plugin is configured with an url from GIT, the plugin is installed each time
* If the plugin come from NPM, the plugin is installed only if the version is different from the already installed
*
* @param name
* @param from previously installation information with version or git url with branch
* @returns {boolean} true if the plugin must be installed, false if not
*/
function needInstall(name, from) {
var
packageDefinition,
packagePath,
pluginPath = getPathPlugin(name);

// If we want to install a plugin with git, maybe there is no version and we want to 'pull' the plugin
if (from.indexOf('git') !== -1) {
return true;
}

if (!fs.existsSync(pluginPath)) {
return true;
}

packagePath = path.join(pluginPath, 'package.json');
if (!fs.existsSync(packagePath)) {
return true;
}

packageDefinition = require(path.join(pluginPath, 'package.json'));

// If version in package.json is different from the version the plugins.json, we want to install the updated plugin
return (packageDefinition._from !== from);
}

/**
* Return the real plugin path
* @param name
* @returns {String}
*/
function getPathPlugin (name) {
return path.join(__dirname, '..', 'node_modules', name);
}

14 changes: 0 additions & 14 deletions bin/kuzzle-perf.js

This file was deleted.

9 changes: 9 additions & 0 deletions bin/kuzzle-start.js
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,15 @@ var
rc = require('rc'),
kuzzle = require('../lib');

if (process.env.NEW_RELIC_APP_NAME) {
require('newrelic');
}

if (process.env.FEATURE_COVERAGE == 1) {
var coverage = require('istanbul-middleware');
console.log('Hook loader for coverage - ensure this is not production!');
coverage.hookLoader(__dirname+'/lib');
}

module.exports = function () {
console.log('Starting Kuzzle');
Expand Down
14 changes: 6 additions & 8 deletions bin/kuzzle.js
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,6 @@ program
.description('Start a Kuzzle instance')
.action(require('./kuzzle-start'));


// $ kuzzle perf
program
.command('perf')
.option('-p, --port [port]', 'Kuzzle port number', parseInt)
.description('Start a Kuzzle instance in performance recording mode')
.action(require('./kuzzle-perf'));

// $ kuzzle enable <service>
program
.command('enable')
Expand All @@ -37,5 +29,11 @@ program
.description('Disable a service without reloading Kuzzle')
.action(require('./kuzzle-disable'));

// $ kuzzle install
program
.command('install')
.description('Install plugin dependencies')
.action(require('./kuzzle-install'));

// Run user command
program.parse(process.argv);
Loading

0 comments on commit 299e016

Please sign in to comment.