From df8514fa09842f01d0890fb7d65dbd339423f116 Mon Sep 17 00:00:00 2001 From: Spencer Date: Fri, 16 Nov 2018 15:11:43 -0800 Subject: [PATCH] [kbn/es] add `--download` flag to snapshot command to warm the cache (#25830) In order to pre-fetch Elasticsearch snapshots and avoid timeouts during download in images, this adds a `--download` option to the `es snapshot` command. --- packages/kbn-es/src/cli_commands/snapshot.js | 11 ++++- packages/kbn-es/src/cluster.js | 24 ++++++++- packages/kbn-es/src/install/index.js | 1 + packages/kbn-es/src/install/snapshot.js | 51 ++++++++++++++++---- 4 files changed, 74 insertions(+), 13 deletions(-) diff --git a/packages/kbn-es/src/cli_commands/snapshot.js b/packages/kbn-es/src/cli_commands/snapshot.js index 0a585e92cfd7..c01a4fa08ec5 100644 --- a/packages/kbn-es/src/cli_commands/snapshot.js +++ b/packages/kbn-es/src/cli_commands/snapshot.js @@ -35,6 +35,7 @@ exports.help = (defaults = {}) => { --install-path Installation path, defaults to 'source' within base-path --password Sets password for elastic user [default: ${password}] -E Additional key=value settings to pass to Elasticsearch + --download-only Download the snapshot but don't actually start it Example: @@ -51,10 +52,16 @@ exports.run = async (defaults = {}) => { esArgs: 'E', }, + boolean: ['download-only'], + default: defaults, }); const cluster = new Cluster(); - const { installPath } = await cluster.installSnapshot(options); - await cluster.run(installPath, { esArgs: options.esArgs }); + if (options['download-only']) { + await cluster.downloadSnapshot(options); + } else { + const { installPath } = await cluster.installSnapshot(options); + await cluster.run(installPath, { esArgs: options.esArgs }); + } }; diff --git a/packages/kbn-es/src/cluster.js b/packages/kbn-es/src/cluster.js index f14be3582f71..50f3c5db2d8c 100644 --- a/packages/kbn-es/src/cluster.js +++ b/packages/kbn-es/src/cluster.js @@ -19,7 +19,7 @@ const execa = require('execa'); const chalk = require('chalk'); -const { installSnapshot, installSource, installArchive } = require('./install'); +const { downloadSnapshot, installSnapshot, installSource, installArchive } = require('./install'); const { ES_BIN } = require('./paths'); const { log: defaultLog, parseEsLog, extractConfigFiles } = require('./utils'); const { createCliError } = require('./errors'); @@ -50,6 +50,28 @@ exports.Cluster = class Cluster { return { installPath }; } + /** + * Download ES from a snapshot + * + * @param {Object} options + * @property {Array} options.installPath + * @property {Array} options.sourcePath + * @returns {Promise<{installPath}>} + */ + async downloadSnapshot(options = {}) { + this._log.info(chalk.bold('Downloading snapshot')); + this._log.indent(4); + + const { installPath } = await downloadSnapshot({ + log: this._log, + ...options, + }); + + this._log.indent(-4); + + return { installPath }; + } + /** * Download and installs ES from a snapshot * diff --git a/packages/kbn-es/src/install/index.js b/packages/kbn-es/src/install/index.js index 69de1004e4e7..e2b3f692b220 100644 --- a/packages/kbn-es/src/install/index.js +++ b/packages/kbn-es/src/install/index.js @@ -19,4 +19,5 @@ exports.installArchive = require('./archive').installArchive; exports.installSnapshot = require('./snapshot').installSnapshot; +exports.downloadSnapshot = require('./snapshot').downloadSnapshot; exports.installSource = require('./source').installSource; diff --git a/packages/kbn-es/src/install/snapshot.js b/packages/kbn-es/src/install/snapshot.js index de1a635b7f9f..39da7432923a 100644 --- a/packages/kbn-es/src/install/snapshot.js +++ b/packages/kbn-es/src/install/snapshot.js @@ -27,6 +27,39 @@ const { BASE_PATH } = require('../paths'); const { installArchive } = require('./archive'); const { log: defaultLog, cache } = require('../utils'); +/** + * Download an ES snapshot + * + * @param {Object} options + * @property {('oss'|'basic'|'trial')} options.license + * @property {String} options.version + * @property {String} options.basePath + * @property {String} options.installPath + * @property {ToolingLog} options.log + */ +exports.downloadSnapshot = async function installSnapshot({ + license = 'basic', + version, + basePath = BASE_PATH, + installPath = path.resolve(basePath, version), + log = defaultLog, +}) { + // TODO: remove -alpha1 once elastic/elasticsearch#35172 has been merged + const fileName = getFilename(license, version + '-alpha1'); + const url = `https://snapshots.elastic.co/downloads/elasticsearch/${fileName}`; + const dest = path.resolve(basePath, 'cache', fileName); + + log.info('version: %s', chalk.bold(version)); + log.info('install path: %s', chalk.bold(installPath)); + log.info('license: %s', chalk.bold(license)); + + await downloadFile(url, dest, log); + + return { + downloadPath: dest, + }; +}; + /** * Installs ES from snapshot * @@ -46,17 +79,15 @@ exports.installSnapshot = async function installSnapshot({ installPath = path.resolve(basePath, version), log = defaultLog, }) { - // TODO: remove -alpha1 once elastic/elasticsearch#35172 has been merged - const fileName = getFilename(license, version + '-alpha1'); - const url = `https://snapshots.elastic.co/downloads/elasticsearch/${fileName}`; - const dest = path.resolve(basePath, 'cache', fileName); + const { downloadPath } = await exports.downloadSnapshot({ + license, + version, + basePath, + installPath, + log, + }); - log.info('version: %s', chalk.bold(version)); - log.info('install path: %s', chalk.bold(installPath)); - log.info('license: %s', chalk.bold(license)); - - await downloadFile(url, dest, log); - return await installArchive(dest, { + return await installArchive(downloadPath, { license, password, basePath,