forked from openshift/jenkins-cloud-plugin
/
OpenShiftCloud.java
executable file
·758 lines (632 loc) · 21.1 KB
/
OpenShiftCloud.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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
package hudson.plugins.openshift;
import hudson.Extension;
import hudson.model.AbstractProject;
import hudson.model.Computer;
import hudson.model.Descriptor;
import hudson.model.Hudson;
import hudson.model.Job;
import hudson.model.Label;
import hudson.model.Node;
import hudson.model.Queue;
import hudson.slaves.Cloud;
import hudson.slaves.NodeProvisioner.PlannedNode;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.KeyManager;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import net.sf.json.JSONObject;
import org.apache.commons.codec.binary.Base64;
import org.jvnet.hudson.reactor.ReactorException;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.StaplerRequest;
import com.openshift.client.IApplication;
import com.openshift.client.IOpenShiftConnection;
import com.openshift.client.IUser;
import com.openshift.client.OpenShiftConnectionFactory;
import com.openshift.client.OpenShiftException;
import com.openshift.client.configuration.DefaultConfiguration;
import com.openshift.client.configuration.SystemConfiguration;
import com.openshift.client.configuration.UserConfiguration;
/**
* Represents the available cloud of OpenShift instances for building.
*/
public final class OpenShiftCloud extends Cloud {
private static final Logger LOGGER = Logger.getLogger(OpenShiftCloud.class
.getName());
public static final int APP_NAME_MAX_LENGTH = 32;
public static final String APP_NAME_BUILDER_EXTENSION = "bldr";
public static final String DEFAULT_LABEL = "raw-build";
public static final long DEFAULT_TIMEOUT = 300000;
private String username;
private String password;
private String authKey;
private String authIV;
private String brokerHost;
private String brokerPort;
private String proxyHost;
private final int proxyPort;
private final String defaultBuilderSize;
private boolean ignoreBrokerCertCheck = true;
private transient File privateKey;
private String brokerAuthKey;
private String brokerAuthIV;
private transient IOpenShiftConnection service;
static {
javax.net.ssl.HttpsURLConnection
.setDefaultHostnameVerifier(new javax.net.ssl.HostnameVerifier() {
public boolean verify(String hostname,
javax.net.ssl.SSLSession sslSession) {
if (hostname.equals("localhost")) {
return true;
}
return false;
}
});
}
@DataBoundConstructor
public OpenShiftCloud(String username, String password, String brokerHost,
String brokerPort, String proxyHost, int proxyPort,
boolean ignoreBrokerCertCheck, String defaultBuilderSize)
throws IOException {
super("OpenShift Cloud");
this.username = username;
this.password = password;
this.brokerHost = brokerHost;
this.brokerPort = brokerPort;
this.proxyHost = proxyHost;
this.proxyPort = proxyPort;
this.defaultBuilderSize = defaultBuilderSize;
this.ignoreBrokerCertCheck = ignoreBrokerCertCheck;
}
public IOpenShiftConnection getOpenShiftConnection() throws IOException {
if (service == null) {
try {
UserConfiguration userConfiguration = new UserConfiguration(
new SystemConfiguration(new DefaultConfiguration()));
if (username == null)
username = userConfiguration.getRhlogin();
authKey = getBrokerAuthKey();
authIV = getBrokerAuthIV();
String url = null;
if (brokerHost == null) {
brokerHost = userConfiguration.getLibraServer();
url = "https://" + brokerHost.trim();
LOGGER.info("Initiating Java Client Service - Configured for default OpenShift Server "
+ url);
} else {
url = "https://" + brokerHost.trim();
if (brokerPort != null && brokerPort.trim().length() > 0)
url += ":" + brokerPort.trim();
LOGGER.info("Initiating Java Client Service - Configured for OpenShift Server "
+ url);
}
service = new OpenShiftConnectionFactory().getConnection(
username,
username,
password,
authKey,
authIV,
url);
service.setEnableSSLCertChecks(!ignoreBrokerCertCheck);
if (proxyHost != null && proxyHost.length() > 0) {
service.setProxySet(true);
service.setProxyHost(proxyHost.trim());
service.setProxyPort(Integer.toString(proxyPort));
}
} catch (OpenShiftException e) {
throw new IOException(e);
}
}
return service;
}
public String getUsername() {
return username;
}
public String getAuthKey() {
return authKey;
}
public String getAuthIV() {
return authIV;
}
public String getDefaultBuilderSize() {
return defaultBuilderSize;
}
public String getPassword() {
return password;
}
public String getBrokerHost() {
return brokerHost;
}
public String getBrokerPort() {
return brokerPort;
}
public File getPrivateKey() {
if (privateKey == null) {
privateKey = locateKey();
}
return privateKey;
}
private String getBrokerAuthKey() throws IOException {
if (brokerAuthKey == null) {
String homeDir = System.getenv("HOME");
brokerAuthKey = fileToString(homeDir + "/.auth/token");
}
return brokerAuthKey;
}
private String getBrokerAuthIV() throws IOException {
if (brokerAuthIV == null) {
String homeDir = System.getenv("HOME");
brokerAuthIV = fileToString(homeDir + "/.auth/iv");
}
return brokerAuthIV;
}
private String fileToString(String filePath) throws IOException {
File file = new File(filePath);
String fileString = null;
if (file.exists()) {
FileReader fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr);
StringBuilder sb = new StringBuilder();
String s;
while ((s = br.readLine()) != null) {
sb.append(s);
}
fr.close();
fileString = sb.toString();
}
return fileString;
}
public String getProxyHost() {
return proxyHost;
}
public int getProxyPort() {
return proxyPort;
}
public boolean getIgnoreBrokerCertCheck() {
return ignoreBrokerCertCheck;
}
private File locateKey() {
// Look in the environment variables for the key location
String dataDir = System.getenv("OPENSHIFT_DATA_DIR");
if (dataDir == null) {
LOGGER.warning("Warning OPENSHIFT_DATA_DIR not found, resorting to test value");
dataDir = System.getenv("HOME");
}
// Get the location of the private key
return new File(dataDir + "/.ssh/jenkins_id_rsa");
}
public boolean canProvision(Label label) {
return true;
}
/**
* Returns whether a new builder should be provisioned. If an existing build
* type exists or the user is out of application capacity, the result will
* be false.
*
* @return whether a new builder should be provisioned
*/
protected boolean hasCapacity(String name, IUser user)
throws IOException {
LOGGER.info("Checking capacity");
long maxGears = user.getMaxGears();
long consumedGears = user.getConsumedGears();
LOGGER.info("User has consumed " + consumedGears + " of " + maxGears
+ " gears.");
boolean hasCapacity = consumedGears < maxGears;
if (!hasCapacity) {
LOGGER.info("No capacity remaining. Not provisioning...");
return false;
}
return true;
}
protected boolean builderExists(String name, IUser userInfo)
throws IOException, OpenShiftException {
List<IApplication> applications = userInfo.getDefaultDomain().getApplications();
LOGGER.info("Capacity remaining - checking for existing type...");
for (@SuppressWarnings("unchecked")
Iterator<IApplication> i = applications.iterator(); i.hasNext();) {
IApplication app = i.next();
if (app.getName().equals(name)) {
LOGGER.info("Found an existing builder. Not provisioning...");
return true;
}
}
LOGGER.info("No suitable builders found.");
return false;
}
/* public UserResource getUserInfo() throws IOException {
return getUserInfo(false);
}
public IUser getIUser() throws IOException {
UserResource user = new UserResource();
service = getOpenShiftService();
if (authKey != null)
user = new InternalUser(username, authKey, authIV, service);
else
user = new InternalUser(username, OpenShiftCloud.get()
.getPassword(), service);
return user;
}
public UserResource getUserInfo(boolean force) throws IOException {
try {
service = getOpenShiftService();
IUser user = getIUser();
if (authKey != null)
user = new InternalUser(username, authKey, authIV, service);
else
user = new InternalUser(username, OpenShiftCloud.get()
.getPassword(), service);
UserInfo userInfo = service.getUserInfo(user);
return userInfo;
} catch (OpenShiftException e) {
e.printStackTrace();
throw new IOException(e);
}
} */
public Collection<PlannedNode> provision(Label label, int excessWorkload) {
service = null;
List<PlannedNode> result = new ArrayList<PlannedNode>();
LOGGER.info("Provisioning new node for workload = " + excessWorkload
+ " and label = " + label);
// First, sync the state of the running applications and the Jenkins
// slaves
List<OpenShiftSlave> slaves = null;
try {
slaves = getSlaves();
for (OpenShiftSlave slave : slaves) {
Hudson.getInstance().addNode(slave);
}
} catch (Exception e) {
LOGGER.log(Level.SEVERE,
"Exception caught trying to load existing slaves", e);
}
String builderType = "diy-0.1";
String builderName = "raw" + APP_NAME_BUILDER_EXTENSION;
String builderSize = OpenShiftCloud.get().getDefaultBuilderSize();
long builderTimeout = DEFAULT_TIMEOUT;
String labelStr = DEFAULT_LABEL;
// Derive the builderType
if (label != null) {
labelStr = label.toString();
AbstractProject job = Hudson.getInstance().getItemByFullName(
labelStr, AbstractProject.class);
if (job != null) {
OpenShiftBuilderSizeJobProperty osbsjp = ((OpenShiftBuilderSizeJobProperty) job
.getProperty(OpenShiftBuilderSizeJobProperty.class));
builderSize = osbsjp.builderSize;
OpenShiftBuilderTypeJobProperty osbtjp = ((OpenShiftBuilderTypeJobProperty) job
.getProperty(OpenShiftBuilderTypeJobProperty.class));
builderType = osbtjp.builderType;
OpenShiftBuilderTimeoutJobProperty timeoutJobProperty = ((OpenShiftBuilderTimeoutJobProperty) job
.getProperty(OpenShiftBuilderTimeoutJobProperty.class));
if (timeoutJobProperty != null)
builderTimeout = timeoutJobProperty.builderTimeout;
else
builderTimeout = -1;
if (labelStr.endsWith("-build")) {
builderName = labelStr.substring(0,
labelStr.indexOf("-build"));
}
if (builderName.length() > (APP_NAME_MAX_LENGTH - APP_NAME_BUILDER_EXTENSION
.length())) {
builderName = builderName.substring(0, APP_NAME_MAX_LENGTH
- APP_NAME_BUILDER_EXTENSION.length());
}
builderName = builderName + APP_NAME_BUILDER_EXTENSION;
}
} else {
LOGGER.info("Cancelling build - Label is null");
cancelBuild(builderName);
throw new UnsupportedOperationException("No Label");
}
final String framework = builderType;
final String name = builderName;
final String size = builderSize;
final String plannedNodeName = labelStr;
final long timeout = builderTimeout;
final int executors = excessWorkload;
try {
// Create builders for any excess workload
if (excessWorkload > 0) {
OpenShiftSlave slave = getSlave(slaves, builderName);
IUser user = this.getOpenShiftConnection().getUser();
if (slave != null && builderExists(builderName, user)) {
LOGGER.info("Slave exists without corresponding builder. Deleting slave");
slave.terminate();
return result;
}
if (!hasCapacity(name, user)) {
LOGGER.info("Not provisioning new builder...");
} else {
reloadConfig(label);
PlannedNode node = new PlannedNode(plannedNodeName,
Computer.threadPoolForRemoting
.submit(new Callable<Node>() {
public Node call() throws Exception {
// Provision a new slave builder
OpenShiftSlave slave = new OpenShiftSlave(
name, framework, size,
plannedNodeName, timeout,
executors);
try {
slave.provision();
Hudson.getInstance().addNode(
slave);
return slave;
} catch (Exception e) {
LOGGER.warning("Unable to provision node "
+ e);
cancelBuild(name,
plannedNodeName);
throw e;
}
}
}), executors);
result.add(node);
}
LOGGER.info("Provisioned " + result.size() + " new nodes");
if (result.size() == 0) {
cancelBuild(builderName, label.getName());
}
}
} catch (Exception e) {
LOGGER.log(Level.SEVERE, "Exception caught trying to provision", e);
}
return result;
}
protected void reloadConfig(Label label) throws IOException,
InterruptedException, ReactorException {
LOGGER.info("Reloading configuration for " + label.toString() + "...");
String ip = System.getenv("OPENSHIFT_INTERNAL_IP");
String port = System.getenv("OPENSHIFT_INTERNAL_PORT");
String username = System.getenv("JENKINS_USERNAME");
String password = System.getenv("JENKINS_PASSWORD");
String name = label.toString();
// if (name.endsWith("-build")) {
// name = name.substring(0, name.indexOf("-build"));
// }
URL url = new URL("http://" + ip + ":" + port + "/job/" + name
+ "/config.xml");
HttpURLConnection connection = createConnection(url, username, password);
connection.setRequestMethod("GET");
String get = readToString(connection.getInputStream());
LOGGER.info("Reload config " + get);
connection = createConnection(url, username, password);
connection.setRequestMethod("POST");
connection.setDoOutput(true);
writeTo(get.getBytes(), connection.getOutputStream());
String result = readToString(connection.getInputStream());
LOGGER.info("Reload result " + result);
}
protected void writeTo(byte[] data, OutputStream outputStream)
throws IOException {
outputStream.write(data);
outputStream.flush();
outputStream.close();
}
protected HttpURLConnection createConnection(URL url, String username,
String password) throws IOException {
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
// HttpsURLConnection httpsConnection = (HttpsURLConnection) service;
// httpsConnection.setHostnameVerifier(new NoopHostnameVerifier());
// setPermissiveSSLSocketFactory(httpsConnection);
connection.setUseCaches(false);
connection.setDoInput(true);
connection.setAllowUserInteraction(true);
connection.setRequestProperty("Content-Type", "text/plain");
connection.setInstanceFollowRedirects(true);
LOGGER.info("Using credentials " + username + ":" + password);
String basicAuth = "Basic "
+ new String(Base64.encodeBase64(new String(username + ":"
+ password).getBytes()));
connection.setRequestProperty("Authorization", basicAuth);
return connection;
}
private void setPermissiveSSLSocketFactory(HttpsURLConnection service) {
try {
SSLContext sslContext = SSLContext.getInstance("SSL");
sslContext.init(new KeyManager[0],
new TrustManager[] { new PermissiveTrustManager() },
new SecureRandom());
SSLSocketFactory socketFactory = sslContext.getSocketFactory();
((HttpsURLConnection) service)
.setSSLSocketFactory(socketFactory);
} catch (KeyManagementException e) {
// ignore
} catch (NoSuchAlgorithmException e) {
// ignore
}
}
private String readToString(InputStream inputStream) throws IOException {
if (inputStream == null) {
return null;
}
byte[] data = new byte[inputStream.available()];
inputStream.read(data);
return new String(data);
}
protected boolean isBuildRunning(Label label) {
boolean running = false;
Job job = (Job) Hudson.getInstance().getItem(label.getName());
if (job != null) {
Queue.Item item = job.getQueueItem();
if (item != null)
running = true;
}
return running;
}
protected OpenShiftSlave getSlave(List<OpenShiftSlave> slaves,
String builderName) {
if (slaves != null){
for (OpenShiftSlave slave : slaves) {
LOGGER.info("slaveExists " + slave.getDisplayName() + " "
+ builderName);
if (slave.getDisplayName().equals(builderName))
return slave;
}
}
return null;
}
protected void cancelBuild(String builderName) {
cancelBuild(builderName, null);
}
protected void cancelBuild(String builderName, String label) {
LOGGER.info("Cancelling build");
try {
Node existingNode = Hudson.getInstance().getNode(builderName);
if (existingNode != null && existingNode instanceof OpenShiftSlave) {
((OpenShiftSlave) existingNode).terminate();
}
Job job = null;
if (label != null)
job = (Job) Hudson.getInstance().getItem(label);
if (job != null) {
Queue.Item item = job.getQueueItem();
if (item != null) {
Queue queue = Queue.getInstance();
boolean cancelled = queue.cancel(item);
LOGGER.warning("Build for job " + job.getName()
+ " has been cancelled");
}
} else {
Queue queue = Hudson.getInstance().getQueue();
Queue.Item[] items = queue.getItems();
if (items != null && items.length > 0) {
boolean cancelled = queue.cancel(items[0]);
LOGGER.warning("Build for label/builderName " + label + "/"
+ builderName + " has been cancelled");
}
}
} catch (Exception e) {
LOGGER.log(Level.SEVERE,
"Exception caught trying to terminate slave", e);
}
}
@Extension
public static final class DescriptorImpl extends Descriptor<Cloud> {
private String username;
private String password;
private String brokerHost;
private String brokerPort;
private String proxyHost;
private String defaultBuilderSize;
private int proxyPort;
private boolean ignoreBrokerCertCheck;
public String getDisplayName() {
return "OpenShift Cloud";
}
@Override
public boolean configure(StaplerRequest req, JSONObject o)
throws FormException {
username = o.getString("username");
password = o.getString("password");
brokerHost = o.getString("brokerHost");
brokerPort = o.getString("brokerPort");
proxyHost = o.getString("proxyHost");
proxyPort = o.getInt("proxyPort");
ignoreBrokerCertCheck = o.getBoolean("ignoreBrokerCertCheck");
defaultBuilderSize = o.getString("defaultBuilderSize");
save();
return super.configure(req, o);
}
public String getUsername() {
return username;
}
public String getPassword() {
return password;
}
public String getBrokerHost() {
return brokerHost;
}
public String getBrokerPort() {
return brokerPort;
}
public String getProxyHost() {
return proxyHost;
}
public int getProxyPort() {
return proxyPort;
}
public boolean getIgnoreBrokerCertCheck() {
return ignoreBrokerCertCheck;
}
public String getDefaultBuilderSize() {
return defaultBuilderSize;
}
}
/**
* Gets the first {@link OpenShiftCloud} instance configured or null
*/
public static OpenShiftCloud get() {
return Hudson.getInstance().clouds.get(OpenShiftCloud.class);
}
@SuppressWarnings("unchecked")
protected List<OpenShiftSlave> getSlaves() throws IOException, OpenShiftException {
List<IApplication> appInfos = this.getOpenShiftConnection().getUser().getDefaultDomain().getApplications();
List<OpenShiftSlave> slaveList = new ArrayList<OpenShiftSlave>();
for (Iterator<IApplication> i = appInfos.iterator(); i.hasNext();) {
IApplication appInfo = i.next();
String appName = appInfo.getName();
if (appName.endsWith(APP_NAME_BUILDER_EXTENSION)) {
Node node = Hudson.getInstance().getNode(appName);
OpenShiftSlave slave = null;
if (node == null || !(node instanceof OpenShiftSlave)) {
LOGGER.info("Didn't find existing slave for: " + appName);
try {
String framework = appInfo.getCartridge().getName();
slave = new OpenShiftSlave(appName, framework,
OpenShiftCloud.get().getDefaultBuilderSize(),
DEFAULT_LABEL, DEFAULT_TIMEOUT, 1);
} catch (Exception e) {
throw new IOException(e);
}
} else {
slave = (OpenShiftSlave) node;
LOGGER.info("Found existing slave for: " + appName);
}
slaveList.add(slave);
}
}
return slaveList;
}
private static class NoopHostnameVerifier implements HostnameVerifier {
public boolean verify(String hostname, SSLSession sslSession) {
return true;
}
}
private static class PermissiveTrustManager implements X509TrustManager {
public X509Certificate[] getAcceptedIssuers() {
return null;
}
public void checkServerTrusted(X509Certificate[] chain, String authType)
throws CertificateException {
}
public void checkClientTrusted(X509Certificate[] chain, String authType)
throws CertificateException {
}
}
}