Skip to content
This repository has been archived by the owner on Jul 11, 2022. It is now read-only.

Commit

Permalink
Merge pull request #225 from tsegismont/bug/1311094
Browse files Browse the repository at this point in the history
Bug 1311094 - EAP7 - Native management API port incorrectly detected
  • Loading branch information
burmanm committed Mar 18, 2016
2 parents e305e75 + 1231659 commit 089407e
Show file tree
Hide file tree
Showing 11 changed files with 22 additions and 510 deletions.
Expand Up @@ -42,16 +42,13 @@
import org.rhq.core.domain.configuration.PropertyList;
import org.rhq.core.domain.configuration.PropertyMap;
import org.rhq.core.domain.configuration.PropertySimple;
import org.rhq.core.domain.resource.ResourceUpgradeReport;
import org.rhq.core.pluginapi.event.log.LogFileEventResourceComponentHelper;
import org.rhq.core.pluginapi.inventory.DiscoveredResourceDetails;
import org.rhq.core.pluginapi.inventory.InvalidPluginConfigurationException;
import org.rhq.core.pluginapi.inventory.ManualAddFacet;
import org.rhq.core.pluginapi.inventory.ProcessScanResult;
import org.rhq.core.pluginapi.inventory.ResourceDiscoveryComponent;
import org.rhq.core.pluginapi.inventory.ResourceDiscoveryContext;
import org.rhq.core.pluginapi.upgrade.ResourceUpgradeContext;
import org.rhq.core.pluginapi.upgrade.ResourceUpgradeFacet;
import org.rhq.core.pluginapi.util.CommandLineOption;
import org.rhq.core.pluginapi.util.FileUtils;
import org.rhq.core.pluginapi.util.JavaCommandLine;
Expand All @@ -69,7 +66,7 @@
* Abstract base discovery component for the two server types - "JBossAS7 Host Controller" and
* "JBossAS7 Standalone Server".
*/
public abstract class BaseProcessDiscovery implements ResourceDiscoveryComponent, ManualAddFacet, ResourceUpgradeFacet {
public abstract class BaseProcessDiscovery implements ResourceDiscoveryComponent, ManualAddFacet {
private static final Log LOG = LogFactory.getLog(BaseProcessDiscovery.class);

private static final String JBOSS_EAP_PREFIX = "jboss-eap-";
Expand Down Expand Up @@ -197,12 +194,13 @@ protected DiscoveredResourceDetails buildResourceDetails(ResourceDiscoveryContex
initLogEventSourcesConfigProp(logFile.getPath(), pluginConfig);

HostPort managementHostPort = hostConfig.getManagementHostPort(commandLine, getMode());
if ("0.0.0.0".equals(managementHostPort.host)) {
LOG.debug("Discovered management host set to 0.0.0.0, falling back to 127.0.0.1");
managementHostPort.host = "127.0.0.1";
}
serverPluginConfig.setHostname(managementHostPort.host);
serverPluginConfig.setPort(managementHostPort.port);
serverPluginConfig.setSecure(managementHostPort.isSecure);
HostPort nativeHostPort = hostConfig.getNativeHostPort(commandLine, getMode());
serverPluginConfig.setNativeHost(nativeHostPort.host);
serverPluginConfig.setNativePort(nativeHostPort.port);
serverPluginConfig.setNativeLocalAuth(hostConfig.isNativeLocalOnly());
pluginConfig.setSimpleValue("realm", hostConfig.getManagementSecurityRealm());
String apiVersion = hostConfig.getDomainApiVersion();
Expand Down Expand Up @@ -491,139 +489,6 @@ public DiscoveredResourceDetails discoverResource(Configuration pluginConfig, Re
return detail;
}

@Override
public ResourceUpgradeReport upgrade(ResourceUpgradeContext inventoriedResource) {
ResourceUpgradeReport report = new ResourceUpgradeReport();
boolean upgraded = false;

String currentResourceKey = inventoriedResource.getResourceKey();
Configuration pluginConfiguration = inventoriedResource.getPluginConfiguration();
ServerPluginConfiguration serverPluginConfiguration = new ServerPluginConfiguration(pluginConfiguration);

HostConfiguration hostConfig = null;
// load hostConfiguration just once - we need it several times
File hostXmlFile = serverPluginConfiguration.getHostConfigFile();
if (hostXmlFile != null && hostXmlFile.exists()) {
try {
hostConfig = loadHostConfiguration(hostXmlFile);
} catch (Exception e) {
LOG.error("Unable to upgrade resource " + inventoriedResource, e);
return null;
}
} else {
LOG.warn("Unable to upgrade resource " + inventoriedResource
+ "Server configuration file not found at the expected location (" + hostXmlFile + ").");
return null;
}

boolean hasLocalResourcePrefix = currentResourceKey.startsWith(LOCAL_RESOURCE_KEY_PREFIX);
boolean hasRemoteResourcePrefix = currentResourceKey.startsWith(REMOTE_RESOURCE_KEY_PREFIX);
if (!hasLocalResourcePrefix && !hasRemoteResourcePrefix) {
// Resource key in wrong format
upgraded = true;
if (new File(currentResourceKey).isDirectory()) {
// Old key format for a local resource (key is base dir)
report.setNewResourceKey(createKeyForLocalResource(serverPluginConfiguration));
} else if (currentResourceKey.contains(":")) {
// Old key format for a remote (manually added) resource (key is base dir)
report.setNewResourceKey(createKeyForRemoteResource(currentResourceKey));
} else {
upgraded = false;
LOG.warn("Unknown format, cannot upgrade resource key [" + currentResourceKey + "]");
}
} else if (hasLocalResourcePrefix) {
String configFilePath = currentResourceKey.substring(LOCAL_RESOURCE_KEY_PREFIX.length());
File configFile = new File(configFilePath);
try {
String configFileCanonicalPath = configFile.getCanonicalPath();
if (!configFileCanonicalPath.equals(configFilePath)) {
upgraded = true;
report.setNewResourceKey(LOCAL_RESOURCE_KEY_PREFIX + configFileCanonicalPath);
}
} catch (IOException e) {
LOG.warn("Unexpected IOException while converting host config file path to its canonical form", e);
}
}

if (pluginConfiguration.getSimpleValue("expectedRuntimeProductName") == null) {
upgraded = true;
pluginConfiguration.setSimpleValue("expectedRuntimeProductName",
serverPluginConfiguration.getProductType().PRODUCT_NAME);
report.setNewPluginConfiguration(pluginConfiguration);
}

String supportsPatching = pluginConfiguration.getSimpleValue("supportsPatching");
if (supportsPatching == null || supportsPatching.startsWith("__UNINITIALIZED_")) {
upgraded = true;

JBossProductType productType = serverPluginConfiguration.getProductType();
if (productType == null) {
if (inventoriedResource.getNativeProcess() != null) {
// if resource seems to run, detect product type
try {
AS7CommandLine commandLine = new AS7CommandLine(inventoriedResource.getNativeProcess());
File homeDir = getHomeDir(inventoriedResource.getNativeProcess(), commandLine);
String apiVersion = hostConfig.getDomainApiVersion();
productType = JBossProductType.determineJBossProductType(homeDir, apiVersion);
serverPluginConfiguration.setProductType(productType);
pluginConfiguration.setSimpleValue("supportsPatching", Boolean.toString(true)); // @TODO Remove
report.setNewPluginConfiguration(pluginConfiguration);
} catch (Exception e) {
LOG.warn("Unable to detect productType", e);
}

} else {
// we have to skip upgrading at this point since it is not running and we're unable to detect productType
LOG.warn("Unable to upgrade resource "
+ inventoriedResource
+ " : resource is missing productType pluginConfiguration property and needs to be running in order to discover it");
upgraded = false;
}
} else {
pluginConfiguration
.setSimpleValue("supportsPatching", Boolean.toString(true)); // Remove
report.setNewPluginConfiguration(pluginConfiguration);
}


}

if (inventoriedResource.getNativeProcess() != null) {
String origHost = serverPluginConfiguration.getNativeHost();
Integer origPort = serverPluginConfiguration.getNativePort();
// detect if server is running and has default values
if ("127.0.0.1".equals(origHost) && Integer.valueOf(9999).equals(origPort)) {
try {
AS7CommandLine commandLine = new AS7CommandLine(inventoriedResource.getNativeProcess());
HostPort hp = hostConfig.getNativeHostPort(commandLine, getMode());

// only update pluginConfig if we detected a difference
if (!origHost.equals(hp.host) || origPort.intValue() != hp.port) {
serverPluginConfiguration.setNativeHost(hp.host);
serverPluginConfiguration.setNativePort(hp.port);
report.setNewPluginConfiguration(serverPluginConfiguration.getPluginConfig());
LOG.info("Detected native host/port " + hp + " for " + inventoriedResource.getResourceKey());
upgraded = true;
}

} catch (Exception e) {
LOG.error("Unable to detect and upgrade native management host and port", e);
}
}
}

if (pluginConfiguration.getSimpleValue(ServerPluginConfiguration.Property.NATIVE_LOCAL_AUTH) == null) {
serverPluginConfiguration.setNativeLocalAuth(hostConfig.isNativeLocalOnly());
report.setNewPluginConfiguration(serverPluginConfiguration.getPluginConfig());
upgraded = true;
}

if (upgraded) {
return report;
}
return null;
}

private String createKeyForRemoteResource(String hostPort) {
return REMOTE_RESOURCE_KEY_PREFIX + hostPort;
}
Expand Down
Expand Up @@ -115,6 +115,10 @@ public void start(ResourceContext<T> resourceContext) throws Exception {
super.start(resourceContext);
serverPluginConfig = new ServerPluginConfiguration(pluginConfiguration);
serverPluginConfig.validate();
if ("0.0.0.0".equals(serverPluginConfig.getHostname())) {
LOG.warn("Management host is set to 0.0.0.0 on server " + resourceContext.getResourceKey()
+ ". Please change this to avoid operation failures");
}
connection = new ASConnection(ASConnectionParams.createFrom(serverPluginConfig));
setASHostName(findASDomainHostName());
getAvailability();
Expand Down
Expand Up @@ -293,8 +293,8 @@ private ProcessExecutionResults executeCliCommand(String commands, String additi
commands = commands.replace('\n', ',');
String user = disconnected || local ? null : "--user=" + serverPluginConfig.getUser();
String password = disconnected || local ? null : "--password=" + serverPluginConfig.getPassword();
String controller = disconnected ? null : "--controller=" + serverPluginConfig.getNativeHost() + ":"
+ serverPluginConfig.getNativePort();
String controller = disconnected ? null : "--controller=" + serverPluginConfig.getHostname() + ":"
+ serverPluginConfig.getPort();

if (!disconnected && checkCertificate) {
// check whether we're able to talk to server and CLI won't block because it needs user to accept SSL certificate
Expand Down Expand Up @@ -341,8 +341,8 @@ public ProcessExecutionResults executeCliScript(File scriptFile) {
String file = "--file=" + script.getAbsolutePath();
String user = disconnected || local ? null : "--user=" + serverPluginConfig.getUser();
String password = disconnected || local ? null : "--password=" + serverPluginConfig.getPassword();
String controller = disconnected ? null : "--controller=" + serverPluginConfig.getNativeHost() + ":"
+ serverPluginConfig.getNativePort();
String controller = disconnected ? null : "--controller=" + serverPluginConfig.getHostname() + ":"
+ serverPluginConfig.getPort();

if (!disconnected && checkCertificate) {
// check whether we're able to talk to server and CLI won't block because it needs user to accept SSL certificate
Expand Down
Expand Up @@ -29,20 +29,16 @@
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.rhq.core.domain.resource.ResourceUpgradeReport;
import org.rhq.core.pluginapi.inventory.DiscoveredResourceDetails;
import org.rhq.core.pluginapi.inventory.ResourceDiscoveryContext;
import org.rhq.core.pluginapi.upgrade.ResourceUpgradeContext;
import org.rhq.core.pluginapi.upgrade.ResourceUpgradeFacet;

/**
* Discover domain deployments. This is analogous to {@link VersionedSubsystemDiscovery} but addresses only
* domain deployments. See {@link VersionedSubsystemDiscovery} for more on controlling/overriding the version handling.
*
* @author Jay Shaughnessy
*/
public class VersionedDomainDeploymentDiscovery extends AbstractVersionedDomainDeploymentDiscovery implements
ResourceUpgradeFacet {
public class VersionedDomainDeploymentDiscovery extends AbstractVersionedDomainDeploymentDiscovery {

private static final Log LOG = LogFactory.getLog(VersionedDomainDeploymentDiscovery.class);

Expand Down Expand Up @@ -119,53 +115,4 @@ public Set<DiscoveredResourceDetails> discoverResources(ResourceDiscoveryContext
details.addAll(updatedDetails);
return details;
}

// The Matching logic here is the same as above, but instead of setting the discovery details we
// set new values in the upgrade report for name, version and key. Note that if multiple resources
// upgrade to the same resource key it will be caught and fail downstream.
@Override
public ResourceUpgradeReport upgrade(ResourceUpgradeContext inventoriedResource) {
ResourceUpgradeReport result = null;

if (DISABLED) {
return result;
}

MATCHER.reset(inventoriedResource.getName());
if (MATCHER.matches()) {
result = new ResourceUpgradeReport();
result.setForceGenericPropertyUpgrade(true); // It is critical the name and version get upgraded

// reset the resource name with the stripped value
result.setNewName(MATCHER.group(1) + MATCHER.group(3));
result.setNewVersion(MATCHER.group(2));
}

StringBuilder sb = new StringBuilder();
String comma = "";
boolean upgradeKey = false;
for (String segment : COMMA_PATTERN.split(inventoriedResource.getResourceKey())) {
sb.append(comma);
comma = ",";
MATCHER.reset(segment);
if (MATCHER.matches()) {
upgradeKey = true;
sb.append(MATCHER.group(1)).append(MATCHER.group(3));
} else {
sb.append(segment);
}
}
if (upgradeKey) {
if (null == result) {
result = new ResourceUpgradeReport();
}
result.setNewResourceKey(sb.toString());
}

if (null != result && LOG.isDebugEnabled()) {
LOG.debug("Requesting upgrade: " + result);
}

return result;
}
}

0 comments on commit 089407e

Please sign in to comment.