forked from elastic/kibana
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request elastic#8014 from tsullivan/uuid-rewrite-datafile
uuid: rewrite using path.data file instead of .kibana index
- Loading branch information
Showing
8 changed files
with
95 additions
and
108 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import uuid from 'node-uuid'; | ||
import Promise from 'bluebird'; | ||
import { join as pathJoin } from 'path'; | ||
import { readFile as readFileCallback, writeFile as writeFileCallback } from 'fs'; | ||
|
||
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 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; | ||
} | ||
} | ||
|
||
async function writeUuid(uuid) { | ||
const writeFile = Promise.promisify(writeFileCallback); | ||
try { | ||
return await writeFile(uuidFile, uuid, { encoding: FILE_ENCODING }); | ||
} catch (e) { | ||
return false; | ||
} | ||
} | ||
|
||
// detect if uuid exists already from before a restart | ||
const logToServer = (msg) => server.log(['server', 'uuid', fileName], msg); | ||
const dataFileUuid = await detectUuid(); | ||
let serverConfigUuid = config.get('server.uuid'); // check if already set in config | ||
|
||
if (dataFileUuid) { | ||
// data uuid found | ||
if (serverConfigUuid === dataFileUuid) { | ||
// config uuid exists, data uuid exists and matches | ||
logToServer(`Kibana instance UUID: ${dataFileUuid}`); | ||
return; | ||
} | ||
|
||
if (!serverConfigUuid) { | ||
// config uuid missing, data uuid exists | ||
serverConfigUuid = dataFileUuid; | ||
logToServer(`Resuming persistent Kibana instance UUID: ${serverConfigUuid}`); | ||
config.set('server.uuid', serverConfigUuid); | ||
return; | ||
} | ||
|
||
if (serverConfigUuid !== dataFileUuid) { | ||
// config uuid exists, data uuid exists but mismatches | ||
logToServer(`Updating Kibana instance UUID to: ${serverConfigUuid} (was: ${dataFileUuid})`); | ||
return writeUuid(serverConfigUuid); | ||
} | ||
} | ||
|
||
// data uuid missing | ||
|
||
if (!serverConfigUuid) { | ||
// config uuid missing | ||
serverConfigUuid = uuid.v4(); | ||
config.set('server.uuid', serverConfigUuid); | ||
} | ||
|
||
logToServer(`Setting new Kibana instance UUID: ${serverConfigUuid}`); | ||
return writeUuid(serverConfigUuid); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters