This repository was archived by the owner on Nov 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 169
Expand file tree
/
Copy pathRemoteBuildConfiguration.java
More file actions
1301 lines (1077 loc) · 51.8 KB
/
Copy pathRemoteBuildConfiguration.java
File metadata and controls
1301 lines (1077 loc) · 51.8 KB
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
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package org.jenkinsci.plugins.ParameterizedRemoteTrigger;
import hudson.AbortException;
import hudson.FilePath;
import hudson.EnvVars;
import hudson.Launcher;
import hudson.console.ConsoleNote;
import hudson.Extension;
import hudson.util.CopyOnWriteList;
import hudson.util.ListBoxModel;
import hudson.model.AbstractBuild;
import hudson.model.BuildListener;
import hudson.model.Cause;
import hudson.model.Result;
import hudson.model.AbstractProject;
import hudson.tasks.Builder;
import hudson.tasks.BuildStepDescriptor;
import net.sf.json.JSONObject;
import net.sf.json.JSONArray;
import net.sf.json.JSONSerializer;
//import net.sf.json.
//import net.sf.json.
import net.sf.json.util.JSONUtils;
import org.jenkinsci.plugins.tokenmacro.TokenMacro;
import org.apache.commons.lang.StringUtils;
import org.jenkinsci.plugins.tokenmacro.MacroEvaluationException;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.StaplerRequest;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import org.apache.commons.codec.binary.Base64;
/**
*
* @author Maurice W.
*
*/
public class RemoteBuildConfiguration extends Builder {
private final String token;
private final String remoteJenkinsName;
private final String job;
private final boolean shouldNotFailBuild;
private final int pollInterval;
private final int connectionRetryLimit = 5;
private final boolean preventRemoteBuildQueue;
private final boolean blockBuildUntilComplete;
private final boolean enhancedLogging;
// "parameters" is the raw string entered by the user
private final String parameters;
// "parameterList" is the cleaned-up version of "parameters" (stripped out comments, character encoding, etc)
private final List<String> parameterList;
private static String paramerizedBuildUrl = "/buildWithParameters";
private static String normalBuildUrl = "/build";
//private static String normalBuildUrl = "/buildWithParameters";
private static String buildTokenRootUrl = "/buildByToken";
private final boolean overrideAuth;
private CopyOnWriteList<Auth> auth = new CopyOnWriteList<Auth>();
private final boolean loadParamsFromFile;
private String parameterFile = "";
private String queryString = "";
@DataBoundConstructor
public RemoteBuildConfiguration(String remoteJenkinsName, boolean shouldNotFailBuild, String job, String token,
String parameters, boolean enhancedLogging, JSONObject overrideAuth, JSONObject loadParamsFromFile, boolean preventRemoteBuildQueue,
boolean blockBuildUntilComplete, int pollInterval) throws MalformedURLException {
this.token = token.trim();
this.remoteJenkinsName = remoteJenkinsName;
this.job = job.trim();
this.shouldNotFailBuild = shouldNotFailBuild;
this.preventRemoteBuildQueue = preventRemoteBuildQueue;
this.blockBuildUntilComplete = blockBuildUntilComplete;
this.pollInterval = pollInterval;
this.enhancedLogging = enhancedLogging;
if (overrideAuth != null && overrideAuth.has("auth")) {
this.overrideAuth = true;
this.auth.replaceBy(new Auth(overrideAuth.getJSONObject("auth")));
} else {
this.overrideAuth = false;
this.auth.replaceBy(new Auth(new JSONObject()));
}
if (loadParamsFromFile != null && loadParamsFromFile.has("parameterFile")) {
this.loadParamsFromFile = true;
this.parameterFile = loadParamsFromFile.getString("parameterFile");
this.parameters = "";
//manually add a leading-slash if we don't have one
if( this.parameterFile.charAt(0) != '/' ){
this.parameterFile = "/" + this.parameterFile;
}
} else {
this.loadParamsFromFile = false;
this.parameters = parameters;
}
// TODO: clean this up a bit
// split the parameter-string into an array based on the new-line character
String[] params = parameters.split("\n");
// convert the String array into a List of Strings, and remove any empty entries
this.parameterList = new ArrayList<String>(Arrays.asList(params));
}
public RemoteBuildConfiguration(String remoteJenkinsName, boolean shouldNotFailBuild,
boolean preventRemoteBuildQueue, boolean blockBuildUntilComplete, int pollInterval, String job,
String token, String parameters, boolean enhancedLogging) throws MalformedURLException {
this.token = token.trim();
this.remoteJenkinsName = remoteJenkinsName;
this.parameters = parameters;
this.enhancedLogging = enhancedLogging;
this.job = job.trim();
this.shouldNotFailBuild = shouldNotFailBuild;
this.preventRemoteBuildQueue = preventRemoteBuildQueue;
this.blockBuildUntilComplete = blockBuildUntilComplete;
this.pollInterval = pollInterval;
this.overrideAuth = false;
this.auth.replaceBy(new Auth(null));
this.loadParamsFromFile = false;
// split the parameter-string into an array based on the new-line character
String[] params = parameters.split("\n");
// convert the String array into a List of Strings, and remove any empty entries
this.parameterList = new ArrayList<String>(Arrays.asList(params));
}
/**
* Reads a file from the jobs workspace, and loads the list of parameters from with in it. It will also call
* ```getCleanedParameters``` before returning.
*
* @param build
* @return List<String> of build parameters
*/
private List<String> loadExternalParameterFile(AbstractBuild<?, ?> build) {
FilePath workspace = build.getWorkspace();
BufferedReader br = null;
List<String> ParameterList = new ArrayList<String>();
try {
String filePath = workspace + this.getParameterFile();
String sCurrentLine;
String fileContent = "";
br = new BufferedReader(new FileReader(filePath));
while ((sCurrentLine = br.readLine()) != null) {
// fileContent += sCurrentLine;
ParameterList.add(sCurrentLine);
}
// ParameterList = new ArrayList<String>(Arrays.asList(fileContent));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
if (br != null) {
br.close();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
// FilePath.
return getCleanedParameters(ParameterList);
}
/**
* Strip out any empty strings from the parameterList
*/
private void removeEmptyElements(Collection<String> collection) {
collection.removeAll(Arrays.asList(null, ""));
collection.removeAll(Arrays.asList(null, " "));
}
/**
* Convenience method
*
* @return List<String> of build parameters
*/
private List<String> getCleanedParameters() {
return getCleanedParameters(this.getParameterList());
}
/**
* Same as "getParameterList", but removes comments and empty strings Notice that no type of character encoding is
* happening at this step. All encoding happens in the "buildUrlQueryString" method.
*
* @param List
* <String> parameters
* @return List<String> of build parameters
*/
private List<String> getCleanedParameters(List<String> parameters) {
List<String> params = new ArrayList<String>(parameters);
removeEmptyElements(params);
removeCommentsFromParameters(params);
return params;
}
/**
* Similar to "replaceToken", but acts on a list in place of just a single string
*
* @param build
* @param listener
* @param params
* List<String> of params to be tokenized/replaced
* @return List<String> of resolved variables/tokens
*/
private List<String> replaceTokens(AbstractBuild<?, ?> build, BuildListener listener, List<String> params) {
List<String> tokenizedParams = new ArrayList<String>();
for (int i = 0; i < params.size(); i++) {
tokenizedParams.add(replaceToken(build, listener, params.get(i)));
// params.set(i, replaceToken(build, listener, params.get(i)));
}
return tokenizedParams;
}
/**
* Resolves any environment variables in the string
*
* @param build
* @param listener
* @param input
* String to be tokenized/replaced
* @return String with resolved Environment variables
*/
private String replaceToken(AbstractBuild<?, ?> build, BuildListener listener, String input) {
try {
return TokenMacro.expandAll(build, listener, input);
} catch (Exception e) {
listener.getLogger().println(
String.format("Failed to resolve parameters in string %s due to following error:\n%s", input,
e.getMessage()));
}
return input;
}
/**
* Strip out any comments (lines that start with a #) from the collection that is passed in.
*/
private void removeCommentsFromParameters(Collection<String> collection) {
List<String> itemsToRemove = new ArrayList<String>();
for (String parameter : collection) {
if (parameter.indexOf("#") == 0) {
itemsToRemove.add(parameter);
}
}
collection.removeAll(itemsToRemove);
}
/**
* Return the Collection<String> in an encoded query-string
*
* @return query-parameter-formated URL-encoded string
* @throws InterruptedException
* @throws IOException
* @throws MacroEvaluationException
*/
private String buildUrlQueryString(Collection<String> parameters) {
// List to hold the encoded parameters
List<String> encodedParameters = new ArrayList<String>();
for (String parameter : parameters) {
// Step #1 - break apart the parameter-pairs (because we don't want to encode the "=" character)
String[] splitParameters = parameter.split("=");
// List to hold each individually encoded parameter item
List<String> encodedItems = new ArrayList<String>();
for (String item : splitParameters) {
try {
// Step #2 - encode each individual parameter item add the encoded item to its corresponding list
encodedItems.add(encodeValue(item));
} catch (Exception e) {
// do nothing
// because we are "hard-coding" the encoding type, there is a 0% chance that this will fail.
}
}
// Step #3 - reunite the previously separated parameter items and add them to the corresponding list
encodedParameters.add(StringUtils.join(encodedItems, "="));
}
return StringUtils.join(encodedParameters, "&");
}
/**
* Lookup up a Remote Jenkins Server based on display name
*
* @param displayName
* Name of the configuration you are looking for
* @return A RemoteSitez object
*/
public RemoteJenkinsServer findRemoteHost(String displayName) {
RemoteJenkinsServer match = null;
for (RemoteJenkinsServer host : this.getDescriptor().remoteSites) {
// if we find a match, then stop looping
if (displayName.equals(host.getDisplayName())) {
match = host;
break;
}
}
return match;
}
/**
* Helper function to allow values to be added to the query string from any method.
*
* @param item
*/
private void addToQueryString(String item) {
String currentQueryString = this.getQueryString();
String newQueryString = "";
if (currentQueryString == null || currentQueryString.equals("")) {
newQueryString = item;
} else {
newQueryString = currentQueryString + "&" + item;
}
this.setQueryString(newQueryString);
}
/**
* Build the proper URL to trigger the remote build
*
* All passed in string have already had their tokens replaced with real values. All 'params' also have the proper
* character encoding
*
* @param job
* Name of the remote job
* @param securityToken
* Security token used to trigger remote job
* @param params
* Parameters for the remote job
* @return fully formed, fully qualified remote trigger URL
*/
private String buildTriggerUrl(String job, String securityToken, Collection<String> params, boolean isRemoteJobParameterized) {
RemoteJenkinsServer remoteServer = this.findRemoteHost(this.getRemoteJenkinsName());
String triggerUrlString = remoteServer.getAddress().toString();
// start building the proper URL based on known capabiltiies of the remote server
if (remoteServer.getHasBuildTokenRootSupport()) {
triggerUrlString += buildTokenRootUrl;
triggerUrlString += getBuildTypeUrl(isRemoteJobParameterized);
this.addToQueryString("job=" + job);
} else {
triggerUrlString += "/job/";
triggerUrlString += job;
triggerUrlString += getBuildTypeUrl(isRemoteJobParameterized);
}
// don't try to include a security token in the URL if none is provided
if (!securityToken.equals("")) {
this.addToQueryString("token=" + encodeValue(securityToken));
}
// turn our Collection into a query string
String buildParams = buildUrlQueryString(params);
if (!buildParams.isEmpty()) {
this.addToQueryString(buildParams);
}
// by adding "delay=0", this will (theoretically) force this job to the top of the remote queue
this.addToQueryString("delay=0");
triggerUrlString += "?" + this.getQueryString();
return triggerUrlString;
}
/**
* Build the proper URL for GET calls
*
* All passed in string have already had their tokens replaced with real values.
*
* @param job
* Name of the remote job
* @param securityToken
* Security token used to trigger remote job
* @return fully formed, fully qualified remote trigger URL
*/
private String buildGetUrl(String job, String securityToken) {
RemoteJenkinsServer remoteServer = this.findRemoteHost(this.getRemoteJenkinsName());
String urlString = remoteServer.getAddress().toString();
urlString += "/job/";
urlString += job;
// don't try to include a security token in the URL if none is provided
if (!securityToken.equals("")) {
this.addToQueryString("token=" + encodeValue(securityToken));
}
return urlString;
}
/**
* Convenience function to mark the build as failed. It's intended to only be called from this.perform();
*
* @param e
* Exception that caused the build to fail
* @param listener
* Build Listener
* @throws IOException
*/
private void failBuild(Exception e, BuildListener listener) throws IOException {
System.out.print(e.getStackTrace());
if (this.getShouldNotFailBuild()) {
listener.error("Remote build failed for the following reason, but the build will continue:");
listener.error(e.getMessage());
} else {
listener.error("Remote build failed for the following reason:");
throw new AbortException(e.getMessage());
}
}
@Override
public boolean perform(AbstractBuild build, Launcher launcher, BuildListener listener) throws InterruptedException,
IOException, IllegalArgumentException {
String tempJobName=(this.getJob().trim().replace("/", "/job/"));
RemoteJenkinsServer remoteServer = this.findRemoteHost(this.getRemoteJenkinsName());
String remoteJobURL=remoteServer.getAddress().toString() + "/job/" + tempJobName;
// Stores the status of the remote build
String buildStatusStr = "UNKNOWN";
if (remoteServer == null) {
this.failBuild(new Exception("No remote host is defined for this job."), listener);
return true;
}
String remoteServerURL = remoteServer.getAddress().toString();
List<String> cleanedParams = null;
if (this.getLoadParamsFromFile()) {
cleanedParams = loadExternalParameterFile(build);
} else {
// tokenize all variables and encode all variables, then build the fully-qualified trigger URL
cleanedParams = getCleanedParameters();
cleanedParams = replaceTokens(build, listener, cleanedParams);
}
String jobName = replaceToken(build, listener, tempJobName);
String securityToken = replaceToken(build, listener, this.getToken());
boolean isRemoteParameterized = isRemoteJobParameterized(tempJobName, build, listener);
String triggerUrlString = this.buildTriggerUrl(tempJobName, securityToken, cleanedParams, isRemoteParameterized);
// Trigger remote job
// print out some debugging information to the console
listener.getLogger().println("Triggering this remote job: " + this.getJob());
// get the ID of the Next Job to run.
if (this.getPreventRemoteBuildQueue()) {
listener.getLogger().println("Checking that the remote job " + jobName + " is not currently building.");
String preCheckUrlString = this.buildGetUrl(jobName, securityToken);
preCheckUrlString += "/lastBuild";
preCheckUrlString += "/api/json/";
JSONObject preCheckResponse = sendHTTPCall(preCheckUrlString, "GET", build, listener);
if ( preCheckResponse != null ) {
// check the latest build on the remote server to see if it's running - if so wait until it has stopped.
// if building is true then the build is running
// if result is null the build hasn't finished - but might not have started running.
while (preCheckResponse.getBoolean("building") == true || preCheckResponse.getString("result") == null) {
listener.getLogger().println("Remote build is currently running - waiting for it to finish.");
preCheckResponse = sendHTTPCall(preCheckUrlString, "POST", build, listener);
listener.getLogger().println("Waiting for " + this.pollInterval + " seconds until next retry.");
// Sleep for 'pollInterval' seconds.
// Sleep takes miliseconds so need to convert this.pollInterval to milisecopnds (x 1000)
try {
Thread.sleep(this.pollInterval * 1000);
} catch (InterruptedException e) {
this.failBuild(e, listener);
}
}
listener.getLogger().println("Remote job remote job " + jobName + " is not currenlty building.");
} else {
this.failBuild(new Exception("Got a blank response from Remote Jenkins Server, cannot continue."), listener);
}
} else {
listener.getLogger().println("Not checking if the remote job " + this.getJob() + " is building.");
}
String queryUrlString = this.buildGetUrl(tempJobName, securityToken);
queryUrlString += "/api/json/";
JSONObject queryResponseObject = sendHTTPCall(queryUrlString, "GET", build, listener);
if (queryResponseObject == null ) {
//This should not happen as this page should return a JSON object
this.failBuild(new Exception("Got a blank response from Remote Jenkins Server [" + remoteServerURL + "], cannot continue."), listener);
}
int nextBuildNumber = queryResponseObject.getInt("nextBuildNumber");
if (this.getOverrideAuth()) {
listener.getLogger().println(
"Using job-level defined credentails in place of those from remote Jenkins config ["
+ this.getRemoteJenkinsName() + "]");
}
listener.getLogger().println("Triggering remote job now.");
sendHTTPCall(triggerUrlString, "POST", build, listener);
// Validate the build number via parameters
foundIt: for (int tries = 3; tries > 0; tries--) {
for (int buildNumber : new SearchPattern(nextBuildNumber, 2)) {
listener.getLogger().println("Checking parameters of #" + buildNumber);
String validateUrlString = this.buildGetUrl(tempJobName, securityToken) + "/" + buildNumber + "/api/json/";
JSONObject validateResponse = sendHTTPCall(validateUrlString, "GET", build, listener);
if (validateResponse == null) {
listener.getLogger().println("Query failed.");
continue;
}
JSONArray actions = validateResponse.getJSONArray("actions");
for (int i = 0; i < actions.size(); i++) {
JSONObject action = actions.getJSONObject(i);
if (!action.has("parameters")) continue;
JSONArray parameters = action.getJSONArray("parameters");
// Check if the parameters match
if (compareParameters(listener, parameters, cleanedParams)) {
// We now have a very high degree of confidence that this is the correct build.
// It is still possible that this is a false positive if there are no parameters,
// or multiple jobs use the same parameters.
nextBuildNumber = buildNumber;
break foundIt;
}
// This is the wrong build
break;
}
// Sleep for 'pollInterval' seconds.
// Sleep takes miliseconds so need to convert this.pollInterval to milisecopnds (x 1000)
try {
Thread.sleep(this.pollInterval * 1000);
} catch (InterruptedException e) {
this.failBuild(e, listener);
}
}
}
listener.getLogger().println("This job is build #[" + Integer.toString(nextBuildNumber) + "] on the remote server.");
BuildInfoExporterAction.addBuildInfoExporterAction(build, jobName, nextBuildNumber, Result.NOT_BUILT);
//Have to form the string ourselves, as we might not get a response from non-parameterized builds
String jobURL = remoteServerURL + "/job/" + tempJobName + "/";
// This is only for Debug
// This output whether there is another job running on the remote host that this job had conflicted with.
// The first condition is what is expected, The second is what would happen if two jobs launched jobs at the
// same time (and two remote builds were triggered).
// The third is what would happen if this job was triggers and the remote queue was already full (as the 'next
// build bumber' would still be the same after this job has triggered the remote job)
// int newNextBuildNumber = responseObject.getInt( "nextBuildNumber" ); // This should be nextBuildNumber + 1 OR
// there has been another job scheduled.
// if (newNextBuildNumber == (nextBuildNumber + 1)) {
// listener.getLogger().println("DEBUG: No other jobs triggered" );
// } else if( newNextBuildNumber > (nextBuildNumber + 1) ) {
// listener.getLogger().println("DEBUG: WARNING Other jobs triggered," + newNextBuildNumber + ", " +
// nextBuildNumber );
// } else {
// listener.getLogger().println("DEBUG: WARNING Did not get the correct build number for the triggered job, previous nextBuildNumber:"
// + newNextBuildNumber + ", newNextBuildNumber" + nextBuildNumber );
// }
// If we are told to block until remoteBuildComplete:
if (this.getBlockBuildUntilComplete()) {
listener.getLogger().println("Blocking local job until remote job completes");
// Form the URL for the triggered job
String jobLocation = jobURL + nextBuildNumber + "/api/json";
buildStatusStr = getBuildStatus(jobLocation, build, listener);
while (buildStatusStr.equals("not started")) {
listener.getLogger().println("Waiting for remote build to start.");
listener.getLogger().println("Waiting for " + this.pollInterval + " seconds until next poll.");
buildStatusStr = getBuildStatus(jobLocation, build, listener);
// Sleep for 'pollInterval' seconds.
// Sleep takes miliseconds so need to convert this.pollInterval to milisecopnds (x 1000)
try {
// Could do with a better way of sleeping...
Thread.sleep(this.pollInterval * 1000);
} catch (InterruptedException e) {
this.failBuild(e, listener);
}
}
listener.getLogger().println("Remote build started!");
listener.getLogger().println("Triggered Remote Job : " + remoteJobURL.toString() + "/" + nextBuildNumber);
while (buildStatusStr.equals("running")) {
listener.getLogger().println("Waiting for remote build to finish.");
listener.getLogger().println("Waiting for " + this.pollInterval + " seconds until next poll.");
buildStatusStr = getBuildStatus(jobLocation, build, listener);
// Sleep for 'pollInterval' seconds.
// Sleep takes miliseconds so need to convert this.pollInterval to milisecopnds (x 1000)
try {
// Could do with a better way of sleeping...
Thread.sleep(this.pollInterval * 1000);
} catch (InterruptedException e) {
this.failBuild(e, listener);
}
}
listener.getLogger().println("Remote build finished with status " + buildStatusStr + ".");
BuildInfoExporterAction.addBuildInfoExporterAction(build, jobName, nextBuildNumber, Result.fromString(buildStatusStr));
if (this.getEnhancedLogging()) {
String buildUrl = getBuildUrl(jobLocation, build, listener);
String consoleOutput = getConsoleOutput(buildUrl, "GET", build, listener);
listener.getLogger().println();
listener.getLogger().println("Console output of remote job:");
listener.getLogger().println("--------------------------------------------------------------------------------");
listener.getLogger().println(consoleOutput);
listener.getLogger().println("--------------------------------------------------------------------------------");
}
// If build did not finish with 'success' then fail build step.
if (!buildStatusStr.equals("SUCCESS")) {
// failBuild will check if the 'shouldNotFailBuild' parameter is set or not, so will decide how to
// handle the failure.
this.failBuild(new Exception("The remote job did not succeed."), listener);
}
} else {
listener.getLogger().println("Not blocking local job until remote job completes - fire and forget.");
}
return true;
}
private String findParameter(String parameter, List<String> parameters) {
for (String search : parameters) {
if (search.startsWith(parameter + "=")) {
return search.substring(parameter.length() + 1);
}
}
return null;
}
private boolean compareParameters(BuildListener listener, JSONArray parameters, List<String> expectedParams) {
for (int j = 0; j < parameters.size(); j++) {
JSONObject parameter = parameters.getJSONObject(j);
String name = parameter.getString("name");
String expected = findParameter(name, expectedParams);
if (expected == null) {
// If we didn't specify all of the parameters, this will happen, so we can not infer that this it he wrong build
listener.getLogger().println("Unable to find expected value for " + name);
continue;
}
String value = parameter.getString("value");
// If we got the expected value, skip to the next parameter
if (expected.equals(value)) continue;
// We didn't get the expected value
listener.getLogger().println("Param " + name + " doesn't match!");
return false;
}
// All found parameters matched. This if there are no uniquely identifying parameters, this could still be a false positive.
return true;
}
public String getBuildStatus(String buildUrlString, AbstractBuild build, BuildListener listener) throws IOException {
String buildStatus = "UNKNOWN";
RemoteJenkinsServer remoteServer = this.findRemoteHost(this.getRemoteJenkinsName());
if (remoteServer == null) {
this.failBuild(new Exception("No remote host is defined for this job."), listener);
return null;
}
// print out some debugging information to the console
if (this.getOverrideAuth()) {
listener.getLogger().println(
"Using job-level defined credentails in place of those from remote Jenkins config ["
+ this.getRemoteJenkinsName() + "]");
}
JSONObject responseObject = sendHTTPCall(buildUrlString, "GET", build, listener);
// get the next build from the location
if (responseObject == null || responseObject.getString("result") == null && responseObject.getBoolean("building") == false) {
// build not started
buildStatus = "not started";
} else if (responseObject.getBoolean("building")) {
// build running
buildStatus = "running";
} else if (responseObject.getString("result") != null) {
// build finished
buildStatus = responseObject.getString("result");
} else {
// Add additional else to check for unhandled conditions
listener.getLogger().println("WARNING: Unhandled condition!");
}
return buildStatus;
}
public String getBuildUrl(String buildUrlString, AbstractBuild build, BuildListener listener) throws IOException {
String buildUrl = "";
RemoteJenkinsServer remoteServer = this.findRemoteHost(this.getRemoteJenkinsName());
if (remoteServer == null) {
this.failBuild(new Exception("No remote host is defined for this job."), listener);
return null;
}
// print out some debugging information to the console
//listener.getLogger().println("Checking Status of this job: " + buildUrlString);
if (this.getOverrideAuth()) {
listener.getLogger().println(
"Using job-level defined credentails in place of those from remote Jenkins config ["
+ this.getRemoteJenkinsName() + "]");
}
JSONObject responseObject = sendHTTPCall(buildUrlString, "GET", build, listener);
// get the next build from the location
if (responseObject != null && responseObject.getString("url") != null) {
buildUrl = responseObject.getString("url");
} else {
// Add additional else to check for unhandled conditions
listener.getLogger().println("WARNING: URL not found in JSON Response!");
return null;
}
return buildUrl;
}
public String getConsoleOutput(String urlString, String requestType, AbstractBuild build, BuildListener listener)
throws IOException {
return getConsoleOutput( urlString, requestType, build, listener, 1 );
}
/**
* Orchestrates all calls to the remote server.
* Also takes care of any credentials or failed-connection retries.
*
* @param urlString the URL that needs to be called
* @param requestType the type of request (GET, POST, etc)
* @param build the build that is being triggered
* @param listener build listener
* @return a valid JSON object, or null
* @throws IOException
*/
public JSONObject sendHTTPCall(String urlString, String requestType, AbstractBuild build, BuildListener listener)
throws IOException {
return sendHTTPCall( urlString, requestType, build, listener, 1 );
}
public String getConsoleOutput(String urlString, String requestType, AbstractBuild build, BuildListener listener, int numberOfAttempts)
throws IOException {
RemoteJenkinsServer remoteServer = this.findRemoteHost(this.getRemoteJenkinsName());
int retryLimit = this.getConnectionRetryLimit();
if (remoteServer == null) {
this.failBuild(new Exception("No remote host is defined for this job."), listener);
return null;
}
HttpURLConnection connection = null;
String consoleOutput = null;
URL buildUrl = new URL(urlString+"consoleText");
connection = (HttpURLConnection) buildUrl.openConnection();
// if there is a username + apiToken defined for this remote host, then use it
String usernameTokenConcat;
if (this.getOverrideAuth()) {
usernameTokenConcat = this.getAuth()[0].getUsername() + ":" + this.getAuth()[0].getPassword();
} else {
usernameTokenConcat = remoteServer.getAuth()[0].getUsername() + ":"
+ remoteServer.getAuth()[0].getPassword();
}
if (!usernameTokenConcat.equals(":")) {
// token-macro replacment
try {
usernameTokenConcat = TokenMacro.expandAll(build, listener, usernameTokenConcat);
} catch (MacroEvaluationException e) {
this.failBuild(e, listener);
} catch (InterruptedException e) {
this.failBuild(e, listener);
}
byte[] encodedAuthKey = Base64.encodeBase64(usernameTokenConcat.getBytes());
connection.setRequestProperty("Authorization", "Basic " + new String(encodedAuthKey));
}
try {
connection.setDoInput(true);
connection.setRequestProperty("Accept", "application/json");
connection.setRequestMethod(requestType);
// wait up to 5 seconds for the connection to be open
connection.setConnectTimeout(5000);
connection.connect();
InputStream is;
try {
is = connection.getInputStream();
} catch (FileNotFoundException e) {
// In case of a e.g. 404 status
is = connection.getErrorStream();
}
BufferedReader rd = new BufferedReader(new InputStreamReader(is));
String line;
// String response = "";
StringBuilder response = new StringBuilder();
while ((line = rd.readLine()) != null) {
response.append(line+"\n");
}
rd.close();
consoleOutput = response.toString();
} catch (IOException e) {
//If we have connectionRetryLimit set to > 0 then retry that many times.
if( numberOfAttempts <= retryLimit) {
listener.getLogger().println("Connection to remote server failed, waiting for to retry - " + this.pollInterval + " seconds until next attempt.");
e.printStackTrace();
// Sleep for 'pollInterval' seconds.
// Sleep takes miliseconds so need to convert this.pollInterval to milisecopnds (x 1000)
try {
// Could do with a better way of sleeping...
Thread.sleep(this.pollInterval * 1000);
} catch (InterruptedException ex) {
this.failBuild(ex, listener);
}
listener.getLogger().println("Retry attempt #" + numberOfAttempts + " out of " + retryLimit );
numberOfAttempts++;
consoleOutput = getConsoleOutput(urlString, requestType, build, listener, numberOfAttempts);
} else if(numberOfAttempts > retryLimit){
//reached the maximum number of retries, time to fail
this.failBuild(new Exception("Max number of connection retries have been exeeded."), listener);
} else{
//something failed with the connection and we retried the max amount of times... so throw an exception to mark the build as failed.
this.failBuild(e, listener);
}
} finally {
// always make sure we close the connection
if (connection != null) {
connection.disconnect();
}
// and always clear the query string and remove some "global" values
this.clearQueryString();
// this.build = null;
// this.listener = null;
}
return consoleOutput;
}
/**
* Same as sendHTTPCall, but keeps track of the number of failed connection attempts (aka: the number of times this
* method has been called).
* In the case of a failed connection, the method calls it self recursively and increments numberOfAttempts
*
* @see sendHTTPCall
* @param numberOfAttempts number of time that the connection has been attempted
* @return
* @throws IOException
*/
public JSONObject sendHTTPCall(String urlString, String requestType, AbstractBuild build, BuildListener listener, int numberOfAttempts)
throws IOException {
RemoteJenkinsServer remoteServer = this.findRemoteHost(this.getRemoteJenkinsName());
int retryLimit = this.getConnectionRetryLimit();
if (remoteServer == null) {
this.failBuild(new Exception("No remote host is defined for this job."), listener);
return null;
}
HttpURLConnection connection = null;
JSONObject responseObject = null;
URL buildUrl = new URL(urlString);
connection = (HttpURLConnection) buildUrl.openConnection();
// if there is a username + apiToken defined for this remote host, then use it
String usernameTokenConcat;
if (this.getOverrideAuth()) {
usernameTokenConcat = this.getAuth()[0].getUsername() + ":" + this.getAuth()[0].getPassword();
} else {
usernameTokenConcat = remoteServer.getAuth()[0].getUsername() + ":"
+ remoteServer.getAuth()[0].getPassword();
}
if (!usernameTokenConcat.equals(":")) {
// token-macro replacment
try {
usernameTokenConcat = TokenMacro.expandAll(build, listener, usernameTokenConcat);
} catch (MacroEvaluationException e) {
this.failBuild(e, listener);
} catch (InterruptedException e) {
this.failBuild(e, listener);
}
byte[] encodedAuthKey = Base64.encodeBase64(usernameTokenConcat.getBytes());
connection.setRequestProperty("Authorization", "Basic " + new String(encodedAuthKey));
}
try {
connection.setDoInput(true);
connection.setRequestProperty("Accept", "application/json");
connection.setRequestMethod(requestType);
// wait up to 5 seconds for the connection to be open
connection.setConnectTimeout(5000);
connection.connect();
InputStream is;
try {
is = connection.getInputStream();
} catch (FileNotFoundException e) {
// In case of a e.g. 404 status
is = connection.getErrorStream();
}
BufferedReader rd = new BufferedReader(new InputStreamReader(is));
String line;
// String response = "";
StringBuilder response = new StringBuilder();
while ((line = rd.readLine()) != null) {
response.append(line);
}
rd.close();
// JSONSerializer serializer = new JSONSerializer();
// need to parse the data we get back into struct
//listener.getLogger().println("Called URL: '" + urlString + "', got response: '" + response.toString() + "'");