From c0db2695f0979b400ebea561bccd72a8cd9aee75 Mon Sep 17 00:00:00 2001 From: Shireesh Anjal Date: Mon, 9 May 2011 15:20:04 +0530 Subject: Story #42 - Volume logs download --- .../storage/management/client/AbstractClient.java | 33 ++++++---- .../storage/management/client/VolumesClient.java | 6 +- .../storage/management/core/utils/FileUtil.java | 18 ++++-- src/com.gluster.storage.management.gui/plugin.xml | 16 +++++ .../gui/actions/DownloadVolumeLogsAction.java | 70 ++++++++++++++++++++++ .../server/resources/VolumesResource.java | 20 +++---- .../management/server/utils/GlusterUtil.java | 3 + 7 files changed, 136 insertions(+), 30 deletions(-) create mode 100644 src/com.gluster.storage.management.gui/src/com/gluster/storage/management/gui/actions/DownloadVolumeLogsAction.java (limited to 'src') diff --git a/src/com.gluster.storage.management.client/src/com/gluster/storage/management/client/AbstractClient.java b/src/com.gluster.storage.management.client/src/com/gluster/storage/management/client/AbstractClient.java index b9a0ef56..a077c721 100644 --- a/src/com.gluster.storage.management.client/src/com/gluster/storage/management/client/AbstractClient.java +++ b/src/com.gluster.storage.management.client/src/com/gluster/storage/management/client/AbstractClient.java @@ -1,12 +1,15 @@ package com.gluster.storage.management.client; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; import java.net.URI; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.MultivaluedMap; import com.gluster.storage.management.client.utils.ClientUtil; -import com.gluster.storage.management.core.constants.RESTConstants; +import com.gluster.storage.management.core.exceptions.GlusterRuntimeException; import com.sun.jersey.api.client.Client; import com.sun.jersey.api.client.ClientResponse; import com.sun.jersey.api.client.WebResource; @@ -49,15 +52,25 @@ public abstract class AbstractClient { .get(responseClass); } - private Object downloadResource(WebResource res, MultivaluedMap queryParams, Class responseClass) { - return res.queryParams(queryParams).header(HTTP_HEADER_AUTH, authHeader).accept(MediaType.TEXT_XML) - .get(responseClass); - } - - protected Object downloadResource(WebResource res) { + protected void downloadResource(WebResource res, String filePath) { ClientResponse response = res.header(HTTP_HEADER_AUTH, authHeader).accept(MediaType.APPLICATION_OCTET_STREAM) .get(ClientResponse.class); - return response; + try { + if(!response.hasEntity()) { + throw new GlusterRuntimeException("No entity in response!"); + } + + InputStream inputStream = response.getEntityInputStream(); + byte[] data = new byte[inputStream.available()]; + inputStream.read(data); + inputStream.close(); + + FileOutputStream os = new FileOutputStream(filePath); + os.write(data); + os.close(); + } catch (IOException e) { + throw new GlusterRuntimeException("Error while downloading resource [" + res.getURI().getPath() + "]", e); + } } /** @@ -104,8 +117,8 @@ public abstract class AbstractClient { return fetchResource(resource.path(subResourceName), NO_PARAMS, responseClass); } - protected Object downloadSubResource(String subResourceName) { - return downloadResource(resource.path(subResourceName)); + protected void downloadSubResource(String subResourceName, String filePath) { + downloadResource(resource.path(subResourceName), filePath); } /** diff --git a/src/com.gluster.storage.management.client/src/com/gluster/storage/management/client/VolumesClient.java b/src/com.gluster.storage.management.client/src/com/gluster/storage/management/client/VolumesClient.java index f1464211..c0ce8620 100644 --- a/src/com.gluster.storage.management.client/src/com/gluster/storage/management/client/VolumesClient.java +++ b/src/com.gluster.storage.management.client/src/com/gluster/storage/management/client/VolumesClient.java @@ -141,8 +141,8 @@ public class VolumesClient extends AbstractClient { queryParams, LogMessageListResponse.class); } - public void downloadLogs(String volumeName) { - downloadSubResource((volumeName) + "/" + RESTConstants.SUBRESOURCE_LOGS + "/" + RESTConstants.SUBRESOURCE_DOWNLOAD); + public void downloadLogs(String volumeName, String filePath) { + downloadSubResource((volumeName) + "/" + RESTConstants.SUBRESOURCE_LOGS + "/" + RESTConstants.SUBRESOURCE_DOWNLOAD, filePath); } public Status removeBricks(String volumeName, List diskList, boolean deleteOption) { @@ -258,7 +258,7 @@ public class VolumesClient extends AbstractClient { // // Status status = client.addDisks("Volume3", disks); // System.out.println(status.getMessage()); - client.downloadLogs("vol1"); + client.downloadLogs("vol1", "/tmp/temp1.tar.gz"); } } } diff --git a/src/com.gluster.storage.management.core/src/com/gluster/storage/management/core/utils/FileUtil.java b/src/com.gluster.storage.management.core/src/com/gluster/storage/management/core/utils/FileUtil.java index c6394a3e..8c77fbab 100644 --- a/src/com.gluster.storage.management.core/src/com/gluster/storage/management/core/utils/FileUtil.java +++ b/src/com.gluster.storage.management.core/src/com/gluster/storage/management/core/utils/FileUtil.java @@ -20,27 +20,32 @@ package com.gluster.storage.management.core.utils; import java.io.File; import java.io.FileInputStream; +import java.io.FileNotFoundException; import java.io.FileWriter; import java.io.IOException; import java.io.InputStream; import java.util.UUID; +import com.gluster.storage.management.core.constants.CoreConstants; import com.gluster.storage.management.core.exceptions.GlusterRuntimeException; public class FileUtil { public String readFileAsString(File file) { try { - FileInputStream fileInputStream = new FileInputStream(file); - byte[] data = new byte[fileInputStream.available()]; - fileInputStream.read(data); - fileInputStream.close(); - - return new String(data); + return new String(readFileAsByteArray(file), CoreConstants.ENCODING_UTF8); } catch (Exception e) { e.printStackTrace(); throw new GlusterRuntimeException("Could not read file [" + file + "]", e); } } + + public byte[] readFileAsByteArray(File file) throws FileNotFoundException, IOException { + FileInputStream fileInputStream = new FileInputStream(file); + byte[] data = new byte[fileInputStream.available()]; + fileInputStream.read(data); + fileInputStream.close(); + return data; + } public InputStream loadResource(String resourcePath) { return this.getClass().getClassLoader().getResourceAsStream(resourcePath); @@ -50,6 +55,7 @@ public class FileUtil { try { FileWriter writer = new FileWriter(fileName); writer.write(contents); + writer.close(); } catch (Exception e) { throw new GlusterRuntimeException("Exception while trying to create text file [" + fileName + "]", e); } diff --git a/src/com.gluster.storage.management.gui/plugin.xml b/src/com.gluster.storage.management.gui/plugin.xml index b208fc24..6c6983a3 100644 --- a/src/com.gluster.storage.management.gui/plugin.xml +++ b/src/com.gluster.storage.management.gui/plugin.xml @@ -524,6 +524,22 @@ toolbarPath="Normal" tooltip="Delete Volume"> + + + * 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 + * . + *******************************************************************************/ +package com.gluster.storage.management.gui.actions; + +import java.io.File; + +import org.eclipse.jface.action.IAction; +import org.eclipse.swt.SWT; +import org.eclipse.swt.widgets.Display; +import org.eclipse.swt.widgets.FileDialog; + +import com.gluster.storage.management.client.GlusterDataModelManager; +import com.gluster.storage.management.client.VolumesClient; +import com.gluster.storage.management.core.model.Volume; + +/** + * + */ +public class DownloadVolumeLogsAction extends AbstractActionDelegate { + + /* (non-Javadoc) + * @see org.eclipse.ui.IWorkbenchWindowActionDelegate#dispose() + */ + @Override + public void dispose() { + // TODO Auto-generated method stub + + } + + /* (non-Javadoc) + * @see com.gluster.storage.management.gui.actions.AbstractActionDelegate#performAction(org.eclipse.jface.action.IAction) + */ + @Override + protected void performAction(IAction action) { + final Volume volume = (Volume)selectedEntity; + final VolumesClient client = new VolumesClient(GlusterDataModelManager.getInstance().getSecurityToken()); + Display.getDefault().asyncExec(new Runnable() { + + @Override + public void run() { + FileDialog dialog = new FileDialog(getShell(), SWT.SAVE); + dialog.setFilterNames(new String[] {"GZipped Tar"}); + dialog.setFilterExtensions(new String[] {"*.tar.gz"}); + dialog.open(); + + try { + client.downloadLogs(volume.getName(), dialog.getFilterPath() + File.separator + dialog.getFileName()); + } catch(Exception e) { + showErrorDialog("Download Volume Logs [" + volume.getName() + "]", e.getMessage()); + } + } + }); + } +} diff --git a/src/com.gluster.storage.management.server/src/com/gluster/storage/management/server/resources/VolumesResource.java b/src/com.gluster.storage.management.server/src/com/gluster/storage/management/server/resources/VolumesResource.java index fe03fffd..521c6175 100644 --- a/src/com.gluster.storage.management.server/src/com/gluster/storage/management/server/resources/VolumesResource.java +++ b/src/com.gluster.storage.management.server/src/com/gluster/storage/management/server/resources/VolumesResource.java @@ -102,7 +102,11 @@ public class VolumesResource { @InjectParam private static ServerUtil serverUtil; - private final GlusterUtil glusterUtil = new GlusterUtil(); + + @InjectParam + private static GlusterUtil glusterUtil; + + private static final FileUtil fileUtil = new FileUtil(); @InjectParam private VolumeOptionsDefaults volumeOptionsDefaults; @@ -386,13 +390,9 @@ public class VolumesResource { public void write(OutputStream output) throws IOException, WebApplicationException { Volume volume = getVolume(volumeName); try { - String archiveFileName = downloadLogs(volume); - FileInputStream inputStream = new FileInputStream(archiveFileName); - int size = inputStream.available(); - byte[] data = new byte[size]; - inputStream.read(data); - inputStream.close(); - output.write(data); + File archiveFile = new File(downloadLogs(volume)); + output.write(fileUtil.readFileAsByteArray(archiveFile)); + archiveFile.delete(); } catch (Exception e) { e.printStackTrace(); throw new GlusterRuntimeException("Exception while downloading/archiving volume log files!", e); @@ -402,8 +402,6 @@ public class VolumesResource { } private String downloadLogs(Volume volume) { - FileUtil fileUtil = new FileUtil(); - // create temporary directory File tempDir = fileUtil.createTempDir(); String tempDirPath = tempDir.getPath(); @@ -423,7 +421,7 @@ public class VolumesResource { } String gzipPath = fileUtil.getTempDirName() + CoreConstants.FILE_SEPARATOR + volume.getName() + "-logs.tar.gz"; - new ProcessUtil().executeCommand("tar", "czvf", gzipPath, tempDirPath); + new ProcessUtil().executeCommand("tar", "czvf", gzipPath, "-C", tempDir.getParent(), tempDir.getName()); // delete the temp directory fileUtil.recursiveDelete(tempDir); diff --git a/src/com.gluster.storage.management.server/src/com/gluster/storage/management/server/utils/GlusterUtil.java b/src/com.gluster.storage.management.server/src/com/gluster/storage/management/server/utils/GlusterUtil.java index 476b5a14..14117aff 100644 --- a/src/com.gluster.storage.management.server/src/com/gluster/storage/management/server/utils/GlusterUtil.java +++ b/src/com.gluster.storage.management.server/src/com/gluster/storage/management/server/utils/GlusterUtil.java @@ -25,6 +25,8 @@ import java.util.List; import java.util.Map; import java.util.Map.Entry; +import org.springframework.stereotype.Component; + import com.gluster.storage.management.core.constants.CoreConstants; import com.gluster.storage.management.core.constants.RESTConstants; import com.gluster.storage.management.core.exceptions.GlusterRuntimeException; @@ -38,6 +40,7 @@ import com.gluster.storage.management.core.model.Volume.VOLUME_TYPE; import com.gluster.storage.management.core.utils.ProcessResult; import com.gluster.storage.management.core.utils.ProcessUtil; +@Component public class GlusterUtil { private static final String glusterFSminVersion = "3.1"; -- cgit