Skip to content

Commit

Permalink
Closes elastic#2377 - Migrate config from > GA
Browse files Browse the repository at this point in the history
- Closes elastic#2377
- Added testing framework for server side tests
- Fixed jshint
- Added config migration
- Added framework for server initialization
  • Loading branch information
simianhacker committed Jan 23, 2015
1 parent caaca43 commit 2b98020
Show file tree
Hide file tree
Showing 11 changed files with 169 additions and 18 deletions.
17 changes: 9 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@
"dashboarding"
],
"private": false,
"version": "4.0.0-beta3",
"version": "4.0.0-rc1-snapshot",
"main": "src/server/app.js",
"homepage": "http://www.elasticsearch.org/overview/kibana/",
"bugs": "https://github.com/elasticsearch/kibana/issues",
"license": "Apache-2.0",
"bugs": {
"url": "https://github.com/elasticsearch/kibana/issues"
},
"license": "Apache 2.0",
"author": "Rashid Khan <rashid.khan@elasticsearch.com>",
"contributors": [
"Spencer Alger <spencer.alger@elasticsearch.com>",
Expand All @@ -35,12 +37,14 @@
},
"dependencies": {
"ansicolors": "^0.3.2",
"bluebird": "~2.0.7",
"body-parser": "~1.10.1",
"bunyan": "^1.2.3",
"commander": "^2.6.0",
"compression": "^1.3.0",
"cookie-parser": "~1.3.3",
"debug": "~2.1.1",
"elasticsearch": "^3.1.1",
"express": "~4.10.6",
"glob": "^4.3.2",
"http-proxy": "^1.8.1",
Expand All @@ -49,10 +53,10 @@
"less-middleware": "1.0.x",
"lodash": "^2.4.1",
"morgan": "~1.5.1",
"semver": "^4.2.0",
"serve-favicon": "~2.2.0"
},
"devDependencies": {
"bluebird": "~2.0.7",
"connect": "~2.19.5",
"event-stream": "~3.1.5",
"expect.js": "~0.3.1",
Expand All @@ -72,6 +76,7 @@
"grunt-run": "^0.2.3",
"grunt-s3": "~0.2.0-alpha.3",
"grunt-saucelabs": "~8.3.2",
"grunt-simple-mocha": "^0.4.0",
"html-entities": "^1.1.1",
"http-proxy": "~1.8.1",
"husky": "~0.6.0",
Expand All @@ -91,9 +96,5 @@
"rjs-build-analysis": "0.0.3",
"simple-git": "^0.11.0",
"tar": "^1.0.1"
},
"license": "Apache 2.0",
"bugs": {
"url": "https://github.com/elasticsearch/kibana/issues"
}
}
10 changes: 9 additions & 1 deletion src/server/config/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@ try {
public_folder = path.resolve(__dirname, '..', '..', 'kibana');
}

var packagePath = path.resolve(__dirname, '..', 'package.json');
try {
fs.statSync(packagePath);
} catch (err) {
packagePath = path.resolve(__dirname, '..', '..', '..', 'package.json');
}

