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

Commit

Permalink
Bug 1538697 - Allow to specify if manually imported EAP as local (#344)
Browse files Browse the repository at this point in the history
* Bug 1538697 - Allow to specify if manually imported EAP as local
  Importing local EAPs follow a similar flow as the autodiscovery
  and are allowed to use operations.

* Bug 1538697 - Defaults to local server, but allow remotes if we can't fill all the required info

* Bug 1538697 - Moves code out of the try to avoid catching more Exceptions than expected
  • Loading branch information
josejulio authored and simeon pinder committed Mar 7, 2018
1 parent 332f7f3 commit cb1e34e
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 31 deletions.
Expand Up @@ -58,6 +58,7 @@
import org.rhq.modules.plugins.wildfly10.helper.HostConfiguration;
import org.rhq.modules.plugins.wildfly10.helper.HostPort;
import org.rhq.modules.plugins.wildfly10.helper.ServerPluginConfiguration;
import org.rhq.modules.plugins.wildfly10.json.Address;
import org.rhq.modules.plugins.wildfly10.json.Operation;
import org.rhq.modules.plugins.wildfly10.json.ReadAttribute;
import org.rhq.modules.plugins.wildfly10.json.Result;
Expand Down Expand Up @@ -468,14 +469,63 @@ public DiscoveredResourceDetails discoverResource(Configuration pluginConfig, Re
+ "] as user [" + user + "]. Did you provide the correct credentials?");
}

HostPort hostPort = new HostPort(false);
HostPort managementHostPort = new HostPort(false);
HostPort hostPort = new HostPort(true);
HostPort managementHostPort = new HostPort(true);
managementHostPort.host = hostname;
managementHostPort.port = port;
String key = createKeyForRemoteResource(hostname + ":" + port);
String name = buildDefaultResourceName(hostPort, managementHostPort, productType, null);
//FIXME this is inconsistent with how the version looks like when autodiscovered
String version = productInfo.getProductVersion();
hostPort.port = HostConfiguration.DEFAULT_NATIVE_PORT;
String key, name, version;
// Assume the server is local and follow a similar flow as if it was auto-discovered
ASConnection connection = new ASConnection(ASConnectionParams.createFrom(serverPluginConfig));
File homeDir = serverPluginConfig.getHomeDir();
if (homeDir == null) {
homeDir = getHomeDirFromConnection(connection);
serverPluginConfig.setHomeDir(homeDir);
}
if (serverPluginConfig.getBaseDir() == null) {
serverPluginConfig.setBaseDir(getBaseDirFromConnection(connection));
}
if (serverPluginConfig.getConfigDir() == null) {
serverPluginConfig.setConfigDir(getConfigDirFromConnection(connection));
}

File hostXmlFile = serverPluginConfig.getHostConfigFile();
if (hostXmlFile == null) {
hostXmlFile = getHostXmlFileFromConnection(connection);
serverPluginConfig.setHostConfigFile(hostXmlFile);
}
File logDir = serverPluginConfig.getLogDir();
if (logDir == null) {
logDir = getLogDirFromConnection(connection);
serverPluginConfig.setLogDir(logDir);
}
File logFile = getLogFile(logDir);
initLogEventSourcesConfigProp(logFile.getPath(), pluginConfig);

HostConfiguration hostConfig;
try {
hostConfig = loadHostConfiguration(hostXmlFile);
} catch (Exception exception) {
LOG.info("Manually imported server marked as [REMOTE]. Unable to load configuration file: [" + hostXmlFile.getPath() + "]", exception);
hostConfig = null;
}
if (hostConfig != null) {
serverPluginConfig.setApiVersion(hostConfig.getDomainApiVersion());
pluginConfig.setSimpleValue("hostXmlFileName", hostXmlFile.getName());
key = createKeyForLocalResource(serverPluginConfig);
name = buildDefaultResourceName(hostPort, managementHostPort, productType, hostConfig.getHostName());
version = getVersion(homeDir, productType);
serverPluginConfig.setLocal(true);
} else {
hostPort.isLocal = false;
managementHostPort.isLocal = false;
serverPluginConfig.setLocal(false);
key = createKeyForRemoteResource(hostname + ":" + port);
name = buildDefaultResourceName(hostPort, managementHostPort, productType, null);
//FIXME this is inconsistent with how the version looks like when autodiscovered
version = productInfo.getProductVersion();
}

