Search in sources :

Example 81 with InternalErrorException

use of com.cloud.exception.InternalErrorException in project cloudstack by apache.

the class ExtractIsoCmd method execute.

@Override
public void execute() {
    try {
        CallContext.current().setEventDetails(getEventDescription());
        String uploadUrl = _templateService.extract(this);
        if (uploadUrl != null) {
            ExtractResponse response = _responseGenerator.createExtractResponse(id, zoneId, getEntityOwnerId(), mode, uploadUrl);
            response.setResponseName(getCommandName());
            response.setObjectName("iso");
            this.setResponseObject(response);
        } else {
            throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to extract ISO");
        }
    } catch (InternalErrorException ex) {
        s_logger.warn("Exception: ", ex);
        throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, ex.getMessage());
    }
}
Also used : ExtractResponse(org.apache.cloudstack.api.response.ExtractResponse) ServerApiException(org.apache.cloudstack.api.ServerApiException) InternalErrorException(com.cloud.exception.InternalErrorException)

Example 82 with InternalErrorException

use of com.cloud.exception.InternalErrorException in project cloudstack by apache.

the class OVAProcessorTest method testGetVirtualSizeWhenVirtualSizeThrowsException.

@Test
public void testGetVirtualSizeWhenVirtualSizeThrowsException() throws Exception {
    long virtualSize = 2000;
    long actualSize = 1000;
    String templatePath = "/tmp";
    String templateName = "template";
    File mockFile = Mockito.mock(File.class);
    Mockito.when(mockFile.length()).thenReturn(actualSize);
    Mockito.when(mockFile.getParent()).thenReturn(templatePath);
    Mockito.when(mockFile.getName()).thenReturn(templateName);
    Mockito.doThrow(new InternalErrorException("virtual size calculation failed")).when(processor).getTemplateVirtualSize(templatePath, templateName);
    Assert.assertEquals(actualSize, processor.getVirtualSize(mockFile));
    Mockito.verify(mockFile, Mockito.times(1)).length();
}
Also used : InternalErrorException(com.cloud.exception.InternalErrorException) File(java.io.File) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 83 with InternalErrorException

use of com.cloud.exception.InternalErrorException in project cloudstack by apache.

the class QCOW2Processor method process.

@Override
public FormatInfo process(String templatePath, ImageFormat format, String templateName) throws InternalErrorException {
    if (format != null) {
        s_logger.debug("We currently don't handle conversion from " + format + " to QCOW2.");
        return null;
    }
    String qcow2Path = templatePath + File.separator + templateName + "." + ImageFormat.QCOW2.getFileExtension();
    if (!_storage.exists(qcow2Path)) {
        s_logger.debug("Unable to find the qcow2 file: " + qcow2Path);
        return null;
    }
    FormatInfo info = new FormatInfo();
    info.format = ImageFormat.QCOW2;
    info.filename = templateName + "." + ImageFormat.QCOW2.getFileExtension();
    File qcow2File = _storage.getFile(qcow2Path);
    info.size = _storage.getSize(qcow2Path);
    try {
        info.virtualSize = getTemplateVirtualSize(qcow2File);
    } catch (IOException e) {
        s_logger.error("Unable to get virtual size from " + qcow2File.getName());
        throw new InternalErrorException("unable to get virtual size from qcow2 file");
    }
    return info;
}
Also used : IOException(java.io.IOException) InternalErrorException(com.cloud.exception.InternalErrorException) File(java.io.File)

Example 84 with InternalErrorException

use of com.cloud.exception.InternalErrorException in project cloudstack by apache.

the class VhdProcessor method process.

