forked from SeleniumHQ/selenium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SelfRegisteringRemote.java
385 lines (327 loc) · 13.8 KB
/
SelfRegisteringRemote.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
// Licensed to the Software Freedom Conservancy (SFC) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The SFC 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.
package org.openqa.grid.internal.utils;
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.openqa.selenium.json.Json.MAP_TYPE;
import static org.openqa.selenium.remote.http.HttpMethod.GET;
import static org.openqa.selenium.remote.http.HttpMethod.POST;
import org.openqa.grid.common.RegistrationRequest;
import org.openqa.grid.common.exception.GridConfigurationException;
import org.openqa.grid.common.exception.GridException;
import org.openqa.grid.internal.utils.configuration.GridHubConfiguration;
import org.openqa.grid.internal.utils.configuration.GridNodeConfiguration;
import org.openqa.grid.shared.GridNodeServer;
import org.openqa.grid.web.servlet.DisplayHelpServlet;
import org.openqa.grid.web.servlet.NodeW3CStatusServlet;
import org.openqa.grid.web.servlet.ResourceServlet;
import org.openqa.grid.web.utils.ExtraServletUtil;
import org.openqa.selenium.Platform;
import org.openqa.selenium.json.Json;
import org.openqa.selenium.json.JsonInput;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.http.HttpClient;
import org.openqa.selenium.remote.http.HttpRequest;
import org.openqa.selenium.remote.http.HttpResponse;
import org.openqa.selenium.remote.server.log.LoggingManager;
import java.io.Reader;
import java.io.StringReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.security.InvalidParameterException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;
import javax.servlet.Servlet;
public class SelfRegisteringRemote {
private static final Logger LOG = Logger.getLogger(SelfRegisteringRemote.class.getName());
private final RegistrationRequest registrationRequest;
private final HttpClient.Factory httpClientFactory;
private final Map<String, Class<? extends Servlet>> nodeServlets;
private boolean hasId;
public SelfRegisteringRemote(GridNodeConfiguration configuration) {
this(RegistrationRequest.build(configuration, null, null));
}
public SelfRegisteringRemote(RegistrationRequest request) {
this.registrationRequest = request;
this.httpClientFactory = HttpClient.Factory.createDefault();
this.nodeServlets = new HashMap<>();
registrationRequest.validate();
// add the status servlet
nodeServlets.put("/status", NodeW3CStatusServlet.class);
nodeServlets.put("/wd/hub/status", NodeW3CStatusServlet.class);
// add the resource servlet for nodes
if (!registrationRequest.getConfiguration().isWithOutServlet(ResourceServlet.class)) {
nodeServlets.put("/resources/*", ResourceServlet.class);
}
// add the display help servlet for nodes
if (!registrationRequest.getConfiguration().isWithOutServlet(DisplayHelpServlet.class)) {
nodeServlets.put("/*", DisplayHelpServlet.class);
}
// add the user supplied servlet(s) for nodes
addExtraServlets(registrationRequest.getConfiguration().servlets);
}
public URL getRemoteURL() {
String host = registrationRequest.getConfiguration().host;
Integer port = registrationRequest.getConfiguration().port;
String url = "http://" + host + ":" + port;
try {
return new URL(url);
} catch (MalformedURLException e) {
throw new GridConfigurationException("error building the node url " + e.getMessage(), e);
}
}
private GridNodeServer server;
public void setRemoteServer(GridNodeServer server) {
this.server = server;
}
public boolean startRemoteServer() throws Exception {
if (server == null) {
throw new GridConfigurationException("no server set to register to the hub");
}
server.setExtraServlets(nodeServlets);
return server.boot();
}
public void stopRemoteServer() {
if (server != null) {
server.stop();
}
}
public void deleteAllBrowsers() {
registrationRequest.getConfiguration().capabilities.clear();
}
/**
* Adding the browser described by the capability, automatically finding out what platform the
* node is launched from
*
* @param cap describing the browser
* @param instances number of times this browser can be started on the node.
*/
public void addBrowser(DesiredCapabilities cap, int instances) {
String s = cap.getBrowserName();
if (s == null || "".equals(s)) {
throw new InvalidParameterException(cap + " does seems to be a valid browser.");
}
if (cap.getPlatform() == null) {
cap.setPlatform(Platform.getCurrent());
}
cap.setCapability(RegistrationRequest.MAX_INSTANCES, instances);
registrationRequest.getConfiguration().capabilities.add(cap);
registrationRequest.getConfiguration().fixUpCapabilities();
}
/**
* sends 1 registration request, bypassing the retry logic and the proxy already registered check.
* Use only for testing.
*/
public void sendRegistrationRequest() {
registerToHub(false);
}
/**
* register the hub following the configuration :
* <p>
* - check if the proxy is already registered before sending a reg request.
* <p>
* - register again every X ms is specified in the config of the node.
*/
public void startRegistrationProcess() {
// don't advertise that the remote (node) is bound to all IPv4 interfaces (default behavior)
if (registrationRequest.getConfiguration().host.equals("0.0.0.0")) {
// remove the value and call fixUpHost to determine the address of a public (non-loopback) IPv4 interface
registrationRequest.getConfiguration().host = null;
registrationRequest.getConfiguration().fixUpHost();
}
fixUpId();
LOG.fine("Using the json request : " + new Json().toJson(registrationRequest));
Boolean register = registrationRequest.getConfiguration().register;
if (register == null) {
register = false;
}
if (!register) {
LOG.info("No registration sent ( register = false )");
} else {
final int registerCycleInterval = registrationRequest.getConfiguration().registerCycle != null ?
registrationRequest.getConfiguration().registerCycle : 0;
if (registerCycleInterval > 0) {
new Thread(new Runnable() { // Thread safety reviewed
public void run() {
boolean first = true;
LOG.info("Starting auto registration thread. Will try to register every "
+ registerCycleInterval + " ms.");
while (true) {
try {
boolean checkForPresence = true;
if (first) {
first = false;
checkForPresence = false;
}
registerToHub(checkForPresence);
} catch (GridException e) {
LOG.info("Couldn't register this node: " + e.getMessage());
}
try {
Thread.sleep(registerCycleInterval);
} catch (InterruptedException e) {
e.printStackTrace();
}
// While we wait for someone to rewrite server logging.
LoggingManager.perSessionLogHandler().clearThreadTempLogs();
}
}
}).start();
} else {
registerToHub(false);
}
}
LoggingManager.perSessionLogHandler().clearThreadTempLogs();
}
public void setTimeout(int timeout, int cycle) {
registrationRequest.getConfiguration().timeout = timeout;
registrationRequest.getConfiguration().cleanUpCycle = cycle;
}
public void setMaxConcurrent(int max) {
registrationRequest.getConfiguration().maxSession = max;
}
public GridNodeConfiguration getConfiguration() {
return registrationRequest.getConfiguration();
}
/**
* @return the {@link GridNodeServer} for this remote
*/
protected GridNodeServer getServer() {
return server;
}
/**
* @return the list of {@link Servlet}s that this remote will bind
*/
protected Map<String, Class <? extends Servlet>> getNodeServlets() {
return nodeServlets;
}
private void registerToHub(boolean checkPresenceFirst) {
if (!checkPresenceFirst || !isAlreadyRegistered(registrationRequest)) {
String tmp =
"http://" + registrationRequest.getConfiguration().getHubHost() + ":"
+ registrationRequest.getConfiguration().getHubPort() + "/grid/register";
try {
try {
LOG.fine("Updating the node configuration from the hub before sending registration request");
GridHubConfiguration hubConfiguration = getHubConfiguration();
LOG.fine("Hub configuration: " + new Json().toJson(hubConfiguration));
// the node can not set these values. They must come from the hub
if (hubConfiguration.timeout != null && hubConfiguration.timeout >= 0) {
registrationRequest.getConfiguration().timeout = hubConfiguration.timeout;
}
if (hubConfiguration.browserTimeout != null && hubConfiguration.browserTimeout >= 0) {
registrationRequest.getConfiguration().browserTimeout = hubConfiguration.browserTimeout;
}
LOG.fine("Updated node configuration: " + new Json().toJson(registrationRequest.getConfiguration()));
} catch (Exception e) {
LOG.warning(
"error getting the parameters from the hub. The node may end up with wrong timeouts." + e
.getMessage());
}
URL registration = new URL(tmp);
LOG.info("Registering the node to the hub: " + registration);
HttpRequest request = new HttpRequest(POST, registration.toExternalForm());
updateConfigWithRealPort();
String json = new Json().toJson(registrationRequest);
request.setContent(json.getBytes(UTF_8));
HttpClient client = httpClientFactory.createClient(registration);
HttpResponse response = client.execute(request);
if (response.getStatus() != 200) {
throw new GridException(String.format("The hub responded with %s", response.getStatus()));
}
LOG.info("The node is registered to the hub and ready to use");
} catch (Exception e) {
throw new GridException("Error sending the registration request: " + e.getMessage());
}
}
}
private void addExtraServlets(List<String> servlets) {
if (servlets == null || servlets.size() == 0) {
return;
}
for (String s : servlets) {
Class<? extends Servlet> servletClass = ExtraServletUtil.createServlet(s);
if (servletClass != null) {
String path = "/extra/" + servletClass.getSimpleName() + "/*";
LOG.info("binding " + servletClass.getCanonicalName() + " to " + path);
nodeServlets.put(path, servletClass);
}
}
}
private void fixUpId() {
if (hasId) {
return;
}
// make sure 'id' has a value.
if (registrationRequest.getConfiguration().id == null || registrationRequest
.getConfiguration().id.isEmpty()) {
registrationRequest.getConfiguration().id =
registrationRequest.getConfiguration().getRemoteHost();
}
hasId = true;
}
void updateConfigWithRealPort() {
if (registrationRequest.getConfiguration().port != 0) {
return;
}
registrationRequest.getConfiguration().port = server.getRealPort();
}
/**
* uses the hub API to get some of its configuration.
* @return json object of the current hub configuration
*/
private GridHubConfiguration getHubConfiguration() throws Exception {
String hubApi =
"http://" + registrationRequest.getConfiguration().getHubHost() + ":"
+ registrationRequest.getConfiguration().getHubPort() + "/grid/api/hub";
URL api = new URL(hubApi);
HttpClient client = httpClientFactory.createClient(api);
String url = api.toExternalForm();
HttpRequest request = new HttpRequest(GET, url);
HttpResponse response = client.execute(request);
try (Reader reader = new StringReader(response.getContentString());
JsonInput jsonInput = new Json().newInput(reader)) {
return GridHubConfiguration.loadFromJSON(jsonInput);
}
}
private boolean isAlreadyRegistered(RegistrationRequest node) {
try {
String tmp =
"http://" + node.getConfiguration().getHubHost() + ":"
+ node.getConfiguration().getHubPort() + "/grid/api/proxy";
URL api = new URL(tmp);
HttpClient client = httpClientFactory.createClient(api);
String id = node.getConfiguration().id;
if (id == null) {
id = node.getConfiguration().getRemoteHost();
}
HttpRequest request = new HttpRequest(GET, api.toExternalForm() + "?id=" + id);
HttpResponse response = client.execute(request);
if (response.getStatus() != 200) {
throw new GridException(String.format("The hub responded with %s", response.getStatus()));
}
Map<String, Object> o = extractObject(response);
return (Boolean) o.get("success");
} catch (Exception e) {
throw new GridException("The hub is down or not responding: " + e.getMessage());
}
}
private static Map<String, Object> extractObject(HttpResponse resp) {
return new Json().toType(resp.getContentString(), MAP_TYPE);
}
}