use of com.cloud.legacymodel.exceptions.InternalErrorException in project cosmic by MissionCriticalCloud.
the class DownloadManagerImpl method postLocalDownload.
/**
* Post local download activity (install and cleanup). Executed in context of
* downloader thread
*
* @throws IOException
*/
private String postLocalDownload(final String jobId) {
final DownloadJob dnld = this.jobs.get(jobId);
final TemplateDownloader td = dnld.getTemplateDownloader();
// path with mount
final String resourcePath = dnld.getInstallPathPrefix();
// directory
// template download
final String finalResourcePath = dnld.getTmpltPath();
// path on secondary
// storage
final ResourceType resourceType = dnld.getResourceType();
final File originalTemplate = new File(td.getDownloadLocalPath());
final String checkSum = computeCheckSum(originalTemplate);
if (checkSum == null) {
s_logger.warn("Something wrong happened when try to calculate the checksum of downloaded template!");
}
dnld.setCheckSum(checkSum);
int imgSizeGigs = (int) Math.ceil(this._storage.getSize(td.getDownloadLocalPath()) * 1.0d / (1024 * 1024 * 1024));
// add one just in case
imgSizeGigs++;
final long timeout = (long) imgSizeGigs * this.installTimeoutPerGig;
Script scr = null;
final String script = resourceType == ResourceType.TEMPLATE ? this.createTmpltScr : this.createVolScr;
scr = new Script(script, timeout, s_logger);
scr.add("-s", Integer.toString(imgSizeGigs));
scr.add("-S", Long.toString(td.getMaxTemplateSizeInBytes()));
if (dnld.getDescription() != null && dnld.getDescription().length() > 1) {
scr.add("-d", dnld.getDescription());
}
// add options common to ISO and template
final String extension = dnld.getFormat().toString().toLowerCase();
String templateName = "";
if (extension.equals("iso")) {
templateName = this.jobs.get(jobId).getTmpltName().trim().replace(" ", "_");
} else {
templateName = java.util.UUID.nameUUIDFromBytes((this.jobs.get(jobId).getTmpltName() + System.currentTimeMillis()).getBytes(StringUtils.getPreferredCharset())).toString();
}
// run script to mv the temporary template file to the final template
// file
final String templateFilename = templateName + "." + extension;
dnld.setTmpltPath(finalResourcePath + "/" + templateFilename);
scr.add("-n", templateFilename);
scr.add("-t", resourcePath);
// this is the temporary
scr.add("-f", td.getDownloadLocalPath());
// template file downloaded
if (dnld.getChecksum() != null && dnld.getChecksum().length() > 1) {
scr.add("-c", dnld.getChecksum());
}
// cleanup
scr.add("-u");
final String result;
result = scr.execute();
if (result != null) {
return result;
}
// Set permissions for the downloaded template
final File downloadedTemplate = new File(resourcePath + "/" + templateFilename);
this._storage.setWorldReadableAndWriteable(downloadedTemplate);
// Set permissions for template/volume.properties
String propertiesFile = resourcePath;
if (resourceType == ResourceType.TEMPLATE) {
propertiesFile += "/template.properties";
} else {
propertiesFile += "/volume.properties";
}
final File templateProperties = new File(propertiesFile);
this._storage.setWorldReadableAndWriteable(templateProperties);
final TemplateLocation loc = new TemplateLocation(this._storage, resourcePath);
try {
loc.create(dnld.getId(), true, dnld.getTmpltName());
} catch (final IOException e) {
s_logger.warn("Something is wrong with template location " + resourcePath, e);
loc.purge();
return "Unable to download due to " + e.getMessage();
}
final Iterator<Processor> en = this._processors.values().iterator();
while (en.hasNext()) {
final Processor processor = en.next();
TemplateFormatInfo info = null;
try {
info = processor.process(resourcePath, null, templateName);
} catch (final InternalErrorException e) {
s_logger.error("Template process exception ", e);
return e.toString();
}
if (info != null) {
if (!loc.addFormat(info)) {
loc.purge();
return "Unable to install due to invalid file format";
}
dnld.setTemplatesize(info.virtualSize);
dnld.setTemplatePhysicalSize(info.size);
break;
}
}
if (!loc.save()) {
s_logger.warn("Cleaning up because we're unable to save the formats");
loc.purge();
}
return null;
}
use of com.cloud.legacymodel.exceptions.InternalErrorException in project cosmic by MissionCriticalCloud.
the class NfsSecondaryStorageResource method postUpload.
public String postUpload(final String uuid, final String filename) {
final UploadEntity uploadEntity = this.uploadEntityStateMap.get(uuid);
final int installTimeoutPerGig = 180 * 60 * 1000;
final String resourcePath = uploadEntity.getInstallPathPrefix();
// template download
final String finalResourcePath = uploadEntity.getTmpltPath();
final UploadEntity.ResourceType resourceType = uploadEntity.getResourceType();
final String fileSavedTempLocation = uploadEntity.getInstallPathPrefix() + "/" + filename;
final String uploadedFileExtension = FilenameUtils.getExtension(filename);
String userSelectedFormat = uploadEntity.getFormat().toString();
if (uploadedFileExtension.equals("zip") || uploadedFileExtension.equals("bz2") || uploadedFileExtension.equals("gz")) {
userSelectedFormat += "." + uploadedFileExtension;
}
final String formatError = ImageStoreUtil.checkTemplateFormat(fileSavedTempLocation, userSelectedFormat);
if (StringUtils.isNotBlank(formatError)) {
final String errorString = "File type mismatch between uploaded file and selected format. Selected file format: " + userSelectedFormat + ". Received: " + formatError;
s_logger.error(errorString);
return errorString;
}
int imgSizeGigs = getSizeInGB(this._storage.getSize(fileSavedTempLocation));
final int maxSize = uploadEntity.getMaxSizeInGB();
if (imgSizeGigs > maxSize) {
final String errorMessage = "Maximum file upload size exceeded. Physical file size: " + imgSizeGigs + "GB. Maximum allowed size: " + maxSize + "GB.";
s_logger.error(errorMessage);
return errorMessage;
}
// add one just in case
imgSizeGigs++;
final long timeout = (long) imgSizeGigs * installTimeoutPerGig;
final Script scr = new Script(getScriptLocation(resourceType), timeout, s_logger);
scr.add("-s", Integer.toString(imgSizeGigs));
scr.add("-S", Long.toString(UploadEntity.s_maxTemplateSize));
if (uploadEntity.getDescription() != null && uploadEntity.getDescription().length() > 1) {
scr.add("-d", uploadEntity.getDescription());
}
final String checkSum = uploadEntity.getChksum();
if (StringUtils.isNotBlank(checkSum)) {
scr.add("-c", checkSum);
}
// add options common to ISO and template
final String extension = uploadEntity.getFormat().toString().toLowerCase();
String templateName = "";
if (extension.equals("iso")) {
templateName = uploadEntity.getUuid().trim().replace(" ", "_");
} else {
try {
templateName = UUID.nameUUIDFromBytes((uploadEntity.getFilename() + System.currentTimeMillis()).getBytes("UTF-8")).toString();
} catch (final UnsupportedEncodingException e) {
templateName = uploadEntity.getUuid().trim().replace(" ", "_");
}
}
// run script to mv the temporary template file to the final template
// file
final String templateFilename = templateName + "." + extension;
uploadEntity.setTemplatePath(finalResourcePath + "/" + templateFilename);
scr.add("-n", templateFilename);
scr.add("-t", resourcePath);
// this is the temporary
scr.add("-f", fileSavedTempLocation);
// template file downloaded
if (uploadEntity.getChksum() != null && uploadEntity.getChksum().length() > 1) {
scr.add("-c", uploadEntity.getChksum());
}
// cleanup
scr.add("-u");
final String result;
result = scr.execute();
if (result != null) {
return result;
}
// Set permissions for the downloaded template
final File downloadedTemplate = new File(resourcePath + "/" + templateFilename);
this._storage.setWorldReadableAndWriteable(downloadedTemplate);
// Set permissions for template/volume.properties
String propertiesFile = resourcePath;
if (resourceType == UploadEntity.ResourceType.TEMPLATE) {
propertiesFile += "/template.properties";
} else {
propertiesFile += "/volume.properties";
}
final File templateProperties = new File(propertiesFile);
this._storage.setWorldReadableAndWriteable(templateProperties);
final TemplateLocation loc = new TemplateLocation(this._storage, resourcePath);
try {
loc.create(uploadEntity.getEntityId(), true, uploadEntity.getFilename());
} catch (final IOException e) {
s_logger.warn("Something is wrong with template location " + resourcePath, e);
loc.purge();
return "Unable to upload due to " + e.getMessage();
}
final Map<String, Processor> processors = this._dlMgr.getProcessors();
for (final Processor processor : processors.values()) {
TemplateFormatInfo info = null;
try {
info = processor.process(resourcePath, null, templateName);
} catch (final InternalErrorException e) {
s_logger.error("Template process exception ", e);
return e.toString();
}
if (info != null) {
loc.addFormat(info);
uploadEntity.setVirtualSize(info.virtualSize);
uploadEntity.setPhysicalSize(info.size);
break;
}
}
if (!loc.save()) {
s_logger.warn("Cleaning up because we're unable to save the formats");
loc.purge();
}
uploadEntity.setStatus(UploadEntity.Status.COMPLETED);
this.uploadEntityStateMap.put(uploadEntity.getUuid(), uploadEntity);
return null;
}
use of com.cloud.legacymodel.exceptions.InternalErrorException in project cosmic by MissionCriticalCloud.
the class LibvirtStartCommandWrapper method execute.
@Override
public Answer execute(final StartCommand command, final LibvirtComputingResource libvirtComputingResource) {
final VirtualMachineTO vmSpec = command.getVirtualMachine();
final String vmName = vmSpec.getName();
LibvirtVmDef vm = null;
DomainState state = DomainState.VIR_DOMAIN_SHUTOFF;
final KvmStoragePoolManager storagePoolMgr = libvirtComputingResource.getStoragePoolMgr();
final LibvirtUtilitiesHelper libvirtUtilitiesHelper = libvirtComputingResource.getLibvirtUtilitiesHelper();
Connect conn = null;
try {
vm = libvirtComputingResource.createVmFromSpec(vmSpec);
conn = libvirtUtilitiesHelper.getConnectionByType(vm.getHvsType());
final Long remainingMem = getFreeMemory(conn, libvirtComputingResource);
if (remainingMem == null) {
return new StartAnswer(command, "Failed to get free memory");
} else if (remainingMem < vmSpec.getMinRam()) {
return new StartAnswer(command, "Not enough memory on the host, remaining: " + remainingMem + ", asking: " + vmSpec.getMinRam());
}
final NicTO[] nics = vmSpec.getNics();
libvirtComputingResource.createVbd(conn, vmSpec, vmName, vm);
if (!storagePoolMgr.connectPhysicalDisksViaVmSpec(vmSpec)) {
return new StartAnswer(command, "Failed to connect physical disks to host");
}
libvirtComputingResource.createVifs(vmSpec, vm);
s_logger.debug("starting " + vmName + ": " + vm.toString());
libvirtComputingResource.startVm(conn, vmName, vm.toString());
// system vms
if (vmSpec.getType() != VirtualMachineType.User) {
// pass cmdline with config for the systemvm to configure itself
if (libvirtComputingResource.passCmdLine(vmName, vmSpec.getBootArgs())) {
s_logger.debug("Passing cmdline succeeded");
} else {
final String errorMessage = "Passing cmdline failed, aborting.";
s_logger.debug(errorMessage);
return new StartAnswer(command, errorMessage);
}
String controlIp = null;
for (final NicTO nic : nics) {
if (nic.getType() == TrafficType.Control) {
controlIp = nic.getIp();
break;
}
}
// connect to the router by using its linklocal address (that should now be configured)
s_logger.debug("Starting ssh attempts to " + controlIp);
final VirtualRoutingResource virtRouterResource = libvirtComputingResource.getVirtRouterResource();
if (!virtRouterResource.connect(controlIp, 30, 5000)) {
final String errorMessage = "Unable to login to router via linklocal address " + controlIp + " after 30 tries, aborting.";
s_logger.debug(errorMessage);
return new StartAnswer(command, errorMessage);
}
s_logger.debug("Successfully completed ssh attempts to " + controlIp);
}
state = DomainState.VIR_DOMAIN_RUNNING;
return new StartAnswer(command);
} catch (final LibvirtException | InternalErrorException | URISyntaxException e) {
s_logger.warn("Exception while starting VM: " + ExceptionUtils.getRootCauseMessage(e));
if (conn != null) {
libvirtComputingResource.handleVmStartFailure(vm);
}
return new StartAnswer(command, e.getMessage());
} finally {
if (state != DomainState.VIR_DOMAIN_RUNNING) {
storagePoolMgr.disconnectPhysicalDisksViaVmSpec(vmSpec);
}
}
}
use of com.cloud.legacymodel.exceptions.InternalErrorException in project cosmic by MissionCriticalCloud.
the class LibvirtComputingResourceTest method testStartCommand.
@Test
public void testStartCommand() {
final VirtualMachineTO vmSpec = Mockito.mock(VirtualMachineTO.class);
final Host host = Mockito.mock(Host.class);
final boolean executeInSequence = false;
final StartCommand command = new StartCommand(vmSpec, host, executeInSequence);
final KvmStoragePoolManager storagePoolMgr = Mockito.mock(KvmStoragePoolManager.class);
final LibvirtUtilitiesHelper libvirtUtilitiesHelper = Mockito.mock(LibvirtUtilitiesHelper.class);
final Connect conn = Mockito.mock(Connect.class);
final LibvirtVmDef vmDef = Mockito.mock(LibvirtVmDef.class);
final VirtualRoutingResource virtRouterResource = Mockito.mock(VirtualRoutingResource.class);
final NicTO nic = Mockito.mock(NicTO.class);
final NicTO[] nics = new NicTO[] { nic };
final int[] vms = new int[0];
final String vmName = "Test";
final String controlIp = "127.0.0.1";
when(this.libvirtComputingResource.getStoragePoolMgr()).thenReturn(storagePoolMgr);
when(vmSpec.getNics()).thenReturn(nics);
when(vmSpec.getType()).thenReturn(VirtualMachineType.DomainRouter);
when(vmSpec.getName()).thenReturn(vmName);
when(this.libvirtComputingResource.createVmFromSpec(vmSpec)).thenReturn(vmDef);
when(this.libvirtComputingResource.getLibvirtUtilitiesHelper()).thenReturn(libvirtUtilitiesHelper);
try {
when(libvirtUtilitiesHelper.getConnectionByType(vmDef.getHvsType())).thenReturn(conn);
when(conn.listDomains()).thenReturn(vms);
doNothing().when(this.libvirtComputingResource).createVbd(conn, vmSpec, vmName, vmDef);
} catch (final LibvirtException e) {
fail(e.getMessage());
} catch (final InternalErrorException e) {
fail(e.getMessage());
} catch (final URISyntaxException e) {
fail(e.getMessage());
}
when(storagePoolMgr.connectPhysicalDisksViaVmSpec(vmSpec)).thenReturn(true);
try {
doNothing().when(this.libvirtComputingResource).createVifs(vmSpec, vmDef);
when(this.libvirtComputingResource.startVm(conn, vmName, vmDef.toString())).thenReturn("SUCCESS");
when(vmSpec.getBootArgs()).thenReturn("ls -lart");
when(this.libvirtComputingResource.passCmdLine(vmName, vmSpec.getBootArgs())).thenReturn(true);
when(nic.getIp()).thenReturn(controlIp);
when(nic.getType()).thenReturn(TrafficType.Control);
when(this.libvirtComputingResource.getVirtRouterResource()).thenReturn(virtRouterResource);
when(virtRouterResource.connect(controlIp, 30, 5000)).thenReturn(true);
} catch (final InternalErrorException e) {
fail(e.getMessage());
} catch (final LibvirtException e) {
fail(e.getMessage());
}
final LibvirtRequestWrapper wrapper = LibvirtRequestWrapper.getInstance();
assertNotNull(wrapper);
final Answer answer = wrapper.execute(command, this.libvirtComputingResource);
assertTrue(answer.getResult());
verify(this.libvirtComputingResource, times(1)).getStoragePoolMgr();
verify(this.libvirtComputingResource, times(1)).getLibvirtUtilitiesHelper();
try {
verify(libvirtUtilitiesHelper, times(1)).getConnectionByType(vmDef.getHvsType());
} catch (final LibvirtException e) {
fail(e.getMessage());
}
}
use of com.cloud.legacymodel.exceptions.InternalErrorException in project cosmic by MissionCriticalCloud.
the class LibvirtComputingResourceTest method testCreatePrivateTemplateFromSnapshotCommand.
@Test
public void testCreatePrivateTemplateFromSnapshotCommand() {
final StoragePool pool = Mockito.mock(StoragePool.class);
final String secondaryStoragePoolURL = "nfs:/127.0.0.1/storage/secondary";
final Long dcId = 1l;
final Long accountId = 1l;
final Long volumeId = 1l;
final String backedUpSnapshotUuid = "/run/9a0afe7c-26a7-4585-bf87-abf82ae106d9/";
final String backedUpSnapshotName = "snap";
final String origTemplateInstallPath = "/install/path/";
final Long newTemplateId = 2l;
final String templateName = "templ";
final int wait = 0;
final CreatePrivateTemplateFromSnapshotCommand command = new CreatePrivateTemplateFromSnapshotCommand(pool, secondaryStoragePoolURL, dcId, accountId, volumeId, backedUpSnapshotUuid, backedUpSnapshotName, origTemplateInstallPath, newTemplateId, templateName, wait);
final String templatePath = "/template/path";
final String localPath = "/mnt/local";
final String tmplName = "ce97bbc1-34fe-4259-9202-74bbce2562ab";
final KvmStoragePoolManager storagePoolMgr = Mockito.mock(KvmStoragePoolManager.class);
final KvmStoragePool secondaryPool = Mockito.mock(KvmStoragePool.class);
final KvmStoragePool snapshotPool = Mockito.mock(KvmStoragePool.class);
final KvmPhysicalDisk snapshot = Mockito.mock(KvmPhysicalDisk.class);
final StorageLayer storage = Mockito.mock(StorageLayer.class);
final LibvirtUtilitiesHelper libvirtUtilitiesHelper = Mockito.mock(LibvirtUtilitiesHelper.class);
final TemplateLocation location = Mockito.mock(TemplateLocation.class);
final Processor qcow2Processor = Mockito.mock(Processor.class);
final TemplateFormatInfo info = Mockito.mock(TemplateFormatInfo.class);
when(this.libvirtComputingResource.getStoragePoolMgr()).thenReturn(storagePoolMgr);
String snapshotPath = command.getSnapshotUuid();
final int index = snapshotPath.lastIndexOf("/");
snapshotPath = snapshotPath.substring(0, index);
when(storagePoolMgr.getStoragePoolByUri(command.getSecondaryStorageUrl() + snapshotPath)).thenReturn(snapshotPool);
when(storagePoolMgr.getStoragePoolByUri(command.getSecondaryStorageUrl())).thenReturn(secondaryPool);
when(snapshotPool.getPhysicalDisk(command.getSnapshotName())).thenReturn(snapshot);
when(secondaryPool.getLocalPath()).thenReturn(localPath);
when(this.libvirtComputingResource.getStorage()).thenReturn(storage);
when(this.libvirtComputingResource.createTmplPath()).thenReturn(templatePath);
when(this.libvirtComputingResource.getCmdsTimeout()).thenReturn(1);
final String templateFolder = command.getAccountId() + File.separator + command.getNewTemplateId();
final String templateInstallFolder = "template/tmpl/" + templateFolder;
final String tmplPath = secondaryPool.getLocalPath() + File.separator + templateInstallFolder;
when(this.libvirtComputingResource.getLibvirtUtilitiesHelper()).thenReturn(libvirtUtilitiesHelper);
when(libvirtUtilitiesHelper.buildTemplateLocation(storage, tmplPath)).thenReturn(location);
when(libvirtUtilitiesHelper.generateUuidName()).thenReturn(tmplName);
try {
when(libvirtUtilitiesHelper.buildQcow2Processor(storage)).thenReturn(qcow2Processor);
when(qcow2Processor.process(tmplPath, null, tmplName)).thenReturn(info);
} catch (final ConfigurationException e) {
fail(e.getMessage());
} catch (final InternalErrorException e) {
fail(e.getMessage());
}
final LibvirtRequestWrapper wrapper = LibvirtRequestWrapper.getInstance();
assertNotNull(wrapper);
final Answer answer = wrapper.execute(command, this.libvirtComputingResource);
assertTrue(answer.getResult());
verify(this.libvirtComputingResource, times(1)).getStoragePoolMgr();
verify(storagePoolMgr, times(1)).getStoragePoolByUri(command.getSecondaryStorageUrl() + snapshotPath);
verify(storagePoolMgr, times(1)).getStoragePoolByUri(command.getSecondaryStorageUrl());
}
Aggregations