var config = module.exports = {
port : kibana.port || 5601,
host : kibana.host || '0.0.0.0',
Expand All @@ -25,7 +32,8 @@ var config = module.exports = {
public_folder : public_folder,
external_plugins_folder : process.env.PLUGINS_FOLDER || null,
bundled_plugins_folder : path.resolve(public_folder, 'plugins'),
kibana : kibana
kibana : kibana,
package : require(packagePath)
};

config.plugins = listPlugins(config);
15 changes: 11 additions & 4 deletions src/server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ var app = require('./app');
var http = require('http');
var config = require('./config');
var logger = require('./lib/logger');
var Promise = require('bluebird');
var initialization = require('./lib/serverInitialization');


/**
Expand Down Expand Up @@ -50,13 +52,18 @@ function onListening() {
logger.info('Listening on %s:%d', address.address, address.port);
}

function start() {
var port = parseInt(process.env.PORT, 10) || config.port || 3000;
var host = process.env.HOST || config.host || '127.0.0.1';
var listen = Promise.promisify(server.listen.bind(server));
app.set('port', port);
return listen(port, host);
}

module.exports = {
server: server,
start: function (cb) {
var port = parseInt(process.env.PORT, 10) || config.port || 3000;
var host = process.env.HOST || config.host || '127.0.0.1';
app.set('port', port);
server.listen(port, host, cb);
return initialization().then(start).nodeify(cb);
}
};

Expand Down
30 changes: 30 additions & 0 deletions src/server/lib/isUpgradeable.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
var config = require('../config');
var semver = require('semver');
var rcVersionRegex = /(\d+\.\d+\.\d+)\-rc(\d+)/i;

module.exports = function (doc) {
if (/beta|snapshot/i.test(doc._id)) return false;
if (doc._id === config.package.version) return false;

var packageVersion, packageRcRelease, version, rcRelease;
packageRcRelease = rcRelease = Infinity;
var matches = doc._id.match(rcVersionRegex);
var packageMatches = config.package.version.match(rcVersionRegex);

if (matches) {
version = matches[1];
rcRelease = parseInt(matches[2], 10);
} else {
version = doc._id;
}

if (packageMatches) {
packageVersion = packageMatches[1];
packageRcRelease = parseInt(packageMatches[2], 10);
} else {
packageVersion = config.package.version;
}

if (semver.gte(version, packageVersion) && rcRelease >= packageRcRelease) return false;
return true;
};
29 changes: 29 additions & 0 deletions src/server/lib/migrateConfig.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
var config = require('../config');
var elasticsearch = require('elasticsearch');
var upgrade = require('./upgradeConfig');
var client = new elasticsearch.Client({
host: config.elasticsearch
});

module.exports = function () {
var options = {
index: '.kibana',
type: 'config',
body: {
size: 1000,
sort: [ { buildNum: { order: 'desc' } } ],
query: {
filtered: {
filter: {
bool: {
must_not: [ { query: { match: { _id: '@@version' } } } ]
}
}
}
}
}
};

return client.search(options).then(upgrade);
};

10 changes: 10 additions & 0 deletions src/server/lib/serverInitialization.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
var Promise = require('bluebird');
var migrateConfig = require('./migrateConfig');

module.exports = function () {
var tasks = [
migrateConfig()
];

return Promise.all(tasks);
};
28 changes: 28 additions & 0 deletions src/server/lib/upgradeConfig.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
var Promise = require('bluebird');
var isUpgradeable = require('./isUpgradeable');
var config = require('../config');
var _ = require('lodash');
var elasticsearch = require('elasticsearch');
var client = new elasticsearch.Client({
host: config.elasticsearch
});

module.exports = function (response) {
var newConfig = {};
// Check to see if there are any doc. If not then we can assume
// nothing needs to be done
if (response.hits.hits.length === 0) return Promise.resolve(null);

// Look for upgradeable configs. If none of them are upgradeable
// then resolve with null.
var body = _.find(response.hits.hits, isUpgradeable);
if (body) return Promise.resolve(null);

return client.create({
index: '.kibana',
type: 'config',
id: config.package.version,
body: body
});

};
1 change: 1 addition & 0 deletions tasks/config/jshint.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ module.exports = function (grunt) {
'Gruntfile.js',
'<%= root %>/tasks/**/*.js',
'<%= src %>/kibana/*.js',
'<%= src %>/server/*.js',
'<%= src %>/kibana/{components,directives,factories,filters,plugins,registry,services,utils}/**/*.js',
'<%= unitTestDir %>/**/*.js'
]
Expand Down
8 changes: 8 additions & 0 deletions tasks/config/simplemocha.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module.exports = {
options: {
timeout: 2000,
ignoreLeaks: false,
reporter: 'dot'
},
all: { src: ['<%= root %>/test/server/unit/**/*.js']}
};
12 changes: 7 additions & 5 deletions tasks/test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
var _ = require('lodash');
module.exports = function (grunt) {
function getTestTask() {
function addTestTask(tasks) {
var testTask = 'mocha:unit';

if (grunt.option('use-sauce') || process.env.TRAVIS) {
Expand All @@ -13,6 +13,8 @@ module.exports = function (grunt) {
}
}

tasks.push('simplemocha:all', testTask);

return testTask;
}

Expand All @@ -26,17 +28,17 @@ module.exports = function (grunt) {
'jshint',
'maybe_start_kibana',
'jade',
'less',
getTestTask()
'less'
];
addTestTask(tasks);
grunt.task.run(tasks);
});

grunt.registerTask('quick-test', function () {
var tasks = [
'maybe_start_kibana',
getTestTask()
'maybe_start_kibana'
];
addTestTask(tasks);
grunt.task.run(tasks);
});

Expand Down
27 changes: 27 additions & 0 deletions test/server/unit/lib/isUpgradeable.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
var isUpgradeable = require('../../../../src/server/lib/isUpgradeable');
var expect = require('expect.js');
var util = require('util');
var package = require('../../../../package.json');

describe('lib/isUpgradeable', function () {

function upgradeDoc(_id, version, bool) {
it(util.format('should return %s for %s <= %s', bool, _id, version), function () {
var doc = { _id: _id };
package.version = version;
expect(isUpgradeable(doc)).to.be(bool);
});
}

upgradeDoc('1.0.0-beta1', package.version, false);
upgradeDoc(package.version, package.version, false);
upgradeDoc('4.0.0-RC1', '4.0.0-RC2', true);
upgradeDoc('4.0.0-rc2', '4.0.0-rc1', false);
upgradeDoc('4.0.0-rc2', '4.0.0', true);
upgradeDoc('4.0.0-rc2', '4.0.2', true);
upgradeDoc('4.0.1', '4.1.0-rc', true);
upgradeDoc('4.0.0-rc1', '4.0.0', true);
upgradeDoc('4.0.0-rc1-snapshot', '4.0.0', false);
upgradeDoc('4.1.0-rc1-snapshot', '4.1.0-rc1', false);

});

0 comments on commit 2b98020

Please sign in to comment.