From 4cff3dbea91cde120a74adf0fdbe92b4e74d5914 Mon Sep 17 00:00:00 2001 From: Jonathan Budzenski Date: Tue, 21 Jun 2016 11:25:44 -0500 Subject: [PATCH] Backport PR #7457 --------- **Commit 1:** [build] Add data directory that plugins can write to. Closes #7157 * Original sha: 5b195343140663dd732ff12dff2d1dab1078b4e9 * Authored by Jonathan Budzenski on 2016-06-14T16:50:40Z **Commit 2:** [config] Add path.conf, path.data * Original sha: 16e8975f3a9cba463e12ecb1e557939448dfee75 * Authored by Jonathan Budzenski on 2016-06-16T16:17:51Z --- installedPlugins/{ => .data}/.empty | 0 src/cli/plugin/plugin.js | 3 +- src/cli/serve/serve.js | 3 +- src/server/config/schema.js | 5 ++++ src/server/path/__tests__/index.js | 15 ++++++++++ src/server/path/index.js | 31 +++++++++++++++++++++ tasks/build/installedPlugins.js | 1 + tasks/build/osPackages.js | 6 +++- tasks/build/package_scripts/post_install.sh | 2 ++ tasks/build/package_scripts/post_remove.sh | 4 +++ tasks/config/packages.js | 2 ++ 11 files changed, 69 insertions(+), 3 deletions(-) rename installedPlugins/{ => .data}/.empty (100%) create mode 100644 src/server/path/__tests__/index.js create mode 100644 src/server/path/index.js diff --git a/installedPlugins/.empty b/installedPlugins/.data/.empty similarity index 100% rename from installedPlugins/.empty rename to installedPlugins/.data/.empty diff --git a/src/cli/plugin/plugin.js b/src/cli/plugin/plugin.js index 7c2bce16a773b..4f2cdae726e3b 100644 --- a/src/cli/plugin/plugin.js +++ b/src/cli/plugin/plugin.js @@ -5,6 +5,7 @@ const installer = require('./plugin_installer'); const remover = require('./plugin_remover'); const lister = require('./plugin_lister'); const pluginLogger = require('./plugin_logger'); +const getConfig = require('../../server/path').getConfig; export default function pluginCli(program) { function processCommand(command, options) { @@ -43,7 +44,7 @@ export default function pluginCli(program) { .option( '-c, --config ', 'Path to the config file', - fromRoot('config/kibana.yml') + getConfig() ) .option( '-t, --timeout ', diff --git a/src/cli/serve/serve.js b/src/cli/serve/serve.js index b98f5b28a8e98..683a530117861 100644 --- a/src/cli/serve/serve.js +++ b/src/cli/serve/serve.js @@ -5,6 +5,7 @@ let { resolve } = require('path'); let cwd = process.cwd(); let src = require('requirefrom')('src'); let fromRoot = src('utils/fromRoot'); +const getConfig = require('../../server/path').getConfig; let canCluster; try { @@ -35,7 +36,7 @@ module.exports = function (program) { .option( '-c, --config ', 'Path to the config file, can be changed with the CONFIG_PATH environment variable as well', - process.env.CONFIG_PATH || fromRoot('config/kibana.yml')) + getConfig()) .option('-p, --port ', 'The port to bind to', parseInt) .option('-q, --quiet', 'Prevent all logging except errors') .option('-Q, --silent', 'Prevent all logging') diff --git a/src/server/config/schema.js b/src/server/config/schema.js index dc11a8ed6b1c5..daaa8a1da2853 100644 --- a/src/server/config/schema.js +++ b/src/server/config/schema.js @@ -6,6 +6,7 @@ let path = require('path'); let utils = require('requirefrom')('src/utils'); let fromRoot = utils('fromRoot'); const randomBytes = require('crypto').randomBytes; +const getData = require('../path').getData; module.exports = () => Joi.object({ pkg: Joi.object({ @@ -84,6 +85,10 @@ module.exports = () => Joi.object({ initialize: Joi.boolean().default(true) }).default(), + path: Joi.object({ + data: Joi.string().default(getData()) + }).default(), + optimize: Joi.object({ enabled: Joi.boolean().default(true), bundleFilter: Joi.string().default('!tests'), diff --git a/src/server/path/__tests__/index.js b/src/server/path/__tests__/index.js new file mode 100644 index 0000000000000..c709df29dc0e3 --- /dev/null +++ b/src/server/path/__tests__/index.js @@ -0,0 +1,15 @@ +import expect from 'expect.js'; +import path from '../'; +import { accessSync, R_OK} from 'fs'; + +describe('Default path finder', function () { + it('should find a kibana.yml', () => { + const configPath = path.getConfig(); + expect(() => accessSync(configPath, R_OK)).to.not.throwError(); + }); + + it('should find a data directory', () => { + const dataPath = path.getData(); + expect(() => accessSync(dataPath, R_OK)).to.not.throwError(); + }); +}); diff --git a/src/server/path/index.js b/src/server/path/index.js new file mode 100644 index 0000000000000..15ca518055632 --- /dev/null +++ b/src/server/path/index.js @@ -0,0 +1,31 @@ +import { accessSync, R_OK} from 'fs'; +import { find } from 'lodash'; +import fromRoot from '../../utils/fromRoot'; + +const CONFIG_PATHS = [ + process.env.CONFIG_PATH, + fromRoot('config/kibana.yml') +].filter(Boolean); + +const DATA_PATHS = [ + process.env.DATA_PATH, + fromRoot('installedPlugins/.data'), + '/var/lib/kibana' +].filter(Boolean); + +function findFile(paths) { + const availablePath = find(paths, configPath => { + try { + accessSync(configPath, R_OK); + return true; + } catch (e) { + //Check the next path + } + }); + return availablePath || paths[0]; +} + +export default { + getConfig: () => findFile(CONFIG_PATHS), + getData: () => findFile(DATA_PATHS) +}; diff --git a/tasks/build/installedPlugins.js b/tasks/build/installedPlugins.js index 7d8d6618ff488..5d2fcc4d4ed37 100644 --- a/tasks/build/installedPlugins.js +++ b/tasks/build/installedPlugins.js @@ -1,5 +1,6 @@ module.exports = function (grunt) { grunt.registerTask('_build:installedPlugins', function () { grunt.file.mkdir('build/kibana/installedPlugins'); + grunt.file.mkdir('build/kibana/installedPlugins/.data'); }); }; diff --git a/tasks/build/osPackages.js b/tasks/build/osPackages.js index 963bb85cf6a2d..4532ccb302b33 100644 --- a/tasks/build/osPackages.js +++ b/tasks/build/osPackages.js @@ -42,7 +42,10 @@ export default (grunt) => { '--template-value', `user=${config.user}`, '--template-value', `group=${config.group}`, '--template-value', `optimizeDir=${config.path.home}/optimize`, - '--template-value', `pluginsDir=${config.path.plugins}` + '--template-value', `pluginsDir=${config.path.plugins}`, + '--template-value', `dataDir=${config.path.data}`, + //uses relative path to --prefix, strip the leading / + '--exclude', `${config.path.home.slice(1)}/installedPlugins/.data` ]; const debOptions = [ '-t', 'deb', @@ -54,6 +57,7 @@ export default (grunt) => { ]; const args = [ `${buildDir}/=${config.path.home}/`, + `${buildDir}/installedPlugins/.data/=${config.path.data}/`, `${servicesByName.sysv.outputDir}/etc/=/etc/`, `${servicesByName.systemd.outputDir}/lib/=/lib/` ]; diff --git a/tasks/build/package_scripts/post_install.sh b/tasks/build/package_scripts/post_install.sh index beef49af21753..bc31f19f19953 100644 --- a/tasks/build/package_scripts/post_install.sh +++ b/tasks/build/package_scripts/post_install.sh @@ -15,3 +15,5 @@ if ! user_check "<%= user %>" ; then user_create "<%= user %>" fi chown -R <%= user %>:<%= group %> <%= optimizeDir %> +chown <%= user %>:<%= group %> <%= dataDir %> +chown <%= user %>:<%= group %> <%= pluginsDir %> diff --git a/tasks/build/package_scripts/post_remove.sh b/tasks/build/package_scripts/post_remove.sh index 5c2858e34ad3f..4e33c25aaa767 100644 --- a/tasks/build/package_scripts/post_remove.sh +++ b/tasks/build/package_scripts/post_remove.sh @@ -55,4 +55,8 @@ if [ "$REMOVE_DIRS" = "true" ]; then if [ -d "<%= pluginsDir %>" ]; then rm -rf "<%= pluginsDir %>" fi + + if [ -d "<%= dataDir %>" ]; then + rmdir --ignore-fail-on-non-empty "<%= dataDir %>" + fi fi diff --git a/tasks/config/packages.js b/tasks/config/packages.js index 66226e938df50..d7fa93c4f62f5 100644 --- a/tasks/config/packages.js +++ b/tasks/config/packages.js @@ -14,6 +14,7 @@ export default (grunt) => { const FOLDER_CONFIG = '/opt/kibana/config'; const FOLDER_HOME = '/opt/kibana'; + const FOLDER_DATA = '/var/lib/kibana'; const FOLDER_PLUGINS = `${FOLDER_HOME}/installedPlugins`; const FILE_KIBANA_CONF = `${FOLDER_CONFIG}/kibana.yml`; @@ -43,6 +44,7 @@ export default (grunt) => { version: VERSION, path: { conf: FOLDER_CONFIG, + data: FOLDER_DATA, plugins: FOLDER_PLUGINS, home: FOLDER_HOME, kibanaBin: FILE_KIBANA_BINARY,