diff --git a/src/cli_plugin/install/kibana.js b/src/cli_plugin/install/kibana.js index c329992837c264..18f8d1cffc1991 100644 --- a/src/cli_plugin/install/kibana.js +++ b/src/cli_plugin/install/kibana.js @@ -35,6 +35,7 @@ export async function rebuildCache(settings, logger) { }, plugins: { initialize: false, + install: true, scanDirs: [settings.pluginDir, fromRoot('src/plugins')] } } diff --git a/src/plugins/i18n/index.js b/src/plugins/i18n/index.js new file mode 100644 index 00000000000000..53d2203ec0b1a9 --- /dev/null +++ b/src/plugins/i18n/index.js @@ -0,0 +1,13 @@ +import _ from 'lodash'; + +export default function ({ Plugin }) { + return new Plugin({ + + install(installContext) { + _.set(installContext, 'i18n.registerTranslations', function (dir) { + console.log(`Translation registered at ${dir}`); + }); + } + + }); +}; diff --git a/src/plugins/i18n/package.json b/src/plugins/i18n/package.json new file mode 100644 index 00000000000000..060c459e22be8c --- /dev/null +++ b/src/plugins/i18n/package.json @@ -0,0 +1,4 @@ +{ + "name": "i18n", + "version": "1.0.0" +} diff --git a/src/server/config/schema.js b/src/server/config/schema.js index bb78856f1c72bc..9e22cd2d9760b7 100644 --- a/src/server/config/schema.js +++ b/src/server/config/schema.js @@ -93,7 +93,8 @@ module.exports = () => Joi.object({ plugins: Joi.object({ paths: Joi.array().items(Joi.string()).default([]), scanDirs: Joi.array().items(Joi.string()).default([]), - initialize: Joi.boolean().default(true) + initialize: Joi.boolean().default(true), + install: Joi.boolean().default(false) }).default(), path: Joi.object({ diff --git a/src/server/kbn_server.js b/src/server/kbn_server.js index 8ad9d9c0709d80..7a240d98eac347 100644 --- a/src/server/kbn_server.js +++ b/src/server/kbn_server.js @@ -31,6 +31,9 @@ module.exports = class KbnServer { // tell the config we are done loading plugins require('./config/complete'), + // run plugin install hooks + require('./plugins/install'), + // setup this.uiExports and this.bundles require('../ui'), diff --git a/src/server/plugins/install.js b/src/server/plugins/install.js new file mode 100644 index 00000000000000..d1c244bb647226 --- /dev/null +++ b/src/server/plugins/install.js @@ -0,0 +1,42 @@ +import { includes } from 'lodash'; + +export default async function (kbnServer, server, config) { + + if (!config.get('plugins.install')) { + server.log(['info'], 'Plugin installation disabled.'); + return []; + } + + const installContext = {}; + const { plugins } = kbnServer; + + + const path = []; + + const initialize = async function (id) { + const plugin = plugins.byId[id]; + + if (includes(path, id)) { + throw new Error(`circular dependencies found: "${path.concat(id).join(' -> ')}"`); + } + + path.push(id); + + for (let reqId of plugin.requiredIds) { + if (!plugins.byId[reqId]) { + throw new Error(`Unmet requirement "${reqId}" for plugin "${id}"`); + } + + await initialize(reqId); + } + + await plugin.install(installContext); + path.pop(); + }; + + const collection = plugins.toArray(); + + for (let { id } of collection) { + await initialize(id); + } +}; diff --git a/src/server/plugins/plugin.js b/src/server/plugins/plugin.js index 877ff2d887bb8b..49fcbd9e9389ee 100644 --- a/src/server/plugins/plugin.js +++ b/src/server/plugins/plugin.js @@ -61,6 +61,7 @@ module.exports = class Plugin { this.version = opts.version || pkg.version; this.externalPreInit = opts.preInit || _.noop; this.externalInit = opts.init || _.noop; + this.externalInstall = opts.install || _.noop; this.configPrefix = opts.configPrefix || this.id; this.getConfigSchema = opts.config || _.noop; this.preInit = _.once(this.preInit); @@ -157,6 +158,10 @@ module.exports = class Plugin { this[extendInitFns].push(fn); } + async install(installContext) { + return await this.externalInstall(installContext); + } + toJSON() { return this.pkg; }