use of com.cloud.agent.api.to.DataStoreTO in project cloudstack by apache.
the class Xenserver625StorageProcessor method copyTemplateToPrimaryStorage.
@Override
public Answer copyTemplateToPrimaryStorage(final CopyCommand cmd) {
final DataTO srcData = cmd.getSrcTO();
final DataTO destData = cmd.getDestTO();
final int wait = cmd.getWait();
final DataStoreTO srcStore = srcData.getDataStore();
final Connection conn = hypervisorResource.getConnection();
SR srcSr = null;
SR destSr = null;
boolean removeSrAfterCopy = false;
Task task = null;
try {
if (srcStore instanceof NfsTO && srcData.getObjectType() == DataObjectType.TEMPLATE) {
final NfsTO srcImageStore = (NfsTO) srcStore;
final TemplateObjectTO srcTemplate = (TemplateObjectTO) srcData;
final String storeUrl = srcImageStore.getUrl();
final URI uri = new URI(storeUrl);
String volumePath = srcData.getPath();
volumePath = StringUtils.stripEnd(volumePath, "/");
final String[] splits = volumePath.split("/");
String volumeDirectory = volumePath;
if (splits.length > 4) {
// "template/tmpl/dcid/templateId/templatename"
final int index = volumePath.lastIndexOf("/");
volumeDirectory = volumePath.substring(0, index);
}
srcSr = createFileSr(conn, uri.getHost() + ":" + uri.getPath(), volumeDirectory);
final Set<VDI> setVdis = srcSr.getVDIs(conn);
if (setVdis.size() != 1) {
return new CopyCmdAnswer("Expected 1 VDI template, but found " + setVdis.size() + " VDI templates on: " + uri.getHost() + ":" + uri.getPath() + "/" + volumeDirectory);
}
final VDI srcVdi = setVdis.iterator().next();
boolean managed = false;
String storageHost = null;
String managedStoragePoolName = null;
String managedStoragePoolRootVolumeName = null;
String managedStoragePoolRootVolumeSize = null;
String chapInitiatorUsername = null;
String chapInitiatorSecret = null;
final PrimaryDataStoreTO destStore = (PrimaryDataStoreTO) destData.getDataStore();
Map<String, String> details = destStore.getDetails();
if (details != null) {
managed = Boolean.parseBoolean(details.get(PrimaryDataStoreTO.MANAGED));
if (managed) {
storageHost = details.get(PrimaryDataStoreTO.STORAGE_HOST);
managedStoragePoolName = details.get(PrimaryDataStoreTO.MANAGED_STORE_TARGET);
managedStoragePoolRootVolumeName = details.get(PrimaryDataStoreTO.MANAGED_STORE_TARGET_ROOT_VOLUME);
managedStoragePoolRootVolumeSize = details.get(PrimaryDataStoreTO.VOLUME_SIZE);
chapInitiatorUsername = details.get(PrimaryDataStoreTO.CHAP_INITIATOR_USERNAME);
chapInitiatorSecret = details.get(PrimaryDataStoreTO.CHAP_INITIATOR_SECRET);
removeSrAfterCopy = Boolean.parseBoolean(details.get(PrimaryDataStoreTO.REMOVE_AFTER_COPY));
}
}
if (managed) {
details = new HashMap<String, String>();
details.put(DiskTO.STORAGE_HOST, storageHost);
details.put(DiskTO.IQN, managedStoragePoolName);
details.put(DiskTO.VOLUME_SIZE, managedStoragePoolRootVolumeSize);
details.put(DiskTO.CHAP_INITIATOR_USERNAME, chapInitiatorUsername);
details.put(DiskTO.CHAP_INITIATOR_SECRET, chapInitiatorSecret);
destSr = hypervisorResource.prepareManagedSr(conn, details);
} else {
final String srName = destStore.getUuid();
final Set<SR> srs = SR.getByNameLabel(conn, srName);
if (srs.size() != 1) {
final String msg = "There are " + srs.size() + " SRs with same name: " + srName;
s_logger.warn(msg);
return new CopyCmdAnswer(msg);
} else {
destSr = srs.iterator().next();
}
}
task = srcVdi.copyAsync(conn, destSr, null, null);
// poll every 1 seconds ,
hypervisorResource.waitForTask(conn, task, 1000, wait * 1000);
hypervisorResource.checkForSuccess(conn, task);
final VDI tmplVdi = Types.toVDI(task, conn);
final String uuidToReturn;
final Long physicalSize = tmplVdi.getPhysicalUtilisation(conn);
if (managed) {
uuidToReturn = tmplVdi.getUuid(conn);
tmplVdi.setNameLabel(conn, managedStoragePoolRootVolumeName);
} else {
final VDI snapshotVdi = tmplVdi.snapshot(conn, new HashMap<String, String>());
uuidToReturn = snapshotVdi.getUuid(conn);
snapshotVdi.setNameLabel(conn, "Template " + srcTemplate.getName());
tmplVdi.destroy(conn);
}
destSr.scan(conn);
try {
Thread.sleep(5000);
} catch (final Exception e) {
}
final TemplateObjectTO newVol = new TemplateObjectTO();
newVol.setUuid(uuidToReturn);
newVol.setPath(uuidToReturn);
if (physicalSize != null) {
newVol.setSize(physicalSize);
}
newVol.setFormat(Storage.ImageFormat.VHD);
return new CopyCmdAnswer(newVol);
}
} catch (final Exception e) {
final String msg = "Catch Exception " + e.getClass().getName() + " for template due to " + e.toString();
s_logger.warn(msg, e);
return new CopyCmdAnswer(msg);
} finally {
if (task != null) {
try {
task.destroy(conn);
} catch (final Exception e) {
s_logger.debug("unable to destroy task (" + task.toWireString() + ") due to " + e.toString());
}
}
if (srcSr != null) {
hypervisorResource.removeSR(conn, srcSr);
}
if (removeSrAfterCopy && destSr != null) {
hypervisorResource.removeSR(conn, destSr);
}
}
return new CopyCmdAnswer("not implemented yet");
}
use of com.cloud.agent.api.to.DataStoreTO in project cloudstack by apache.
the class NfsSecondaryStorageResource method execute.
private Answer execute(SecStorageSetupCommand cmd) {
if (!_inSystemVM) {
return new Answer(cmd, true, null);
}
Answer answer = null;
DataStoreTO dStore = cmd.getDataStore();
if (dStore instanceof NfsTO) {
String secUrl = cmd.getSecUrl();
try {
URI uri = new URI(secUrl);
String nfsHostIp = getUriHostIp(uri);
addRouteToInternalIpOrCidr(_storageGateway, _storageIp, _storageNetmask, nfsHostIp);
String dir = mountUri(uri, cmd.getNfsVersion());
configCerts(cmd.getCerts());
nfsIps.add(nfsHostIp);
answer = new SecStorageSetupAnswer(dir);
} catch (Exception e) {
String msg = "GetRootDir for " + secUrl + " failed due to " + e.toString();
s_logger.error(msg);
answer = new Answer(cmd, false, msg);
}
} else {
// TODO: what do we need to setup for S3/Swift, maybe need to mount
// to some cache storage
answer = new Answer(cmd, true, null);
}
savePostUploadPSK(cmd.getPostUploadKey());
startPostUploadServer();
return answer;
}
use of com.cloud.agent.api.to.DataStoreTO in project cloudstack by apache.
the class NfsSecondaryStorageResource method copyFromNfsToImage.
protected Answer copyFromNfsToImage(CopyCommand cmd) {
DataTO destData = cmd.getDestTO();
DataStoreTO destDataStore = destData.getDataStore();
if (destDataStore instanceof S3TO) {
return copyFromNfsToS3(cmd);
} else if (destDataStore instanceof SwiftTO) {
return copyFromNfsToSwift(cmd);
} else {
return new CopyCmdAnswer("unsupported ");
}
}
use of com.cloud.agent.api.to.DataStoreTO in project cloudstack by apache.
the class NfsSecondaryStorageResource method createTemplateFromSnapshot.
protected Answer createTemplateFromSnapshot(CopyCommand cmd) {
DataTO srcData = cmd.getSrcTO();
DataTO destData = cmd.getDestTO();
DataStoreTO srcDataStore = srcData.getDataStore();
DataStoreTO destDataStore = destData.getDataStore();
if (srcDataStore.getRole() == DataStoreRole.Image || srcDataStore.getRole() == DataStoreRole.ImageCache || srcDataStore.getRole() == DataStoreRole.Primary) {
if (!(srcDataStore instanceof NfsTO)) {
s_logger.debug("only support nfs storage as src, when create template from snapshot");
return Answer.createUnsupportedCommandAnswer(cmd);
}
if (destDataStore instanceof NfsTO) {
return copySnapshotToTemplateFromNfsToNfs(cmd, (SnapshotObjectTO) srcData, (NfsTO) srcDataStore, (TemplateObjectTO) destData, (NfsTO) destDataStore);
} else if (destDataStore instanceof SwiftTO) {
//create template on the same data store
CopyCmdAnswer answer = (CopyCmdAnswer) copySnapshotToTemplateFromNfsToNfs(cmd, (SnapshotObjectTO) srcData, (NfsTO) srcDataStore, (TemplateObjectTO) destData, (NfsTO) srcDataStore);
if (!answer.getResult()) {
return answer;
}
s_logger.debug("starting copy template to swift");
TemplateObjectTO newTemplate = (TemplateObjectTO) answer.getNewData();
newTemplate.setDataStore(srcDataStore);
CopyCommand newCpyCmd = new CopyCommand(newTemplate, destData, cmd.getWait(), cmd.executeInSequence());
Answer result = copyFromNfsToSwift(newCpyCmd);
cleanupStagingNfs(newTemplate);
return result;
} else if (destDataStore instanceof S3TO) {
//create template on the same data store
CopyCmdAnswer answer = (CopyCmdAnswer) copySnapshotToTemplateFromNfsToNfs(cmd, (SnapshotObjectTO) srcData, (NfsTO) srcDataStore, (TemplateObjectTO) destData, (NfsTO) srcDataStore);
if (!answer.getResult()) {
return answer;
}
TemplateObjectTO newTemplate = (TemplateObjectTO) answer.getNewData();
newTemplate.setDataStore(srcDataStore);
CopyCommand newCpyCmd = new CopyCommand(newTemplate, destData, cmd.getWait(), cmd.executeInSequence());
Answer result = copyFromNfsToS3(newCpyCmd);
cleanupStagingNfs(newTemplate);
return result;
}
}
s_logger.debug("Failed to create template from snapshot");
return new CopyCmdAnswer("Unsupported protocol");
}
use of com.cloud.agent.api.to.DataStoreTO in project cloudstack by apache.
the class NfsSecondaryStorageResource method copyFromNfsToSwift.
/**
* Copies data from NFS and uploads it into a Swift container
*
* @param cmd CopyComand
* @return CopyCmdAnswer
*/
protected Answer copyFromNfsToSwift(CopyCommand cmd) {
final DataTO srcData = cmd.getSrcTO();
final DataTO destData = cmd.getDestTO();
DataStoreTO srcDataStore = srcData.getDataStore();
NfsTO srcStore = (NfsTO) srcDataStore;
DataStoreTO destDataStore = destData.getDataStore();
File srcFile = getFile(srcData.getPath(), srcStore.getUrl(), _nfsVersion);
SwiftTO swift = (SwiftTO) destDataStore;
long pathId = destData.getId();
try {
if (destData instanceof SnapshotObjectTO) {
pathId = ((SnapshotObjectTO) destData).getVolume().getId();
}
String containerName = SwiftUtil.getContainerName(destData.getObjectType().toString(), pathId);
String swiftPath = SwiftUtil.putObject(swift, srcFile, containerName, srcFile.getName());
DataTO retObj = null;
if (destData.getObjectType() == DataObjectType.TEMPLATE) {
TemplateObjectTO destTemplateData = (TemplateObjectTO) destData;
String uniqueName = destTemplateData.getName();
swiftUploadMetadataFile(swift, srcFile, containerName, uniqueName);
TemplateObjectTO newTemplate = new TemplateObjectTO();
newTemplate.setPath(swiftPath);
newTemplate.setSize(getVirtualSize(srcFile, getTemplateFormat(srcFile.getName())));
newTemplate.setPhysicalSize(srcFile.length());
newTemplate.setFormat(getTemplateFormat(srcFile.getName()));
retObj = newTemplate;
} else if (destData.getObjectType() == DataObjectType.VOLUME) {
VolumeObjectTO newVol = new VolumeObjectTO();
newVol.setPath(containerName);
newVol.setSize(getVirtualSize(srcFile, getTemplateFormat(srcFile.getName())));
retObj = newVol;
} else if (destData.getObjectType() == DataObjectType.SNAPSHOT) {
SnapshotObjectTO newSnapshot = new SnapshotObjectTO();
newSnapshot.setPath(containerName + File.separator + srcFile.getName());
retObj = newSnapshot;
}
return new CopyCmdAnswer(retObj);
} catch (Exception e) {
s_logger.error("failed to upload " + srcData.getPath(), e);
return new CopyCmdAnswer("failed to upload " + srcData.getPath() + e.toString());
}
}
Aggregations