// do the install work
String parentPath = customData.getParentPath();
Credentials credentials = getCredentialsToUse();
String serverAddress = LookupUtil.getServerManager().getServer().getAddress();
AgentInstallInfo info = new AgentInstallInfo(parentPath, credentials.getUsername(), agentVersion,
serverAddress, accessInfo.getHost());
executeCommand("uname -a", "Machine uname", info);
executeCommand("java -version", "Java Version Check", info);
executeCommand("mkdir -p '" + parentPath + "'", "Create Agent Install Directory", info);
executeCommand("rm -rf '" + parentPath + "/rhq-agent'", "Remove any previously installed agent", info);
executeCommand("rm -f '" + parentPath + "/rhq-agent-update.log'", "Remove any old agent update logs", info);
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);
}
}
log.info("Will start new agent @ [" + accessInfo.getHost() + "] pointing to server @ [" + serverAddress + "]");
String agentScript = parentPath + "/rhq-agent/bin/rhq-agent.sh"; // NOTE: NOT the wrapper script
String startStringArgs = info.getConfigurationStartString();
// this ID will be used by the agent when it registered, thus allowing the server to link this install with that agent
if (installId != null) {
startStringArgs += " -D" + AgentRegistrationRequest.SYSPROP_INSTALL_ID + "=" + installId;
}
// Tell the script to store a pid file to make the wrapper script work
String envCmd1 = "RHQ_AGENT_IN_BACKGROUND='" + parentPath + "/rhq-agent/bin/rhq-agent.pid'";
String envCmd2 = "export RHQ_AGENT_IN_BACKGROUND";
String startCommand = envCmd1 + " ; " + envCmd2 + " ; nohup '" + agentScript + "' " + startStringArgs + " &";
executeCommand(startCommand, "Start New Agent", info);
// see if we can confirm the agent connection now
Boolean pingResults = checkAgentConnection(info, 5);
if (pingResults == null) {
log.warn("Just installed an agent at [" + info.getAgentAddress()
+ "] but could not determine its port. No validation check will be made.");
} else if (!pingResults.booleanValue()) {
log.warn("Just installed an agent at [" + info.getAgentAddress()
+ "] but could not ping its port. Something might be bad with the install or it is behind a firewall.");
}
return info;
}