diff options
| author | Selvasundaram <selvam@gluster.com> | 2011-08-04 18:14:18 +0530 |
|---|---|---|
| committer | Selvasundaram <selvam@gluster.com> | 2011-08-04 18:14:18 +0530 |
| commit | a1a6e57b9b9327746f2bd5062f01f1646c883549 (patch) | |
| tree | 68f690d3d942173542d438c3b3beec9cd3a4ce53 /src | |
| parent | 899c2623e4fe877c46c457f19f48524771551d9a (diff) | |
| parent | 5cc54f2bb36fc4455d52eb24315c0e00e7eea448 (diff) | |
Merge branch 'master' of github.com:gluster/console
Diffstat (limited to 'src')
15 files changed, 232 insertions, 184 deletions
diff --git a/src/com.gluster.storage.management.core/src/com/gluster/storage/management/core/response/GenericResponse.java b/src/com.gluster.storage.management.core/src/com/gluster/storage/management/core/response/GenericResponse.java deleted file mode 100644 index 6c8e87b5..00000000 --- a/src/com.gluster.storage.management.core/src/com/gluster/storage/management/core/response/GenericResponse.java +++ /dev/null @@ -1,58 +0,0 @@ -/** - * GenericServerResponse.java - * - * Copyright (c) 2011 Gluster, Inc. <http://www.gluster.com> - * This file is part of Gluster Management Console. - * - * Gluster Management Console is free software; you can redistribute it and/or - * modify it under the terms of the GNU Affero General Public License as published - * by the Free Software Foundation; either version 3 of the License, or - * (at your option) any later version. - * - * Gluster Management Console is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY - * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License - * for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see - * <http://www.gnu.org/licenses/>. - */ -package com.gluster.storage.management.core.response; - -import javax.xml.bind.annotation.XmlRootElement; - -import com.gluster.storage.management.core.model.Status; - -@XmlRootElement(name = "response") -public class GenericResponse<T> extends AbstractResponse { - private T data; - private Status status; - - @Override - public Status getStatus() { - return status; - } - - public void setStatus(Status status) { - this.status = status; - } - - public GenericResponse(Status status, T data) { - setStatus(status); - this.data = data; - } - - public GenericResponse() { - } - - @Override - public T getData() { - return data; - } - - public void setData(T data) { - this.data = data; - } - -} diff --git a/src/com.gluster.storage.management.gateway.scripts/src/multicast-discoverd.py b/src/com.gluster.storage.management.gateway.scripts/src/multicast-discoverd.py index dea08c2d..fbadd048 100755 --- a/src/com.gluster.storage.management.gateway.scripts/src/multicast-discoverd.py +++ b/src/com.gluster.storage.management.gateway.scripts/src/multicast-discoverd.py @@ -94,10 +94,13 @@ def main(): continue time.sleep(0.2) - sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) - sock.connect((request[1][0], Globals.SERVER_PORT)) - sock.send("%s,%s,%s,%s,%s,%s\n" % (tokens[0], tokens[1], tokens[2], socket.gethostname(), socket.getfqdn(), GLUSTERD_UUID)) - sock.close() + try: + sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + sock.connect((request[1][0], Globals.SERVER_PORT)) + sock.send("%s,%s,%s,%s,%s,%s\n" % (tokens[0], tokens[1], tokens[2], socket.gethostname(), socket.getfqdn(), GLUSTERD_UUID)) + sock.close() + except socket.error, e: + Utils.log("failed to send reply to [%s:%s]: %s" % (request[1][0], Globals.SERVER_PORT, str(e))) sys.exit(0) diff --git a/src/com.gluster.storage.management.gateway/WebContent/scripts/add_user_cifs_all.py b/src/com.gluster.storage.management.gateway/WebContent/scripts/add_user_cifs_all.py index fd952cff..02855cd7 100755 --- a/src/com.gluster.storage.management.gateway/WebContent/scripts/add_user_cifs_all.py +++ b/src/com.gluster.storage.management.gateway/WebContent/scripts/add_user_cifs_all.py @@ -15,7 +15,7 @@ import Utils defaultUid = 1024000 -cifsUserFile = "/etc/glustermg/.users.cifs" +cifsUserFile = "/opt/glustermg/etc/users.cifs" def getLastUid(): diff --git a/src/com.gluster.storage.management.gateway/WebContent/scripts/create_volume_cifs_all.py b/src/com.gluster.storage.management.gateway/WebContent/scripts/create_volume_cifs_all.py new file mode 100755 index 00000000..80b6da7c --- /dev/null +++ b/src/com.gluster.storage.management.gateway/WebContent/scripts/create_volume_cifs_all.py @@ -0,0 +1,59 @@ +#!/usr/bin/python +# Copyright (C) 2011 Gluster, Inc. <http://www.gluster.com> +# This file is part of Gluster Management Gateway. +# + +import os +import sys +p1 = os.path.abspath(os.path.dirname(sys.argv[0])) +p2 = "%s/common" % os.path.dirname(p1) +if not p1 in sys.path: + sys.path.append(p1) +if not p2 in sys.path: + sys.path.append(p2) +import Utils + + +cifsVolumeFile = "/opt/glustermg/etc/volumes.cifs" + + +def addVolumeCifsConf(volumeName, userList): + try: + fp = open(cifsVolumeFile) + content = fp.read() + fp.close() + except IOError, e: + Utils.log("failed to read file %s: %s" % (cifsVolumeFile, str(e))) + content = "" + + try: + fp = open(cifsVolumeFile, "w") + for line in content.split(): + if line.split(":")[0] != volumeName: + fp.write("%s\n" % line) + fp.write("%s:%s\n" % (volumeName, ":".join(userList))) + fp.close() + except IOError, e: + Utils.log("failed to write file %s: %s" % (cifsVolumeFile, str(e))) + return False + return True + + +def main(): + if len(sys.argv) < 4: + sys.stderr.write("usage: %s SERVER_FILE VOLUME_NAME USER1 USER2 ...\n" % os.path.basename(sys.argv[0])) + sys.exit(-1) + + serverFile = sys.argv[1] + volumeName = sys.argv[2] + userList = sys.argv[3:] + + rv = Utils.runCommand(["grun.py", serverFile, "create_volume_cifs.py", volumeName] + userList) + if rv == 0: + if not addVolumeCifsConf(volumeName, userList): + sys.exit(11) + sys.exit(rv) + + +if __name__ == "__main__": + main() diff --git a/src/com.gluster.storage.management.gateway/WebContent/scripts/delete_user_cifs_all.py b/src/com.gluster.storage.management.gateway/WebContent/scripts/delete_user_cifs_all.py index ad8ce46c..d414fc2c 100755 --- a/src/com.gluster.storage.management.gateway/WebContent/scripts/delete_user_cifs_all.py +++ b/src/com.gluster.storage.management.gateway/WebContent/scripts/delete_user_cifs_all.py @@ -14,7 +14,7 @@ if not p2 in sys.path: import Utils -cifsUserFile = "/etc/glustermg/.users.cifs" +cifsUserFile = "/opt/glustermg/etc/users.cifs" def removeUser(userName): diff --git a/src/com.gluster.storage.management.gateway/WebContent/scripts/delete_volume_cifs_all.py b/src/com.gluster.storage.management.gateway/WebContent/scripts/delete_volume_cifs_all.py new file mode 100755 index 00000000..b41e3d65 --- /dev/null +++ b/src/com.gluster.storage.management.gateway/WebContent/scripts/delete_volume_cifs_all.py @@ -0,0 +1,57 @@ +#!/usr/bin/python +# Copyright (C) 2011 Gluster, Inc. <http://www.gluster.com> +# This file is part of Gluster Management Gateway. +# + +import os +import sys +p1 = os.path.abspath(os.path.dirname(sys.argv[0])) +p2 = "%s/common" % os.path.dirname(p1) +if not p1 in sys.path: + sys.path.append(p1) +if not p2 in sys.path: + sys.path.append(p2) +import Utils + + +cifsVolumeFile = "/opt/glustermg/etc/volumes.cifs" + + +def removeVolumeCifsConf(volumeName): + try: + fp = open(cifsVolumeFile) + content = fp.read() + fp.close() + except IOError, e: + Utils.log("failed to read file %s: %s" % (cifsVolumeFile, str(e))) + content = "" + + try: + fp = open(cifsVolumeFile, "w") + for line in content.split(): + if line.split(":")[0] != volumeName: + fp.write("%s\n" % line) + fp.close() + except IOError, e: + Utils.log("failed to write file %s: %s" % (cifsVolumeFile, str(e))) + return False + return True + + +def main(): + if len(sys.argv) < 3: + sys.stderr.write("usage: %s SERVER_FILE VOLUME_NAME\n" % os.path.basename(sys.argv[0])) + sys.exit(-1) + + serverFile = sys.argv[1] + volumeName = sys.argv[2] + + rv = Utils.runCommand(["grun.py", serverFile, "delete_volume_cifs.py", volumeName]) + if rv == 0: + if not removeVolumeCifsConf(volumeName): + sys.exit(11) + sys.exit(rv) + + +if __name__ == "__main__": + main() diff --git a/src/com.gluster.storage.management.gateway/WebContent/scripts/get_volume_user_cifs.py b/src/com.gluster.storage.management.gateway/WebContent/scripts/get_volume_user_cifs.py new file mode 100755 index 00000000..0593257b --- /dev/null +++ b/src/com.gluster.storage.management.gateway/WebContent/scripts/get_volume_user_cifs.py @@ -0,0 +1,43 @@ +#!/usr/bin/python +# Copyright (C) 2011 Gluster, Inc. <http://www.gluster.com> +# This file is part of Gluster Management Gateway. +# + +import os +import sys +p1 = os.path.abspath(os.path.dirname(sys.argv[0])) +p2 = "%s/common" % os.path.dirname(p1) +if not p1 in sys.path: + sys.path.append(p1) +if not p2 in sys.path: + sys.path.append(p2) +import Utils + + +cifsVolumeFile = "/opt/glustermg/etc/volumes.cifs" + + +def main(): + if len(sys.argv) < 2: + sys.stderr.write("usage: %s VOLUME_NAME\n" % os.path.basename(sys.argv[0])) + sys.exit(-1) + + volumeName = sys.argv[2] + + try: + fp = open(cifsVolumeFile) + content = fp.read() + fp.close() + for line in content.split(): + tokens = line.split(":") + if tokens[0] == volumeName: + print "\n".join(tokens[1:]) + sys.exit(0) + sys.exit(1) + except IOError, e: + Utils.log("failed to read file %s: %s" % (cifsVolumeFile, str(e))) + sys.exit(2) + + +if __name__ == "__main__": + main() diff --git a/src/com.gluster.storage.management.gateway/WebContent/scripts/grun.py b/src/com.gluster.storage.management.gateway/WebContent/scripts/grun.py index 459265ce..f5ef1568 100755 --- a/src/com.gluster.storage.management.gateway/WebContent/scripts/grun.py +++ b/src/com.gluster.storage.management.gateway/WebContent/scripts/grun.py @@ -35,11 +35,17 @@ def main(): Utils.log("Failed to read server file %s: %s\n" % (serverFile, str(e))) sys.exit(1) + status = True for serverName in serverNameList: rv = Utils.runCommand(sshCommandPrefix + [serverName.strip()] + command) - print rv - - sys.exit(0) + print "%s: %s" % (serverName, rv) + if rv != 0: + status = False + + if status: + sys.exit(0) + else: + sys.exit(2) if __name__ == "__main__": diff --git a/src/com.gluster.storage.management.gateway/src/com/gluster/storage/management/gateway/constants/VolumeOptionsDefaults.java b/src/com.gluster.storage.management.gateway/src/com/gluster/storage/management/gateway/constants/VolumeOptionsDefaults.java index bfc5106d..bdc9ce58 100644 --- a/src/com.gluster.storage.management.gateway/src/com/gluster/storage/management/gateway/constants/VolumeOptionsDefaults.java +++ b/src/com.gluster.storage.management.gateway/src/com/gluster/storage/management/gateway/constants/VolumeOptionsDefaults.java @@ -140,18 +140,18 @@ public class VolumeOptionsDefaults { GlusterServer onlineServer = clusterService.getOnlineServer(clusterName); try { - options = runDefaultVolumeOptions(onlineServer.getName(), command); + options = getVolumeOptionsInfo(onlineServer.getName(), command); return options.getOptions(); } catch (ConnectionException e) { onlineServer = clusterService.getNewOnlineServer(clusterName); - options = runDefaultVolumeOptions(onlineServer.getName(), command); + options = getVolumeOptionsInfo(onlineServer.getName(), command); return options.getOptions(); } catch (Exception e) { throw new GlusterRuntimeException("Fetching volume options default failed! [" + e.getMessage() + "]"); } } - private VolumeOptionInfoListResponse runDefaultVolumeOptions(String serverName, String command) { + private VolumeOptionInfoListResponse getVolumeOptionsInfo(String serverName, String command) { return (VolumeOptionInfoListResponse) serverUtil.executeOnServer(true, serverName, command, VolumeOptionInfoListResponse.class); } diff --git a/src/com.gluster.storage.management.gateway/src/com/gluster/storage/management/gateway/resources/v1_0/VolumesResource.java b/src/com.gluster.storage.management.gateway/src/com/gluster/storage/management/gateway/resources/v1_0/VolumesResource.java index 5762c89f..82644c4d 100644 --- a/src/com.gluster.storage.management.gateway/src/com/gluster/storage/management/gateway/resources/v1_0/VolumesResource.java +++ b/src/com.gluster.storage.management.gateway/src/com/gluster/storage/management/gateway/resources/v1_0/VolumesResource.java @@ -678,12 +678,13 @@ public class VolumesResource extends AbstractResource { String serverName = brickInfo[0]; String brickDirectory = brickInfo[1]; - // String mountPoint = brickDirectory.substring(0, - // brickDirectory.lastIndexOf("/")); - Object output = serverUtil.executeScriptOnServer(true, serverName, VOLUME_DIRECTORY_CLEANUP_SCRIPT + " " + try { + String output = serverUtil.executeScriptOnServer(true, serverName, VOLUME_DIRECTORY_CLEANUP_SCRIPT + " " + brickDirectory + " " + (deleteFlag ? "-d" : ""), String.class); - if (output instanceof Status) { - errors += "[" + brickDirectory + "] => " + output + CoreConstants.NEWLINE; + } catch(Exception e) { + logger.error("Error while cleaning brick [" + serverName + ":" + brickDirectory + "] of volume [" + + volumeName + "] : " + e.getMessage(), e); + errors += "[" + brickDirectory + "] => " + e.getMessage() + CoreConstants.NEWLINE; } } if(!errors.trim().isEmpty()) { @@ -696,13 +697,9 @@ public class VolumesResource extends AbstractResource { String brickDirectory = brick.getBrickDirectory(); // String mountPoint = brickDirectory.substring(0, brickDirectory.lastIndexOf("/")); - Object output = serverUtil.executeScriptOnServer(true, brick.getServerName(), + serverUtil.executeScriptOnServer(true, brick.getServerName(), VOLUME_DIRECTORY_CLEANUP_SCRIPT + " " + brickDirectory + " " + (deleteFlag ? "-d" : ""), String.class); - if (output instanceof Status) { - throw new GlusterRuntimeException("Error in post-delete operation of volume [" + volumeName + "]: " - + output); - } } } @@ -825,22 +822,15 @@ public class VolumesResource extends AbstractResource { String logFilePath = logDir + CoreConstants.FILE_SEPARATOR + logFileName; // Usage: get_volume_disk_log.py <volumeName> <diskName> <lineCount> - Object responseObj = serverUtil.executeScriptOnServer(true, brick.getServerName(), VOLUME_BRICK_LOG_SCRIPT + LogMessageListResponse response = serverUtil.executeScriptOnServer(true, brick.getServerName(), VOLUME_BRICK_LOG_SCRIPT + " " + logFilePath + " " + lineCount, LogMessageListResponse.class); - LogMessageListResponse response = null; - if (responseObj instanceof LogMessageListResponse) { - response = (LogMessageListResponse) responseObj; - // populate disk and trim other fields - List<VolumeLogMessage> logMessages = response.getLogMessages(); - for (VolumeLogMessage logMessage : logMessages) { - logMessage.setBrickDirectory(brick.getBrickDirectory()); - } - return logMessages; - } else { - Status status = (Status) responseObj; - throw new GlusterRuntimeException(status.toString()); + // populate disk and trim other fields + List<VolumeLogMessage> logMessages = response.getLogMessages(); + for (VolumeLogMessage logMessage : logMessages) { + logMessage.setBrickDirectory(brick.getBrickDirectory()); } + return logMessages; } @GET diff --git a/src/com.gluster.storage.management.gateway/src/com/gluster/storage/management/gateway/tasks/InitializeDiskTask.java b/src/com.gluster.storage.management.gateway/src/com/gluster/storage/management/gateway/tasks/InitializeDiskTask.java index ba1cba21..e15ed62e 100644 --- a/src/com.gluster.storage.management.gateway/src/com/gluster/storage/management/gateway/tasks/InitializeDiskTask.java +++ b/src/com.gluster.storage.management.gateway/src/com/gluster/storage/management/gateway/tasks/InitializeDiskTask.java @@ -120,13 +120,9 @@ public class InitializeDiskTask extends Task { private void startInitializeDisk(String serverName) { String fsTypeCommand = (getFsType().equals(GlusterConstants.FSTYPE_DEFAULT)) ? "" : " -t " + getFsType(); - Object output = serverUtil.executeScriptOnServer(true, serverName, INITIALIZE_DISK_SCRIPT + String output = serverUtil.executeScriptOnServer(true, serverName, INITIALIZE_DISK_SCRIPT + fsTypeCommand + " " + getDiskName(), String.class); - if(output instanceof Status) { - // Status object will be returned only in case of failure - throw new GlusterRuntimeException(((Status)output).toString()); - } - TaskStatus taskStatus = new TaskStatus(new Status(Status.STATUS_CODE_RUNNING, (String)output)); + TaskStatus taskStatus = new TaskStatus(new Status(Status.STATUS_CODE_RUNNING, output)); taskStatus.setPercentageSupported((getFsType().equals(GlusterConstants.FSTYPE_XFS)) ? false : true); getTaskInfo().setStatus(taskStatus); } diff --git a/src/com.gluster.storage.management.gateway/src/com/gluster/storage/management/gateway/tasks/RebalanceVolumeTask.java b/src/com.gluster.storage.management.gateway/src/com/gluster/storage/management/gateway/tasks/RebalanceVolumeTask.java index 134359fc..1be71c0d 100644 --- a/src/com.gluster.storage.management.gateway/src/com/gluster/storage/management/gateway/tasks/RebalanceVolumeTask.java +++ b/src/com.gluster.storage.management.gateway/src/com/gluster/storage/management/gateway/tasks/RebalanceVolumeTask.java @@ -75,12 +75,8 @@ public class RebalanceVolumeTask extends Task { private void startRebalance(String serverName) { String command = "gluster volume rebalance " + getTaskInfo().getReference() + " " + getLayout() + " start"; - Object output = serverUtil.executeOnServer(true, serverName, command, String.class); - if(output instanceof Status) { - // Status object will be returned only in case of failure - throw new GlusterRuntimeException(((Status)output).toString()); - } - getTaskInfo().setStatus(new TaskStatus(new Status(Status.STATUS_CODE_RUNNING, (String)output))); + String output = serverUtil.executeOnServer(true, serverName, command, String.class); + getTaskInfo().setStatus(new TaskStatus(new Status(Status.STATUS_CODE_RUNNING, output))); } @Override diff --git a/src/com.gluster.storage.management.gateway/src/com/gluster/storage/management/gateway/utils/AbstractStatsFactory.java b/src/com.gluster.storage.management.gateway/src/com/gluster/storage/management/gateway/utils/AbstractStatsFactory.java index 13a58729..4953785c 100644 --- a/src/com.gluster.storage.management.gateway/src/com/gluster/storage/management/gateway/utils/AbstractStatsFactory.java +++ b/src/com.gluster.storage.management.gateway/src/com/gluster/storage/management/gateway/utils/AbstractStatsFactory.java @@ -155,14 +155,8 @@ public abstract class AbstractStatsFactory implements StatsFactory { argsStr += " " + arg; } } - Object output = serverUtil.executeScriptOnServer(true, serverName, getStatsScriptName() + argsStr + " " + return serverUtil.executeScriptOnServer(true, serverName, getStatsScriptName() + argsStr + " " + period, ServerStats.class); - //String cpuUsageData = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?> <xport> <meta> <start>1310468100</start> <step>300</step> <end>1310471700</end> <rows>13</rows> <columns>3</columns> <legend> <entry>user</entry> <entry>system</entry> <entry>total</entry> </legend> </meta> <data> <row><t>1310468100</t><v>2.23802952e-1</v><v>4.3747778209e-01</v><v>6.6128073384e-01</v></row> <row><t>1310468400</t><v>2.3387347338e-01</v><v>4.4642717442e-01</v><v>6.8030064780e-01</v></row> <row><t>1310468700</t><v>5.5043873220e+00</v><v>6.2462376636e+00</v><v>1.1750624986e+01</v></row> <row><t>1310469000</t><v>2.4350593653e+01</v><v>2.6214585217e+01</v><v>5.0565178869e+01</v></row> <row><t>1310469300</t><v>4.0786489953e+01</v><v>4.6784713828e+01</v><v>8.7571203781e+01</v></row> <row><t>1310469600</t><v>4.1459955508e+01</v><v>5.2546309044e+01</v><v>9.4006264551e+01</v></row> <row><t>1310469900</t><v>4.2312286165e+01</v><v>5.2390588332e+01</v><v>9.4702874497e+01</v></row> <row><t>1310470200</t><v>4.2603794982e+01</v><v>5.1598861493e+01</v><v>9.4202656475e+01</v></row> <row><t>1310470500</t><v>3.8238751290e+01</v><v>4.5312089966e+01</v><v>8.3550841256e+01</v></row> <row><t>1310470800</t><v>1.7949961224e+01</v><v>2.1282058418e+01</v><v>3.9232019642e+01</v></row> <row><t>1310471100</t><v>1.2330371421e-01</v><v>4.6347832868e-01</v><v>5.8678204289e-01</v></row> <row><t>1310471400</t><v>1.6313260492e-01</v><v>5.4088119561e-01</v><v>7.0401380052e-01</v></row> <row><t>1310471700</t><v>NaN</v><v>NaN</v><v>NaN</v></row> </data> </xport>"; - //Object output = unmarshal(ServerStats.class, cpuUsageData, false); - if(output instanceof Status) { - throw new GlusterRuntimeException(((Status)output).toString()); - } - return (ServerStats) output; } public abstract String getStatsScriptName(); diff --git a/src/com.gluster.storage.management.gateway/src/com/gluster/storage/management/gateway/utils/GlusterUtil.java b/src/com.gluster.storage.management.gateway/src/com/gluster/storage/management/gateway/utils/GlusterUtil.java index 62ea6839..598dae13 100644 --- a/src/com.gluster.storage.management.gateway/src/com/gluster/storage/management/gateway/utils/GlusterUtil.java +++ b/src/com.gluster.storage.management.gateway/src/com/gluster/storage/management/gateway/utils/GlusterUtil.java @@ -411,24 +411,15 @@ public class GlusterUtil { } private void addBrickToVolume(Volume volume, String serverName, String brickDir, BRICK_STATUS status) { - //If brick directory has standard path, find and assign device name otherwise null -// String stdBrickDirPattern = "^/export/.*/.*"; // e.g: /export/sdb/test - String deviceName = null; -// if (Pattern.matches(stdBrickDirPattern, brickDir) ) { -// deviceName = brickDir.split("/")[2].trim(); -// } volume.addBrick(new Brick(serverName, status, brickDir)); } // Do not throw exception, Gracefully handle as Offline brick. private BRICK_STATUS getBrickStatus(String serverName, String volumeName, String brick){ try { - Object output = serverUtil.executeScriptOnServer(true, serverName, BRICK_STATUS_SCRIPT + " " + volumeName + " " + brick, String.class); - if(output instanceof Status) { - // script failed. throw exception. - throw new GlusterRuntimeException(((Status)output).toString()); - } - if (((String)output).equals(CoreConstants.ONLINE)) { + String output = serverUtil.executeScriptOnServer(true, serverName, BRICK_STATUS_SCRIPT + " " + volumeName + + " " + brick, String.class); + if (output.equals(CoreConstants.ONLINE)) { return BRICK_STATUS.ONLINE; } else { return BRICK_STATUS.OFFLINE; @@ -638,18 +629,18 @@ public class GlusterUtil { } public TaskStatus getInitializingDeviceStatus(String serverName, String diskName) { - Object response = serverUtil.executeScriptOnServer(true, serverName, INITIALIZE_DISK_STATUS_SCRIPT + " " - + diskName, InitDiskStatusResponse.class); - + InitDiskStatusResponse initDiskStatusResponse; TaskStatus taskStatus = new TaskStatus(); - if (response instanceof Status) { + + try { + initDiskStatusResponse = serverUtil.executeScriptOnServer(true, serverName, INITIALIZE_DISK_STATUS_SCRIPT + " " + + diskName, InitDiskStatusResponse.class); + } catch(RuntimeException e) { taskStatus.setCode(Status.STATUS_CODE_FAILURE); - taskStatus.setMessage(((Status) response).getMessage()); - throw new GlusterRuntimeException(((Status) response).getMessage()); + taskStatus.setMessage(e.getMessage()); + throw e; } - InitDiskStatusResponse initDiskStatusResponse = (InitDiskStatusResponse) response; - if (initDiskStatusResponse.getFormatStatus() == FORMAT_STATUS.COMPLETED) { taskStatus.setCode(Status.STATUS_CODE_SUCCESS); } else if (initDiskStatusResponse.getFormatStatus() == FORMAT_STATUS.IN_PROGRESS) { diff --git a/src/com.gluster.storage.management.gateway/src/com/gluster/storage/management/gateway/utils/ServerUtil.java b/src/com.gluster.storage.management.gateway/src/com/gluster/storage/management/gateway/utils/ServerUtil.java index 84514893..73292197 100644 --- a/src/com.gluster.storage.management.gateway/src/com/gluster/storage/management/gateway/utils/ServerUtil.java +++ b/src/com.gluster.storage.management.gateway/src/com/gluster/storage/management/gateway/utils/ServerUtil.java @@ -105,13 +105,9 @@ public class ServerUtil { return ((Server) response).getName(); } - private Object fetchServerDetails(String serverName) { + private Server fetchServerDetails(String serverName) { // fetch standard server details like cpu, disk, memory details - Object response = executeScriptOnServer(true, serverName, REMOTE_SCRIPT_GET_SERVER_DETAILS, Server.class); - if (response instanceof Status) { - throw new GlusterRuntimeException(((Status) response).getMessage()); - } - return response; + return executeScriptOnServer(true, serverName, REMOTE_SCRIPT_GET_SERVER_DETAILS, Server.class); } /** @@ -128,9 +124,10 @@ public class ServerUtil { * @return Object of the expected class from remote execution of the command. In case the remote execution fails * ungracefully, an object of class {@link Status} will be returned. */ - public Object executeScriptOnServer(boolean runInForeground, String serverName, String scriptWithArgs, - @SuppressWarnings("rawtypes") Class expectedClass) { - return executeOnServer(runInForeground, serverName, getRemoteScriptDir() + File.separator + scriptWithArgs, expectedClass); + public <T> T executeScriptOnServer(boolean runInForeground, String serverName, String scriptWithArgs, + Class<T> expectedClass) { + return executeOnServer(runInForeground, serverName, getRemoteScriptDir() + File.separator + scriptWithArgs, + expectedClass); } /** @@ -144,33 +141,15 @@ public class ServerUtil { * @return Object of the expected class from remote execution of the command. In case the remote execution fails * ungracefully, an object of class {@link Status} will be returned. */ - @SuppressWarnings("rawtypes") - public Object executeOnServer(boolean runInForeground, String serverName, String commandWithArgs, - Class expectedClass) { - try { - String output = executeOnServer(serverName, commandWithArgs); - if(expectedClass == String.class) { - return output; - } - - // In case the script execution exits ungracefully, the agent would return a GenericResponse. - // hence pass last argument as true to try GenericResponse unmarshalling in such cases. - Object response = unmarshal(expectedClass, output, expectedClass != GenericResponse.class); - if (expectedClass != GenericResponse.class && response instanceof GenericResponse) { - // expected class was not GenericResponse, but that's what we got. This means the - // script failed ungracefully. Extract and return the status object from the response - return ((GenericResponse) response).getStatus(); - } - return response; - } catch (RuntimeException e) { - // Except for connection exception, wrap any other exception in the a object and return it. - if (e instanceof ConnectionException) { - throw e; - } else { - // error during unmarshalling. return status with error from exception. - return new Status(e); - } + @SuppressWarnings("unchecked") + public <T> T executeOnServer(boolean runInForeground, String serverName, String commandWithArgs, + Class<T> expectedClass) { + String output = executeOnServer(serverName, commandWithArgs); + if (expectedClass == String.class) { + return (T) output; } + + return unmarshal(expectedClass, output); } private String executeOnServer(String serverName, String commandWithArgs) { @@ -231,27 +210,20 @@ public class ServerUtil { * Class whose object is expected * @param input * Input string - * @param tryGenericResponseOnFailure - * If true, and if the unmarshalling fails for given class, another unmarshalling will be attempted with - * class {@link GenericResponse}. If this also fails, a status object with exception message is created - * and returned. - * @return Object of given expected class, or a status object in case first unmarshalling fails. + * @return Object of given expected class */ - @SuppressWarnings("rawtypes") - public Object unmarshal(Class expectedClass, String input, boolean tryGenericResponseOnFailure) { + @SuppressWarnings("unchecked") + public <T> T unmarshal(Class<T> expectedClass, String input) { try { // create JAXB context and instantiate marshaller JAXBContext context = JAXBContext.newInstance(expectedClass); Unmarshaller um = context.createUnmarshaller(); - return um.unmarshal(new ByteArrayInputStream(input.getBytes())); + return (T)um.unmarshal(new ByteArrayInputStream(input.getBytes())); } catch (JAXBException e) { - if (tryGenericResponseOnFailure) { - // unmarshalling failed. try to unmarshal a GenericResponse object - return unmarshal(GenericResponse.class, input, false); - - } - return new Status(Status.STATUS_CODE_FAILURE, "Error during unmarshalling string [" + input - + "] for class [" + expectedClass.getName() + ": [" + e.getMessage() + "]"); + String errMsg = "Error during unmarshalling string [" + input + "] for class [" + expectedClass.getName() + + ": [" + e.getMessage() + "]"; + logger.error(errMsg, e); + throw new GlusterRuntimeException(errMsg, e); } } @@ -263,7 +235,7 @@ public class ServerUtil { * @return Status object containing the disk name, or error message in case the remote script fails. */ public Status getDiskForDir(String serverName, String brickDir) { - return (Status) executeScriptOnServer(true, serverName, REMOTE_SCRIPT_GET_DISK_FOR_DIR + " " + brickDir, Status.class); + return executeScriptOnServer(true, serverName, REMOTE_SCRIPT_GET_DISK_FOR_DIR + " " + brickDir, Status.class); } public static void main(String[] args) { @@ -282,7 +254,6 @@ public class ServerUtil { // System.out.println(row.getUsageData().get(2)); // } // } catch (JAXBException e) { -// // TODO Auto-generated catch block // e.printStackTrace(); // } } |
