Skip to content

Commit

Permalink
Merge pull request elastic#8206 from tsullivan/kibana-monitoring-uuid…
Browse files Browse the repository at this point in the history
…-singlefile-5.x

[5.x] UUID: Change UUID file naming to ensure that multiple running instances of Kibana use separate data directory
  • Loading branch information
epixa authored Sep 9, 2016
2 parents 63768a2 + fd712ab commit 679af88
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 7 deletions.
16 changes: 16 additions & 0 deletions src/core_plugins/kibana/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import Promise from 'bluebird';
import { mkdirp as mkdirpNode } from 'mkdirp';
import manageUuid from './server/lib/manage_uuid';
import ingest from './server/routes/api/ingest';
import search from './server/routes/api/search';
import settings from './server/routes/api/settings';
import scripts from './server/routes/api/scripts';
import * as systemApi from './server/lib/system_api';

const mkdirp = Promise.promisify(mkdirpNode);

module.exports = function (kibana) {
const kbnBaseUrl = '/app/kibana';
return new kibana.Plugin({
Expand Down Expand Up @@ -85,6 +89,18 @@ module.exports = function (kibana) {
},
},

preInit: async function (server) {
try {
// Create the data directory (recursively, if the a parent dir doesn't exist).
// If it already exists, does nothing.
await mkdirp(server.config().get('path.data'));
} catch (err) {
server.log(['error', 'init'], err);
// Stop the server startup with a fatal error
throw err;
}
},

init: function (server, options) {
// uuid
manageUuid(server);
Expand Down
20 changes: 13 additions & 7 deletions src/core_plugins/kibana/server/lib/manage_uuid.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,33 @@ const FILE_ENCODING = 'utf8';

export default async function manageUuid(server) {
const config = server.config();
const serverPort = server.info.port;
const serverHostname = config.get('server.host');
const fileName = `${serverHostname}:${serverPort}`;
const fileName = 'uuid';
const uuidFile = pathJoin(config.get('path.data'), fileName);

async function detectUuid() {
const readFile = Promise.promisify(readFileCallback);
try {
const result = await readFile(uuidFile);
return result.toString(FILE_ENCODING);
} catch (e) {
return false;
} catch (err) {
if (err.code === 'ENOENT') {
// non-existant uuid file is ok
return false;
}
server.log(['error', 'read-uuid'], err);
// Note: this will most likely be logged as an Unhandled Rejection
throw err;
}
}

async function writeUuid(uuid) {
const writeFile = Promise.promisify(writeFileCallback);
try {
return await writeFile(uuidFile, uuid, { encoding: FILE_ENCODING });
} catch (e) {
return false;
} catch (err) {
server.log(['error', 'write-uuid'], err);
// Note: this will most likely be logged as an Unhandled Rejection
throw err;
}
}

Expand Down

0 comments on commit 679af88

Please sign in to comment.