2014-03-07 01:37:43 +01:00
|
|
|
module.exports = function (grunt) {
|
2015-01-14 22:46:04 +01:00
|
|
|
var maybeStartServer = function (options) {
|
|
|
|
return function () {
|
|
|
|
var http = require('http');
|
2015-01-21 19:16:14 +01:00
|
|
|
var opts = {
|
2015-01-14 22:46:04 +01:00
|
|
|
method: 'HEAD',
|
2015-07-27 12:54:08 +02:00
|
|
|
path: '/api/status',
|
2015-01-14 22:46:04 +01:00
|
|
|
host: 'localhost',
|
|
|
|
port: options.port
|
2015-01-21 19:16:14 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
grunt.log.debug('checking for server', JSON.stringify(opts));
|
|
|
|
|
|
|
|
var req = http.request(opts);
|
2014-03-07 01:37:43 +01:00
|
|
|
|
2015-08-01 01:33:01 +02:00
|
|
|
var done = (function (cb) {
|
|
|
|
return function (res) {
|
|
|
|
req.removeListener('error', onError);
|
|
|
|
req.removeListener('response', onResponse);
|
|
|
|
if (res) res.socket.destroy();
|
|
|
|
cb();
|
|
|
|
};
|
|
|
|
}(this.async()));
|
|
|
|
|
2015-01-14 22:46:04 +01:00
|
|
|
function onResponse(res) {
|
2015-01-21 19:16:14 +01:00
|
|
|
grunt.log.debug('Server responded with', res.statusCode);
|
2015-06-25 22:40:15 +02:00
|
|
|
if (res.statusCode === 200 && res.headers['x-app-name'] === 'kibana') {
|
2015-01-21 19:16:14 +01:00
|
|
|
grunt.log.ok('Kibana server already started on port', options.port);
|
2015-01-14 22:46:04 +01:00
|
|
|
} else {
|
2015-01-21 19:16:14 +01:00
|
|
|
grunt.log.error('Another server is already running on port', options.port);
|
2015-08-01 01:33:01 +02:00
|
|
|
process.exit(1); // eslint-disable-line no-process-exit
|
2015-01-14 22:46:04 +01:00
|
|
|
}
|
2015-08-01 01:33:01 +02:00
|
|
|
|
2015-01-14 22:46:04 +01:00
|
|
|
done(res);
|
2014-03-07 01:37:43 +01:00
|
|
|
}
|
|
|
|
|
2015-01-21 19:16:14 +01:00
|
|
|
function onError(err) {
|
|
|
|
if (err.code !== 'ECONNREFUSED') {
|
|
|
|
grunt.log.error('Kibana server check failed', err);
|
|
|
|
}
|
|
|
|
|
2015-01-14 22:46:04 +01:00
|
|
|
grunt.config.set(options.name, true);
|
|
|
|
grunt.task.run(options.tasks);
|
|
|
|
done();
|
|
|
|
}
|
2014-03-07 01:37:43 +01:00
|
|
|
|
2015-01-14 22:46:04 +01:00
|
|
|
req.on('error', onError);
|
|
|
|
req.on('response', onResponse);
|
|
|
|
req.end();
|
|
|
|
};
|
|
|
|
};
|
2015-01-21 19:16:14 +01:00
|
|
|
|
2015-08-07 20:49:56 +02:00
|
|
|
grunt.registerTask('maybeStartTestServer', maybeStartServer({
|
2015-01-14 22:46:04 +01:00
|
|
|
name: 'kibana-server',
|
2015-06-25 05:51:54 +02:00
|
|
|
port: grunt.option('port') || 5601,
|
2015-08-07 20:49:56 +02:00
|
|
|
tasks: ['run:testServer']
|
2015-01-14 22:46:04 +01:00
|
|
|
}));
|
|
|
|
};
|