Skip to content

Commit

Permalink
[FIXED JENKINS-20362] Added CLI command to generate a support bundle.
Browse files Browse the repository at this point in the history
  • Loading branch information
jglick committed Dec 2, 2013
1 parent 7a262d5 commit 152fc6e
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 0 deletions.
105 changes: 105 additions & 0 deletions src/main/java/com/cloudbees/jenkins/support/SupportCommand.java
@@ -0,0 +1,105 @@
/*
* The MIT License
*
* Copyright 2013 Jesse Glick.
*
* 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.SupportProvider;
import hudson.Extension;
import hudson.cli.CLICommand;
import hudson.remoting.Callable;
import hudson.remoting.RemoteOutputStream;
import hudson.security.ACL;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.List;
import jenkins.model.Jenkins;
import org.acegisecurity.context.SecurityContext;
import org.acegisecurity.context.SecurityContextHolder;
import org.kohsuke.args4j.Argument;

@Extension public class SupportCommand extends CLICommand {

@Argument(metaVar="COMPONENTS") public List<String> components = new ArrayList<String>();

@Override public String getShortDescription() {
return Messages.SupportCommand_generates_a_diagnostic_support_bundle_();
}

@Override protected void printUsageSummary(PrintStream stderr) {
stderr.println(Messages.SupportCommand_if_no_arguments_are_given_generate_a_bun());
for (Component c : SupportPlugin.getComponents()) {
stderr.print(c.getId());
stderr.print('\t');
stderr.println(c.getDisplayName());
}
}

@Override protected int run() throws Exception {
Jenkins.getInstance().checkPermission(SupportPlugin.CREATE_BUNDLE);
List<Component> selected = new ArrayList<Component>();
for (Component c : SupportPlugin.getComponents()) {
if (c.isEnabled() && (components.isEmpty() || components.contains(c.getId()))) {
selected.add(c);
}
}
SupportPlugin.setRequesterAuthentication(Jenkins.getAuthentication());
try {
SecurityContext old = ACL.impersonate(ACL.SYSTEM);
try {
String filename = "support";
SupportPlugin supportPlugin = SupportPlugin.getInstance();
if (supportPlugin != null) {
SupportProvider supportProvider = supportPlugin.getSupportProvider();
if (supportProvider != null) {
filename = supportProvider.getName();
}
}
SupportPlugin.writeBundle(channel.call(new SaveBundle(filename)), selected);
} finally {
SecurityContextHolder.setContext(old);
}
} finally {
SupportPlugin.clearRequesterAuthentication();
}
return 0;
}

private static class SaveBundle implements Callable<OutputStream, IOException> {
private final String filename;
SaveBundle(String filename) {
this.filename = filename;
}
@Override public OutputStream call() throws IOException {
File f = File.createTempFile(filename, ".zip");
System.err.println("Creating: " + f);
return new RemoteOutputStream(new FileOutputStream(f));
}
}

}
Expand Up @@ -30,6 +30,8 @@ SupportAction_DefaultActionBlurb=\
the Jenkins community issue tracker. Be mindful that attachments to the Jenkins community issue tracker are public \
so be extra careful not to include sensitive information.

SupportCommand.generates_a_diagnostic_support_bundle_=Generates a diagnostic support bundle.
SupportCommand.if_no_arguments_are_given_generate_a_bun=If no arguments are given, generate a bundle with all components. Otherwise specify components by ID:
SupportPlugin_PermissionGroup=Support
SupportPlugin_CreateBundle=Generate bundle
SupportPlugin_loadConfig=Read support plugin configuration
Expand Down

0 comments on commit 152fc6e

Please sign in to comment.