use of com.cloud.legacymodel.to.PrimaryDataStoreTO in project cosmic by MissionCriticalCloud.
the class LibvirtRevertSnapshotCommandWrapper method execute.
@Override
public Answer execute(final RevertSnapshotCommand command, final LibvirtComputingResource libvirtComputingResource) {
final SnapshotObjectTO snapshot = command.getData();
final VolumeObjectTO volume = snapshot.getVolume();
final PrimaryDataStoreTO primaryStore = (PrimaryDataStoreTO) volume.getDataStore();
final DataStoreTO snapshotImageStore = snapshot.getDataStore();
if (!(snapshotImageStore instanceof NfsTO)) {
return new Answer(command, false, "revert snapshot on object storage is not implemented yet");
}
final NfsTO nfsImageStore = (NfsTO) snapshotImageStore;
final String secondaryStoragePoolUrl = nfsImageStore.getUrl();
final String volumePath = volume.getPath();
String snapshotPath = null;
String snapshotRelPath = null;
KvmStoragePool secondaryStoragePool = null;
try {
final KvmStoragePoolManager storagePoolMgr = libvirtComputingResource.getStoragePoolMgr();
secondaryStoragePool = storagePoolMgr.getStoragePoolByUri(secondaryStoragePoolUrl);
final String ssPmountPath = secondaryStoragePool.getLocalPath();
snapshotRelPath = snapshot.getPath();
snapshotPath = ssPmountPath + File.separator + snapshotRelPath;
final KvmPhysicalDisk snapshotDisk = storagePoolMgr.getPhysicalDisk(primaryStore.getPoolType(), primaryStore.getUuid(), volumePath);
final KvmStoragePool primaryPool = snapshotDisk.getPool();
if (primaryPool.getType() == StoragePoolType.RBD) {
return new Answer(command, false, "revert snapshot to RBD is not implemented yet");
} else {
final Script cmd = new Script(libvirtComputingResource.manageSnapshotPath(), libvirtComputingResource.getCmdsTimeout(), s_logger);
cmd.add("-v", snapshotPath);
cmd.add("-n", snapshotDisk.getName());
cmd.add("-p", snapshotDisk.getPath());
final String result = cmd.execute();
if (result != null) {
s_logger.debug("Failed to revert snaptshot: " + result);
return new Answer(command, false, result);
}
}
return new Answer(command, true, "RevertSnapshotCommand executes successfully");
} catch (final CloudRuntimeException e) {
return new Answer(command, false, e.toString());
}
}
use of com.cloud.legacymodel.to.PrimaryDataStoreTO in project cosmic by MissionCriticalCloud.
the class XenServerStorageProcessor method copyTemplateToPrimaryStorage.
@Override
public Answer copyTemplateToPrimaryStorage(final CopyCommand cmd) {
final DataTO srcDataTo = cmd.getSrcTO();
final DataTO destDataTo = cmd.getDestTO();
final int wait = cmd.getWait();
final DataStoreTO srcDataStoreTo = srcDataTo.getDataStore();
try {
if (srcDataStoreTo instanceof NfsTO && srcDataTo.getObjectType() == DataObjectType.TEMPLATE) {
final NfsTO srcImageStore = (NfsTO) srcDataStoreTo;
final TemplateObjectTO srcTemplateObjectTo = (TemplateObjectTO) srcDataTo;
final String storeUrl = srcImageStore.getUrl();
final URI uri = new URI(storeUrl);
final String tmplPath = uri.getHost() + ":" + uri.getPath() + "/" + srcDataTo.getPath();
final DataStoreTO destDataStoreTo = destDataTo.getDataStore();
boolean managed = false;
String storageHost = null;
String managedStoragePoolName = null;
String managedStoragePoolRootVolumeName = null;
String managedStoragePoolRootVolumeSize = null;
String chapInitiatorUsername = null;
String chapInitiatorSecret = null;
if (destDataStoreTo instanceof PrimaryDataStoreTO) {
final PrimaryDataStoreTO destPrimaryDataStoreTo = (PrimaryDataStoreTO) destDataStoreTo;
final Map<String, String> details = destPrimaryDataStoreTo.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);
}
}
}
final Connection conn = hypervisorResource.getConnection();
final SR sr;
if (managed) {
final Map<String, String> details = new HashMap<>();
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);
sr = hypervisorResource.prepareManagedSr(conn, details);
} else {
final String srName = destDataStoreTo.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 {
sr = srs.iterator().next();
}
}
final String srUuid = sr.getUuid(conn);
final String tmplUuid = copy_vhd_from_secondarystorage(conn, tmplPath, srUuid, wait);
final VDI tmplVdi = getVDIbyUuid(conn, tmplUuid);
final String uuidToReturn;
final Long physicalSize = tmplVdi.getPhysicalUtilisation(conn);
if (managed) {
uuidToReturn = tmplUuid;
tmplVdi.setNameLabel(conn, managedStoragePoolRootVolumeName);
} else {
final VDI snapshotVdi = tmplVdi.snapshot(conn, new HashMap<>());
uuidToReturn = snapshotVdi.getUuid(conn);
snapshotVdi.setNameLabel(conn, "Template " + srcTemplateObjectTo.getName());
tmplVdi.destroy(conn);
}
sr.scan(conn);
try {
Thread.sleep(5000);
} catch (final InterruptedException e) {
}
final TemplateObjectTO newVol = new TemplateObjectTO();
newVol.setUuid(uuidToReturn);
newVol.setPath(uuidToReturn);
if (physicalSize != null) {
newVol.setSize(physicalSize);
}
newVol.setFormat(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);
}
return new CopyCmdAnswer("not implemented yet");
}
use of com.cloud.legacymodel.to.PrimaryDataStoreTO in project cosmic by MissionCriticalCloud.
the class XenServerStorageProcessor method createVolumeFromSnapshot.
@Override
public Answer createVolumeFromSnapshot(final CopyCommand cmd) {
final Connection conn = hypervisorResource.getConnection();
final DataTO srcData = cmd.getSrcTO();
final SnapshotObjectTO snapshot = (SnapshotObjectTO) srcData;
final DataTO destData = cmd.getDestTO();
final DataStoreTO imageStore = srcData.getDataStore();
if (srcData.getDataStore() instanceof PrimaryDataStoreTO && destData.getDataStore() instanceof PrimaryDataStoreTO) {
return createVolumeFromSnapshot2(cmd);
}
if (!(imageStore instanceof NfsTO)) {
return new CopyCmdAnswer("unsupported protocol");
}
final NfsTO nfsImageStore = (NfsTO) imageStore;
final String primaryStorageNameLabel = destData.getDataStore().getUuid();
final String secondaryStorageUrl = nfsImageStore.getUrl();
final int wait = cmd.getWait();
boolean result = false;
// Generic error message.
String details = null;
String volumeUUID = null;
if (secondaryStorageUrl == null) {
details += " because the URL passed: " + secondaryStorageUrl + " is invalid.";
return new CopyCmdAnswer(details);
}
try {
final SR primaryStorageSR = hypervisorResource.getSRByNameLabelandHost(conn, primaryStorageNameLabel);
if (primaryStorageSR == null) {
throw new InternalErrorException("Could not create volume from snapshot because the primary Storage SR could not be created from the name label: " + primaryStorageNameLabel);
}
// Get the absolute path of the snapshot on the secondary storage.
String snapshotInstallPath = snapshot.getPath();
final int index = snapshotInstallPath.lastIndexOf(nfsImageStore.getPathSeparator());
final String snapshotName = snapshotInstallPath.substring(index + 1);
if (!snapshotName.startsWith("VHD-") && !snapshotName.endsWith(".vhd")) {
snapshotInstallPath = snapshotInstallPath + ".vhd";
}
final URI snapshotURI = new URI(secondaryStorageUrl + nfsImageStore.getPathSeparator() + snapshotInstallPath);
final String snapshotPath = snapshotURI.getHost() + ":" + snapshotURI.getPath();
final String srUuid = primaryStorageSR.getUuid(conn);
volumeUUID = copy_vhd_from_secondarystorage(conn, snapshotPath, srUuid, wait);
result = true;
final VDI volume = VDI.getByUuid(conn, volumeUUID);
final VDI.Record vdir = volume.getRecord(conn);
final VolumeObjectTO newVol = new VolumeObjectTO();
newVol.setPath(volumeUUID);
newVol.setSize(vdir.virtualSize);
return new CopyCmdAnswer(newVol);
} catch (final XenAPIException e) {
details += " due to " + e.toString();
s_logger.warn(details, e);
} catch (final Exception e) {
details += " due to " + e.getMessage();
s_logger.warn(details, e);
}
if (!result) {
// Is this logged at a higher level?
s_logger.error(details);
}
// In all cases return something.
return new CopyCmdAnswer(details);
}
use of com.cloud.legacymodel.to.PrimaryDataStoreTO in project cosmic by MissionCriticalCloud.
the class Xenserver625StorageProcessor method createTemplateFromSnapshot.
@Override
public Answer createTemplateFromSnapshot(final CopyCommand cmd) {
final Connection conn = hypervisorResource.getConnection();
final DataTO srcData = cmd.getSrcTO();
final DataTO destData = cmd.getDestTO();
if (srcData.getDataStore() instanceof PrimaryDataStoreTO && destData.getDataStore() instanceof NfsTO) {
return createTemplateFromSnapshot2(cmd);
}
final int wait = cmd.getWait();
final SnapshotObjectTO srcObj = (SnapshotObjectTO) srcData;
final TemplateObjectTO destObj = (TemplateObjectTO) destData;
final NfsTO srcStore = (NfsTO) srcObj.getDataStore();
final NfsTO destStore = (NfsTO) destObj.getDataStore();
URI srcUri = null;
URI destUri = null;
try {
srcUri = new URI(srcStore.getUrl());
destUri = new URI(destStore.getUrl());
} catch (final Exception e) {
s_logger.debug("incorrect url", e);
return new CopyCmdAnswer("incorrect url" + e.toString());
}
final String srcPath = srcObj.getPath();
final int index = srcPath.lastIndexOf("/");
final String srcDir = srcPath.substring(0, index);
final String destDir = destObj.getPath();
SR srcSr = null;
SR destSr = null;
VDI destVdi = null;
boolean result = false;
try {
srcSr = createFileSr(conn, srcUri.getHost() + ":" + srcUri.getPath(), srcDir);
final String destNfsPath = destUri.getHost() + ":" + destUri.getPath();
final String localDir = "/var/cloud_mount/" + UUID.nameUUIDFromBytes(destNfsPath.getBytes());
mountNfs(conn, destUri.getHost() + ":" + destUri.getPath(), localDir);
makeDirectory(conn, localDir + "/" + destDir);
destSr = createFileSR(conn, localDir + "/" + destDir);
final String nameLabel = "cloud-" + UUID.randomUUID().toString();
final String[] parents = srcObj.getParents();
final List<VDI> snapshotChains = new ArrayList<>();
if (parents != null) {
for (int i = 0; i < parents.length; i++) {
final String snChainPath = parents[i];
final String uuid = getSnapshotUuid(snChainPath);
final VDI chain = VDI.getByUuid(conn, uuid);
snapshotChains.add(chain);
}
}
final String snapshotUuid = getSnapshotUuid(srcPath);
final VDI snapshotVdi = VDI.getByUuid(conn, snapshotUuid);
snapshotChains.add(snapshotVdi);
final long templateVirtualSize = snapshotChains.get(0).getVirtualSize(conn);
destVdi = createVdi(conn, nameLabel, destSr, templateVirtualSize);
final String destVdiUuid = destVdi.getUuid(conn);
for (final VDI snapChain : snapshotChains) {
final Task task = snapChain.copyAsync(conn, null, null, destVdi);
// poll every 1 seconds ,
hypervisorResource.waitForTask(conn, task, 1000, wait * 1000);
hypervisorResource.checkForSuccess(conn, task);
task.destroy(conn);
}
destVdi = VDI.getByUuid(conn, destVdiUuid);
// scan makes XenServer pick up VDI physicalSize
destSr.scan(conn);
final String templateUuid = destVdi.getUuid(conn);
final String templateFilename = templateUuid + ".vhd";
final long virtualSize = destVdi.getVirtualSize(conn);
final long physicalSize = destVdi.getPhysicalUtilisation(conn);
String templatePath = destNfsPath + "/" + destDir;
templatePath = templatePath.replaceAll("//", "/");
result = hypervisorResource.postCreatePrivateTemplate(conn, templatePath, templateFilename, templateUuid, nameLabel, null, physicalSize, virtualSize, destObj.getId());
if (!result) {
throw new CloudRuntimeException("Could not create the template.properties file on secondary storage dir");
}
final TemplateObjectTO newTemplate = new TemplateObjectTO();
newTemplate.setPath(destDir + "/" + templateFilename);
newTemplate.setFormat(ImageFormat.VHD);
newTemplate.setSize(destVdi.getVirtualSize(conn));
newTemplate.setPhysicalSize(destVdi.getPhysicalUtilisation(conn));
newTemplate.setName(destVdiUuid);
result = true;
return new CopyCmdAnswer(newTemplate);
} catch (final Exception e) {
s_logger.error("Failed create template from snapshot", e);
return new CopyCmdAnswer("Failed create template from snapshot " + e.toString());
} finally {
if (!result) {
if (destVdi != null) {
try {
destVdi.destroy(conn);
} catch (final Exception e) {
s_logger.debug("Clean up left over on dest storage failed: ", e);
}
}
}
if (srcSr != null) {
hypervisorResource.removeSR(conn, srcSr);
}
if (destSr != null) {
hypervisorResource.removeSR(conn, destSr);
}
}
}
use of com.cloud.legacymodel.to.PrimaryDataStoreTO in project cosmic by MissionCriticalCloud.
the class Xenserver625StorageProcessor method createVolumeFromSnapshot.
@Override
public Answer createVolumeFromSnapshot(final CopyCommand cmd) {
final Connection conn = hypervisorResource.getConnection();
final DataTO srcData = cmd.getSrcTO();
final SnapshotObjectTO snapshot = (SnapshotObjectTO) srcData;
final DataTO destData = cmd.getDestTO();
final PrimaryDataStoreTO pool = (PrimaryDataStoreTO) destData.getDataStore();
final VolumeObjectTO volume = (VolumeObjectTO) destData;
final DataStoreTO imageStore = srcData.getDataStore();
if (srcData.getDataStore() instanceof PrimaryDataStoreTO && destData.getDataStore() instanceof PrimaryDataStoreTO) {
return createVolumeFromSnapshot2(cmd);
}
if (!(imageStore instanceof NfsTO)) {
return new CopyCmdAnswer("unsupported protocol");
}
final NfsTO nfsImageStore = (NfsTO) imageStore;
final String primaryStorageNameLabel = pool.getUuid();
final String secondaryStorageUrl = nfsImageStore.getUrl();
final int wait = cmd.getWait();
boolean result = false;
// Generic error message.
String details = null;
String volumeUUID = null;
if (secondaryStorageUrl == null) {
details += " because the URL passed: " + secondaryStorageUrl + " is invalid.";
return new CopyCmdAnswer(details);
}
SR srcSr = null;
VDI destVdi = null;
try {
final SR primaryStorageSR = hypervisorResource.getSRByNameLabelandHost(conn, primaryStorageNameLabel);
if (primaryStorageSR == null) {
throw new InternalErrorException("Could not create volume from snapshot because the primary Storage SR could not be created from the name label: " + primaryStorageNameLabel);
}
final String nameLabel = "cloud-" + UUID.randomUUID().toString();
destVdi = createVdi(conn, nameLabel, primaryStorageSR, volume.getSize());
volumeUUID = destVdi.getUuid(conn);
final String snapshotInstallPath = snapshot.getPath();
final int index = snapshotInstallPath.lastIndexOf(File.separator);
final String snapshotDirectory = snapshotInstallPath.substring(0, index);
final String snapshotUuid = getSnapshotUuid(snapshotInstallPath);
final URI uri = new URI(secondaryStorageUrl);
srcSr = createFileSr(conn, uri.getHost() + ":" + uri.getPath(), snapshotDirectory);
final String[] parents = snapshot.getParents();
final List<VDI> snapshotChains = new ArrayList<>();
if (parents != null) {
for (int i = 0; i < parents.length; i++) {
final String snChainPath = parents[i];
final String uuid = getSnapshotUuid(snChainPath);
final VDI chain = VDI.getByUuid(conn, uuid);
snapshotChains.add(chain);
}
}
final VDI snapshotVdi = VDI.getByUuid(conn, snapshotUuid);
snapshotChains.add(snapshotVdi);
for (final VDI snapChain : snapshotChains) {
final Task task = snapChain.copyAsync(conn, null, null, destVdi);
// poll every 1 seconds ,
hypervisorResource.waitForTask(conn, task, 1000, wait * 1000);
hypervisorResource.checkForSuccess(conn, task);
task.destroy(conn);
}
result = true;
destVdi = VDI.getByUuid(conn, volumeUUID);
final VDI.Record vdir = destVdi.getRecord(conn);
final VolumeObjectTO newVol = new VolumeObjectTO();
newVol.setPath(volumeUUID);
newVol.setSize(vdir.virtualSize);
return new CopyCmdAnswer(newVol);
} catch (final Types.XenAPIException e) {
details += " due to " + e.toString();
s_logger.warn(details, e);
} catch (final Exception e) {
details += " due to " + e.getMessage();
s_logger.warn(details, e);
} finally {
if (srcSr != null) {
hypervisorResource.removeSR(conn, srcSr);
}
if (!result && destVdi != null) {
try {
destVdi.destroy(conn);
} catch (final Exception e) {
s_logger.debug("destroy dest vdi failed", e);
}
}
}
if (!result) {
// Is this logged at a higher level?
s_logger.error(details);
}
// In all cases return something.
return new CopyCmdAnswer(details);
}
Aggregations