diff options
| author | Shireesh Anjal <shireesh@gluster.com> | 2011-07-01 13:59:21 +0530 |
|---|---|---|
| committer | Shireesh Anjal <shireesh@gluster.com> | 2011-07-01 13:59:21 +0530 |
| commit | 40d4024c47ca1e1e15e2500a5412791d364bd8b0 (patch) | |
| tree | 6a2ca47c39ed016d634d9556b841c28096ca93eb /src | |
| parent | d0d2cd23f6f64b3645a108033586383158b2f7ac (diff) | |
Introduced configuration for SSH related timeouts
Diffstat (limited to 'src')
2 files changed, 56 insertions, 22 deletions
diff --git a/src/com.gluster.storage.management.server/src/com/gluster/storage/management/server/utils/SshUtil.java b/src/com.gluster.storage.management.server/src/com/gluster/storage/management/server/utils/SshUtil.java index 2ebf011b..d56cd47c 100644 --- a/src/com.gluster.storage.management.server/src/com/gluster/storage/management/server/utils/SshUtil.java +++ b/src/com.gluster.storage.management.server/src/com/gluster/storage/management/server/utils/SshUtil.java @@ -26,6 +26,8 @@ import java.io.InputStreamReader; import java.io.UnsupportedEncodingException; import java.util.Arrays; +import org.apache.log4j.Logger; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import ch.ethz.ssh2.ChannelCondition; @@ -59,6 +61,15 @@ public class SshUtil { private static final String USER_NAME = "root"; // TODO: Make default password configurable private static final String DEFAULT_PASSWORD = "syst3m"; + + private static final Logger logger = Logger.getLogger(SshUtil.class); + + @Autowired + private Integer sshConnectTimeout; + @Autowired + private Integer sshKexTimeout; + @Autowired + private Integer sshExecTimeout; public boolean hasDefaultPassword(String serverName) { try { @@ -207,29 +218,28 @@ public class SshUtil { Connection conn; conn = new Connection(serverName); try { - // tcp connection timeout = 3 sec, ssh connection timeout = 10 sec - conn.connect(null, 3000, 10000); + conn.connect(null, sshConnectTimeout, sshKexTimeout); } catch (IOException e) { - e.printStackTrace(); + logger.error("Couldn't establish SSH connection with server [" + serverName + "]", e); throw new ConnectionException("Exception while creating SSH connection with server [" + serverName + "]", e); } return conn; } - private boolean wasTerminated(int condition) { - return ((condition | ChannelCondition.EXIT_SIGNAL) == condition); - } - private boolean hasErrors(int condition, Session session) { return (hasErrorStream(condition) || (exitedGracefully(condition) && exitedWithError(session))); } + + private boolean timedOut(int condition) { + return (condition == ChannelCondition.TIMEOUT); + } private boolean exitedWithError(Session session) { return session.getExitStatus() != ProcessResult.SUCCESS; } private boolean exitedGracefully(int condition) { - return (condition | ChannelCondition.EXIT_STATUS) == condition; + return (condition == ChannelCondition.EXIT_STATUS); } private boolean hasErrorStream(int condition) { @@ -248,8 +258,10 @@ public class SshUtil { session.close(); return result; } catch (IOException e) { - throw new GlusterRuntimeException("Exception while executing command [" + command + "] on [" - + sshConnection.getHostname() + "]", e); + String errMsg = "Exception while executing command [" + command + "] on [" + sshConnection.getHostname() + + "]"; + logger.error(errMsg, e); + throw new GlusterRuntimeException(errMsg, e); } } @@ -258,29 +270,37 @@ public class SshUtil { // a) gracefully with an exit status, OR // b) because of a termination signal // c) command takes to long to exit (timeout) - int condition = session.waitForCondition(ChannelCondition.EXIT_SIGNAL | ChannelCondition.EXIT_STATUS - | ChannelCondition.TIMEOUT, 5000); + int condition = session.waitForCondition(ChannelCondition.EXIT_SIGNAL | ChannelCondition.EXIT_STATUS, + sshExecTimeout); StringBuilder output = new StringBuilder(); try { - readFromStream(stdoutReader, output); - if (hasErrors(condition, session)) { - readFromStream(stderrReader, output); + if(!timedOut(condition)) { + readFromStream(stdoutReader, output); + if (hasErrors(condition, session)) { + readFromStream(stderrReader, output); + } } return prepareProcessResult(session, condition, output.toString().trim()); } catch (IOException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - return null; + String errMsg = "Error while reading output stream from SSH connection!"; + logger.error(errMsg, e); + return new ProcessResult(ProcessResult.FAILURE, errMsg); } } private ProcessResult prepareProcessResult(Session session, int condition, String output) { ProcessResult result = null; - if (wasTerminated(condition)) { + switch(condition) { + case ChannelCondition.TIMEOUT: + result = new ProcessResult(ProcessResult.FAILURE, "Command timed out!"); + break; + case ChannelCondition.EXIT_SIGNAL: + // terminated result = new ProcessResult(ProcessResult.FAILURE, output); - } else { + break; + default: if (hasErrors(condition, session)) { Integer exitStatus = session.getExitStatus(); int statusCode = (exitStatus == null ? ProcessResult.FAILURE : exitStatus); @@ -288,12 +308,12 @@ public class SshUtil { } else { result = new ProcessResult(ProcessResult.SUCCESS, output); } + break; } return result; } - private void readFromStream(BufferedReader streamReader, StringBuilder output) throws IOException, - UnsupportedEncodingException { + private void readFromStream(BufferedReader streamReader, StringBuilder output) throws IOException { while (true) { String line = streamReader.readLine(); if (line == null) { diff --git a/src/com.gluster.storage.management.server/src/spring/gluster-server-base.xml b/src/com.gluster.storage.management.server/src/spring/gluster-server-base.xml index 3c7d6436..700d996f 100644 --- a/src/com.gluster.storage.management.server/src/spring/gluster-server-base.xml +++ b/src/com.gluster.storage.management.server/src/spring/gluster-server-base.xml @@ -21,6 +21,20 @@ <constructor-arg value="vmware" /> </bean> + <!-- SSH timeouts - all in milliseconds. zero means no timeout. --> + <!-- Connect the underlying TCP socket to the server with the given timeout value (SSH) --> + <bean id="sshConnectTimeout" class="java.lang.Integer"> + <constructor-arg value="10000" /> + </bean> + <!-- Timeout for complete connection establishment (SSH) --> + <bean id="sshKexTimeout" class="java.lang.Integer"> + <constructor-arg value="60000" /> + </bean> + <!-- Command execution timeout (SSH) --> + <bean id="sshExecTimeout" class="java.lang.Integer"> + <constructor-arg value="120000" /> + </bean> + <!-- Gluster Management Gateway Version --> <bean id="appVersion" class="java.lang.String"> <constructor-arg value="1.0.0" /> |
