Skip to content

Commit

Permalink
Move BasePathProxy to the new platform (#19424)
Browse files Browse the repository at this point in the history
  • Loading branch information
azasypkin authored Jun 25, 2018
1 parent f9c6e08 commit 9fd5e43
Show file tree
Hide file tree
Showing 25 changed files with 984 additions and 420 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@
"glob-all": "3.0.1",
"good-squeeze": "2.1.0",
"h2o2": "5.1.1",
"h2o2-latest": "npm:[email protected]",
"handlebars": "4.0.5",
"hapi": "14.2.0",
"hapi-latest": "npm:[email protected]",
Expand Down
140 changes: 0 additions & 140 deletions src/cli/cluster/base_path_proxy.js

This file was deleted.

28 changes: 19 additions & 9 deletions src/cli/cluster/cluster_manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ import { debounce, invoke, bindAll, once, uniq } from 'lodash';

import Log from '../log';
import Worker from './worker';
import BasePathProxy from './base_path_proxy';
import { Config } from '../../server/config/config';
import { transformDeprecations } from '../../server/config/transform_deprecations';
import { configureBasePathProxy } from './configure_base_path_proxy';

process.env.kbnWorkerType = 'managr';

Expand All @@ -33,10 +33,14 @@ export default class ClusterManager {
const transformedSettings = transformDeprecations(settings);
const config = await Config.withDefaultSchema(transformedSettings);

return new ClusterManager(opts, config);
const basePathProxy = opts.basePath
? await configureBasePathProxy(config)
: undefined;

return new ClusterManager(opts, config, basePathProxy);
}

constructor(opts, config) {
constructor(opts, config, basePathProxy) {
this.log = new Log(opts.quiet, opts.silent);
this.addedCount = 0;
this.inReplMode = !!opts.repl;
Expand All @@ -47,17 +51,17 @@ export default class ClusterManager {
'--server.autoListen=false',
];

if (opts.basePath) {
this.basePathProxy = new BasePathProxy(this, config);
if (basePathProxy) {
this.basePathProxy = basePathProxy;

optimizerArgv.push(
`--server.basePath=${this.basePathProxy.basePath}`,
`--server.basePath=${this.basePathProxy.getBasePath()}`,
'--server.rewriteBasePath=true',
);

serverArgv.push(
`--server.port=${this.basePathProxy.targetPort}`,
`--server.basePath=${this.basePathProxy.basePath}`,
`--server.port=${this.basePathProxy.getTargetPort()}`,
`--server.basePath=${this.basePathProxy.getBasePath()}`,
'--server.rewriteBasePath=true',
);
}
Expand All @@ -78,6 +82,12 @@ export default class ClusterManager {
})
];

if (basePathProxy) {
// Pass server worker to the basepath proxy so that it can hold off the
// proxying until server worker is ready.
this.basePathProxy.serverWorker = this.server;
}

// broker messages between workers
this.workers.forEach((worker) => {
worker.on('broadcast', (msg) => {
Expand Down Expand Up @@ -120,7 +130,7 @@ export default class ClusterManager {
this.setupManualRestart();
invoke(this.workers, 'start');
if (this.basePathProxy) {
this.basePathProxy.listen();
this.basePathProxy.start();
}
}

Expand Down
64 changes: 64 additions & 0 deletions src/cli/cluster/configure_base_path_proxy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* 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.
*/

import { Server } from 'hapi';
import { createBasePathProxy } from '../../core';
import { setupLogging } from '../../server/logging';

export async function configureBasePathProxy(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);

const basePathProxy = createBasePathProxy({ server, config });

await basePathProxy.configure({
shouldRedirectFromOldBasePath: path => {
const isApp = path.startsWith('app/');
const isKnownShortPath = ['login', 'logout', 'status'].includes(path);

return isApp || isKnownShortPath;
},

blockUntil: () => {
// Wait until `serverWorker either crashes or starts to listen.
// The `serverWorker` property should be set by the ClusterManager
// once it creates the worker.
const serverWorker = basePathProxy.serverWorker;
if (serverWorker.listening || serverWorker.crashed) {
return Promise.resolve();
}

return new Promise(resolve => {
const done = () => {
serverWorker.removeListener('listening', done);
serverWorker.removeListener('crashed', done);

resolve();
};

serverWorker.on('listening', done);
serverWorker.on('crashed', done);
});
},
});

return basePathProxy;
}
Loading

0 comments on commit 9fd5e43

Please sign in to comment.