Skip to content

Commit

Permalink
Unify "normal" and "with base path proxy" bootstrap paths.
Browse files Browse the repository at this point in the history
  • Loading branch information
azasypkin committed Jul 24, 2018
1 parent 0e232ab commit f5b304e
Show file tree
Hide file tree
Showing 15 changed files with 254 additions and 414 deletions.
19 changes: 13 additions & 6 deletions src/cli/cluster/cluster_manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,24 @@ import { setupLogging } from '../../server/logging';
process.env.kbnWorkerType = 'managr';

export default class ClusterManager {
static createConfig(settings = {}) {
const transformedSettings = transformDeprecations(settings);
return Config.withDefaultSchema(transformedSettings);
}
static create(opts, settings = {}, basePathProxy) {
const config = Config.withDefaultSchema(transformDeprecations(settings));

static createServer(config) {
// New platform forwards all logs to the legacy platform so we need HapiJS server
// here just for logging purposes and nothing else.
const server = new Server();
setupLogging(server, config);
return server;

return {
kbnServer: {
async ready() {},
server,
close() {},
listen() {}
},

clusterManager: new ClusterManager(opts, config, basePathProxy)
};
}

constructor(opts, config, basePathProxy) {
Expand Down
5 changes: 5 additions & 0 deletions src/core/cli/apply_config_overrides.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,11 @@ function* getUnknownArgsOverrides(
) {
// Merge unknown CLI args into config.
for (const unknownArgKey of args.getUnknownOptions(argv, installationFeatures)) {
// Include only arguments that looks like config keys (with `.` as a separator).
if (!unknownArgKey.includes('.')) {
continue;
}

try {
yield [unknownArgKey, JSON.parse(argv[unknownArgKey])];
} catch (e) {
Expand Down
127 changes: 29 additions & 98 deletions src/core/cli/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,15 @@

import yargs, { Arguments } from 'yargs';

import { $combineLatest, k$, map } from '../lib/kbn_observable';
import { ConfigService, Env, RawConfig, RawConfigService } from '../server/config';
import { OnShutdown, Root } from '../server/root';

import { isWorker } from 'cluster';
import { DevConfig } from '../server/dev';
import { BasePathProxyServer, HttpConfig } from '../server/http';
import { LegacyService } from '../server/legacy_compat/legacy_service';
import { LoggingConfig, LoggingService, MutableLoggerFactory } from '../server/logging';
import { k$, map } from '../lib/kbn_observable';
import { Env, RawConfigService } from '../server/config';
import { Root } from '../server/root';

import { isMaster } from 'cluster';
import { applyConfigOverrides } from './apply_config_overrides';
import * as args from './args';
import { createServeCommand } from './commands';
import {
CLUSTER_MANAGER_PATH,
detectInstallationFeatures,
REPL_PATH,
} from './installation_features';
import { detectInstallationFeatures } from './installation_features';

const installationFeatures = detectInstallationFeatures();

Expand All @@ -61,115 +53,54 @@ export const parseArgv = (argv: string[]): Arguments => {
);
};

const onShutdown: OnShutdown = reason => {
const onShutdown = (reason?: Error | string) => {
if (reason !== undefined) {
// tslint:disable no-console
console.log(`FAIL ${reason}\n`);
console.error(`FAIL ${reason}\n`);
}

process.exit(reason === undefined ? 0 : 1);
};

export const run = (argv: string[]) => {
const parsedArgs = parseArgv(argv);
if (parsedArgs._[0]) {
// tslint:disable no-console
console.error(`Unknown command: ${parsedArgs._[0]}`);
process.exit(1);
const cliArgs = parseArgv(argv);
if (cliArgs._[0]) {
onShutdown(`Unknown command: ${cliArgs._[0]}`);
}

const env = Env.createDefault({
configs: parsedArgs.config,
...parsedArgs,
configs: cliArgs.config,
cliArgs,
});

const rawConfigService = new RawConfigService(env.getConfigFiles());
rawConfigService.loadConfig();

const rawConfig$ = k$(rawConfigService.getConfig$())(
map(rawConfig => applyConfigOverrides(rawConfig, parsedArgs, installationFeatures))
);

const loggerFactory = new MutableLoggerFactory(env);
const configService = new ConfigService(rawConfig$, env, loggerFactory);
const loggingService = new LoggingService(loggerFactory);
loggingService.upgrade(configService.atPath('logging', LoggingConfig));

function shutdown(reason?: Error) {
async function shutdown(reason?: Error) {
rawConfigService.stop();
onShutdown(reason);
await root.shutdown(reason);
}

const root$ = k$(
$combineLatest<DevConfig, HttpConfig, RawConfig>(
configService.atPath('dev', DevConfig),
configService.atPath('server', HttpConfig),
rawConfig$
)
)(
map(([devConfig, httpConfig, rawConfig]) => {
/* envOptions.kbnServer.applyLoggingConfiguration(
rawConfigWithOverrides.getRaw()
);*/

if (!isWorker && parsedArgs.dev && installationFeatures.isClusterModeSupported) {
const ClusterManager = require(CLUSTER_MANAGER_PATH);
const config = ClusterManager.createConfig(rawConfig.getRaw());

const legacyService = new LegacyService(env, loggerFactory, configService, {
ready: () => Promise.resolve(),
server: ClusterManager.createServer(config),
});

const clusterManager = new ClusterManager(
parsedArgs,
config,
new BasePathProxyServer(loggerFactory.get('server'), httpConfig, devConfig)
);

return;
}

const KbnServer = require('../../server/kbn_server');
const kbnServer = new KbnServer(rawConfig.getRaw(), env.legacy);
const legacyService = new LegacyService(env, loggerFactory, configService, kbnServer);

// The kbnWorkerType check is necessary to prevent the repl
// from being started multiple times in different processes.
// We only want one REPL.
if (parsedArgs.repl && process.env.kbnWorkerType === 'server') {
require(REPL_PATH).startRepl(kbnServer);
}

const root = new Root(
configService,
loggerFactory,
loggingService,
legacyService,
env,
onShutdown
);

root.start().catch(err => shutdown(err));
})
/* envOptions.kbnServer.applyLoggingConfiguration(
rawConfigWithOverrides.getRaw()
);*/

const root = new Root(
k$(rawConfigService.getConfig$())(
map(rawConfig => applyConfigOverrides(rawConfig, cliArgs, installationFeatures))
),
env,
{
isDevClusterMaster: isMaster && cliArgs.dev && installationFeatures.isClusterModeSupported,
onShutdown,
}
);

/* function shutdown(reason?: Error) {
rawConfigService.stop();
return root.shutdown(reason);
}
const root = new Root(config$, env, onShutdown);
root.start().catch(err => shutdown(err));*/
root.start().catch(err => shutdown(err));

process.on('SIGHUP', () => rawConfigService.reloadConfig());
process.on('SIGINT', () => shutdown());
process.on('SIGTERM', () => shutdown());

root$.subscribe({
error: err => console.error(`Root error: ${err.message || err}`, err),
});
};

run((process.env.kbnWorkerArgv ? JSON.parse(process.env.kbnWorkerArgv) : process.argv).slice(2));
1 change: 0 additions & 1 deletion src/core/cli/commands/serve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,6 @@ export function getServeCommandOptions(installationFeatures: InstallationFeature
'watch',
{
default: true,
implies: 'dev',
description:
'Automatically restarts server in --dev mode. Use --no-watch to disable that behavior',
type: 'boolean',
Expand Down
20 changes: 0 additions & 20 deletions src/core/index.ts

This file was deleted.

31 changes: 24 additions & 7 deletions src/core/server/config/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ interface EnvironmentMode {

export interface EnvOptions {
configs: string[];
[key: string]: any;
cliArgs: Record<string, any>;
}

export class Env {
Expand Down Expand Up @@ -70,20 +70,33 @@ export class Env {
*/
public readonly legacy: EventEmitter;

/**
* Arguments provided through command line.
*/
private readonly cliArgs: Record<string, any>;

/**
* Paths to the configuration files.
*/
private readonly configs: ReadonlyArray<string>;

/**
* @internal
*/
constructor(readonly homeDir: string, private readonly options: EnvOptions) {
constructor(readonly homeDir: string, options: EnvOptions) {
this.configDir = resolve(this.homeDir, 'config');
this.corePluginsDir = resolve(this.homeDir, 'core_plugins');
this.binDir = resolve(this.homeDir, 'bin');
this.logDir = resolve(this.homeDir, 'log');
this.staticFilesDir = resolve(this.homeDir, 'ui');

this.cliArgs = Object.freeze(options.cliArgs);
this.configs = Object.freeze(options.configs);

this.mode = {
dev: this.options.dev,
name: this.options.dev ? 'development' : 'production',
prod: !this.options.dev,
dev: this.cliArgs.dev,
name: this.cliArgs.dev ? 'development' : 'production',
prod: !this.cliArgs.dev,
};

const isKibanaDistributable = pkg.build && pkg.build.distributable === true;
Expand All @@ -98,10 +111,14 @@ export class Env {
}

public getConfigFiles() {
return this.options.configs;
return this.configs;
}

public getPackageInfo() {
return this.options.packageInfo;
return this.packageInfo;
}

public getCliArgs() {
return this.cliArgs;
}
}
2 changes: 1 addition & 1 deletion src/core/server/config/raw_config_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export class RawConfigService {

private readonly config$: Observable<RawConfig>;

constructor(readonly configFiles: string[]) {
constructor(readonly configFiles: ReadonlyArray<string>) {
this.config$ = k$(this.rawConfigFromFile$)(
filter(rawConfig => rawConfig !== notRead),
map(rawConfig => {
Expand Down
2 changes: 1 addition & 1 deletion src/core/server/config/read_config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export const getConfigFromFile = (configFile: string) => {
return yaml == null ? yaml : ensureDeepObject(yaml);
};

export const getConfigFromFiles = (configFiles: string[]) => {
export const getConfigFromFiles = (configFiles: ReadonlyArray<string>) => {
let mergedYaml = null;

for (const configFile of configFiles) {
Expand Down
60 changes: 1 addition & 59 deletions src/core/server/legacy_compat/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,62 +20,4 @@
/** @internal */
export { LegacyPlatformProxifier } from './legacy_platform_proxifier';
/** @internal */
export { LegacyConfigToRawConfigAdapter, LegacyConfig } from './legacy_platform_config';
/** @internal */
export { LegacyKbnServer } from './legacy_kbn_server';

import {
LegacyConfig,
LegacyConfigToRawConfigAdapter,
LegacyKbnServer,
LegacyPlatformProxifier,
} from '.';
import { BehaviorSubject, k$, map } from '../../lib/kbn_observable';
import { Env } from '../config';
import { Root } from '../root';
import { BasePathProxyRoot } from '../root/base_path_proxy_root';

function initEnvironment(rawKbnServer: any) {
const config: LegacyConfig = rawKbnServer.config;

const legacyConfig$ = new BehaviorSubject(config);
const config$ = k$(legacyConfig$)(
map(legacyConfig => new LegacyConfigToRawConfigAdapter(legacyConfig))
);

const env = Env.createDefault({
kbnServer: new LegacyKbnServer(rawKbnServer),
// The defaults for the following parameters are retrieved by the legacy
// platform from the command line or from `package.json` and stored in the
// config, so we can borrow these parameters and avoid double parsing.
mode: config.get('env'),
packageInfo: config.get('pkg'),
});

return {
config$,
env,
// Propagates legacy config updates to the new platform.
updateConfig(legacyConfig: LegacyConfig) {
legacyConfig$.next(legacyConfig);
},
};
}

/**
* @internal
*/
export const injectIntoKbnServer = (rawKbnServer: any) => {
const { env, config$, updateConfig } = initEnvironment(rawKbnServer);

rawKbnServer.newPlatform = {
// Custom HTTP Listener that will be used within legacy platform by HapiJS server.
proxyListener: new LegacyPlatformProxifier(new Root(config$, env)),
updateConfig,
};
};

export const createBasePathProxy = (rawKbnServer: any) => {
const { env, config$ } = initEnvironment(rawKbnServer);
return new BasePathProxyRoot(config$, env);
};
export { LegacyConfigToRawConfigAdapter } from './legacy_platform_config';
Loading

0 comments on commit f5b304e

Please sign in to comment.