|
5 | 5 | */ |
6 | 6 |
|
7 | 7 | import { Server } from 'hapi'; |
| 8 | +import fetch from 'node-fetch'; |
8 | 9 | import { checkRepos } from './check_repos'; |
9 | | -import { clientWithInternalUser, CodeNodeInfo } from './code_node_client'; |
10 | 10 | import { LspIndexerFactory, RepositoryIndexInitializerFactory, tryMigrateIndices } from './indexer'; |
11 | 11 | import { EsClient, Esqueue } from './lib/esqueue'; |
12 | 12 | import { Logger } from './log'; |
@@ -64,71 +64,51 @@ function getServerUuid(server: Server): Promise<string> { |
64 | 64 | return Promise.resolve(uid); |
65 | 65 | } |
66 | 66 |
|
| 67 | +async function getCodeNodeUuid(url: string, log: Logger) { |
| 68 | + const res = await fetch(`${url}/api/stats`, {}); |
| 69 | + if (res.ok) { |
| 70 | + return (await res.json()).kibana.uuid; |
| 71 | + } |
| 72 | + |
| 73 | + log.info(`Access code node ${url} failed, try again later.`); |
| 74 | + return null; |
| 75 | +} |
| 76 | + |
67 | 77 | export function init(server: Server, options: any) { |
68 | 78 | const log = new Logger(server); |
69 | 79 | const serverOptions = new ServerOptions(options, server.config()); |
70 | 80 | // @ts-ignore |
71 | 81 | const kbnServer = this.kbnServer; |
72 | 82 | kbnServer.ready().then(async () => { |
73 | 83 | const serverUuid = await retryUntilAvailable(() => getServerUuid(server), 50); |
74 | | - const codeNodeClient = await clientWithInternalUser(server); |
75 | 84 | // enable security check in routes |
76 | 85 | enableSecurity(server); |
77 | | - if (serverOptions.codeNode) { |
78 | | - let info = await codeNodeClient.getCodeNodeInfo(); |
79 | | - if (!info) { |
80 | | - let url: string = server.info.uri; |
81 | | - const serverHost = server.config().get('server.host'); |
82 | | - if (serverHost !== undefined && serverHost !== 'localhost') { |
83 | | - const serverPort = server.config().get('server.port'); |
84 | | - const schema = server.config().get('server.ssl.enabled') ? 'https' : 'http'; |
85 | | - let basePath: string = server.config().get('server.basePath') || ''; |
86 | | - if (!basePath.startsWith('/')) { |
87 | | - basePath = '/' + basePath; |
88 | | - } |
89 | | - url = `${schema}://${serverHost}:${serverPort}}${basePath}`; |
90 | | - } |
91 | | - info = await codeNodeClient.createNodeInfo({ |
92 | | - uuid: serverUuid, |
93 | | - url, |
94 | | - }); |
95 | | - } |
96 | | - if (info.uuid === serverUuid) { |
| 86 | + const codeNodeUrl = serverOptions.codeNodeUrl; |
| 87 | + if (codeNodeUrl) { |
| 88 | + const codeNodeUuid = (await retryUntilAvailable( |
| 89 | + async () => await getCodeNodeUuid(codeNodeUrl, log), |
| 90 | + 5000 |
| 91 | + )) as string; |
| 92 | + if (codeNodeUuid === serverUuid) { |
97 | 93 | await initCodeNode(server, serverOptions, log); |
98 | 94 | } else { |
99 | | - const adminCluster = server.plugins.elasticsearch.getCluster('admin'); |
100 | | - // @ts-ignore |
101 | | - const esUrl = adminCluster.clusterClient.config.hosts[0]; |
102 | | - log.error( |
103 | | - `A code node with different uuid:${info.uuid} is already registered as code nodes. |
104 | | - 1. If this kibana node should be a non-code node, then please set "xpack.code.codeNode" to false in kibana.yml. |
105 | | - 2. If you want to replace the code-node to this kibana node, then please delete the old info by execute: |
106 | | - curl --request DELETE --url ${esUrl}/.code_node/_doc/code-node-info |
107 | | - ` |
108 | | - ); |
109 | | - await initNonCodeNode(info, server, serverOptions, log); |
| 95 | + await initNonCodeNode(codeNodeUrl, server, serverOptions, log); |
110 | 96 | } |
111 | 97 | } else { |
112 | | - const info = await retryUntilAvailable( |
113 | | - async () => await codeNodeClient.getCodeNodeInfo(), |
114 | | - 50 |
115 | | - ); |
116 | | - await initNonCodeNode(info!, server, serverOptions, log); |
| 98 | + // codeNodeUrl not set, single node mode |
| 99 | + await initCodeNode(server, serverOptions, log); |
117 | 100 | } |
118 | 101 | }); |
119 | 102 | } |
120 | 103 |
|
121 | 104 | async function initNonCodeNode( |
122 | | - info: CodeNodeInfo, |
| 105 | + url: string, |
123 | 106 | server: Server, |
124 | 107 | serverOptions: ServerOptions, |
125 | 108 | log: Logger |
126 | 109 | ) { |
127 | | - log.info( |
128 | | - `Initializing Code plugin as non-code node, redirecting all code requests to ${info.url}` |
129 | | - ); |
130 | | - |
131 | | - redirectRoute(server, info.url, log); |
| 110 | + log.info(`Initializing Code plugin as non-code node, redirecting all code requests to ${url}`); |
| 111 | + redirectRoute(server, url, log); |
132 | 112 | } |
133 | 113 |
|
134 | 114 | async function initCodeNode(server: Server, serverOptions: ServerOptions, log: Logger) { |
|
0 commit comments