-
Notifications
You must be signed in to change notification settings - Fork 69
/
SupportPlugin.java
916 lines (821 loc) · 38 KB
/
SupportPlugin.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
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
/*
* The MIT License
*
* Copyright (c) 2013, CloudBees, Inc.
*
* 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 com.cloudbees.jenkins.support;
import com.cloudbees.jenkins.support.api.Component;
import com.cloudbees.jenkins.support.api.Container;
import com.cloudbees.jenkins.support.api.Content;
import com.cloudbees.jenkins.support.api.SupportProvider;
import com.cloudbees.jenkins.support.api.SupportProviderDescriptor;
import com.cloudbees.jenkins.support.api.UnfilteredStringContent;
import com.cloudbees.jenkins.support.filter.ContentFilter;
import com.cloudbees.jenkins.support.filter.ContentFilters;
import com.cloudbees.jenkins.support.filter.ContentMappings;
import com.cloudbees.jenkins.support.filter.FilteredOutputStream;
import com.cloudbees.jenkins.support.filter.PrefilteredContent;
import com.cloudbees.jenkins.support.impl.ThreadDumps;
import com.cloudbees.jenkins.support.util.IgnoreCloseOutputStream;
import com.cloudbees.jenkins.support.util.OutputStreamSelector;
import com.codahale.metrics.Histogram;
import edu.umd.cs.findbugs.annotations.NonNull;
import hudson.BulkChange;
import hudson.Extension;
import hudson.ExtensionList;
import hudson.FilePath;
import hudson.Functions;
import hudson.Main;
import hudson.Plugin;
import hudson.init.InitMilestone;
import hudson.init.Initializer;
import hudson.model.Computer;
import hudson.model.Descriptor;
import hudson.model.Node;
import hudson.model.PeriodicWork;
import hudson.model.TaskListener;
import hudson.remoting.Future;
import hudson.remoting.VirtualChannel;
import hudson.security.ACL;
import hudson.security.Permission;
import hudson.security.PermissionGroup;
import hudson.security.PermissionScope;
import hudson.slaves.ComputerListener;
import jenkins.metrics.impl.JenkinsMetricProviderImpl;
import jenkins.model.GlobalConfiguration;
import jenkins.model.Jenkins;
import jenkins.security.MasterToSlaveCallable;
import net.sf.json.JSONObject;
import org.acegisecurity.Authentication;
import org.acegisecurity.context.SecurityContext;
import org.acegisecurity.context.SecurityContextHolder;
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream;
import org.apache.commons.lang.StringUtils;
import org.kohsuke.accmod.Restricted;
import org.kohsuke.accmod.restrictions.NoExternalUse;
import org.kohsuke.stapler.StaplerRequest;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.text.MessageFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.TimeZone;
import java.util.TreeSet;
import java.util.WeakHashMap;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.atomic.AtomicLong;
import java.util.logging.Handler;
import java.util.logging.Level;
import java.util.logging.LogRecord;
import java.util.logging.Logger;
/**
* Main entry point for the support plugin.
*
* @author Stephen Connolly
*/
public class SupportPlugin extends Plugin {
/**
* How long remote operations can block support bundle generation for.
*/
public static final int REMOTE_OPERATION_TIMEOUT_MS =
Integer.getInteger(SupportPlugin.class.getName() + ".REMOTE_OPERATION_TIMEOUT_MS", 500);
/**
* How long remote operations fallback caching can wait for
*/
public static final int REMOTE_OPERATION_CACHE_TIMEOUT_SEC =
Integer.getInteger(SupportPlugin.class.getName() + ".REMOTE_OPERATION_CACHE_TIMEOUT_SEC", 300);
/**
* How often automatic support bundles should be collected. Should be {@code 1} unless you have very good reason
* to use a different period. {@code 0} disables bundle generation and {@code 24} is the longest period permitted.
*/
public static final int AUTO_BUNDLE_PERIOD_HOURS =
Math.max(Math.min(24, Integer.getInteger(SupportPlugin.class.getName() + ".AUTO_BUNDLE_PERIOD_HOURS", 1)), 0);
public static final PermissionGroup SUPPORT_PERMISSIONS =
new PermissionGroup(SupportPlugin.class, Messages._SupportPlugin_PermissionGroup());
public static final Permission CREATE_BUNDLE =
new Permission(SUPPORT_PERMISSIONS, "DownloadBundle", Messages._SupportPlugin_CreateBundle(),
Jenkins.ADMINISTER, PermissionScope.JENKINS);
private static final ThreadLocal<Authentication> requesterAuthentication = new InheritableThreadLocal<>();
private static final AtomicLong nextBundleWrite = new AtomicLong(Long.MIN_VALUE);
private static final Logger logger = Logger.getLogger(SupportPlugin.class.getName());
public static final String SUPPORT_DIRECTORY_NAME = "support";
private transient final SupportLogHandler handler = new SupportLogHandler(256, 2048, 8);
private transient SupportContextImpl context = null;
private transient Logger rootLogger;
private transient WeakHashMap<Node, List<LogRecord>> logRecords;
private SupportProvider supportProvider;
/**
* class names of {@link Component}
*/
private Set<String> excludedComponents;
public SupportPlugin() {
super();
handler.setLevel(getLogLevel());
handler.setDirectory(getRootDirectory(), "all");
}
public SupportProvider getSupportProvider() {
if (supportProvider == null) {
// if this is not set, pick the first one that we can get our hands on
for (Descriptor<SupportProvider> d : Jenkins.get().getDescriptorList(SupportProvider.class)) {
if (d instanceof SupportProviderDescriptor) {
try {
supportProvider = ((SupportProviderDescriptor) (d)).newDefaultInstance();
} catch (Throwable t) {
// ignore, we'll try somebody else
}
}
}
}
return supportProvider;
}
/**
* Working directory that the support-core plugin uses to write out files.
*
* @return the wrking directory that the support-core plugin uses to write out files.
*/
public static File getRootDirectory() {
return new File(Jenkins.get().getRootDir(), SUPPORT_DIRECTORY_NAME);
}
public static Authentication getRequesterAuthentication() {
return requesterAuthentication.get();
}
public static void setRequesterAuthentication(Authentication authentication) {
requesterAuthentication.set(authentication);
}
public static void clearRequesterAuthentication() {
requesterAuthentication.remove();
}
public void setSupportProvider(SupportProvider supportProvider) throws IOException {
if (supportProvider != this.supportProvider) {
this.supportProvider = supportProvider;
save();
}
}
public Set<String> getExcludedComponents() {
return excludedComponents != null ? excludedComponents : Collections.emptySet();
}
/**
* Sets the ids of the components to be excluded.
*
* @param excludedComponents Component Ids (by default class names) to exclude.
* @throws IOException if an error occurs while saving the configuration.
* @see Component#getId
*/
public void setExcludedComponents(Set<String> excludedComponents) throws IOException {
this.excludedComponents = excludedComponents;
save();
}
public Histogram getJenkinsExecutorTotalCount() {
return JenkinsMetricProviderImpl.instance().getJenkinsExecutorTotalCount();
}
public Histogram getJenkinsExecutorUsedCount() {
return JenkinsMetricProviderImpl.instance().getJenkinsExecutorUsedCount();
}
public Histogram getJenkinsNodeOnlineCount() {
return JenkinsMetricProviderImpl.instance().getJenkinsNodeOnlineCount();
}
public Histogram getJenkinsNodeTotalCount() {
return JenkinsMetricProviderImpl.instance().getJenkinsNodeTotalCount();
}
private static Level getLogLevel() {
return Level.parse(System.getProperty(SupportPlugin.class.getName() + ".LogLevel", "INFO"));
}
public static void setLogLevel(String level) {
setLogLevel(Level.parse(StringUtils.defaultIfEmpty(level, "INFO")));
}
public static void setLogLevel(Level level) {
SupportPlugin instance = getInstance();
instance.handler.setLevel(level);
for (Node n : Jenkins.get().getNodes()) {
Computer c = n.toComputer();
if (c == null) {
continue;
}
VirtualChannel channel = c.getChannel();
if (channel != null) {
try {
channel.callAsync(new LogUpdater(level));
} catch (IOException e) {
// ignore
}
}
}
}
public static SupportPlugin getInstance() {
return Jenkins.get().getPlugin(SupportPlugin.class);
}
public static ExtensionList<Component> getComponents() {
return Jenkins.get().getExtensionList(Component.class);
}
public static void writeBundle(OutputStream outputStream) throws IOException {
writeBundle(outputStream, getComponents());
}
public static void writeBundle(OutputStream outputStream, final List<Component> components) throws IOException {
StringBuilder manifest = new StringBuilder();
StringWriter errors = new StringWriter();
PrintWriter errorWriter = new PrintWriter(errors);
try {
try (BulkChange change = new BulkChange(ContentMappings.get());
ZipArchiveOutputStream binaryOut = new ZipArchiveOutputStream(new BufferedOutputStream(outputStream, 16384))) {
// Get the filter to be used
Optional<ContentFilter> maybeFilter = getContentFilter();
// Recalculate the mappings and stop words and save it to disk
if(maybeFilter.isPresent()) {
reloadAndSaveMappings(maybeFilter.get());
}
// Generate the content of the manifest.md going trough all the components which will be included. It
// also returns the contents to include. We pass maybeFilter to filter the names written in the manifest
appendManifestHeader(manifest);
List<Content> contents = appendManifestContents(manifest, errorWriter, components, maybeFilter);
contents.add(new UnfilteredStringContent("manifest.md", manifest.toString()));
Optional<FilteredOutputStream> maybeFilteredOut = maybeFilter.map(filter -> new FilteredOutputStream(binaryOut, filter));
OutputStream textOut = maybeFilteredOut.map(OutputStream.class::cast).orElse(binaryOut);
OutputStreamSelector selector = new OutputStreamSelector(() -> binaryOut, () -> textOut);
IgnoreCloseOutputStream unfilteredOut = new IgnoreCloseOutputStream(binaryOut);
IgnoreCloseOutputStream filteredOut = new IgnoreCloseOutputStream(selector);
for (Content content : contents) {
if (content == null) {
continue;
}
final String name = getNameFiltered(maybeFilter, content.getName(), content.getFilterableParameters());
final ZipArchiveEntry entry = new ZipArchiveEntry(name);
entry.setTime(content.getTime());
try {
binaryOut.putArchiveEntry(entry);
binaryOut.flush();
OutputStream out = content.shouldBeFiltered() ? filteredOut : unfilteredOut;
if (content instanceof PrefilteredContent && maybeFilter.isPresent()) {
((PrefilteredContent)content).writeTo(out, maybeFilter.get());
} else {
content.writeTo(out);
}
out.flush();
} catch (Throwable e) {
String msg = "Could not attach ''" + name + "'' to support bundle";
logger.log(Level.WARNING, msg, e);
errorWriter.println(msg);
errorWriter.println("-----------------------------------------------------------------------");
errorWriter.println();
Functions.printStackTrace(e, errorWriter);
errorWriter.println();
} finally {
maybeFilteredOut.ifPresent(FilteredOutputStream::reset);
selector.reset();
binaryOut.closeArchiveEntry();
}
}
errorWriter.close();
String errorContent = errors.toString();
if (StringUtils.isNotBlank(errorContent)) {
try {
binaryOut.putArchiveEntry(new ZipArchiveEntry("manifest/errors.txt"));
textOut.write(errorContent.getBytes(StandardCharsets.UTF_8));
textOut.flush();
binaryOut.closeArchiveEntry();
} catch (IOException e) {
logger.log(Level.WARNING, "Could not write manifest/errors.txt to zip archive", e);
}
}
binaryOut.flush();
change.commit();
}
} finally {
outputStream.flush();
}
}
/**
* Filter the name of a content depending on the filterableParameters in the name that need to be replaced.
* @param maybeFilter an Optional with a {@link ContentFilter} or not
* @param name the name of the content to be filtered
* @param filterableParameters filterableParameters in the name to be filtered. If null, no filter takes place to avoid corruption
* @return the name filtered
*/
private static String getNameFiltered(Optional<ContentFilter> maybeFilter, String name, String[] filterableParameters) {
String filteredName;
if (filterableParameters != null) {
// Filter each token or return the token depending on whether the filter is active or not
String[] replacedParameters = Arrays.stream(filterableParameters).map(filterableParameter -> maybeFilter.map(filter -> filter.filter(filterableParameter)).orElse(filterableParameter)).toArray(String[]::new);
// Replace each placeholder {0}, {1} in the name, with the replaced token
filteredName = MessageFormat.format(name, replacedParameters);
} else {
// Previous behavior was filter all the name, but it could end up in having a corrupted bundle. So we expect
// implementors to use the appropriate constructor of Content.
//filteredName = maybeFilter.map(filter -> filter.filter(name)).orElse(name);
filteredName = name;
}
return filteredName;
}
/**
* Get the filter to be used in an {@link Optional} just in case.
* @return the filter.
*/
public static Optional<ContentFilter> getContentFilter() {
ContentFilters filters = ContentFilters.get();
if (filters.isEnabled()) {
ContentFilter filter = ContentFilter.ALL;
return Optional.of(filter);
}
return Optional.empty();
}
/**
* Reload mappings and filters and save it to disk.
* @param filter filter to reload.
*/
private static void reloadAndSaveMappings(ContentFilter filter) {
ContentMappings mappings = ContentMappings.get();
try (BulkChange change = new BulkChange(mappings)) {
mappings.reload();
filter.reload();
change.commit();
} catch (IOException e) {
e.printStackTrace();
}
}
private static void appendManifestHeader(StringBuilder manifest) {
SupportPlugin plugin = SupportPlugin.getInstance();
SupportProvider supportProvider = plugin == null ? null : plugin.getSupportProvider();
String bundleName =
(supportProvider == null ? "Support" : supportProvider.getDisplayName()) + " Bundle Manifest";
manifest.append(bundleName)
.append('\n')
.append(StringUtils.repeat("=", bundleName.length()))
.append("\n\n");
SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSSZ");
f.setTimeZone(TimeZone.getTimeZone("UTC"));
manifest.append("Generated on ")
.append(f.format(new Date()))
.append("\n\n");
}
/**
* Populate the manifest with the content names which are going to be added to the bundle. In addition, it returns
* the list of content added. To be able to add the names in the manifest.md properly filtered, the current set of
* filters are specified.
* @param manifest where to append the names of the contents added to the bundle
* @param errors where to print error messages
* @param components components to add their contents to the bundle
* @param maybeFilter filter to be used when writing the content names
* @return the list of contents whose names has been added to the manifest and their content will be added to the
* bundle.
*/
private static List<Content> appendManifestContents(StringBuilder manifest, PrintWriter errors, List<Component> components, Optional<ContentFilter> maybeFilter) {
manifest.append("Requested components:\n\n");
ContentContainer contentsContainer = new ContentContainer(maybeFilter);
for (Component component : components) {
try {
manifest.append(" * ").append(component.getDisplayName()).append("\n\n");
component.addContents(contentsContainer);
Set<String> names = contentsContainer.getLatestNames();
for (String name : names) {
manifest.append(" - `").append(name).append("`\n\n");
}
} catch (Throwable e) {
String displayName;
try {
displayName = component.getDisplayName();
} catch (Throwable ignored) {
// be very defensive
displayName = component.getClass().getName();
}
String msg = "Could not get content from " + displayName + " for support bundle";
logger.log(Level.WARNING, msg, e);
errors.println(msg);
errors.println("-----------------------------------------------------------------------");
errors.println();
Functions.printStackTrace(e, errors);
errors.println();
}
}
return contentsContainer.contents;
}
private static class ContentContainer extends Container {
private final List<Content> contents = new ArrayList<>();
private final Set<String> names = new TreeSet<>();
//The filter to return the names filtered
private final Optional<ContentFilter> maybeFilter;
/**
* We need the filter to be able to filter the contents written to the manifest
* @param maybeFilter filter to use when writing the name of the contents
*/
public ContentContainer(Optional<ContentFilter> maybeFilter) {
this.maybeFilter = maybeFilter;
}
@Override
public void add(Content content) {
if (content != null) {
contents.add(content);
names.add(getNameFiltered(maybeFilter, content.getName(), content.getFilterableParameters()));
}
}
private Set<String> getLatestNames() {
Set<String> copy = new TreeSet<>(names);
names.clear();
return copy;
}
}
public List<LogRecord> getAllLogRecords() {
return handler.getRecent();
}
@Initializer(after = InitMilestone.EXTENSIONS_AUGMENTED, before = InitMilestone.JOB_LOADED)
public static void loadConfig() throws IOException {
SupportPlugin instance = getInstance();
if (instance != null)
instance.load();
}
private static final boolean logStartupPerformanceIssues = Boolean.getBoolean(SupportPlugin.class.getCanonicalName() + ".threadDumpStartup");
private static final int secondsPerThreadDump = Integer.getInteger(SupportPlugin.class.getCanonicalName() + ".secondsPerTD", 60);
@Deprecated
@Restricted(NoExternalUse.class)
public static void completedMilestones() throws IOException {
// Do nothing
}
@Initializer(after = InitMilestone.STARTED)
public static void threadDumpStartup() throws Exception {
if (!logStartupPerformanceIssues) return;
final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd_HH.mm.ss");
dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
final File f = new File(getRootDirectory(), "/startup-threadDump" + dateFormat.format(new Date()) + ".txt");
if (!f.exists()) {
try {
f.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
Thread t = new Thread("Support core plugin startup diagnostics") {
@Override
public void run() {
try {
while (true) {
final Jenkins jenkins = Jenkins.getInstanceOrNull();
if (jenkins == null || jenkins.getInitLevel() != InitMilestone.COMPLETED) {
continue;
}
try (PrintStream ps = new PrintStream(new FileOutputStream(f, true), false, "UTF-8")) {
ps.println("=== Thread dump at " + new Date() + " ===");
ThreadDumps.threadDump(ps);
// Generate a thread dump every few seconds/minutes
ps.flush();
TimeUnit.SECONDS.sleep(secondsPerThreadDump);
} catch (FileNotFoundException | UnsupportedEncodingException e) {
e.printStackTrace();
}
}
} catch (InterruptedException e) {
e.printStackTrace();
Thread.currentThread().interrupt();
}
}
};
t.start();
}
@Override
public synchronized void start() throws Exception {
super.start();
rootLogger = Logger.getLogger("");
rootLogger.addHandler(handler);
context = new SupportContextImpl();
}
@NonNull
public synchronized SupportContextImpl getContext() {
return context;
}
@Override
public void postInitialize() throws Exception {
super.postInitialize();
for (Component component : getComponents()) {
try {
component.start(getContext());
} catch (Throwable t) {
LogRecord logRecord = new LogRecord(Level.WARNING, "Exception propagated from component: {0}");
logRecord.setThrown(t);
logRecord.setParameters(new Object[]{component.getDisplayName()});
logger.log(logRecord);
}
}
}
@Override
public synchronized void stop() throws Exception {
if (rootLogger != null) {
rootLogger.removeHandler(handler);
rootLogger = null;
handler.close();
}
context.shutdown();
super.stop();
}
public List<LogRecord> getAllLogRecords(final Node node) throws IOException, InterruptedException {
if (node != null) {
VirtualChannel channel = node.getChannel();
if (channel != null) {
final Future<List<LogRecord>> future = channel.callAsync(new LogFetcher());
try {
return future.get(REMOTE_OPERATION_TIMEOUT_MS, TimeUnit.MILLISECONDS);
} catch (ExecutionException e) {
final LogRecord lr = new LogRecord(Level.WARNING, "Could not retrieve remote log records");
lr.setThrown(e);
return Collections.singletonList(lr);
} catch (TimeoutException e) {
Computer.threadPoolForRemoting.submit(() -> {
List<LogRecord> records;
try {
records = future.get(REMOTE_OPERATION_CACHE_TIMEOUT_SEC, TimeUnit.SECONDS);
} catch (InterruptedException e1) {
final LogRecord lr =
new LogRecord(Level.WARNING, "Could not retrieve remote log records");
lr.setThrown(e1);
records = Collections.singletonList(lr);
} catch (ExecutionException e1) {
final LogRecord lr =
new LogRecord(Level.WARNING, "Could not retrieve remote log records");
lr.setThrown(e1);
records = Collections.singletonList(lr);
} catch (TimeoutException e1) {
final LogRecord lr =
new LogRecord(Level.WARNING, "Could not retrieve remote log records");
lr.setThrown(e1);
records = Collections.singletonList(lr);
future.cancel(true);
}
synchronized (SupportPlugin.this) {
if (logRecords == null) {
logRecords = new WeakHashMap<>();
}
logRecords.put(node, records);
}
});
synchronized (this) {
if (logRecords != null) {
List<LogRecord> result = logRecords.get(node);
if (result != null) {
result = new ArrayList<>(result);
final LogRecord lr = new LogRecord(Level.WARNING, "Using cached remote log records");
lr.setThrown(e);
result.add(lr);
return result;
}
} else {
final LogRecord lr = new LogRecord(Level.WARNING, "No previous cached remote log records");
lr.setThrown(e);
return Collections.singletonList(lr);
}
}
}
}
}
return Collections.emptyList();
}
/**
* Returns the full bundle name.
*
* @return the full bundle name.
*/
@NonNull
public static String getBundleFileName() {
StringBuilder filename = new StringBuilder();
filename.append(getBundlePrefix());
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd_HH.mm.ss");
dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
filename.append("_").append(dateFormat.format(new Date()));
filename.append(".zip");
return filename.toString();
}
/**
* Returns the prefix of the bundle name.
*
* @return the prefix of the bundle name.
*/
private static String getBundlePrefix() {
String filename = "support"; // default bundle filename
final SupportPlugin instance = getInstance();
if (instance != null) {
SupportProvider supportProvider = instance.getSupportProvider();
if (supportProvider != null) {
// let the provider name it
filename = supportProvider.getName();
}
}
final String instanceType = BundleNameInstanceTypeProvider.getInstance().getInstanceType();
if (StringUtils.isNotBlank(instanceType)) {
filename = filename + "_" + instanceType;
}
return filename;
}
public static class LogHolder {
private static final SupportLogHandler SLAVE_LOG_HANDLER = new SupportLogHandler(256, 2048, 8);
}
private static class LogInitializer extends MasterToSlaveCallable<Void, RuntimeException> {
private static final long serialVersionUID = 1L;
private static final Logger ROOT_LOGGER = Logger.getLogger("");
private final FilePath rootPath;
private final Level level;
public LogInitializer(FilePath rootPath, Level level) {
this.rootPath = rootPath;
this.level = level;
}
public Void call() {
// avoid double installation of the handler. JNLP agents can reconnect to the master multiple times
// and each connection gets a different RemoteClassLoader, so we need to evict them by class name,
// not by their identity.
for (Handler h : ROOT_LOGGER.getHandlers()) {
if (h.getClass().getName().equals(LogHolder.SLAVE_LOG_HANDLER.getClass().getName())) {
ROOT_LOGGER.removeHandler(h);
try {
h.close();
} catch (Throwable t) {
// ignore
}
}
}
LogHolder.SLAVE_LOG_HANDLER.setLevel(level);
LogHolder.SLAVE_LOG_HANDLER.setDirectory(new File(rootPath.getRemote(), SUPPORT_DIRECTORY_NAME), "all");
ROOT_LOGGER.addHandler(LogHolder.SLAVE_LOG_HANDLER);
return null;
}
}
public static class LogFetcher extends MasterToSlaveCallable<List<LogRecord>, RuntimeException> {
private static final long serialVersionUID = 1L;
public List<LogRecord> call() throws RuntimeException {
return new ArrayList<>(LogHolder.SLAVE_LOG_HANDLER.getRecent());
}
}
public static class LogUpdater extends MasterToSlaveCallable<Void, RuntimeException> {
private static final long serialVersionUID = 1L;
private final Level level;
public LogUpdater(Level level) {
this.level = level;
}
public Void call() throws RuntimeException {
LogHolder.SLAVE_LOG_HANDLER.setLevel(level);
return null;
}
}
@Extension
public static class ComputerListenerImpl extends ComputerListener {
@Override
public void onOnline(Computer c, TaskListener listener) throws IOException, InterruptedException {
final Node node = c.getNode();
if (node instanceof Jenkins) {
return;
}
try {
final VirtualChannel channel = c.getChannel();
if (channel != null) {
final FilePath rootPath = node.getRootPath();
if (rootPath != null) {
channel.callAsync(new LogInitializer(rootPath, getLogLevel()));
}
}
} catch (IOException e) {
Logger.getLogger(SupportPlugin.class.getName())
.log(Level.WARNING, "Could not install root log handler on node: " + c.getName(), e);
} catch (RuntimeException e) {
Logger.getLogger(SupportPlugin.class.getName()).log(Level.WARNING,
"Could not install root log handler on node: " + c.getName(), e);
}
}
}
@Extension
public static class PeriodicWorkImpl extends PeriodicWork {
private Thread thread;
@Override
public long getRecurrencePeriod() {
return TimeUnit.SECONDS.toMillis(15);
}
@Override
protected synchronized void doRun() throws Exception {
if (Main.isUnitTest) {
return;
}
final SupportPlugin plugin = getInstance();
if (plugin == null) {
return;
}
if (nextBundleWrite.get() < System.currentTimeMillis() && AUTO_BUNDLE_PERIOD_HOURS > 0) {
if (thread != null && thread.isAlive()) {
logger.log(Level.INFO, "Periodic bundle generating thread is still running. Execution aborted.");
return;
}
try {
thread = new Thread(() -> {
nextBundleWrite.set(System.currentTimeMillis() + TimeUnit.HOURS.toMillis(AUTO_BUNDLE_PERIOD_HOURS));
thread.setName(String.format("%s periodic bundle generator: since %s",
SupportPlugin.class.getSimpleName(), new Date()));
clearRequesterAuthentication();
SecurityContext old = ACL.impersonate(ACL.SYSTEM);
try {
File bundleDir = getRootDirectory();
if (!bundleDir.exists()) {
if (!bundleDir.mkdirs()) {
return;
}
}
File file = new File(bundleDir, SupportPlugin.getBundleFileName());
thread.setName(String.format("%s periodic bundle generator: writing %s since %s",
SupportPlugin.class.getSimpleName(), file.getName(), new Date()));
try (FileOutputStream fos = new FileOutputStream(file)) {
writeBundle(fos);
}
cleanupOldBundles(bundleDir, file);
} catch (Throwable t) {
logger.log(Level.WARNING, "Could not save support bundle", t);
} finally {
SecurityContextHolder.setContext(old);
}
}, SupportPlugin.class.getSimpleName() + " periodic bundle generator");
thread.start();
} catch (Throwable t) {
logger.log(Level.SEVERE, "Periodic bundle generating thread failed with error", t);
}
}
}
@edu.umd.cs.findbugs.annotations.SuppressWarnings(
value = {"RV_RETURN_VALUE_IGNORED_BAD_PRACTICE", "IS2_INCONSISTENT_SYNC"},
justification = "RV_RETURN_VALUE_IGNORED_BAD_PRACTICE=Best effort, " +
"IS2_INCONSISTENT_SYNC=only called from an already synchronized method"
)
private void cleanupOldBundles(File bundleDir, File justGenerated) {
thread.setName(String.format("%s periodic bundle generator: tidying old bundles since %s",
SupportPlugin.class.getSimpleName(), new Date()));
File[] files = bundleDir.listFiles((dir, name) -> name.endsWith(".zip"));
if (files == null) {
logger.log(Level.WARNING, "Something is wrong: {0} does not exist or there was an IO issue.",
bundleDir.getAbsolutePath());
return;
}
long pivot = System.currentTimeMillis();
for (long l = 1; l * 2 > 0; l *= 2) {
boolean seen = false;
for (File f : files) {
if (!f.isFile() || f == justGenerated) {
continue;
}
long age = pivot - f.lastModified();
if (l <= age && age < l * 2) {
if (seen) {
f.delete();
} else {
seen = true;
}
}
}
}
}
}
@Extension
public static class GlobalConfigurationImpl extends GlobalConfiguration {
public boolean isSelectable() {
return Jenkins.get().getDescriptorList(SupportProvider.class).size() > 1;
}
public SupportProvider getSupportProvider() {
return getInstance().getSupportProvider();
}
@Override
public boolean configure(StaplerRequest req, JSONObject json) throws FormException {
if (json.has("supportProvider")) {
try {
getInstance().setSupportProvider(req.bindJSON(SupportProvider.class,
json.getJSONObject("supportProvider")));
} catch (IOException e) {
throw new FormException(e, "supportProvider");
}
}
return true;
}
}
}