Skip to content

Commit

Permalink
Merge pull request #7635 from ehsavoie/WFLY-4357
Browse files Browse the repository at this point in the history
[WFLY-4357] : Update the JDR Subsystem to Include a Type 4 UUID in its Report
  • Loading branch information
bstansberry committed Jul 6, 2015
2 parents fe46d78 + 7bdf448 commit 444a8d3
Show file tree
Hide file tree
Showing 8 changed files with 182 additions and 59 deletions.
2 changes: 2 additions & 0 deletions feature-pack/src/main/resources/content/bin/jdr.sh
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ if $cygwin; then
JBOSS_MODULEPATH=`cygpath --path --windows "$JBOSS_MODULEPATH"`
fi

#JAVA_OPTS="$JAVA_OPTS -agentlib:jdwp=transport=dt_socket,address=8787,server=y,suspend=y"

eval \"$JAVA\" $JAVA_OPTS \
-Djboss.home.dir=\""$JBOSS_HOME"\" \
-jar \""$JBOSS_HOME"/jboss-modules.jar\" \
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@

import org.jboss.as.controller.client.helpers.ClientConstants;
import org.jboss.as.controller.OperationFailedException;
import org.jboss.as.controller.client.helpers.Operations;

import org.jboss.as.jdr.logger.JdrLogger;