String description = buildDefaultResourceDescription(hostPort, productType);

pluginConfig.put(new PropertySimple("manuallyAdded", true));
Expand All @@ -488,6 +538,31 @@ public DiscoveredResourceDetails discoverResource(Configuration pluginConfig, Re
return detail;
}

private File getFileFromServerEnvironment(ASConnection connection, String environment) {
String file = getServerEnvironment(connection, environment);
return new File(FileUtils.getCanonicalPath(file));
}

private File getHomeDirFromConnection(ASConnection connection) {
return getFileFromServerEnvironment(connection, "home-dir");
}

private File getBaseDirFromConnection(ASConnection connection) {
return getFileFromServerEnvironment(connection, "base-dir");
}

private File getConfigDirFromConnection(ASConnection connection) {
return getFileFromServerEnvironment(connection, "config-dir");
}

private File getHostXmlFileFromConnection(ASConnection connection) {
return getFileFromServerEnvironment(connection, "config-file");
}

private File getLogDirFromConnection(ASConnection connection) {
return getFileFromServerEnvironment(connection, "log-dir");
}

private String createKeyForRemoteResource(String hostPort) {
return REMOTE_RESOURCE_KEY_PREFIX + hostPort;
}
Expand Down Expand Up @@ -516,6 +591,18 @@ private <T> T getServerAttribute(ASConnection connection, String attributeName)
return result;
}

private <T> T getServerEnvironment(ASConnection connection, String environment) {
Operation op = new ReadAttribute(new Address("core-service", "server-environment"), environment);
Result res = connection.execute(op);
if (!res.isSuccess()) {
throw new InvalidPluginConfigurationException("Could not connect to remote server ["
+ res.getFailureDescription() + "]. Did you enable management?");
}
@SuppressWarnings("unchecked")
T result = (T) res.getResult();
return result;
}

