Skip to content

Instantly share code, notes, and snippets.

@FireBurn
Created December 14, 2017 10:50
Show Gist options
  • Save FireBurn/2ecaaff4c40a6dd9bb865f732a9754a7 to your computer and use it in GitHub Desktop.
Save FireBurn/2ecaaff4c40a6dd9bb865f732a9754a7 to your computer and use it in GitHub Desktop.
Kibana 6.1 Optimize Backport
From 1bad14b88184e6e0ceb6f416b7a0f265f56e9cba Mon Sep 17 00:00:00 2001
From: Mike Lothian <[email protected]>
Date: Thu, 14 Dec 2017 10:44:05 +0000
Subject: [PATCH] Retarget branch optimize-task-in-kibana-plugin-cli on 6.1
---
src/cli_plugin/cli.js | 2 ++
src/cli_plugin/install/index.js | 5 ++++
src/cli_plugin/install/kibana.js | 7 +++++
src/cli_plugin/install/settings.js | 3 +-
src/cli_plugin/optimize/__tests__/index.js | 41 ++++++++++++++++++++++++++
src/cli_plugin/optimize/__tests__/settings.js | 42 +++++++++++++++++++++++++++
src/cli_plugin/optimize/index.js | 35 ++++++++++++++++++++++
src/cli_plugin/optimize/optimize.js | 16 ++++++++++
src/cli_plugin/optimize/settings.js | 10 +++++++
9 files changed, 160 insertions(+), 1 deletion(-)
create mode 100644 src/cli_plugin/optimize/__tests__/index.js
create mode 100644 src/cli_plugin/optimize/__tests__/settings.js
create mode 100644 src/cli_plugin/optimize/index.js
create mode 100644 src/cli_plugin/optimize/optimize.js
create mode 100644 src/cli_plugin/optimize/settings.js
diff --git a/src/cli_plugin/cli.js b/src/cli_plugin/cli.js
index bd7023d63..cea8ef1a9 100644
--- a/src/cli_plugin/cli.js
+++ b/src/cli_plugin/cli.js
@@ -3,6 +3,7 @@ import { pkg } from '../utils';
import Command from '../cli/command';
import listCommand from './list';
import installCommand from './install';
+import optimizeCommand from './optimize';
import removeCommand from './remove';
const argv = process.env.kbnWorkerArgv ? JSON.parse(process.env.kbnWorkerArgv) : process.argv.slice();
@@ -17,6 +18,7 @@ program
listCommand(program);
installCommand(program);
+optimizeCommand(program);
removeCommand(program);
program
diff --git a/src/cli_plugin/install/index.js b/src/cli_plugin/install/index.js
index 0d72fe01c..68ae9fe56 100644
--- a/src/cli_plugin/install/index.js
+++ b/src/cli_plugin/install/index.js
@@ -43,6 +43,11 @@ export default function pluginInstall(program) {
'path to the directory where plugins are stored (DEPRECATED, known to not work for all plugins)',
fromRoot('plugins')
)
+ .option(
+ '--skip-optimize',
+ 'avoid the mandatory optimization after each plugin installation. \n' +
+ 'Require a manual "kibana-plugin optimize" command to update bundle and reload the available plugins list'
+ )
.description('install a plugin',
`Common examples:
install x-pack
diff --git a/src/cli_plugin/install/kibana.js b/src/cli_plugin/install/kibana.js
index d14f9a83d..e7375481e 100644
--- a/src/cli_plugin/install/kibana.js
+++ b/src/cli_plugin/install/kibana.js
@@ -18,6 +18,13 @@ export function existingInstall(settings, logger) {
}
export async function rebuildCache(settings, logger) {
+
+ if (settings.skipOptimize) {
+ logger.log('Skip optimize task due to given settings (--skip-optimize). ' +
+ 'Remember to run a dedicated optimization task before starting Kibana !');
+ return;
+ }
+
logger.log('Optimizing and caching browser bundles...');
const serverConfig = _.merge(
readYamlConfig(settings.config),
diff --git a/src/cli_plugin/install/settings.js b/src/cli_plugin/install/settings.js
index 628ae5b72..c60b7560e 100644
--- a/src/cli_plugin/install/settings.js
+++ b/src/cli_plugin/install/settings.js
@@ -29,7 +29,8 @@ export function parse(command, options, kbnPackage) {
config: options.config || '',
plugin: command,
version: kbnPackage.version,
- pluginDir: options.pluginDir || ''
+ pluginDir: options.pluginDir || '',
+ skipOptimize: options.skipOptimize || false
};
settings.urls = generateUrls(settings);
diff --git a/src/cli_plugin/optimize/__tests__/index.js b/src/cli_plugin/optimize/__tests__/index.js
new file mode 100644
index 000000000..daadb16a1
--- /dev/null
+++ b/src/cli_plugin/optimize/__tests__/index.js
@@ -0,0 +1,41 @@
+import expect from 'expect.js';
+import sinon from 'sinon';
+import index from '../index';
+
+describe('kibana cli', function () {
+
+ describe('plugin installer', function () {
+
+ describe('commander options', function () {
+
+ const program = {
+ command: function () { return program; },
+ description: function () { return program; },
+ option: function () { return program; },
+ action: function () { return program; }
+ };
+
+ it('should define the command', function () {
+ sinon.spy(program, 'command');
+
+ index(program);
+ expect(program.command.calledWith('optimize')).to.be(true);
+
+ program.command.restore();
+ });
+
+ it('should define the description', function () {
+ sinon.spy(program, 'description');
+
+ index(program);
+ expect(program.description.calledWith('force the optimization for all plugins')).to.be(true);
+
+ program.description.restore();
+ });
+
+
+ });
+
+ });
+
+});
diff --git a/src/cli_plugin/optimize/__tests__/settings.js b/src/cli_plugin/optimize/__tests__/settings.js
new file mode 100644
index 000000000..2089f6c26
--- /dev/null
+++ b/src/cli_plugin/optimize/__tests__/settings.js
@@ -0,0 +1,42 @@
+import expect from 'expect.js';
+import { fromRoot } from '../../../utils';
+import { parse } from '../settings';
+
+describe('kibana cli', function () {
+
+ describe('plugin optimizer', function () {
+
+ describe('command line option parsing', function () {
+
+ describe('parse function', function () {
+
+ let command;
+ const options = {};
+ beforeEach(function () {
+ command = { pluginDir: fromRoot('plugins') };
+ });
+
+ describe('pluginDir option', function () {
+
+ it('should default to plugins', function () {
+ const settings = parse(command, options);
+
+ expect(settings.pluginDir).to.be(fromRoot('plugins'));
+ });
+
+ it('should set settings.config property', function () {
+ command.pluginDir = 'foo bar baz';
+ const settings = parse(command, options);
+
+ expect(settings.pluginDir).to.be('foo bar baz');
+ });
+
+ });
+
+ });
+
+ });
+
+ });
+
+});
diff --git a/src/cli_plugin/optimize/index.js b/src/cli_plugin/optimize/index.js
new file mode 100644
index 000000000..d6ea92c8a
--- /dev/null
+++ b/src/cli_plugin/optimize/index.js
@@ -0,0 +1,35 @@
+import { fromRoot } from '../../utils';
+import optimize from './optimize';
+import Logger from '../lib/logger';
+import { parse } from './settings';
+import logWarnings from '../lib/log_warnings';
+
+function processCommand(command) {
+ let settings;
+ try {
+ settings = parse(command);
+
+ } catch (ex) {
+ //The logger has not yet been initialized.
+ console.error(ex.message);
+ process.exit(64); // eslint-disable-line no-process-exit
+ }
+
+ const logger = new Logger(settings);
+ logWarnings(settings, logger);
+ optimize(settings, logger);
+}
+
+export default function pluginOptimize(program) {
+ program
+ .command('optimize')
+ .option('-q, --quiet', 'disable all process messaging except errors')
+ .option('-s, --silent', 'disable all process messaging')
+ .option(
+ '-d, --plugin-dir <path>',
+ 'path to the directory where plugins are stored',
+ fromRoot('plugins')
+ )
+ .description('force the optimization for all plugins')
+ .action(processCommand);
+}
diff --git a/src/cli_plugin/optimize/optimize.js b/src/cli_plugin/optimize/optimize.js
new file mode 100644
index 000000000..f0071ae8d
--- /dev/null
+++ b/src/cli_plugin/optimize/optimize.js
@@ -0,0 +1,16 @@
+import { cleanArtifacts } from '../install/cleanup';
+import { rebuildCache } from '../install/kibana';
+
+export default async function optimize(settings, logger) {
+ try {
+ logger.log('Start stand-alone plugins optimization');
+
+ await rebuildCache(settings, logger);
+
+ logger.log('Plugin optimization complete');
+ } catch (err) {
+ logger.error(`Plugin optimization was unsuccessful due to error "${err.message}"`);
+ cleanArtifacts(settings);
+ process.exit(70); // eslint-disable-line no-process-exit
+ }
+}
diff --git a/src/cli_plugin/optimize/settings.js b/src/cli_plugin/optimize/settings.js
new file mode 100644
index 000000000..6526828d6
--- /dev/null
+++ b/src/cli_plugin/optimize/settings.js
@@ -0,0 +1,10 @@
+export function parse(command) {
+
+ const settings = {
+ quiet: command.quiet || false,
+ silent: command.silent || false,
+ pluginDir: command.pluginDir || ''
+ };
+
+ return settings;
+}
--
2.15.1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment