forked from elastic/kibana
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[OAS] Capture and commit serverless bundle (elastic#184915)
## Summary Close elastic#184719 Adds the ability to capture OAS for `serverless` build flavor in addition to `traditional`. By default the CLI will run for both, but this can be controlled by passing in one of two new flags: `--no-serverless` or `--no-traditional`. See `oas_docs/bundle.serverless.json` for output. --------- Co-authored-by: kibanamachine <[email protected]>
- Loading branch information
1 parent
8f3359c
commit cf7196f
Showing
8 changed files
with
757 additions
and
97 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
125 changes: 125 additions & 0 deletions
125
packages/kbn-capture-oas-snapshot-cli/src/capture_oas_snapshot.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
import fs from 'node:fs/promises'; | ||
import { encode } from 'node:querystring'; | ||
import type { ChildProcess } from 'node:child_process'; | ||
import fetch from 'node-fetch'; | ||
import * as Rx from 'rxjs'; | ||
import { startTSWorker } from '@kbn/dev-utils'; | ||
import { createTestEsCluster } from '@kbn/test'; | ||
import type { ToolingLog } from '@kbn/tooling-log'; | ||
import { createTestServerlessInstances } from '@kbn/core-test-helpers-kbn-server'; | ||
import type { Result } from './kibana_worker'; | ||
import { sortAndPrettyPrint } from './run_capture_oas_snapshot_cli'; | ||
import { buildFlavourEnvArgName } from './common'; | ||
|
||
interface CaptureOasSnapshotArgs { | ||
log: ToolingLog; | ||
buildFlavour: 'serverless' | 'traditional'; | ||
outputFile: string; | ||
update: boolean; | ||
filters?: { | ||
pathStartsWith?: string[]; | ||
excludePathsMatching?: string[]; | ||
}; | ||
} | ||
|
||
const MB = 1024 * 1024; | ||
const twoDeci = (num: number) => Math.round(num * 100) / 100; | ||
|
||
export async function captureOasSnapshot({ | ||
log, | ||
filters = {}, | ||
buildFlavour, | ||
update, | ||
outputFile, | ||
}: CaptureOasSnapshotArgs): Promise<void> { | ||
const { excludePathsMatching = [], pathStartsWith } = filters; | ||
// internal consts | ||
const port = 5622; | ||
// We are only including /api/status for now | ||
excludePathsMatching.push( | ||
'/{path*}', | ||
// Our internal asset paths | ||
'/XXXXXXXXXXXX/' | ||
); | ||
|
||
let esCluster: undefined | { stop(): Promise<void> }; | ||
let kbWorker: undefined | ChildProcess; | ||
|
||
try { | ||
log.info('Starting es...'); | ||
esCluster = await log.indent(4, async () => { | ||
if (buildFlavour === 'serverless') { | ||
const { startES } = createTestServerlessInstances(); | ||
return await startES(); | ||
} | ||
const cluster = createTestEsCluster({ log }); | ||
await cluster.start(); | ||
return { stop: () => cluster.cleanup() }; | ||
}); | ||
|
||
log.info('Starting Kibana...'); | ||
kbWorker = await log.indent(4, async () => { | ||
log.info('Loading core with all plugins enabled so that we can capture OAS for all...'); | ||
const { msg$, proc } = startTSWorker<Result>({ | ||
log, | ||
src: require.resolve('./kibana_worker'), | ||
env: { ...process.env, [buildFlavourEnvArgName]: buildFlavour }, | ||
}); | ||
await Rx.firstValueFrom( | ||
msg$.pipe( | ||
Rx.map((msg) => { | ||
if (msg !== 'ready') | ||
throw new Error(`received unexpected message from worker (expected "ready"): ${msg}`); | ||
}) | ||
) | ||
); | ||
return proc; | ||
}); | ||
|
||
const qs = encode({ | ||
access: 'public', | ||
version: '2023-10-31', // hard coded for now, we can make this configurable later | ||
pathStartsWith, | ||
excludePathsMatching, | ||
}); | ||
const url = `http://localhost:${port}/api/oas?${qs}`; | ||
log.info(`Fetching OAS at ${url}...`); | ||
const result = await fetch(url, { | ||
headers: { | ||
'kbn-xsrf': 'kbn-oas-snapshot', | ||
authorization: `Basic ${Buffer.from('elastic:changeme').toString('base64')}`, | ||
}, | ||
}); | ||
if (result.status !== 200) { | ||
log.error(`Failed to fetch OAS: ${JSON.stringify(result, null, 2)}`); | ||
throw new Error(`Failed to fetch OAS: ${result.status}`); | ||
} | ||
const currentOas = await result.json(); | ||
log.info(`Recieved OAS, writing to ${outputFile}...`); | ||
if (update) { | ||
await fs.writeFile(outputFile, sortAndPrettyPrint(currentOas)); | ||
const { size: sizeBytes } = await fs.stat(outputFile); | ||
log.success(`OAS written to ${outputFile}. File size ~${twoDeci(sizeBytes / MB)} MB.`); | ||
} else { | ||
log.success( | ||
`OAS recieved, not writing to file. Got OAS for ${ | ||
Object.keys(currentOas.paths).length | ||
} paths.` | ||
); | ||
} | ||
} catch (err) { | ||
log.error(`Failed to capture OAS: ${JSON.stringify(err, null, 2)}`); | ||
throw err; | ||
} finally { | ||
kbWorker?.kill('SIGILL'); | ||
await esCluster?.stop(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
export const buildFlavourEnvArgName = 'CAPTURE_OAS_SNAPSHOT_WORKER_BUILD_FLAVOR'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,5 +21,6 @@ | |
"@kbn/dev-cli-runner", | ||
"@kbn/test", | ||
"@kbn/dev-utils", | ||
"@kbn/tooling-log", | ||
] | ||
} |
Oops, something went wrong.