use of com.cloud.agent.api.to.DatadiskTO in project cloudstack by apache.
the class VolumeOrchestrator method allocateTemplatedVolumes.
@Override
public List<DiskProfile> allocateTemplatedVolumes(Type type, String name, DiskOffering offering, Long rootDisksize, Long minIops, Long maxIops, VirtualMachineTemplate template, VirtualMachine vm, Account owner) {
int volumesNumber = 1;
List<DatadiskTO> templateAsIsDisks = null;
String configurationId = null;
boolean deployVmAsIs = false;
if (template.isDeployAsIs() && vm.getType() != VirtualMachine.Type.SecondaryStorageVm) {
List<SecondaryStorageVmVO> runningSSVMs = secondaryStorageVmDao.getSecStorageVmListInStates(null, vm.getDataCenterId(), State.Running);
if (CollectionUtils.isEmpty(runningSSVMs)) {
s_logger.info("Could not find a running SSVM in datacenter " + vm.getDataCenterId() + " for deploying VM as is, " + "not deploying VM " + vm.getInstanceName() + " as-is");
} else {
UserVmDetailVO configurationDetail = userVmDetailsDao.findDetail(vm.getId(), VmDetailConstants.DEPLOY_AS_IS_CONFIGURATION);
if (configurationDetail != null) {
configurationId = configurationDetail.getValue();
}
templateAsIsDisks = _tmpltMgr.getTemplateDisksOnImageStore(template.getId(), DataStoreRole.Image, configurationId);
if (CollectionUtils.isNotEmpty(templateAsIsDisks)) {
templateAsIsDisks = templateAsIsDisks.stream().filter(x -> !x.isIso()).sorted(Comparator.comparing(DatadiskTO::getDiskNumber)).collect(Collectors.toList());
}
volumesNumber = templateAsIsDisks.size();
deployVmAsIs = true;
}
}
if (volumesNumber < 1) {
throw new CloudRuntimeException("Unable to create any volume from template " + template.getName());
}
List<DiskProfile> profiles = new ArrayList<>();
for (int number = 0; number < volumesNumber; number++) {
String volumeName = name;
Long volumeSize = rootDisksize;
long deviceId = type.equals(Type.ROOT) ? 0L : 1L;
if (deployVmAsIs) {
int volumeNameSuffix = templateAsIsDisks.get(number).getDiskNumber();
volumeName = String.format("%s-%d", volumeName, volumeNameSuffix);
volumeSize = templateAsIsDisks.get(number).getVirtualSize();
deviceId = templateAsIsDisks.get(number).getDiskNumber();
}
s_logger.info(String.format("adding disk object %s to %s", volumeName, vm.getInstanceName()));
DiskProfile diskProfile = allocateTemplatedVolume(type, volumeName, offering, volumeSize, minIops, maxIops, template, vm, owner, deviceId, configurationId);
profiles.add(diskProfile);
}
handleRootDiskControllerTpeForDeployAsIs(templateAsIsDisks, vm);
return profiles;
}
use of com.cloud.agent.api.to.DatadiskTO in project cloudstack by apache.
the class NfsSecondaryStorageResource method execute.
public Answer execute(GetDatadisksCommand cmd) {
DataTO srcData = cmd.getData();
String configurationId = cmd.getConfigurationId();
TemplateObjectTO template = (TemplateObjectTO) srcData;
DataStoreTO srcStore = srcData.getDataStore();
if (!(srcStore instanceof NfsTO)) {
return new CreateDatadiskTemplateAnswer("Unsupported protocol");
}
NfsTO nfsImageStore = (NfsTO) srcStore;
String secondaryStorageUrl = nfsImageStore.getUrl();
assert (secondaryStorageUrl != null);
String templateUrl = secondaryStorageUrl + File.separator + srcData.getPath();
Pair<String, String> templateInfo = decodeTemplateRelativePathAndNameFromUrl(secondaryStorageUrl, templateUrl, template.getName());
String templateRelativeFolderPath = templateInfo.first();
try {
String secondaryMountPoint = getRootDir(secondaryStorageUrl, _nfsVersion);
s_logger.info("MDOVE Secondary storage mount point: " + secondaryMountPoint);
String srcOVAFileName = getTemplateOnSecStorageFilePath(secondaryMountPoint, templateRelativeFolderPath, templateInfo.second(), ImageFormat.OVA.getFileExtension());
String ovfFilePath = getOVFFilePath(srcOVAFileName);
if (ovfFilePath == null) {
Script command = new Script("tar", 0, s_logger);
command.add("--no-same-owner");
command.add("--no-same-permissions");
command.add("-xf", srcOVAFileName);
command.setWorkDir(secondaryMountPoint + File.separator + templateRelativeFolderPath);
s_logger.info("Executing command: " + command.toString());
String result = command.execute();
if (result != null) {
String msg = "Unable to unpack snapshot OVA file at: " + srcOVAFileName;
s_logger.error(msg);
throw new Exception(msg);
}
command = new Script("chmod", 0, s_logger);
command.add("-R");
command.add("666", secondaryMountPoint + File.separator + templateRelativeFolderPath);
result = command.execute();
if (result != null) {
s_logger.warn("Unable to set permissions for " + secondaryMountPoint + File.separator + templateRelativeFolderPath + " due to " + result);
}
}
Script command = new Script("cp", _timeout, s_logger);
command.add(ovfFilePath);
command.add(ovfFilePath + ORIGINAL_FILE_EXTENSION);
String result = command.execute();
if (result != null) {
String msg = "Unable to rename original OVF, error msg: " + result;
s_logger.error(msg);
}
s_logger.debug("Reading OVF " + ovfFilePath + " to retrive the number of disks present in OVA");
OVFHelper ovfHelper = new OVFHelper();
List<DatadiskTO> disks = ovfHelper.getOVFVolumeInfoFromFile(ovfFilePath, configurationId);
return new GetDatadisksAnswer(disks);
} catch (Exception e) {
String msg = "Get Datadisk Template Count failed due to " + e.getMessage();
s_logger.error(msg, e);
return new GetDatadisksAnswer(msg);
}
}
use of com.cloud.agent.api.to.DatadiskTO in project cloudstack by apache.
the class OVFHelper method generateDiskTO.
private DatadiskTO generateDiskTO(OVFFile file, OVFDisk disk, String ovfParentPath, int diskNumber, OVFVirtualHardwareItemTO diskItem) throws InternalErrorException {
String path = file != null ? ovfParentPath + File.separator + file._href : null;
if (StringUtils.isNotBlank(path)) {
File f = new File(path);
if (!f.exists() || f.isDirectory()) {
s_logger.error("One of the attached disk or iso does not exists " + path);
throw new InternalErrorException("One of the attached disk or iso as stated on OVF does not exists " + path);
}
}
Long capacity = disk != null ? disk._capacity : file._size;
Long fileSize = file != null ? file._size : 0L;
String controller = "";
String controllerSubType = "";
if (disk != null) {
OVFDiskController cDiskController = disk._controller;
controller = cDiskController == null ? "" : disk._controller._name;
controllerSubType = cDiskController == null ? "" : disk._controller._subType;
}
boolean isIso = file != null && file.isIso;
boolean bootable = file != null && file._bootable;
String diskId = disk == null ? file._id : disk._diskId;
String configuration = diskItem != null ? diskItem.getConfigurationIds() : null;
return new DatadiskTO(path, capacity, fileSize, diskId, isIso, bootable, controller, controllerSubType, diskNumber, configuration);
}
use of com.cloud.agent.api.to.DatadiskTO in project cloudstack by apache.
the class OVFHelper method getOVFVolumeInfoFromFile.
public List<DatadiskTO> getOVFVolumeInfoFromFile(String ovfFilePath, Document doc, String configurationId) throws InternalErrorException {
if (StringUtils.isBlank(ovfFilePath)) {
return null;
}
File ovfFile = new File(ovfFilePath);
List<OVFVirtualHardwareItemTO> hardwareItems = getVirtualHardwareItemsFromDocumentTree(doc);
List<OVFFile> files = extractFilesFromOvfDocumentTree(ovfFile, doc);
List<OVFDisk> disks = extractDisksFromOvfDocumentTree(doc);
List<OVFVirtualHardwareItemTO> diskHardwareItems = hardwareItems.stream().filter(x -> x.getResourceType() == OVFVirtualHardwareItemTO.HardwareResourceType.DiskDrive && hardwareItemContainsConfiguration(x, configurationId)).collect(Collectors.toList());
return matchHardwareItemsToDiskAndFilesInformation(diskHardwareItems, files, disks, ovfFile.getParent());
}
use of com.cloud.agent.api.to.DatadiskTO in project cloudstack by apache.
the class OVFHelper method matchHardwareItemsToDiskAndFilesInformation.
private List<DatadiskTO> matchHardwareItemsToDiskAndFilesInformation(List<OVFVirtualHardwareItemTO> diskHardwareItems, List<OVFFile> files, List<OVFDisk> disks, String ovfParentPath) throws InternalErrorException {
List<DatadiskTO> diskTOs = new LinkedList<>();
int diskNumber = 0;
for (OVFVirtualHardwareItemTO diskItem : diskHardwareItems) {
if (StringUtils.isBlank(diskItem.getHostResource())) {
s_logger.error("Missing disk information for hardware item " + diskItem.getElementName() + " " + diskItem.getInstanceId());
continue;
}
String diskId = extractDiskIdFromDiskHostResource(diskItem.getHostResource());
OVFDisk diskDefinition = getDiskDefinitionFromDiskId(diskId, disks);
if (diskDefinition == null) {
s_logger.error("Missing disk definition for disk ID " + diskId);
}
OVFFile fileDefinition = getFileDefinitionFromDiskDefinition(diskDefinition._fileRef, files);
DatadiskTO datadiskTO = generateDiskTO(fileDefinition, diskDefinition, ovfParentPath, diskNumber, diskItem);
diskTOs.add(datadiskTO);
diskNumber++;
}
List<OVFFile> isoFiles = files.stream().filter(x -> x.isIso).collect(Collectors.toList());
for (OVFFile isoFile : isoFiles) {
DatadiskTO isoTO = generateDiskTO(isoFile, null, ovfParentPath, diskNumber, null);
diskTOs.add(isoTO);
diskNumber++;
}
return diskTOs;
}
Aggregations