use of com.cloudbees.jenkins.support.api.UnfilteredStringContent in project support-core-plugin by jenkinsci.
the class RootCAs method addContents.
private void addContents(@NonNull Container container, @NonNull final Node node) {
Computer c = node.toComputer();
if (c == null) {
return;
}
String name;
String[] params;
if (node instanceof Jenkins) {
name = "nodes/master/RootCA.txt";
params = new String[0];
} else {
name = "nodes/slave/{0}/RootCA.txt";
params = new String[] { node.getNodeName() };
}
try {
container.add(new UnfilteredStringContent(name, params, getRootCA(node)));
} catch (IOException e) {
container.add(new UnfilteredStringContent(name, params, Functions.printThrowable(e)));
}
}
use of com.cloudbees.jenkins.support.api.UnfilteredStringContent 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.api.UnfilteredStringContent in project support-core-plugin by jenkinsci.
the class SystemConfiguration method afterAddUnixContents.
@Override
protected void afterAddUnixContents(@NonNull Container container, @NonNull final Node node, String name) {
container.add(UnfilteredCommandOutputContent.runOnNodeAndCache(sysCtlCache, node, "nodes/{0}/sysctl.txt", new String[] { name }, "/bin/sh", "-c", "sysctl -a"));
container.add(UnfilteredCommandOutputContent.runOnNode(node, "nodes/{0}/dmesg.txt", new String[] { name }, "/bin/sh", "-c", "(dmesg --ctime 2>/dev/null||dmesg) |tail -1000"));
container.add(CommandOutputContent.runOnNodeAndCache(userIdCache, node, "nodes/{0}/userid.txt", new String[] { name }, "/bin/sh", "-c", "id -a"));
container.add(new UnfilteredStringContent("nodes/{0}/dmi.txt", new String[] { name }, getDmiInfo(node)));
}
Aggregations