executeCommand("rm -f " + parentPath.replace(" ", "\\ ") + "/rhq-enterprise-agent*.jar",
"Remove any old agent update binary jars", info); // because we use * wildcard, can't wrap in quotes, so escape spaces if there are any in the path
log.info("Copying agent binary update distribution file to [" + accessInfo.getHost() + "]...");
AgentInstallStep scpStep = sendFile(agentPath, parentPath, "Remote copy the agent binary update distribution");
info.addStep(scpStep);
if(scpStep.getResultCode() != 0) {
return info; // abort and return what we did - no sense continuing if the agent distro failed to copy
}
log.info("Agent binary update distribution file copied");
executeCommand("cd '" + parentPath + "' ; " + "java -jar '" + parentPath + "/" + agentFile + "' '--install="
+ parentPath + "'", "Install Agent", info);
String agentConfigXmlFilename = parentPath + "/rhq-agent/conf/agent-configuration.xml";
if (customData.getAgentConfigurationXmlFile() != null) {
log.info("Copying custom agent configuration file...");
AgentInstallStep step = sendFile(customData.getAgentConfigurationXmlFile(), agentConfigXmlFilename, "Remote copy the agent configuration file");
info.addStep(step);
if(step.getResultCode() != 0) {
return info; // abort and return what we did - no sense continuing if the custom config file failed to copy
}
log.info("Custom agent configuration file copied.");
// tell the info object - this is needed so it adds the --config command line option
info.setCustomAgentConfigurationFile("agent-configuration.xml");
}
// try to see if we can figure out what the port will be that the agent will bind to
// this will use awk to find a line in the agent config xml that matches this:
// <entry key="rhq.communications.connector.bind-port" value="16163" />
// where we use " as the field separator and the port number will be the fourth field.
String agentPortAwkCommand = "awk '-F\"' '/key.*=.*" + AgentInstallInfo.AGENT_PORT_PROP + "/ {print $4}' "
+ "'" + agentConfigXmlFilename + "'";
String portStr = executeCommand(agentPortAwkCommand, "Determine the agent's bind port", info);
try {
int port = Integer.parseInt(portStr.trim());
info.setAgentPort(port);
} catch (Exception e) {
info.setAgentPort(0); // indicate that we don't know it
}
if (customData.getRhqAgentEnvFile() != null) {
log.info("Copying custom agent environment script...");
String destFilename = parentPath + "/rhq-agent/bin/rhq-agent-env.sh";
AgentInstallStep step = sendFile(customData.getRhqAgentEnvFile(), destFilename, "Remote copy the agent environment script file");
info.addStep(step);
if (step.getResultCode() != 0) {
return info; // abort and return what we did - no sense continuing if the custom env script file failed to copy
}
log.info("Custom agent environment script copied.");
}
// Do a quick check to see if there is something already listening on the agent's port.
long start = System.currentTimeMillis();
Boolean squatterCheck = checkAgentConnection(info, 1);
if (squatterCheck != null) { // if this is null, we weren't even able to check
if (squatterCheck.booleanValue()) {
AgentInstallStep step = new AgentInstallStep("ping " + info.getAgentAddress() + ":"
+ info.getAgentPort(), "See if anything has already taken the agent port", 1,
"Port already in use", getTimeDiff(start));
info.addStep(step);
return info; // abort, don't install an agent if something is already squatting on its port
} else {
AgentInstallStep step = new AgentInstallStep("ping " + info.getAgentAddress() + ":"
+ info.getAgentPort(), "See if anything has already taken the agent port", 0, "Port free",
getTimeDiff(start));
info.addStep(step);
}
}