29
29
import com .cloudbees .jenkins .support .api .Component ;
30
30
import com .cloudbees .jenkins .support .api .SupportProvider ;
31
31
import com .cloudbees .jenkins .support .filter .ContentFilters ;
32
- import com .cloudbees .jenkins .support .impl .AboutBrowser ;
33
- import com .cloudbees .jenkins .support .impl .ReverseProxy ;
32
+ import com .cloudbees .jenkins .support .util .MergeZipFileUtil ;
34
33
import edu .umd .cs .findbugs .annotations .NonNull ;
35
34
import hudson .Extension ;
36
- import hudson .Functions ;
37
35
import hudson .model .Api ;
38
36
import hudson .model .Failure ;
39
37
import hudson .model .RootAction ;
@@ -110,6 +108,7 @@ public class SupportAction implements RootAction, StaplerProxy {
110
108
private static final String SUPPORT_BUNDLE_CREATION_FOLDER = Paths .get (System .getProperty ("java.io.tmpdir" ))
111
109
.resolve ("support-bundle" )
112
110
.toString ();
111
+ public static final String SYNC = "sync_" ;
113
112
114
113
private static final Map <UUID , SupportBundleAsyncGenerator > generatorByTaskId = new ConcurrentHashMap <>();
115
114
@@ -360,25 +359,30 @@ public HttpRedirect doGenerateAllBundlesAsync(StaplerRequest2 req, StaplerRespon
360
359
}
361
360
final List <Component > components = getComponents (req , json );
362
361
363
- for (Component component : components ) {
364
- if (component instanceof AboutBrowser ) {
365
- AboutBrowser aboutBrowser = (AboutBrowser ) component ;
366
- aboutBrowser .setScreenResolution (Functions .getScreenResolution ());
367
- aboutBrowser .setCurrentRequest (Stapler .getCurrentRequest2 ());
368
- aboutBrowser .setGeneratedAsync (true );
362
+ List <Component > syncComponent = components .stream ().filter (c -> !c .canBeGeneratedAsync ()).toList ();
363
+ UUID taskId = UUID .randomUUID ();
364
+ String supportBundleName = BundleFileName .generate ();
365
+
366
+ if (!syncComponent .isEmpty ()){
367
+ File outputDir = new File (SUPPORT_BUNDLE_CREATION_FOLDER + "/" + taskId );
368
+ if (!outputDir .exists ()) {
369
+ if (!outputDir .mkdirs ()) {
370
+ throw new IOException ("Failed to create directory: " + outputDir .getAbsolutePath ());
371
+ }
369
372
}
373
+ try (FileOutputStream fileOutputStream = new FileOutputStream (new File (outputDir , SYNC +supportBundleName ))) {
374
+ SupportPlugin .writeBundle (fileOutputStream , syncComponent );
375
+ } finally {
376
+ logger .fine ("Response completed" );
370
377
371
- if (component instanceof ReverseProxy ) {
372
- ReverseProxy reverseProxy = (ReverseProxy ) component ;
373
- reverseProxy .setCurrentRequest (Stapler .getCurrentRequest2 ());
374
378
}
375
379
}
376
380
377
- UUID taskId = UUID . randomUUID ();
381
+
378
382
SupportBundleAsyncGenerator supportBundleAsyncGenerator = new SupportBundleAsyncGenerator ();
379
- supportBundleAsyncGenerator .init (taskId , components );
383
+ supportBundleAsyncGenerator .init (taskId , components .stream ().filter (Component ::canBeGeneratedAsync ).toList ()
384
+ ,supportBundleName , !syncComponent .isEmpty ());
380
385
generatorByTaskId .put (taskId , supportBundleAsyncGenerator );
381
-
382
386
return new HttpRedirect ("progressPage?taskId=" + taskId );
383
387
}
384
388
@@ -517,10 +521,13 @@ public static class SupportBundleAsyncGenerator extends ProgressiveRendering {
517
521
private List <Component > components ;
518
522
private boolean supportBundleGenerationInProgress = false ;
519
523
private String supportBundleName ;
524
+ private boolean mergeSyncComponents ;
520
525
521
- public SupportBundleAsyncGenerator init (UUID taskId , List <Component > components ) {
526
+ public SupportBundleAsyncGenerator init (UUID taskId , List <Component > components , String supportBundleName , boolean mergeSyncComponents ) {
522
527
this .taskId = taskId ;
523
528
this .components = components ;
529
+ this .supportBundleName = supportBundleName ;
530
+ this .mergeSyncComponents = mergeSyncComponents ;
524
531
return this ;
525
532
}
526
533
@@ -530,7 +537,6 @@ protected void compute() throws Exception {
530
537
logger .fine ("Support bundle generation already in progress, for task id " + taskId );
531
538
return ;
532
539
}
533
- this .supportBundleName = BundleFileName .generate ();
534
540
this .supportBundleGenerationInProgress = true ;
535
541
logger .fine ("Generating support bundle... task id " + taskId );
536
542
File outputDir = new File (SUPPORT_BUNDLE_CREATION_FOLDER + "/" + taskId );
@@ -541,23 +547,21 @@ protected void compute() throws Exception {
541
547
}
542
548
543
549
try (FileOutputStream fileOutputStream = new FileOutputStream (new File (outputDir , supportBundleName ))) {
544
- SupportPlugin .setRequesterAuthentication (Jenkins .getAuthentication2 ());
545
- try {
546
- try (ACLContext ignored = ACL .as2 (ACL .SYSTEM2 )) {
547
- SupportPlugin .writeBundle (fileOutputStream , components );
548
- } catch (IOException e ) {
549
- logger .log (Level .WARNING , e .getMessage (), e );
550
- }
551
- } finally {
552
- SupportPlugin .clearRequesterAuthentication ();
553
- }
550
+ SupportPlugin .writeBundle (fileOutputStream , components ,this ::progress );
554
551
} finally {
555
552
logger .fine ("Response completed" );
556
553
}
557
554
558
555
pathToBundle = outputDir .getAbsolutePath () + "/" + supportBundleName ;
559
- isCompleted = true ;
556
+ if (mergeSyncComponents ){
557
+ MergeZipFileUtil .mergeZipFiles (outputDir .getAbsolutePath () +"/" +supportBundleName
558
+ ,outputDir .getAbsolutePath () +"/" +SYNC +supportBundleName
559
+ ,outputDir .getAbsolutePath () +"/" +"final_" +supportBundleName );
560
+
561
+ pathToBundle = outputDir .getAbsolutePath () +"/" +"final_" +supportBundleName ;
562
+ }
560
563
progress (1 );
564
+ isCompleted = true ;
561
565
}
562
566
563
567
@ NonNull
@@ -578,6 +582,10 @@ public String getSupportBundleName() {
578
582
public void doDownloadBundle (@ QueryParameter ("taskId" ) String taskId , StaplerResponse2 rsp ) throws IOException {
579
583
String supportBundleName =
580
584
generatorByTaskId .get (UUID .fromString (taskId )).getSupportBundleName ();
585
+ if (generatorByTaskId .get (UUID .fromString (taskId )).mergeSyncComponents ){
586
+ supportBundleName = "final_" +supportBundleName ;
587
+ }
588
+
581
589
File bundleFile = new File (SUPPORT_BUNDLE_CREATION_FOLDER + "/" + taskId + "/" + supportBundleName );
582
590
if (!bundleFile .exists ()) {
583
591
rsp .sendError (HttpServletResponse .SC_NOT_FOUND , "Support bundle file not found" );
0 commit comments