Expand All @@ -47,16 +48,17 @@
*/
public class CommandLineMain {

private static CommandLineParser parser = new GnuParser();
private static Options options = new Options();
private static HelpFormatter formatter = new HelpFormatter();
private static final CommandLineParser parser = new GnuParser();
private static final Options options = new Options();
private static final HelpFormatter formatter = new HelpFormatter();
private static final String usage = "jdr.{sh,bat} [options]";

static {
options.addOption("h", "help", false, JdrLogger.ROOT_LOGGER.jdrHelpMessage());
options.addOption("H", "host", true, JdrLogger.ROOT_LOGGER.jdrHostnameMessage());
options.addOption("p", "port", true, JdrLogger.ROOT_LOGGER.jdrPortMessage());
options.addOption("s", "protocol", true, JdrLogger.ROOT_LOGGER.jdrProtocolMessage());
options.addOption("c", "config", true, JdrLogger.ROOT_LOGGER.jdrConfigMessage());
}

/**
Expand All @@ -69,7 +71,7 @@ public static void main(String[] args) {
int port = 9990;
String host = "localhost";
String protocol = "http-remoting";

String config = null;
try {
CommandLine line = parser.parse(options, args, false);

Expand All @@ -88,6 +90,10 @@ public static void main(String[] args) {
if (line.hasOption("protocol")) {
protocol = line.getOptionValue("protocol");
}

if (line.hasOption("config")) {
config = line.getOptionValue("config");
}
} catch (ParseException e) {
System.out.println(e.getMessage());
formatter.printHelp(usage, options);
Expand All @@ -101,35 +107,33 @@ public static void main(String[] args) {
System.out.println("Initializing JBoss Diagnostic Reporter...");

// Try to run JDR on the Wildfly JVM
CLI cli = null;
CLI cli = CLI.newInstance();
boolean embedded = false;
try {
cli.connect(protocol, host, port, null, null);
} catch (IllegalStateException ex) {
String startEmbeddedServer = "embed-server" + ((config != null && ! config.isEmpty()) ? (" --server-config=" + config) : "");
cli.getCommandContext().handleSafe(startEmbeddedServer);
embedded = true;
}
try {
cli = CLI.newInstance();
cli.connect(host, port, null, null);
Result cmdResult = cli.cmd("/subsystem=jdr:generate-jdr-report()");
ModelNode response = cmdResult.getResponse();
reportFailure(response);
ModelNode result = response.get(ClientConstants.RESULT);
String startTime = result.get("start-time").asString();
String endTime = result.get("end-time").asString();
String reportLocation = result.get("report-location").asString();
System.out.println("JDR started: " + startTime);
System.out.println("JDR ended: " + endTime);
System.out.println("JDR location: " + reportLocation);
if(Operations.isSuccessfulOutcome(response) || !embedded) {
reportFailure(response);
ModelNode result = response.get(ClientConstants.RESULT);
String startTime = result.get("start-time").asString();
String endTime = result.get("end-time").asString();
String reportLocation = result.get("report-location").asString();
System.out.println("JDR started: " + startTime);
System.out.println("JDR ended: " + endTime);
System.out.println("JDR location: " + reportLocation);
} else {
standaloneCollect(cli, protocol, host, port);
}
} catch(IllegalStateException ise) {
System.out.println(ise.getMessage());

// Unable to connect to a running server, so proceed without it
JdrReportService reportService = new JdrReportService();

JdrReport response = null;
try {
response = reportService.standaloneCollect(protocol, host, String.valueOf(port));
System.out.println("JDR started: " + response.getStartTime().toString());
System.out.println("JDR ended: " + response.getEndTime().toString());
System.out.println("JDR location: " + response.getLocation());
} catch (OperationFailedException e) {
System.out.println("Failed to complete the JDR report: " + e.getMessage());
}
standaloneCollect(cli, protocol, host, port);
} finally {
if(cli != null) {
try {
Expand All @@ -142,6 +146,19 @@ public static void main(String[] args) {
System.exit(0);
}

private static void standaloneCollect(CLI cli, String protocol, String host, int port) {
// Unable to connect to a running server, so proceed without it
JdrReportService reportService = new JdrReportService();
try {
JdrReport response = reportService.standaloneCollect(cli, protocol, host, port);
System.out.println("JDR started: " + response.getStartTime().toString());
System.out.println("JDR ended: " + response.getEndTime().toString());
System.out.println("JDR location: " + response.getLocation());
} catch (OperationFailedException e) {
System.out.println("Failed to complete the JDR report: " + e.getMessage());
}
}

private static void reportFailure(final ModelNode node) {
if (!node.get(ClientConstants.OUTCOME).asString().equals(ClientConstants.SUCCESS)) {
final String msg;
Expand Down
70 changes: 69 additions & 1 deletion jdr/jboss-as-jdr/src/main/java/org/jboss/as/jdr/JdrReport.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,18 @@
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/

package org.jboss.as.jdr;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.util.Date;
import java.util.Properties;
import static org.jboss.as.jdr.logger.JdrLogger.ROOT_LOGGER;

/**
* Provides metadata about and access to the data collected by a {@link JdrReportCollector}.
Expand All @@ -31,9 +39,25 @@
* @author Mike M. Clark
*/
public class JdrReport {

public static final String JBOSS_PROPERTY_DIR = "jboss.server.data.dir";

public static final String JDR_PROPERTY_FILE_NAME = "jdr.properties";

public static final String UUID_NAME = "UUID";

public static final String JDR_PROPERTIES_COMMENT = "JDR Properties";

public static final String JBOSS_HOME_DIR = "jboss.home.dir";

public static final String DEFAULT_PROPERTY_DIR = "standalone";

public static final String DATA_DIR = "data";

private Date startTime;
private Date endTime;
private String location;
private String jdrUuid;

public JdrReport() {
}
Expand Down Expand Up @@ -80,4 +104,48 @@ public String getLocation() {
public void setLocation(String location) {
this.location = location;
}

public void setJdrUuid(String jdrUuid) {
this.jdrUuid = jdrUuid;
String jbossConfig = System.getProperty(JBOSS_PROPERTY_DIR);
Path jbossConfigPath;
// JDR is being ran from command line
if (jbossConfig == null) {
String jbossHome = System.getProperty(JBOSS_HOME_DIR);
// if JBoss standalone directory does not exist then go no further
Path defaultDir = new File(jbossHome, DEFAULT_PROPERTY_DIR).toPath();
if (Files.notExists(defaultDir)) {
ROOT_LOGGER.couldNotFindJDRPropertiesFile();
}
jbossConfigPath = defaultDir.resolve(DATA_DIR);
} else {
jbossConfigPath = new File(jbossConfig).toPath();
}
Path jdrPropertiesFilePath = jbossConfigPath.resolve(JdrReportExtension.SUBSYSTEM_NAME).resolve(JDR_PROPERTY_FILE_NAME);
Properties jdrProperties = new Properties();
try {
Files.createDirectories(jdrPropertiesFilePath.getParent());
} catch (IOException e) {
ROOT_LOGGER.couldNotCreateJDRPropertiesFile(e, jdrPropertiesFilePath);
}
if (jdrUuid == null && Files.exists(jdrPropertiesFilePath)) {
try (InputStream in = Files.newInputStream(jdrPropertiesFilePath)) {
jdrProperties.load(in);
this.jdrUuid = jdrProperties.getProperty(UUID_NAME);
} catch (IOException e) {
ROOT_LOGGER.couldNotFindJDRPropertiesFile();
}
} else {
try (OutputStream fileOut = Files.newOutputStream(jdrPropertiesFilePath, StandardOpenOption.CREATE)) {
jdrProperties.setProperty(UUID_NAME, jdrUuid);
jdrProperties.store(fileOut, JDR_PROPERTIES_COMMENT);
} catch (IOException e) {
ROOT_LOGGER.couldNotCreateJDRPropertiesFile(e, jdrPropertiesFilePath);
}
}
}

public String getJdrUuid() {
return jdrUuid;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,5 @@ public void execute(OperationContext context, ModelNode operation) throws Operat
context.completeStep(OperationContext.RollbackHandler.NOOP_ROLLBACK_HANDLER);
}
}, OperationContext.Stage.RUNTIME);



context.stepCompleted();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import java.util.concurrent.ThreadFactory;

import static java.security.AccessController.doPrivileged;
import org.jboss.as.cli.scriptsupport.CLI;

/**
* Service that provides a {@link JdrReportCollector}.
Expand Down Expand Up @@ -74,22 +75,10 @@ public static ServiceController<JdrReportCollector> addService(final ServiceTarg

/**
* Collect a JDR report when run outside the Application Server.
* @param cli
*/
public JdrReport standaloneCollect(String protocol, String host, String port) throws OperationFailedException {
String username = null;
String password = null;

if (host == null) {
host = "localhost";
}
if (port == null) {
port = "9990";
}
if(protocol == null) {
protocol = "http-remoting";
}

return new JdrRunner(protocol, username, password, host, port).collect();
public JdrReport standaloneCollect(CLI cli, String protocol, String host, int port) throws OperationFailedException {
return new JdrRunner(cli, protocol, host, port, null, null).collect();
}

/**
Expand Down
46 changes: 35 additions & 11 deletions jdr/jboss-as-jdr/src/main/java/org/jboss/as/jdr/JdrRunner.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,20 @@
package org.jboss.as.jdr;

import org.jboss.as.cli.CommandContext;
import org.jboss.as.cli.CommandContextFactory;
import org.jboss.as.cli.scriptsupport.CLI;
import org.jboss.as.controller.OperationFailedException;
import org.jboss.as.controller.client.ModelControllerClient;
import org.jboss.as.controller.client.helpers.Operations;
import org.jboss.as.jdr.commands.JdrCommand;
import org.jboss.as.jdr.commands.JdrEnvironment;
import org.jboss.as.jdr.logger.JdrLogger;
import org.jboss.as.jdr.plugins.JdrPlugin;
import org.jboss.as.jdr.util.JdrZipFile;
import org.jboss.dmr.ModelNode;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
Expand All @@ -42,7 +45,10 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import org.jboss.as.cli.CommandContextFactory;

import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.INCLUDE_RUNTIME;
import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.UUID;
import static org.jboss.as.jdr.logger.JdrLogger.ROOT_LOGGER;

public class JdrRunner implements JdrReportCollector {
Expand All @@ -54,29 +60,29 @@ public JdrRunner(boolean serverRunning) {
this.env.setServerRunning(serverRunning);
}

public JdrRunner(String protocol, String user, String pass, String host, String port) {
public JdrRunner(CLI cli, String protocol, String host, int port, String user, String pass) {
this.env.setServerRunning(false);
this.env.setUsername(user);
this.env.setPassword(pass);
this.env.setHost(host);
this.env.setPort(port);
this.env.setHost(cli.getCommandContext().getControllerHost());
this.env.setPort("" + cli.getCommandContext().getControllerPort());
this.env.setCli(cli);
try {
ctx = CommandContextFactory.getInstance().newCommandContext(constructUri(protocol, host, Integer.parseInt(port)), null, null);
ctx = CommandContextFactory.getInstance().newCommandContext(constructUri(protocol, host, port), user, pass == null ? new char[0] : pass.toCharArray());
ctx.connectController();
this.env.setClient(ctx.getModelControllerClient());
}
catch (Exception e) {
} catch (Exception e) {
ctx.terminateSession();
// the server isn't available, carry on
}
}

@Override
public JdrReport collect() throws OperationFailedException {

try {
this.env.setZip(new JdrZipFile(new JdrEnvironment(this.env)));
}
catch (Exception e) {
} catch (Exception e) {
ROOT_LOGGER.error(ROOT_LOGGER.couldNotCreateZipfile(), e);
throw new OperationFailedException(JdrLogger.ROOT_LOGGER.couldNotCreateZipfile());
}
Expand Down Expand Up @@ -113,8 +119,8 @@ public JdrReport collect() throws OperationFailedException {
JdrReport report = new JdrReport();
StringBuilder skips = new StringBuilder();
report.setStartTime();

for( JdrCommand command : commands ) {
report.setJdrUuid(obtainServerUUID());
for (JdrCommand command : commands) {
command.setEnvironment(new JdrEnvironment(this.env));
try {
command.execute();
Expand Down Expand Up @@ -173,6 +179,24 @@ public void setServerName(String name) {
this.env.setServerName(name);
}

private String obtainServerUUID() throws OperationFailedException {
try {
ModelNode operation = Operations.createReadAttributeOperation(new ModelNode().setEmptyList(), UUID);
operation.get(INCLUDE_RUNTIME).set(true);
ModelControllerClient client = env.getClient();
if (client == null) {
client = env.getCli().getCommandContext().getModelControllerClient();
}
ModelNode result = client.execute(operation);
if (Operations.isSuccessfulOutcome(result)) {
return Operations.readResult(result).asString();
}
return null;
} catch (IOException ex) {
return null;
}
}

private String constructUri(final String protocol, final String host, final int port) throws URISyntaxException {
URI uri = new URI(protocol, null, host, port, null, null, null);
// String the leading '//' if there is no protocol.
Expand Down
Loading

0 comments on commit 444a8d3

Please sign in to comment.