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());
}
}
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();
}
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;
}
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;
}
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;
}
Aggregations