use of com.cloudbees.jenkins.support.api.StringContent in project support-core-plugin by jenkinsci.
the class ThreadDumps method addContents.
@Override
public void addContents(@NonNull Container container, @NonNull Computer item) {
Node node = item.getNode();
if (node == null) {
return;
}
// let's start collecting thread dumps now... this gives us until the end of the bundle to finish
final Future<String> threadDump;
try {
threadDump = getThreadDump(node);
} catch (IOException e) {
logger.log(Level.WARNING, "Could not record thread dump for " + node.getNodeName(), e);
final StringWriter sw = new StringWriter();
PrintWriter out = new PrintWriter(sw);
Functions.printStackTrace(e, out);
out.close();
container.add(new StringContent("nodes/slave/{0}/thread-dump.txt", new String[] { node.getNodeName() }, sw.toString()));
return;
}
if (threadDump == null) {
StringBuilder buf = new StringBuilder();
buf.append(node.getNodeName()).append("\n");
buf.append("======\n");
buf.append("\n");
buf.append("N/A: No connection to node.\n");
container.add(new StringContent("nodes/slave/{0}/thread-dump.txt", new String[] { node.getNodeName() }, buf.toString()));
} else {
container.add(new Content("nodes/slave/{0}/thread-dump.txt", node.getNodeName()) {
@Override
public void writeTo(OutputStream os) throws IOException {
PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(os, "utf-8")));
try {
out.println(node.getNodeName());
out.println("======");
out.println();
String content = null;
try {
// We want to wait here a bit longer than normal
// as we will not fall back to a cache
content = threadDump.get(Math.min(SupportPlugin.REMOTE_OPERATION_TIMEOUT_MS * 8, TimeUnit.SECONDS.toMillis(SupportPlugin.REMOTE_OPERATION_CACHE_TIMEOUT_SEC)), TimeUnit.MILLISECONDS);
} catch (InterruptedException e) {
logger.log(Level.WARNING, "Could not record thread dump for " + node.getNodeName(), e);
Functions.printStackTrace(e, out);
} catch (ExecutionException e) {
logger.log(Level.WARNING, "Could not record thread dump for " + node.getNodeName(), e);
Functions.printStackTrace(e, out);
} catch (TimeoutException e) {
logger.log(Level.WARNING, "Could not record thread dump for " + node.getNodeName(), e);
Functions.printStackTrace(e, out);
threadDump.cancel(true);
}
if (content != null) {
out.println(content);
}
} finally {
out.flush();
}
}
});
}
}
use of com.cloudbees.jenkins.support.api.StringContent in project support-core-plugin by jenkinsci.
the class SupportPlugin method writeBundle.
public static void writeBundle(OutputStream outputStream, final List<Component> components) throws IOException {
// TODO why is this not SupportPlugin.logger?
Logger logger = Logger.getLogger(SupportPlugin.class.getName());
final java.util.Queue<Content> toProcess = new ConcurrentLinkedQueue<Content>();
final Set<String> names = new TreeSet<String>();
Container container = new Container() {
@Override
public void add(@CheckForNull Content content) {
if (content != null) {
names.add(content.getName());
toProcess.add(content);
}
}
};
StringBuilder manifest = new StringBuilder();
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');
manifest.append(StringUtils.repeat("=", bundleName.length())).append('\n');
manifest.append("\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");
manifest.append("\n");
manifest.append("Requested components:\n\n");
StringWriter errors = new StringWriter();
PrintWriter errorWriter = new PrintWriter(errors);
for (Component c : components) {
try {
manifest.append(" * ").append(c.getDisplayName()).append("\n\n");
names.clear();
c.addContents(container);
for (String name : names) {
manifest.append(" - `").append(name).append("`\n\n");
}
} catch (Throwable e) {
String cDisplayName = null;
try {
cDisplayName = c.getDisplayName();
} catch (Throwable e1) {
// be very defensive
cDisplayName = c.getClass().getName();
}
LogRecord logRecord = new LogRecord(Level.WARNING, "Could not get content from ''{0}'' for support bundle");
logRecord.setThrown(e);
logRecord.setParameters(new Object[] { cDisplayName });
logger.log(logRecord);
errorWriter.println(MessageFormat.format("Could not get content from ''{0}'' for support bundle", cDisplayName));
errorWriter.println("-----------------------------------------------------------------------");
errorWriter.println();
SupportLogFormatter.printStackTrace(e, errorWriter);
errorWriter.println();
}
}
toProcess.add(new StringContent("manifest.md", manifest.toString()));
try {
ZipOutputStream zip = new ZipOutputStream(outputStream);
try {
BufferedOutputStream bos = new BufferedOutputStream(zip, 16384) {
@Override
public void close() throws IOException {
// don't let any of the contents accidentally close the zip stream
super.flush();
}
};
while (!toProcess.isEmpty()) {
Content c = toProcess.poll();
if (c == null) {
continue;
}
final String name = c.getName();
try {
ZipEntry entry = new ZipEntry(name);
entry.setTime(c.getTime());
zip.putNextEntry(entry);
c.writeTo(bos);
} catch (Throwable e) {
LogRecord logRecord = new LogRecord(Level.WARNING, "Could not attach ''{0}'' to support bundle");
logRecord.setThrown(e);
logRecord.setParameters(new Object[] { name });
logger.log(logRecord);
errorWriter.println(MessageFormat.format("Could not attach ''{0}'' to support bundle", name));
errorWriter.println("-----------------------------------------------------------------------");
errorWriter.println();
SupportLogFormatter.printStackTrace(e, errorWriter);
errorWriter.println();
} finally {
bos.flush();
}
zip.flush();
}
errorWriter.close();
String errorContent = errors.toString();
if (!StringUtils.isBlank(errorContent)) {
try {
zip.putNextEntry(new ZipEntry("manifest/errors.txt"));
zip.write(errorContent.getBytes("utf-8"));
} catch (IOException e) {
// ignore
}
zip.flush();
}
} finally {
zip.close();
}
} finally {
outputStream.flush();
}
}
Aggregations