// never returns null
private Properties getManagementUsers(HostConfiguration hostConfig, ServerPluginConfiguration pluginConfig) {
String realm = hostConfig.getManagementSecurityRealm();
Expand Down
Expand Up @@ -331,9 +331,6 @@ public void setConnection(ASConnection connection) {
protected OperationResult restartServer(Configuration parameters) throws Exception {

OperationResult operationResult = new OperationResult();
if (isManuallyAddedServer(operationResult, "Restarting")) {
return operationResult;
}

List<String> errors = validateStartScriptPluginConfigProps();
if (!errors.isEmpty()) {
Expand Down Expand Up @@ -413,9 +410,6 @@ protected boolean waitUntilDown() throws InterruptedException {
*/
protected OperationResult startServer() throws InterruptedException {
OperationResult operationResult = new OperationResult();
if (isManuallyAddedServer(operationResult, "Starting")) {
return operationResult;
}

List<String> errors = validateStartScriptPluginConfigProps();
if (!errors.isEmpty()) {
Expand Down Expand Up @@ -504,8 +498,18 @@ protected OperationResult setupCli(Configuration parameters) {
protected OperationResult runCliCommand(Configuration parameters) throws InterruptedException {
OperationResult result = new OperationResult();

if (isManuallyAddedServer(result, "Executing jboss-cli")) {
return result;
if (isManuallyAddedServer()) {
File homeDir = serverPluginConfig.getHomeDir();
if (!homeDir.exists()) {
result.setErrorMessage("Operation not enabled on servers without a valid Home Directory: [" + homeDir + "]");
return result;
}
File jbossCli = new File(new File(homeDir, "bin"), getMode().getCliScriptFileName());
if (!jbossCli.exists() || !jbossCli.canExecute()) {
result.setErrorMessage(getMode().getCliScriptFileName() +
" not found on Home Directory(" + homeDir + ") or is not executable");
return result;
}
}

long waitTime = Integer.parseInt(parameters.getSimpleValue("waitTime", "3600"));
Expand Down Expand Up @@ -561,14 +565,6 @@ public boolean isManuallyAddedServer() {
return pluginConfiguration.get("manuallyAdded") != null;
}

private boolean isManuallyAddedServer(OperationResult operationResult, String operation) {
if (isManuallyAddedServer()) {
operationResult.setErrorMessage(operation + " is not enabled for manually added servers");
return true;
}
return false;
}

private void setErrorMessage(OperationResult operationResult, List<String> errors) {
StringBuilder buffer = new StringBuilder("This Resource's connection properties contain errors: ");
for (int i = 0, errorsSize = errors.size(); i < errorsSize; i++) {
Expand Down Expand Up @@ -698,13 +694,6 @@ protected OperationResult installManagementUser(Configuration parameters, Config

OperationResult result = new OperationResult();

PropertySimple remoteProp = pluginConfig.getSimple("manuallyAdded");
if (remoteProp != null && remoteProp.getBooleanValue() != null && remoteProp.getBooleanValue()) {
result
.setErrorMessage("This is a manually added server. This operation can not be used to install a management user. Use the server's 'bin/add-user.sh'");
return result;
}

if (user.isEmpty() || password.isEmpty()) {
result.setErrorMessage("User and Password must not be empty");
return result;
Expand Down
Expand Up @@ -74,7 +74,7 @@ public Set<DiscoveredResourceDetails> discoverResources(ResourceDiscoveryContext

private void discoverResponseTimeLogFile(BaseServerComponent serverComponent, Configuration pluginConfig,
String nodePath, String contextRoot) {
if (serverComponent.isManuallyAddedServer()) {
if (serverComponent.isManuallyAddedServer() && !serverComponent.getServerPluginConfiguration().isLocal()) {
return;
}
String rtFilePath = null;
Expand Down
Expand Up @@ -45,6 +45,7 @@ public abstract class Property {
public static final String PASSWORD = "password";
public static final String MANAGEMENT_CONNECTION_TIMEOUT = "managementConnectionTimeout";
public static final String DEPLOYMENT_CONNECTION_TIMEOUT = "deploymentConnectionTimeout";
public static final String LOCAL = "local";
public static final String HOME_DIR = "homeDir";
public static final String BASE_DIR = "baseDir";
public static final String CONFIG_DIR = "configDir";
Expand Down Expand Up @@ -75,7 +76,7 @@ public Configuration getPluginConfig() {
}

/**
* returns detected path based on given path name
* returns detected path based on given path name
* @see <a href="https://docs.jboss.org/author/display/AS7/Admin+Guide#AdminGuide-Paths">https://docs.jboss.org/author/display/AS7/Admin+Guide#AdminGuide-Paths</a>
* @param pathName - is path name defined in AS7 config xml file
* @return File representing absolute path, return null if given pathName is not known
Expand Down Expand Up @@ -158,6 +159,14 @@ public Long getManagementConnectionTimeout() {
return this.pluginConfig.getSimple(Property.MANAGEMENT_CONNECTION_TIMEOUT).getLongValue();
}

public Boolean isLocal() {
return this.pluginConfig.getSimple(Property.LOCAL).getBooleanValue();
}

public void setLocal(boolean isLocal) {
this.pluginConfig.setSimpleValue(Property.LOCAL, String.valueOf(isLocal));
}

public File getHomeDir() {
String stringValue = this.pluginConfig.getSimpleValue(Property.HOME_DIR);
return (stringValue != null && !stringValue.isEmpty()) ? new File(stringValue) : null;
Expand Down

0 comments on commit cb1e34e

Please sign in to comment.