-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Closes #155137, with some extra reorganisation, modularisation and unit tests. ### Refactors to `maybeAddConfig` ### Refactoring serve.js <-> bootstrap.ts ### Unit tests for `compileConfigStack` --------- Co-authored-by: Kibana Machine <[email protected]>
- Loading branch information
1 parent
74102e5
commit f51f5f4
Showing
9 changed files
with
326 additions
and
504 deletions.
There are no files selected for viewing
26 changes: 0 additions & 26 deletions
26
packages/core/root/core-root-server-internal/src/bootstrap.test.mocks.ts
This file was deleted.
Oops, something went wrong.
61 changes: 0 additions & 61 deletions
61
packages/core/root/core-root-server-internal/src/bootstrap.test.ts
This file was deleted.
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
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,156 @@ | ||
/* | ||
* 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 _ from 'lodash'; | ||
|
||
import { readFileSync, writeFileSync, statSync, existsSync } from 'fs'; | ||
import { resolve } from 'path'; | ||
import { getConfigPath, getConfigDirectory } from '@kbn/utils'; | ||
import { getConfigFromFiles } from '@kbn/config'; | ||
|
||
const isNotEmpty = _.negate(_.isEmpty); | ||
const isNotNull = _.negate(_.isNull); | ||
|
||
/** @typedef {'es' | 'oblt' | 'security'} ServerlessProjectMode */ | ||
/** @type {ServerlessProjectMode[]} */ | ||
const VALID_SERVERLESS_PROJECT_MODE = ['es', 'oblt', 'security']; | ||
|
||
/** | ||
* Collects paths to configurations to be included in the final configuration stack. | ||
* @param {{configOverrides?: string[], devConfig?: boolean, dev?: boolean, serverless?: string | true}} options Options impacting the outgoing config list | ||
* @returns List of paths to configurations to be merged, from left to right. | ||
*/ | ||
export function compileConfigStack({ configOverrides, devConfig, dev, serverless }) { | ||
const cliConfigs = configOverrides || []; | ||
const envConfigs = getEnvConfigs(); | ||
const defaultConfig = getConfigPath(); | ||
|
||
let configs = [cliConfigs, envConfigs, [defaultConfig]].find(isNotEmpty); | ||
|
||
if (dev && devConfig !== false) { | ||
configs.push(resolveConfig('kibana.dev.yml')); | ||
} | ||
|
||
if (dev && serverless) { | ||
writeProjectSwitcherConfig('serverless.recent.dev.yml', serverless); | ||
configs.push(resolveConfig('serverless.recent.dev.yml')); | ||
} | ||
|
||
// Filter out all config paths that didn't exist | ||
configs = configs.filter(isNotNull); | ||
|
||
const serverlessMode = validateServerlessMode(serverless) || getServerlessModeFromCfg(configs); | ||
if (serverlessMode) { | ||
configs.unshift(resolveConfig(`serverless.${serverlessMode}.yml`)); | ||
configs.unshift(resolveConfig('serverless.yml')); | ||
|
||
if (dev && devConfig !== false) { | ||
configs.push(resolveConfig('serverless.dev.yml')); | ||
configs.push(resolveConfig(`serverless.${serverlessMode}.dev.yml`)); | ||
} | ||
} | ||
|
||
return configs.filter(isNotNull); | ||
} | ||
|
||
/** | ||
* @param {string[]} configs List of configuration file paths | ||
* @returns {ServerlessProjectMode|undefined} The serverless mode in the summed configs | ||
*/ | ||
function getServerlessModeFromCfg(configs) { | ||
const config = getConfigFromFiles(configs); | ||
|
||
return config.serverless; | ||
} | ||
|
||
/** | ||
* @param {string} fileName Name of the config within the config directory | ||
* @returns {string | null} The resolved path to the config, if it exists, null otherwise | ||
*/ | ||
function resolveConfig(fileName) { | ||
const filePath = resolve(getConfigDirectory(), fileName); | ||
if (fileExists(filePath)) { | ||
return filePath; | ||
} else { | ||
return null; | ||
} | ||
} | ||
|
||
/** | ||
* @param {string} fileName | ||
* @param {object} opts | ||
*/ | ||
function writeProjectSwitcherConfig(fileName, serverlessOption) { | ||
const path = resolve(getConfigDirectory(), fileName); | ||
const configAlreadyExists = existsSync(path); | ||
|
||
const preserveExistingConfig = serverlessOption === true; | ||
const serverlessMode = validateServerlessMode(serverlessOption) || 'es'; | ||
|
||
if (configAlreadyExists && preserveExistingConfig) { | ||
return; | ||
} else { | ||
const content = `xpack.serverless.plugin.developer.projectSwitcher.enabled: true\nserverless: ${serverlessMode}\n`; | ||
if (!configAlreadyExists || readFileSync(path).toString() !== content) { | ||
writeFileSync(path, content); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* @param {string} filePath Path to the config file | ||
* @returns {boolean} Whether the file exists | ||
*/ | ||
function fileExists(filePath) { | ||
try { | ||
return statSync(filePath).isFile(); | ||
} catch (err) { | ||
if (err.code === 'ENOENT') { | ||
return false; | ||
} | ||
|
||
throw err; | ||
} | ||
} | ||
|
||
/** | ||
* @returns {string[]} | ||
*/ | ||
function getEnvConfigs() { | ||
const val = process.env.KBN_CONFIG_PATHS; | ||
if (typeof val === 'string') { | ||
return val | ||
.split(',') | ||
.filter((v) => !!v) | ||
.map((p) => resolve(p.trim())); | ||
} | ||
return []; | ||
} | ||
|
||
/** | ||
* @param {string | true} serverlessMode | ||
* @returns {ServerlessProjectMode | null} | ||
*/ | ||
function validateServerlessMode(serverlessMode) { | ||
if (!serverlessMode) { | ||
return null; | ||
} | ||
|
||
if (serverlessMode === true) { | ||
// Defaulting to read the project-switcher's settings in `serverless.recent.dev.yml` | ||
return null; | ||
} | ||
|
||
if (VALID_SERVERLESS_PROJECT_MODE.includes(serverlessMode)) { | ||
return serverlessMode; | ||
} | ||
|
||
throw new Error( | ||
`invalid --serverless value, must be one of ${VALID_SERVERLESS_PROJECT_MODE.join(', ')}` | ||
); | ||
} |
Oops, something went wrong.