@Override
public FormatInfo process(String templatePath, ImageFormat format, String templateName) throws InternalErrorException {
    if (format != null) {
        s_logger.debug("We currently don't handle conversion from " + format + " to VHD.");
        return null;
    }
    String vhdPath = templatePath + File.separator + templateName + "." + ImageFormat.VHD.getFileExtension();
    if (!_storage.exists(vhdPath)) {
        s_logger.debug("Unable to find the vhd file: " + vhdPath);
        return null;
    }
    File vhdFile = _storage.getFile(vhdPath);
    FormatInfo info = new FormatInfo();
    info.format = ImageFormat.VHD;
    info.filename = templateName + "." + ImageFormat.VHD.getFileExtension();
    info.size = _storage.getSize(vhdPath);
    try {
        info.virtualSize = getTemplateVirtualSize(vhdFile);
    } catch (IOException e) {
        s_logger.error("Unable to get the virtual size for " + vhdPath);
        throw new InternalErrorException("unable to get virtual size from vhd file");
    }
    return info;
}
Also used : IOException(java.io.IOException) InternalErrorException(com.cloud.exception.InternalErrorException) File(java.io.File)

Example 85 with InternalErrorException

use of com.cloud.exception.InternalErrorException in project cloudstack by apache.

the class VmdkProcessor method getTemplateVirtualSize.

public long getTemplateVirtualSize(String templatePath, String templateName) throws InternalErrorException {
    long virtualSize = 0;
    String templateFileFullPath = templatePath.endsWith(File.separator) ? templatePath : templatePath + File.separator;
    templateFileFullPath += templateName.endsWith(ImageFormat.VMDK.getFileExtension()) ? templateName : templateName + "." + ImageFormat.VMDK.getFileExtension();
    try (FileReader fileReader = new FileReader(templateFileFullPath);
        BufferedReader bufferedReader = new BufferedReader(fileReader)) {
        Pattern regex = Pattern.compile("(RW|RDONLY|NOACCESS) (\\d+) (FLAT|SPARSE|ZERO|VMFS|VMFSSPARSE|VMFSDRM|VMFSRAW)");
        String line = null;
        while ((line = bufferedReader.readLine()) != null) {
            Matcher m = regex.matcher(line);
            if (m.find()) {
                long sectors = Long.parseLong(m.group(2));
                virtualSize = sectors * 512;
                break;
            }
        }
    } catch (FileNotFoundException ex) {
        String msg = "Unable to open file '" + templateFileFullPath + "' " + ex.toString();
        s_logger.error(msg);
        throw new InternalErrorException(msg);
    } catch (IOException ex) {
        String msg = "Unable read open file '" + templateFileFullPath + "' " + ex.toString();
        s_logger.error(msg);
        throw new InternalErrorException(msg);
    }
    s_logger.debug("vmdk file had size=" + virtualSize);
    return virtualSize;
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) BufferedReader(java.io.BufferedReader) FileNotFoundException(java.io.FileNotFoundException) FileReader(java.io.FileReader) InternalErrorException(com.cloud.exception.InternalErrorException) IOException(java.io.IOException)

Aggregations

InternalErrorException (com.cloud.exception.InternalErrorException)86 IOException (java.io.IOException)28 LibvirtException (org.libvirt.LibvirtException)27 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)25 Connect (org.libvirt.Connect)23 URISyntaxException (java.net.URISyntaxException)21 ConfigurationException (javax.naming.ConfigurationException)21 Answer (com.cloud.agent.api.Answer)17 AttachAnswer (org.apache.cloudstack.storage.command.AttachAnswer)16 NicTO (com.cloud.agent.api.to.NicTO)15 URI (java.net.URI)15 KVMStoragePoolManager (com.cloud.hypervisor.kvm.storage.KVMStoragePoolManager)14 Script (com.cloud.utils.script.Script)14 Test (org.junit.Test)14 CheckRouterAnswer (com.cloud.agent.api.CheckRouterAnswer)12 LibvirtRequestWrapper (com.cloud.hypervisor.kvm.resource.wrapper.LibvirtRequestWrapper)12 LibvirtUtilitiesHelper (com.cloud.hypervisor.kvm.resource.wrapper.LibvirtUtilitiesHelper)12 PrimaryDataStoreTO (org.apache.cloudstack.storage.to.PrimaryDataStoreTO)12 Processor (com.cloud.storage.template.Processor)11 TemplateLocation (com.cloud.storage.template.TemplateLocation)11