/
Engine.java
411 lines (355 loc) · 15.3 KB
/
Engine.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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
/*
* The MIT License
*
* Copyright (c) 2004-2009, Sun Microsystems, Inc., Kohsuke Kawaguchi
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package hudson.remoting;
import hudson.remoting.Channel.Mode;
import org.jenkinsci.remoting.engine.JnlpProtocol;
import org.jenkinsci.remoting.engine.JnlpProtocolFactory;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.URL;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
import java.util.logging.Logger;
import static java.util.logging.Level.INFO;
import static org.jenkinsci.remoting.engine.EngineUtil.readLine;
/**
* Slave agent engine that proactively connects to Jenkins master.
*
* @author Kohsuke Kawaguchi
*/
public class Engine extends Thread {
/**
* Thread pool that sets {@link #CURRENT}.
*/
private final ExecutorService executor = Executors.newCachedThreadPool(new ThreadFactory() {
private final ThreadFactory defaultFactory = Executors.defaultThreadFactory();
public Thread newThread(final Runnable r) {
Thread t = defaultFactory.newThread(new Runnable() {
public void run() {
CURRENT.set(Engine.this);
r.run();
}
});
t.setDaemon(true);
return t;
}
});
/**
* @deprecated
* Use {@link #events}.
*/
@Deprecated
public final EngineListener listener;
private final EngineListenerSplitter events = new EngineListenerSplitter();
/**
* To make Jenkins more graceful against user error,
* JNLP agent can try to connect to multiple possible Jenkins URLs.
* This field specifies those candidate URLs, such as
* "http://foo.bar/jenkins/".
*/
private List<URL> candidateUrls;
/**
* URL that points to Jenkins's tcp slave agent listener, like <tt>http://myhost/hudson/</tt>
*
* <p>
* This value is determined from {@link #candidateUrls} after a successful connection.
* Note that this URL <b>DOES NOT</b> have "tcpSlaveAgentListener" in it.
*/
private URL hudsonUrl;
private final String secretKey;
public final String slaveName;
private String credentials;
private String proxyCredentials = System.getProperty("proxyCredentials");
/**
* See Main#tunnel in the jnlp-agent module for the details.
*/
private String tunnel;
private boolean noReconnect;
private JarCache jarCache = new FileSystemJarCache(new File(System.getProperty("user.home"),".jenkins/cache/jars"),true);
public Engine(EngineListener listener, List<URL> hudsonUrls, String secretKey, String slaveName) {
this.listener = listener;
this.events.add(listener);
this.candidateUrls = hudsonUrls;
this.secretKey = secretKey;
this.slaveName = slaveName;
if(candidateUrls.isEmpty())
throw new IllegalArgumentException("No URLs given");
}
/**
* Configures JAR caching for better performance.
* @since 2.24
*/
public void setJarCache(JarCache jarCache) {
this.jarCache = jarCache;
}
public URL getHudsonUrl() {
return hudsonUrl;
}
public void setTunnel(String tunnel) {
this.tunnel = tunnel;
}
public void setCredentials(String creds) {
this.credentials = creds;
}
public void setProxyCredentials(String proxyCredentials) {
this.proxyCredentials = proxyCredentials;
}
public void setNoReconnect(boolean noReconnect) {
this.noReconnect = noReconnect;
}
public void addListener(EngineListener el) {
events.add(el);
}
public void removeListener(EngineListener el) {
events.remove(el);
}
@SuppressWarnings({"ThrowableInstanceNeverThrown"})
@Override
public void run() {
// Create the protocols that will be attempted to connect to the master.
List<JnlpProtocol> protocols = JnlpProtocolFactory.createProtocols(secretKey, slaveName, events);
try {
boolean first = true;
while(true) {
if(first) {
first = false;
} else {
if(noReconnect)
return; // exit
}
events.status("Locating server among " + candidateUrls);
Throwable firstError=null;
String host=null;
String port=null;
for (URL url : candidateUrls) {
String s = url.toExternalForm();
if(!s.endsWith("/")) s+='/';
URL salURL = new URL(s+"tcpSlaveAgentListener/");
// find out the TCP port
HttpURLConnection con = (HttpURLConnection)Util.openURLConnection(salURL);
if (credentials != null) {
// TODO /tcpSlaveAgentListener is unprotected so why do we need to pass any credentials?
String encoding = Base64.encode(credentials.getBytes("UTF-8"));
con.setRequestProperty("Authorization", "Basic " + encoding);
}
if (proxyCredentials != null) {
String encoding = Base64.encode(proxyCredentials.getBytes("UTF-8"));
con.setRequestProperty("Proxy-Authorization", "Basic " + encoding);
}
try {
try {
con.setConnectTimeout(30000);
con.setReadTimeout(60000);
con.connect();
} catch (IOException x) {
if (firstError == null) {
firstError = new IOException("Failed to connect to " + salURL + ": " + x.getMessage()).initCause(x);
}
continue;
}
port = con.getHeaderField("X-Hudson-JNLP-Port");
if(con.getResponseCode()!=200) {
if(firstError==null)
firstError = new Exception(salURL+" is invalid: "+con.getResponseCode()+" "+con.getResponseMessage());
continue;
}
if(port ==null) {
if(firstError==null)
firstError = new Exception(url+" is not Jenkins");
continue;
}
host = con.getHeaderField("X-Jenkins-JNLP-Host"); // controlled by hudson.TcpSlaveAgentListener.hostName
if (host == null) host=url.getHost();
} finally {
con.disconnect();
}
// this URL works. From now on, only try this URL
hudsonUrl = url;
firstError = null;
candidateUrls = Collections.singletonList(hudsonUrl);
break;
}
if(firstError!=null) {
events.error(firstError);
return;
}
events.status("Handshaking");
Socket jnlpSocket = connect(host,port);
ChannelBuilder channelBuilder = new ChannelBuilder("channel", executor)
.withJarCache(jarCache)
.withMode(Mode.BINARY);
Channel channel = null;
// Try available protocols.
for (JnlpProtocol protocol : protocols) {
events.status("Trying protocol: " + protocol.getName());
try {
channel = protocol.establishChannel(jnlpSocket, channelBuilder);
} catch (IOException ioe) {
events.status("Protocol failed to establish channel", ioe);
}
// On success do not try other protocols.
if (channel != null) {
break;
}
// On failure form a new connection.
jnlpSocket.close();
jnlpSocket = connect(host,port);
}
// If no protocol worked.
if (channel == null) {
onConnectionRejected("None of the protocols were accepted");
continue;
}
events.status("Connected");
channel.join();
events.status("Terminated");
if(noReconnect)
return; // exit
events.onDisconnect();
// try to connect back to the server every 10 secs.
waitForServerToBack();
events.onReconnect();
}
} catch (Throwable e) {
events.error(e);
}
}
private void onConnectionRejected(String greeting) throws InterruptedException {
events.error(new Exception("The server rejected the connection: " + greeting));
Thread.sleep(10*1000);
}
/**
* Connects to TCP slave host:port, with a few retries.
*/
private Socket connect(String host, String port) throws IOException, InterruptedException {
if(tunnel!=null) {
String[] tokens = tunnel.split(":",3);
if(tokens.length!=2)
throw new IOException("Illegal tunneling parameter: "+tunnel);
if(tokens[0].length()>0) host = tokens[0];
if(tokens[1].length()>0) port = tokens[1];
}
String msg = "Connecting to " + host + ':' + port;
events.status(msg);
int retry = 1;
while(true) {
boolean isHttpProxy = false;
InetSocketAddress targetAddress = null;
try {
Socket s = null;
targetAddress = Util.getResolvedHttpProxyAddress(host, Integer.parseInt(port));
if(targetAddress == null) {
targetAddress = new InetSocketAddress(host, Integer.parseInt(port));
} else {
isHttpProxy = true;
}
s = new Socket();
s.connect(targetAddress);
s.setTcpNoDelay(true); // we'll do buffering by ourselves
// set read time out to avoid infinite hang. the time out should be long enough so as not
// to interfere with normal operation. the main purpose of this is that when the other peer dies
// abruptly, we shouldn't hang forever, and at some point we should notice that the connection
// is gone.
s.setSoTimeout(30*60*1000); // 30 mins. See PingThread for the ping interval
if (isHttpProxy) {
String connectCommand = String.format("CONNECT %s:%s HTTP/1.1\r\nHost: %s\r\n\r\n", host, port, host);
s.getOutputStream().write(connectCommand.getBytes("UTF-8")); // TODO: internationalized domain names
BufferedInputStream is = new BufferedInputStream(s.getInputStream());
String line = readLine(is);
String[] responseLineParts = line.split(" ");
if(responseLineParts.length < 2 || !responseLineParts[1].equals("200"))
throw new IOException("Got a bad response from proxy: " + line);
while(!readLine(is).isEmpty()) {
// Do nothing, scrolling through headers returned from proxy
}
}
return s;
} catch (IOException e) {
if(retry++>10) {
String suffix = "";
if(isHttpProxy) {
suffix = " through proxy " + targetAddress.toString();
}
throw new IOException("Failed to connect to " + host + ':' + port + suffix, e);
}
Thread.sleep(1000*10);
events.status(msg+" (retrying:"+retry+")",e);
}
}
}
/**
* Waits for the server to come back.
*/
private void waitForServerToBack() throws InterruptedException {
Thread t = Thread.currentThread();
String oldName = t.getName();
try {
int retries=0;
while(true) {
Thread.sleep(1000*10);
try {
// Jenkins top page might be read-protected. see http://www.nabble.com/more-lenient-retry-logic-in-Engine.waitForServerToBack-td24703172.html
URL url = new URL(hudsonUrl, "tcpSlaveAgentListener/");
retries++;
t.setName(oldName+": trying "+url+" for "+retries+" times");
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setConnectTimeout(5000);
con.setReadTimeout(5000);
con.connect();
if(con.getResponseCode()==200)
return;
LOGGER.info("Master isn't ready to talk to us. Will retry again: response code=" + con.getResponseCode());
} catch (IOException e) {
// report the failure
LOGGER.log(INFO, "Failed to connect to the master. Will retry again",e);
}
}
} finally {
t.setName(oldName);
}
}
/**
* When invoked from within remoted {@link Callable} (that is,
* from the thread that carries out the remote requests),
* this method returns the {@link Engine} in which the remote operations
* run.
*/
public static Engine current() {
return CURRENT.get();
}
private static final ThreadLocal<Engine> CURRENT = new ThreadLocal<Engine>();
private static final Logger LOGGER = Logger.getLogger(Engine.class.getName());
/**
* @deprecated Use {@link JnlpProtocol#GREETING_SUCCESS}.
*/
@Deprecated
public static final String GREETING_SUCCESS = JnlpProtocol.GREETING_SUCCESS;
}