use of com.cloud.legacymodel.exceptions.InternalErrorException in project cosmic by MissionCriticalCloud.
the class Xenserver625StorageProcessor method copyVolumeFromPrimaryToSecondary.
@Override
public Answer copyVolumeFromPrimaryToSecondary(final CopyCommand cmd) {
final Connection conn = hypervisorResource.getConnection();
final VolumeObjectTO srcVolume = (VolumeObjectTO) cmd.getSrcTO();
final VolumeObjectTO destVolume = (VolumeObjectTO) cmd.getDestTO();
final int wait = cmd.getWait();
final DataStoreTO destStore = destVolume.getDataStore();
if (destStore instanceof NfsTO) {
SR secondaryStorage = null;
Task task = null;
try {
final NfsTO nfsStore = (NfsTO) destStore;
final URI uri = new URI(nfsStore.getUrl());
// Create the volume folder
if (!hypervisorResource.createSecondaryStorageFolder(conn, uri.getHost() + ":" + uri.getPath(), destVolume.getPath())) {
throw new InternalErrorException("Failed to create the volume folder.");
}
// Create a SR for the volume UUID folder
secondaryStorage = createFileSr(conn, uri.getHost() + ":" + uri.getPath(), destVolume.getPath());
// Look up the volume on the source primary storage pool
final VDI srcVdi = getVDIbyUuid(conn, srcVolume.getPath());
// Copy the volume to secondary storage
task = srcVdi.copyAsync(conn, secondaryStorage, null, null);
// poll every 1 seconds ,
hypervisorResource.waitForTask(conn, task, 1000, wait * 1000);
hypervisorResource.checkForSuccess(conn, task);
final VDI destVdi = Types.toVDI(task, conn);
final String destVolumeUUID = destVdi.getUuid(conn);
final VolumeObjectTO newVol = new VolumeObjectTO();
newVol.setPath(destVolume.getPath() + File.separator + destVolumeUUID + ".vhd");
newVol.setSize(srcVolume.getSize());
return new CopyCmdAnswer(newVol);
} catch (final Exception e) {
s_logger.debug("Failed to copy volume to secondary: " + e.toString());
return new CopyCmdAnswer("Failed to copy volume to secondary: " + e.toString());
} finally {
if (task != null) {
try {
task.destroy(conn);
} catch (final Exception e) {
s_logger.warn("unable to destroy task(" + task.toWireString() + ") due to " + e.toString());
}
}
hypervisorResource.removeSR(conn, secondaryStorage);
}
}
return new CopyCmdAnswer("unsupported protocol");
}
use of com.cloud.legacymodel.exceptions.InternalErrorException 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