Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master' into vega
Browse files Browse the repository at this point in the history
  • Loading branch information
alexwizp committed Jul 8, 2019
2 parents 970d10f + 181e65a commit c7d8237
Show file tree
Hide file tree
Showing 64 changed files with 1,358 additions and 3,554 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

## ElasticsearchErrorHelpers class

Helpers provided to simplify future migration away from Boom as internal Error.
Helpers for working with errors returned from the Elasticsearch service.Since the internal data of errors are subject to change, consumers of the Elasticsearch service should always use these helpers to classify errors instead of checking error internals such as `body.error.header[WWW-Authenticate]`

<b>Signature:</b>

Expand All @@ -25,7 +25,7 @@ Handle errors

```js
try {
await client.callWithRequest(request, '...');
await client.asScoped(request).callAsCurrentUser(...);
} catch (err) {
if (ElasticsearchErrorHelpers.isNotAuthorizedError(err)) {
const authHeader = err.output.headers['WWW-Authenticate'];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ Headers used for authentication against Elasticsearch
<b>Signature:</b>

```typescript
headers: Record<string, string>;
headers: Headers;
```
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@ export interface FakeRequest

| Property | Type | Description |
| --- | --- | --- |
| [headers](./kibana-plugin-server.fakerequest.headers.md) | <code>Record&lt;string, string&gt;</code> | Headers used for authentication against Elasticsearch |
| [headers](./kibana-plugin-server.fakerequest.headers.md) | <code>Headers</code> | Headers used for authentication against Elasticsearch |

2 changes: 1 addition & 1 deletion docs/development/core/server/kibana-plugin-server.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ The plugin integrates with the core system via lifecycle events: `setup`<!-- -->
| Class | Description |
| --- | --- |
| [ClusterClient](./kibana-plugin-server.clusterclient.md) | Represents an Elasticsearch cluster API client and allows to call API on behalf of the internal Kibana user and the actual user that is derived from the request headers (via <code>asScoped(...)</code>). |
| [ElasticsearchErrorHelpers](./kibana-plugin-server.elasticsearcherrorhelpers.md) | Helpers provided to simplify future migration away from Boom as internal Error. |
| [ElasticsearchErrorHelpers](./kibana-plugin-server.elasticsearcherrorhelpers.md) | Helpers for working with errors returned from the Elasticsearch service.Since the internal data of errors are subject to change, consumers of the Elasticsearch service should always use these helpers to classify errors instead of checking error internals such as <code>body.error.header[WWW-Authenticate]</code> |
| [KibanaRequest](./kibana-plugin-server.kibanarequest.md) | Kibana specific abstraction for an incoming request. |
| [Router](./kibana-plugin-server.router.md) | |
| [SavedObjectsErrorHelpers](./kibana-plugin-server.savedobjectserrorhelpers.md) | |
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -390,8 +390,8 @@
"intl-messageformat-parser": "^1.4.0",
"is-path-inside": "^2.0.0",
"istanbul-instrumenter-loader": "3.0.1",
"jest": "^24.1.0",
"jest-cli": "^24.1.0",
"jest": "^24.8.0",
"jest-cli": "^24.8.0",
"jest-dom": "^3.1.3",
"jest-raw-loader": "^1.0.1",
"jimp": "0.6.4",
Expand Down
2 changes: 1 addition & 1 deletion packages/kbn-elastic-idx/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"devDependencies": {
"@babel/core": "7.4.5",
"@babel/plugin-transform-async-to-generator": "7.4.4",
"jest": "^24.1.0",
"jest": "^24.8.0",
"typescript": "^3.3.3333"
},
"jest": {
Expand Down
85 changes: 85 additions & 0 deletions packages/kbn-es/src/cli_commands/build_snapshots.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

const { resolve, basename } = require('path');
const { createHash } = require('crypto');
const { promisify } = require('util');
const { pipeline, Transform } = require('stream');
const Fs = require('fs');

const getopts = require('getopts');
const mkdirp = require('mkdirp');
const del = require('del');

const { buildSnapshot, log } = require('../utils');

const pipelineAsync = promisify(pipeline);

exports.description = 'Build and collect ES snapshots';

exports.help = () => ``;

exports.run = async (defaults = {}) => {
const argv = process.argv.slice(2);
const options = getopts(argv, {
alias: {
sourcePath: 'source-path',
},
default: {
...defaults,
output: 'es_snapshots',
},
});

const outputDir = resolve(process.cwd(), options.output);
del.sync(outputDir);
mkdirp.sync(outputDir);

for (const license of ['oss', 'trial']) {
for (const platform of ['darwin', 'win32', 'linux']) {
log.info('Building', platform, license === 'trial' ? 'default' : 'oss', 'snapshot');
log.indent(4);

const snapshotPath = await buildSnapshot({
license,
sourcePath: options.sourcePath,
log,
platform,
});

const filename = basename(snapshotPath);
const outputPath = resolve(outputDir, filename);
const hash = createHash('sha512');
await pipelineAsync(
Fs.createReadStream(snapshotPath),
new Transform({
transform(chunk, _, cb) {
hash.update(chunk);
cb(undefined, chunk);
},
}),
Fs.createWriteStream(outputPath)
);

Fs.writeFileSync(`${outputPath}.sha512`, `${hash.digest('hex')} ${filename}`);
log.success('snapshot and shasum written to', outputPath);
log.indent(-4);
}
}
};
1 change: 1 addition & 0 deletions packages/kbn-es/src/cli_commands/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@
exports.snapshot = require('./snapshot');
exports.source = require('./source');
exports.archive = require('./archive');
exports.build_snapshots = require('./build_snapshots');
80 changes: 3 additions & 77 deletions packages/kbn-es/src/install/source.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,15 @@
* under the License.
*/

const execa = require('execa');
const path = require('path');
const fs = require('fs');
const os = require('os');
const readline = require('readline');
const chalk = require('chalk');
const crypto = require('crypto');
const simpleGit = require('simple-git/promise');
const { installArchive } = require('./archive');
const { createCliError } = require('../errors');
const { findMostRecentlyChanged, log: defaultLog, cache } = require('../utils');
const { GRADLE_BIN, BASE_PATH } = require('../paths');

const onceEvent = (emitter, event) => new Promise(resolve => emitter.once(event, resolve));
const { log: defaultLog, cache, buildSnapshot, archiveForPlatform } = require('../utils');
const { BASE_PATH } = require('../paths');

/**
* Installs ES from source
Expand Down Expand Up @@ -60,7 +55,7 @@ exports.installSource = async function installSource({

const cacheMeta = cache.readMeta(dest);
const isCached = cacheMeta.exists && cacheMeta.etag === metadata.etag;
const archive = isCached ? dest : await createSnapshot({ sourcePath, log, license });
const archive = isCached ? dest : await buildSnapshot({ sourcePath, log, license });

if (isCached) {
log.info('source path unchanged since %s, using cache', chalk.bold(cacheMeta.ts));
Expand Down Expand Up @@ -122,72 +117,3 @@ async function sourceInfo(cwd, license, log = defaultLog) {
branch,
};
}

/**
* Creates archive from source
*
* @param {Object} options
* @property {('oss'|'basic'|'trial')} options.license
* @property {String} options.sourcePath
* @property {ToolingLog} options.log
* @returns {Object} containing archive and optional plugins
*
* Gradle tasks:
* :distribution:archives:darwin-tar:assemble
* :distribution:archives:linux-tar:assemble
* :distribution:archives:windows-zip:assemble
* :distribution:archives:oss-darwin-tar:assemble
* :distribution:archives:oss-linux-tar:assemble
* :distribution:archives:oss-windows-zip:assemble
*/
async function createSnapshot({ license, sourcePath, log = defaultLog }) {
const { task, ext } = archiveForPlatform(os.platform(), license);
const buildArgs = [`:distribution:archives:${task}:assemble`];

log.info('%s %s', GRADLE_BIN, buildArgs.join(' '));

const build = execa(GRADLE_BIN, buildArgs, {
cwd: sourcePath,
stdio: ['ignore', 'pipe', 'pipe'],
});

const stdout = readline.createInterface({ input: build.stdout });
const stderr = readline.createInterface({ input: build.stderr });

stdout.on('line', line => log.debug(line));
stderr.on('line', line => log.error(line));

const [exitCode] = await Promise.all([
onceEvent(build, 'exit'),
onceEvent(stdout, 'close'),
onceEvent(stderr, 'close'),
]);

if (exitCode > 0) {
throw createCliError('unable to build ES');
}

const archivePattern = `distribution/archives/${task}/build/distributions/elasticsearch-*.${ext}`;
const esArchivePath = findMostRecentlyChanged(path.resolve(sourcePath, archivePattern));

if (!esArchivePath) {
throw createCliError('could not locate ES distribution');
}

return esArchivePath;
}

function archiveForPlatform(platform, license) {
const taskPrefix = license === 'oss' ? 'oss-' : '';

switch (platform) {
case 'darwin':
return { format: 'tar', ext: 'tar.gz', task: `${taskPrefix}darwin-tar`, platform: 'darwin' };
case 'win32':
return { format: 'zip', ext: 'zip', task: `${taskPrefix}windows-zip`, platform: 'windows' };
case 'linux':
return { format: 'tar', ext: 'tar.gz', task: `${taskPrefix}linux-tar`, platform: 'linux' };
default:
throw new Error(`unknown platform: ${platform}`);
}
}
103 changes: 103 additions & 0 deletions packages/kbn-es/src/utils/build_snapshot.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

const execa = require('execa');
const path = require('path');
const os = require('os');
const readline = require('readline');
const { createCliError } = require('../errors');
const { findMostRecentlyChanged } = require('../utils');
const { GRADLE_BIN } = require('../paths');

const onceEvent = (emitter, event) => new Promise(resolve => emitter.once(event, resolve));

/**
* Creates archive from source
*
* @param {Object} options
* @property {('oss'|'basic'|'trial')} options.license
* @property {String} options.sourcePath
* @property {ToolingLog} options.log
* @returns {Object} containing archive and optional plugins
*
* Gradle tasks:
* :distribution:archives:darwin-tar:assemble
* :distribution:archives:linux-tar:assemble
* :distribution:archives:windows-zip:assemble
* :distribution:archives:oss-darwin-tar:assemble
* :distribution:archives:oss-linux-tar:assemble
* :distribution:archives:oss-windows-zip:assemble
*/
exports.buildSnapshot = async ({ license, sourcePath, log, platform = os.platform() }) => {
const { task, ext } = exports.archiveForPlatform(platform, license);
const buildArgs = [`:distribution:archives:${task}:assemble`];

log.info('%s %s', GRADLE_BIN, buildArgs.join(' '));
log.debug('cwd:', sourcePath);

const build = execa(GRADLE_BIN, buildArgs, {
cwd: sourcePath,
stdio: ['ignore', 'pipe', 'pipe'],
});

const stdout = readline.createInterface({ input: build.stdout });
const stderr = readline.createInterface({ input: build.stderr });

stdout.on('line', line => log.debug(line));
stderr.on('line', line => log.error(line));

const [exitCode] = await Promise.all([
Promise.race([
onceEvent(build, 'exit'),
onceEvent(build, 'error').then(error => {
throw createCliError(`Error spawning gradle: ${error.message}`);
}),
]),
onceEvent(stdout, 'close'),
onceEvent(stderr, 'close'),
]);

if (exitCode > 0) {
throw createCliError('unable to build ES');
}

const archivePattern = `distribution/archives/${task}/build/distributions/elasticsearch-*.${ext}`;
const esArchivePath = findMostRecentlyChanged(path.resolve(sourcePath, archivePattern));

if (!esArchivePath) {
throw createCliError('could not locate ES distribution');
}

return esArchivePath;
};

exports.archiveForPlatform = (platform, license) => {
const taskPrefix = license === 'oss' ? 'oss-' : '';

switch (platform) {
case 'darwin':
return { format: 'tar', ext: 'tar.gz', task: `${taskPrefix}darwin-tar`, platform: 'darwin' };
case 'win32':
return { format: 'zip', ext: 'zip', task: `${taskPrefix}windows-zip`, platform: 'windows' };
case 'linux':
return { format: 'tar', ext: 'tar.gz', task: `${taskPrefix}linux-tar`, platform: 'linux' };
default:
throw new Error(`unknown platform: ${platform}`);
}
};
2 changes: 2 additions & 0 deletions packages/kbn-es/src/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,5 @@ exports.findMostRecentlyChanged = require('./find_most_recently_changed').findMo
exports.extractConfigFiles = require('./extract_config_files').extractConfigFiles;
exports.decompress = require('./decompress').decompress;
exports.NativeRealm = require('./native_realm').NativeRealm;
exports.buildSnapshot = require('./build_snapshot').buildSnapshot;
exports.archiveForPlatform = require('./build_snapshot').archiveForPlatform;
2 changes: 1 addition & 1 deletion packages/kbn-spec-to-console/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
},
"homepage": "https://github.com/jbudz/spec-to-console#readme",
"devDependencies": {
"jest": "^24.1.0",
"jest": "^24.8.0",
"prettier": "^1.14.3"
},
"dependencies": {
Expand Down
Loading

0 comments on commit c7d8237

Please sign in to comment.