use of com.cloudbees.jenkins.support.filter.ContentFilter in project support-core-plugin by jenkinsci.
the class SupportPlugin method writeBundle.
/**
* Generate a bundle for all components that are selected in the Global Configuration.
*
* @param outputStream an {@link OutputStream}
* @param components a list of {@link Component} to include in the bundle
* @param componentConsumer a {@link ComponentVisitor}
* @throws IOException if an error occurs while generating the bundle.
*/
public static void writeBundle(OutputStream outputStream, final List<? extends Component> components, ComponentVisitor componentConsumer) 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, componentConsumer, 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);
boolean entryCreated = false;
for (Content content : contents) {
if (content == null) {
continue;
}
final String name = getNameFiltered(maybeFilter, content.getName(), content.getFilterableParameters());
try {
final ZipArchiveEntry entry = new ZipArchiveEntry(name);
entry.setTime(content.getTime());
binaryOut.putArchiveEntry(entry);
entryCreated = true;
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(e instanceof ChannelClosedException ? Level.FINE : Level.WARNING, msg, e);
errorWriter.println(msg);
errorWriter.println("-----------------------------------------------------------------------");
errorWriter.println();
Functions.printStackTrace(e, errorWriter);
errorWriter.println();
} finally {
maybeFilteredOut.ifPresent(FilteredOutputStream::reset);
selector.reset();
if (entryCreated) {
binaryOut.closeArchiveEntry();
entryCreated = false;
}
}
}
errorWriter.close();
String errorContent = errors.toString();
if (StringUtils.isNotBlank(errorContent)) {
try {
binaryOut.putArchiveEntry(new ZipArchiveEntry("manifest/errors.txt"));
entryCreated = true;
textOut.write(errorContent.getBytes(StandardCharsets.UTF_8));
textOut.flush();
} catch (IOException e) {
logger.log(Level.WARNING, "Could not write manifest/errors.txt to zip archive", e);
} finally {
if (entryCreated) {
binaryOut.closeArchiveEntry();
}
}
}
binaryOut.flush();
change.commit();
}
} finally {
outputStream.flush();
}
}
use of com.cloudbees.jenkins.support.filter.ContentFilter in project support-core-plugin by jenkinsci.
the class RunningBuildsTest method addContentsFiltered.
@Test
public void addContentsFiltered() throws Exception {
ContentFilters.get().setEnabled(true);
ContentMapping mapping = ContentMapping.of(SENSITIVE_WORD, FILTERED_SENSITIVE_WORD);
ContentMappings.get().getMappingOrCreate(mapping.getOriginal(), original -> mapping);
ContentFilter filter = SupportPlugin.getContentFilter().orElseThrow(AssertionFailedError::new);
FreeStyleProject p = j.createFreeStyleProject(SENSITIVE_JOB_NAME);
SemaphoreBuilder semaphore = new SemaphoreBuilder();
p.getBuildersList().add(semaphore);
FreeStyleBuild build = p.scheduleBuild2(0).waitForStart();
String output = SupportTestUtils.invokeComponentToString(new RunningBuilds(), filter);
semaphore.release();
j.waitForCompletion(build);
assertThat(output, not(containsString(String.format(EXPECTED_OUTPUT_FORMAT, p.getName(), build.getNumber()))));
assertThat(output, containsString(String.format(EXPECTED_OUTPUT_FORMAT, FILTERED_JOB_NAME, build.getNumber())));
}
use of com.cloudbees.jenkins.support.filter.ContentFilter in project support-core-plugin by jenkinsci.
the class SlowRequestChecker method doRun.
@Override
protected void doRun() throws Exception {
if (DISABLED) {
return;
}
// We filter the information written to the slow-requests files
Optional<ContentFilter> contentFilter = SupportPlugin.getContentFilter();
final long now = System.currentTimeMillis();
long iota = System.currentTimeMillis();
final long recurrencePeriosMillis = TimeUnit.SECONDS.toMillis(RECURRENCE_PERIOD_SEC);
long thresholdMillis = recurrencePeriosMillis > THRESHOLD ? recurrencePeriosMillis * 2 : THRESHOLD;
for (InflightRequest req : filter.tracker.values()) {
long totalTime = now - req.startTime;
if (totalTime > thresholdMillis) {
// if the thread has exited while we are taking the thread dump, ignore this.
if (req.ended)
continue;
PrintWriter w = null;
try {
if (req.record == null) {
req.record = logs.file(format.format(new Date(iota++)) + ".txt");
logs.add(req.record);
w = new PrintWriter(req.record, "UTF-8");
req.writeHeader(w, contentFilter);
} else {
w = new PrintWriter(new OutputStreamWriter(new FileOutputStream(req.record, true), "UTF-8"));
logs.touch(req.record);
}
if (req.record.length() >= FileListCapComponent.MAX_FILE_SIZE)
continue;
ThreadInfo lockedThread = ManagementFactory.getThreadMXBean().getThreadInfo(req.thread.getId(), Integer.MAX_VALUE);
if (lockedThread != null) {
w.println(contentFilter.map(cf -> cf.filter(lockedThread.toString())).orElse(lockedThread.toString()));
w.println(totalTime + "msec elapsed in " + contentFilter.map(cf -> cf.filter(lockedThread.getThreadName())).orElse(lockedThread.getThreadName()));
printThreadStackElements(lockedThread, w, contentFilter);
long lockOwnerId = lockedThread.getLockOwnerId();
if (// If the thread is not locked, then getLockOwnerId returns -1.
lockOwnerId != -1) {
ThreadInfo threadInfo = ManagementFactory.getThreadMXBean().getThreadInfo(lockOwnerId, Integer.MAX_VALUE);
w.println(contentFilter.map(cf -> cf.filter(lockedThread.toString())).orElse(lockedThread.toString()));
if (threadInfo != null) {
printThreadStackElements(threadInfo, w, contentFilter);
}
}
}
} finally {
IOUtils.closeQuietly(w);
}
}
}
}
use of com.cloudbees.jenkins.support.filter.ContentFilter in project support-core-plugin by jenkinsci.
the class DeadlockTrackChecker method doRun.
@Override
protected void doRun() throws Exception {
ThreadMXBean mbean = ManagementFactory.getThreadMXBean();
long[] deadLocks;
try {
deadLocks = mbean.findDeadlockedThreads();
} catch (UnsupportedOperationException x) {
deadLocks = null;
}
if (deadLocks != null && deadLocks.length != 0) {
File file = logs.file("DeadlockDetected-" + format.format(new Date()) + ".txt");
logs.add(file);
PrintWriter builder = new PrintWriter(file, "UTF-8");
try {
builder.println("==============");
builder.println("Deadlock Found");
builder.println("==============");
ThreadInfo[] deadLockThreads = mbean.getThreadInfo(deadLocks, Integer.MAX_VALUE);
ContentFilter contentFilter = SupportPlugin.getContentFilter().orElse(null);
for (ThreadInfo threadInfo : deadLockThreads) {
try {
ThreadDumps.printThreadInfo(builder, threadInfo, mbean, contentFilter);
} catch (LinkageError e) {
builder.println(threadInfo);
}
}
} finally {
builder.close();
}
}
}
use of com.cloudbees.jenkins.support.filter.ContentFilter in project support-core-plugin by jenkinsci.
the class FileDescriptorLimit method addContents.
private void addContents(@NonNull Container container, @NonNull final Node node) {
Computer c = node.toComputer();
if (c == null) {
return;
}
if (c instanceof SlaveComputer && !Boolean.TRUE.equals(c.isUnix())) {
return;
}
if (!node.createLauncher(TaskListener.NULL).isUnix()) {
return;
}
String name;
if (node instanceof Jenkins) {
name = "master";
} else {
name = "slave/" + node.getNodeName();
}
container.add(new PrefilteredPrintedContent("nodes/{0}/file-descriptors.txt", name) {
@Override
protected void printTo(PrintWriter out, ContentFilter filter) {
out.println(node.getDisplayName());
out.println("======");
out.println();
try {
out.println(AsyncResultCache.get(node, fileDescriptorCache, new GetUlimit(filter), "file descriptor info", "N/A: Either no connection to node or no cached result"));
} catch (IOException e) {
Functions.printStackTrace(e, out);
} finally {
out.flush();
}
}
});
}
Aggregations