Skip to content

Commit

Permalink
refactored the setup service, moved into a component, added ES versio…
Browse files Browse the repository at this point in the history
…n check #73
  • Loading branch information
Spencer Alger committed May 6, 2014
1 parent 950cfcc commit 3835b38
Show file tree
Hide file tree
Showing 12 changed files with 197 additions and 117 deletions.
4 changes: 2 additions & 2 deletions src/kibana/components/config/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ define(function (require) {
module.constant('configFile', configFile);

// service for delivering config variables to everywhere else
module.service('config', function (Private, $rootScope, Notifier, kbnVersion, Promise, setup) {
module.service('config', function (Private, $rootScope, Notifier, kbnVersion, Promise, kbnSetup) {
var config = this;
var notify = new Notifier({
location: 'Config'
Expand Down Expand Up @@ -52,7 +52,7 @@ define(function (require) {
*/
config.init = _.once(function () {
notify.lifecycle('config init');
return setup.bootstrap().then(function getDoc() {
return kbnSetup().then(function getDoc() {
return doc.fetch().then(function initDoc(resp) {
if (!resp.found) return doc.doIndex({}).then(getDoc);
else {
Expand Down
9 changes: 8 additions & 1 deletion src/kibana/components/notify/_notifier.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,16 @@ define(function (require) {
// Track the groups managed by this logger
var groups = window[type + 'Groups'] = {};

return function (name, success) {
return function logger(name, success) {
var status;
var ret;

if (success === void 0) {
// start
groups[name] = now();
// function that can report on the success or failure of an op, and pass their value along
ret = function (val) { logger(name, true); return val; };
ret.failure = function (err) { logger(name, false); throw err; };
} else {
groups[name] = now() - (groups[name] || 0);
var time = ' in ' + groups[name].toFixed(2) + 'ms';
Expand Down Expand Up @@ -116,6 +121,8 @@ define(function (require) {
} else {
log('KBN: ' + name + (status ? ' - ' + status : ''));
}

return ret;
};
}

Expand Down
18 changes: 18 additions & 0 deletions src/kibana/components/setup/_setup_error.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
define(function (require) {
return function SetupErrorFactory(configFile) {
var _ = require('lodash');

function SetupError(template, err) {
// don't override other setup errors
if (err && err instanceof SetupError) return err;

var err2 = new Error(_.template(template, { configFile: configFile }));
if (err) {
err2.origError = err;
if (err.stack) err2.stack = err.stack;
}
return err2;
}
return SetupError;
};
});
31 changes: 31 additions & 0 deletions src/kibana/components/setup/setup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
define(function (require) {
var _ = require('lodash');
var $ = require('jquery');

require('notify/notify');

require('modules').get('kibana/services')
.service('kbnSetup', function (Private, Promise, Notifier, es, configFile) {
// setup steps
var checkForEs = Private(require('./steps/check_for_es'));
var checkEsVersion = Private(require('./steps/check_es_version'));
var checkForKibanaIndex = Private(require('./steps/check_for_kibana_index'));
var createKibanaIndex = Private(require('./steps/create_kibana_index'));

var notify = new Notifier({ location: 'Setup' });

return _.once(function () {
notify.lifecycle('bootstrap');

return checkForEs()
.then(checkEsVersion)
.then(checkForKibanaIndex)
.then(function (exists) {
if (!exists) return createKibanaIndex();
})
.finally(function () {
notify.lifecycle('bootstrap', true);
});
});
});
});
49 changes: 49 additions & 0 deletions src/kibana/components/setup/steps/check_es_version.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
define(function (require) {
return function CheckEsVersionFn(Private, es, configFile, Notifier) {
return function checkEsVersion() {
var notify = new Notifier({ location: 'Setup: Elasticsearch version check' });
var complete = notify.lifecycle('check es version');

var SetupError = Private(require('../_setup_error'));


// core expression for finding a version
var versionExp = '([\\d\\.]*\\d)(?:\\.\\w+)?';

/**
* Regular Expression to extract a version number from a string
* @type {RegExp}
*/
var versionRE = new RegExp(versionExp);

/**
* Regular Expression to extract a version range from a string
* @type {RegExp}
*/
var versionRangeRE = new RegExp(versionExp + '\\s*\\-\\s*' + versionExp);

var int = function (txt) {
var i = parseInt(txt, 10);
return (!i || isNaN(i)) ? 0 : i;
};

return es.info()
.then(function (info) {
var raw = info.version.number;
var sections = raw.split('-');
var someTypeOfBeta = sections.length > 1;
var all = sections.shift().split('.');
var major = int(all.shift());
var minor = int(all.shift());
var patch = int(all.shift());

var currentRelease = major === 1 && minor >= 1;
var betaPreRelease = major === 1 && minor === 0 && someTypeOfBeta;

if (currentRelease || betaPreRelease) return true;
else throw SetupError('Incompatible Elasticsearch version "' + raw + '". Expected version 1.1 or a 1.0-Beta release');
})
.then(complete, complete.failure);
};
};
});
17 changes: 17 additions & 0 deletions src/kibana/components/setup/steps/check_for_es.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
define(function (require) {
return function checkForEsFunction(Private, Notifier, es, configFile) {
var SetupError = Private(require('../_setup_error'));
var notify = new Notifier({ location: 'Setup: Elasticsearch check' });

return function checkForES() {
var complete = notify.lifecycle('es check');
return es.ping({
requestTimeout: 2000
})
.catch(function (err) {
throw new SetupError('Unable to connect to Elasticsearch at "<%= configFile.elasticsearch %>"', err);
})
.then(complete, complete.failure);
};
};
});
17 changes: 17 additions & 0 deletions src/kibana/components/setup/steps/check_for_kibana_index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
define(function (require) {
return function CheckForKibanaIndexFn(Private, es, Notifier, configFile) {
var SetupError = Private(require('../_setup_error'));
var notify = new Notifier({ location: 'Setup: Kibana index check' });

return function checkForKibana() {
var complete = notify.lifecycle('kibana index check');
return es.indices.exists({
index: configFile.kibanaIndex
})
.catch(function (err) {
throw new SetupError('Unable to check for Kibana index "<%= configFile.kibanaIndex %>"', err);
})
.then(complete, complete.failure);
};
};
});
44 changes: 44 additions & 0 deletions src/kibana/components/setup/steps/create_kibana_index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
define(function (require) {
return function CreateKibanaIndexFn(Private, es, configFile, Notifier) {
return function createKibanaIndex() {
var notify = new Notifier({ location: 'Setup: Kibana Index Creation' });
var complete = notify.lifecycle('kibana index creation');

var SetupError = Private(require('../_setup_error'));

return es.indices.create({
index: configFile.kibanaIndex,
body: {
settings: {
mappings: {
mappings: {
_source: {
enabled: false
},
properties: {
type: {
type: 'string',
index: 'not_analyzed'
}
}
}
}
}
}
})
.catch(function (err) {
throw new SetupError('Unable to create Kibana index "<%= configFile.kibanaIndex %>"', err);
})
.then(function () {
return es.cluster.health({
waitForStatus: 'yellow',
index: configFile.kibanaIndex
});
})
.catch(function (err) {
throw new SetupError('Waiting for Kibana index "<%= configFile.kibanaIndex %>" to come online failed', err);
})
.then(complete, complete.failure);
};
};
});
4 changes: 2 additions & 2 deletions src/kibana/controllers/kibana.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ define(function (require) {
});
});

modules.controller('kibana', function ($scope, Notifier, $injector, $q, $http, config, setup) {
modules.controller('kibana', function ($scope, Notifier, $injector, $q, $http, config, kbnSetup) {
var notify = new Notifier();

$scope.httpActive = $http.pendingRequests;
Expand Down Expand Up @@ -55,7 +55,7 @@ define(function (require) {
});

$q.all([
setup.bootstrap(),
kbnSetup(),
config.init()
]).then(function () {
$injector.invoke(function ($rootScope, courier, config, configFile, $timeout, $location, timefilter, globalState) {
Expand Down
1 change: 1 addition & 0 deletions src/kibana/require.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ require.config({
kibana: './index',
courier: './components/courier',
config: './components/config',
setup: './components/setup',
notify: './components/notify',
state_management: './components/state_management',

Expand Down
105 changes: 0 additions & 105 deletions src/kibana/services/setup.js

This file was deleted.

Loading

0 comments on commit 3835b38

Please sign in to comment.