use of com.cloud.agent.api.storage.CreateDatadiskTemplateAnswer in project cloudstack by apache.
the class NfsSecondaryStorageResource method execute.
public Answer execute(CreateDatadiskTemplateCommand cmd) {
TemplateObjectTO diskTemplate = new TemplateObjectTO();
TemplateObjectTO dataDiskTemplate = (TemplateObjectTO) cmd.getDataDiskTemplate();
DataStoreTO dataStore = dataDiskTemplate.getDataStore();
if (!(dataStore instanceof NfsTO)) {
return new CreateDatadiskTemplateAnswer("Unsupported protocol");
}
NfsTO nfsImageStore = (NfsTO) dataStore;
String secondaryStorageUrl = nfsImageStore.getUrl();
assert (secondaryStorageUrl != null);
try {
String secondaryMountPoint = getRootDir(secondaryStorageUrl, _nfsVersion);
long templateId = dataDiskTemplate.getId();
String templateUniqueName = dataDiskTemplate.getUniqueName();
String origDisk = cmd.getPath();
long virtualSize = dataDiskTemplate.getSize();
String diskName = origDisk.substring((origDisk.lastIndexOf(File.separator)) + 1);
long physicalSize = new File(origDisk).length();
String newTmplDir = getTemplateRelativeDirInSecStorage(dataDiskTemplate.getAccountId(), dataDiskTemplate.getId());
String newTmplDirAbsolute = secondaryMountPoint + File.separator + newTmplDir;
String ovfFilePath = getOVFFilePath(origDisk);
if (!cmd.getBootable()) {
// Create folder to hold datadisk template
synchronized (newTmplDir.intern()) {
Script command = new Script("mkdir", _timeout, s_logger);
command.add("-p");
command.add(newTmplDirAbsolute);
String result = command.execute();
if (result != null) {
String msg = "Unable to prepare template directory: " + newTmplDir + ", storage: " + secondaryStorageUrl + ", error msg: " + result;
s_logger.error(msg);
throw new Exception(msg);
}
}
// Move Datadisk VMDK from parent template folder to Datadisk template folder
synchronized (origDisk.intern()) {
Script command = new Script("mv", _timeout, s_logger);
command.add(origDisk);
command.add(newTmplDirAbsolute);
String result = command.execute();
if (result != null) {
String msg = "Unable to copy VMDK from parent template folder to datadisk template folder" + ", error msg: " + result;
s_logger.error(msg);
throw new Exception(msg);
}
command = new Script("cp", _timeout, s_logger);
command.add(ovfFilePath + ORIGINAL_FILE_EXTENSION);
command.add(newTmplDirAbsolute);
result = command.execute();
if (result != null) {
String msg = "Unable to copy VMDK from parent template folder to datadisk template folder" + ", error msg: " + result;
s_logger.error(msg);
throw new Exception(msg);
}
}
}
// Create OVF for the disk
String newOvfFilePath = newTmplDirAbsolute + File.separator + ovfFilePath.substring(ovfFilePath.lastIndexOf(File.separator) + 1);
OVFHelper ovfHelper = new OVFHelper();
ovfHelper.rewriteOVFFileForSingleDisk(ovfFilePath + ORIGINAL_FILE_EXTENSION, newOvfFilePath, diskName);
postCreatePrivateTemplate(newTmplDirAbsolute, templateId, templateUniqueName, physicalSize, virtualSize);
writeMetaOvaForTemplate(newTmplDirAbsolute, ovfFilePath.substring(ovfFilePath.lastIndexOf(File.separator) + 1), diskName, templateUniqueName, physicalSize);
diskTemplate.setId(templateId);
if (diskName.endsWith("iso")) {
diskTemplate.setPath(newTmplDir + File.separator + diskName);
} else {
diskTemplate.setPath(newTmplDir + File.separator + templateUniqueName + ".ova");
}
diskTemplate.setSize(virtualSize);
diskTemplate.setPhysicalSize(physicalSize);
} catch (Exception e) {
String msg = "Create Datadisk template failed due to " + e.getMessage();
s_logger.error(msg, e);
return new CreateDatadiskTemplateAnswer(msg);
}
return new CreateDatadiskTemplateAnswer(diskTemplate);
}
use of com.cloud.agent.api.storage.CreateDatadiskTemplateAnswer 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.storage.CreateDatadiskTemplateAnswer in project cloudstack by apache.
the class TemplateObject method processEvent.
@Override
public void processEvent(ObjectInDataStoreStateMachine.Event event, Answer answer) {
try {
if (getDataStore().getRole() == DataStoreRole.Primary) {
if (answer instanceof CopyCmdAnswer) {
CopyCmdAnswer cpyAnswer = (CopyCmdAnswer) answer;
TemplateObjectTO newTemplate = (TemplateObjectTO) cpyAnswer.getNewData();
String deployAsIsConfiguration = newTemplate.getDeployAsIsConfiguration();
VMTemplateStoragePoolVO templatePoolRef = templatePoolDao.findByPoolTemplate(getDataStore().getId(), getId(), deployAsIsConfiguration);
templatePoolRef.setDownloadPercent(100);
setTemplateSizeIfNeeded(newTemplate, templatePoolRef);
templatePoolRef.setDownloadState(Status.DOWNLOADED);
setDownloadPathIfNeeded(newTemplate, templatePoolRef);
setInstallPathIfNeeded(newTemplate, templatePoolRef);
templatePoolDao.update(templatePoolRef.getId(), templatePoolRef);
}
} else if (getDataStore().getRole() == DataStoreRole.Image || getDataStore().getRole() == DataStoreRole.ImageCache) {
if (answer instanceof CopyCmdAnswer) {
CopyCmdAnswer cpyAnswer = (CopyCmdAnswer) answer;
TemplateObjectTO newTemplate = (TemplateObjectTO) cpyAnswer.getNewData();
TemplateDataStoreVO templateStoreRef = templateStoreDao.findByStoreTemplate(getDataStore().getId(), getId());
if (newTemplate.getPath() != null) {
templateStoreRef.setInstallPath(newTemplate.getPath());
}
templateStoreRef.setDownloadPercent(100);
templateStoreRef.setDownloadState(Status.DOWNLOADED);
templateStoreRef.setSize(newTemplate.getSize());
if (newTemplate.getPhysicalSize() != null) {
templateStoreRef.setPhysicalSize(newTemplate.getPhysicalSize());
}
templateStoreDao.update(templateStoreRef.getId(), templateStoreRef);
if (getDataStore().getRole() == DataStoreRole.Image) {
VMTemplateVO templateVO = imageDao.findById(getId());
if (newTemplate.getFormat() != null) {
templateVO.setFormat(newTemplate.getFormat());
}
if (newTemplate.getName() != null) {
// For template created from snapshot, template name is determine by resource code.
templateVO.setUniqueName(newTemplate.getName());
}
if (newTemplate.getHypervisorType() != null) {
templateVO.setHypervisorType(newTemplate.getHypervisorType());
}
templateVO.setSize(newTemplate.getSize());
imageDao.update(templateVO.getId(), templateVO);
}
} else if (answer instanceof CreateDatadiskTemplateAnswer) {
CreateDatadiskTemplateAnswer createAnswer = (CreateDatadiskTemplateAnswer) answer;
TemplateObjectTO dataDiskTemplate = createAnswer.getDataDiskTemplate();
TemplateDataStoreVO templateStoreRef = templateStoreDao.findByStoreTemplate(getDataStore().getId(), dataDiskTemplate.getId());
templateStoreRef.setInstallPath(dataDiskTemplate.getPath());
templateStoreRef.setDownloadPercent(100);
templateStoreRef.setDownloadState(Status.DOWNLOADED);
templateStoreRef.setSize(dataDiskTemplate.getSize());
templateStoreRef.setPhysicalSize(dataDiskTemplate.getPhysicalSize());
templateStoreDao.update(templateStoreRef.getId(), templateStoreRef);
}
}
objectInStoreMgr.update(this, event);
} catch (NoTransitionException e) {
s_logger.debug("failed to update state", e);
throw new CloudRuntimeException("Failed to update state" + e.toString());
} catch (Exception ex) {
s_logger.debug("failed to process event and answer", ex);
objectInStoreMgr.delete(this);
throw new CloudRuntimeException("Failed to process event", ex);
} finally {
// in case of OperationFailed, expunge the entry
if (event == ObjectInDataStoreStateMachine.Event.OperationFailed) {
objectInStoreMgr.deleteIfNotReady(this);
}
}
}
Aggregations