Search in sources :

Example 1 with ContentFilter

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();
    }
}
Also used : FilteredOutputStream(com.cloudbees.jenkins.support.filter.FilteredOutputStream) OutputStreamSelector(com.cloudbees.jenkins.support.util.OutputStreamSelector) IgnoreCloseOutputStream(com.cloudbees.jenkins.support.util.IgnoreCloseOutputStream) FilteredOutputStream(com.cloudbees.jenkins.support.filter.FilteredOutputStream) IgnoreCloseOutputStream(com.cloudbees.jenkins.support.util.IgnoreCloseOutputStream) BufferedOutputStream(java.io.BufferedOutputStream) ZipArchiveOutputStream(org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) BulkChange(hudson.BulkChange) ZipArchiveOutputStream(org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream) IOException(java.io.IOException) ContentFilter(com.cloudbees.jenkins.support.filter.ContentFilter) ChannelClosedException(hudson.remoting.ChannelClosedException) UnfilteredStringContent(com.cloudbees.jenkins.support.api.UnfilteredStringContent) StringWriter(java.io.StringWriter) PrefilteredContent(com.cloudbees.jenkins.support.filter.PrefilteredContent) UnfilteredStringContent(com.cloudbees.jenkins.support.api.UnfilteredStringContent) Content(com.cloudbees.jenkins.support.api.Content) ZipArchiveEntry(org.apache.commons.compress.archivers.zip.ZipArchiveEntry) PrefilteredContent(com.cloudbees.jenkins.support.filter.PrefilteredContent) BufferedOutputStream(java.io.BufferedOutputStream) PrintWriter(java.io.PrintWriter)

Example 2 with ContentFilter

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())));
}
Also used : ContentMapping(com.cloudbees.jenkins.support.filter.ContentMapping) FreeStyleBuild(hudson.model.FreeStyleBuild) Matchers.containsString(org.hamcrest.Matchers.containsString) AssertionFailedError(junit.framework.AssertionFailedError) FreeStyleProject(hudson.model.FreeStyleProject) ContentFilter(com.cloudbees.jenkins.support.filter.ContentFilter) Test(org.junit.Test)

Example 3 with ContentFilter

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);
            }
        }
    }
}
Also used : PrintWriter(java.io.PrintWriter) SupportPlugin(com.cloudbees.jenkins.support.SupportPlugin) ContentFilter(com.cloudbees.jenkins.support.filter.ContentFilter) Date(java.util.Date) TimeZone(java.util.TimeZone) Inject(com.google.inject.Inject) Jenkins(jenkins.model.Jenkins) PeriodicWork(hudson.model.PeriodicWork) FileOutputStream(java.io.FileOutputStream) SimpleDateFormat(java.text.SimpleDateFormat) File(java.io.File) FileListCap(com.cloudbees.jenkins.support.timer.FileListCap) TimeUnit(java.util.concurrent.TimeUnit) IOUtils(hudson.util.IOUtils) FileListCapComponent(com.cloudbees.jenkins.support.timer.FileListCapComponent) ThreadInfo(java.lang.management.ThreadInfo) Extension(hudson.Extension) OutputStreamWriter(java.io.OutputStreamWriter) Optional(java.util.Optional) ManagementFactory(java.lang.management.ManagementFactory) ThreadInfo(java.lang.management.ThreadInfo) FileOutputStream(java.io.FileOutputStream) OutputStreamWriter(java.io.OutputStreamWriter) ContentFilter(com.cloudbees.jenkins.support.filter.ContentFilter) Date(java.util.Date) PrintWriter(java.io.PrintWriter)

Example 4 with ContentFilter

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();
        }
    }
}
Also used : ThreadMXBean(java.lang.management.ThreadMXBean) ThreadInfo(java.lang.management.ThreadInfo) File(java.io.File) Date(java.util.Date) ContentFilter(com.cloudbees.jenkins.support.filter.ContentFilter) PrintWriter(java.io.PrintWriter)

Example 5 with ContentFilter

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();
            }
        }
    });
}
Also used : SlaveComputer(hudson.slaves.SlaveComputer) Jenkins(jenkins.model.Jenkins) PrefilteredPrintedContent(com.cloudbees.jenkins.support.api.PrefilteredPrintedContent) Computer(hudson.model.Computer) SlaveComputer(hudson.slaves.SlaveComputer) IOException(java.io.IOException) ContentFilter(com.cloudbees.jenkins.support.filter.ContentFilter) PrintWriter(java.io.PrintWriter)

Aggregations

ContentFilter (com.cloudbees.jenkins.support.filter.ContentFilter)6 PrintWriter (java.io.PrintWriter)4 ContentMapping (com.cloudbees.jenkins.support.filter.ContentMapping)2 FreeStyleProject (hudson.model.FreeStyleProject)2 File (java.io.File)2 FileOutputStream (java.io.FileOutputStream)2 IOException (java.io.IOException)2 ThreadInfo (java.lang.management.ThreadInfo)2 Date (java.util.Date)2 Jenkins (jenkins.model.Jenkins)2 AssertionFailedError (junit.framework.AssertionFailedError)2 Matchers.containsString (org.hamcrest.Matchers.containsString)2 Test (org.junit.Test)2 SupportPlugin (com.cloudbees.jenkins.support.SupportPlugin)1 Content (com.cloudbees.jenkins.support.api.Content)1 PrefilteredPrintedContent (com.cloudbees.jenkins.support.api.PrefilteredPrintedContent)1 UnfilteredStringContent (com.cloudbees.jenkins.support.api.UnfilteredStringContent)1 FilteredOutputStream (com.cloudbees.jenkins.support.filter.FilteredOutputStream)1 PrefilteredContent (com.cloudbees.jenkins.support.filter.PrefilteredContent)1 FileListCap (com.cloudbees.jenkins.support.timer.FileListCap)1