summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorShireesh Anjal <shireesh@gluster.com>2011-12-12 18:27:10 +0530
committerShireesh Anjal <shireesh@gluster.com>2011-12-12 18:31:58 +0530
commit1a405038ab30aadcfb7d2eac33e32a54df537fec (patch)
tree7a3cf6578c01e441d0717dfd323dbea7d3f65599
parent5817247af263f4eca3322936e7a65e66ed54f561 (diff)
Modified ProcessUtil to support passing env variables while executing commands.
Modified ServerUtil to set env variable GMG_VERSION while executing any Gluster script on the gateway.
-rw-r--r--src/org.gluster.storage.management.core/src/org/gluster/storage/management/core/utils/ProcessUtil.java101
-rw-r--r--src/org.gluster.storage.management.gateway/src/org/gluster/storage/management/gateway/utils/ServerUtil.java10
2 files changed, 77 insertions, 34 deletions
diff --git a/src/org.gluster.storage.management.core/src/org/gluster/storage/management/core/utils/ProcessUtil.java b/src/org.gluster.storage.management.core/src/org/gluster/storage/management/core/utils/ProcessUtil.java
index 16f0f2f..9a628ca 100644
--- a/src/org.gluster.storage.management.core/src/org/gluster/storage/management/core/utils/ProcessUtil.java
+++ b/src/org.gluster.storage.management.core/src/org/gluster/storage/management/core/utils/ProcessUtil.java
@@ -25,6 +25,7 @@ import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
+import java.util.Map;
import org.gluster.storage.management.core.exceptions.GlusterRuntimeException;
@@ -68,42 +69,76 @@ public class ProcessUtil {
return executeCommand(commandList);
}
- /**
- * Executes given command in foreground/background
- * @param runInForeground Boolean flag indicating whether the command should
- * be executed in foreground
- * @param command
- * @return {@link ProcessResult} object
- */
- public static ProcessResult executeCommand(boolean runInForeground, List<String> command) {
- StringBuilder output = new StringBuilder();
- try {
- Process process = new ProcessBuilder(command).redirectErrorStream(true).start();
- if (runInForeground) {
- process.waitFor(); // Wait for process to finish
+ /**
+ * Creates a process builder for executing given command with given set of environment variables
+ *
+ * @param command
+ * The command to be executed
+ * @param env
+ * Set of env variables to be made available to the command
+ * @return Process builder that can be used to execute the command
+ */
+ private static ProcessBuilder createProcessBuilder(List<String> command, Map<String, String> env) {
+ ProcessBuilder builder = new ProcessBuilder(command);
+ if (env != null) {
+ builder.environment().putAll(env);
+ }
+ return builder;
+ }
- InputStream is = process.getInputStream();
- InputStreamReader isr = new InputStreamReader(is);
- BufferedReader br = new BufferedReader(isr);
- String line;
+ /**
+ * Executes given command in foreground/background
+ *
+ * @param runInForeground
+ * Boolean flag indicating whether the command should
+ * be executed in foreground
+ * @param command
+ * @return {@link ProcessResult} object
+ */
+ public static ProcessResult executeCommand(boolean runInForeground, List<String> command) {
+ return executeCommand(runInForeground, command, null);
+ }
+
+ /**
+ * Executes given command in foreground/background
+ *
+ * @param runInForeground
+ * Boolean flag indicating whether the command should
+ * be executed in foreground
+ * @param command
+ * @param env
+ * Set of env variables to be made available to the command
+ * @return {@link ProcessResult} object
+ */
+ public static ProcessResult executeCommand(boolean runInForeground, List<String> command, Map<String, String> env) {
+ StringBuilder output = new StringBuilder();
+ try {
+ Process process = createProcessBuilder(command, env).redirectErrorStream(true).start();
+ if (runInForeground) {
+ process.waitFor(); // Wait for process to finish
- while ((line = br.readLine()) != null) {
- output.append(line);
- output.append(NEWLINE);
- }
- br.close();
- isr.close();
- is.close();
- } else {
- output.append("Command [");
- output.append(command);
- output.append("] triggerred in background.");
- }
+ InputStream is = process.getInputStream();
+ InputStreamReader isr = new InputStreamReader(is);
+ BufferedReader br = new BufferedReader(isr);
+ String line;
+
+ while ((line = br.readLine()) != null) {
+ output.append(line);
+ output.append(NEWLINE);
+ }
+ br.close();
+ isr.close();
+ is.close();
+ } else {
+ output.append("Command [");
+ output.append(command);
+ output.append("] triggerred in background.");
+ }
- return new ProcessResult(process.exitValue(), output.toString());
- } catch (Throwable e) {
+ return new ProcessResult(process.exitValue(), output.toString());
+ } catch (Throwable e) {
throw new GlusterRuntimeException("Exception while executing command [" + command + "] : ["
+ e.getMessage() + "]", e);
- }
- }
+ }
+ }
}
diff --git a/src/org.gluster.storage.management.gateway/src/org/gluster/storage/management/gateway/utils/ServerUtil.java b/src/org.gluster.storage.management.gateway/src/org/gluster/storage/management/gateway/utils/ServerUtil.java
index 9a8b140..4f7d125 100644
--- a/src/org.gluster.storage.management.gateway/src/org/gluster/storage/management/gateway/utils/ServerUtil.java
+++ b/src/org.gluster.storage.management.gateway/src/org/gluster/storage/management/gateway/utils/ServerUtil.java
@@ -23,7 +23,9 @@ import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
+import java.util.HashMap;
import java.util.List;
+import java.util.Map;
import javax.servlet.ServletContext;
import javax.xml.bind.JAXBContext;
@@ -65,6 +67,7 @@ public class ServerUtil {
private static final String REMOTE_SCRIPT_GET_FILE_SYSTEM_TYPE = "get_filesystem_type.py";
private static final String REMOTE_SCRIPT_BASE_DIR = "/opt/glustermg";
private static final String REMOTE_SCRIPT_DIR_NAME = "backend";
+ private static final String ENV_GMG_VERSION = "GMG_VERSION";
public void setSshUtil(SshUtil sshUtil) {
this.sshUtil = sshUtil;
@@ -80,7 +83,12 @@ public class ServerUtil {
command.add(SCRIPT_COMMAND);
command.add(getScriptPath(scriptName));
command.addAll(arguments);
- return ProcessUtil.executeCommand(runInForeground, command);
+
+ // Set the Gateway Version number in an env variable which will be available to the script being executed.
+ Map<String, String> env = new HashMap<String, String>(1);
+ env.put(ENV_GMG_VERSION, appVersion);
+
+ return ProcessUtil.executeCommand(runInForeground, command, env);
}
private String getScriptPath(String scriptName) {