diff --git a/src/core_plugins/kibana/index.js b/src/core_plugins/kibana/index.js index eaf49dcbbe3d6..516e8ce5e3d70 100644 --- a/src/core_plugins/kibana/index.js +++ b/src/core_plugins/kibana/index.js @@ -1,3 +1,5 @@ +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'; @@ -5,6 +7,8 @@ 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({ @@ -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); diff --git a/src/core_plugins/kibana/server/lib/manage_uuid.js b/src/core_plugins/kibana/server/lib/manage_uuid.js index b6c13550f92f5..aad9cd3431914 100644 --- a/src/core_plugins/kibana/server/lib/manage_uuid.js +++ b/src/core_plugins/kibana/server/lib/manage_uuid.js @@ -7,9 +7,7 @@ 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() { @@ -17,8 +15,14 @@ export default async function manageUuid(server) { 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; } } @@ -26,8 +30,10 @@ export default async function manageUuid(server) { 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; } }