From 1a405038ab30aadcfb7d2eac33e32a54df537fec Mon Sep 17 00:00:00 2001 From: Shireesh Anjal Date: Mon, 12 Dec 2011 18:27:10 +0530 Subject: 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. --- .../storage/management/core/utils/ProcessUtil.java | 101 ++++++++++++++------- .../management/gateway/utils/ServerUtil.java | 10 +- 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 16f0f2f0..9a628cad 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 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 command, Map 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 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 command, Map 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 9a8b1409..4f7d125c 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 env = new HashMap(1); + env.put(ENV_GMG_VERSION, appVersion); + + return ProcessUtil.executeCommand(runInForeground, command, env); } private String getScriptPath(String scriptName) { -- cgit