use of org.apache.cloudstack.storage.command.CopyCmdAnswer in project cloudstack by apache.
the class VmwareStorageProcessor method copyVolumeFromImageCacheToPrimary.
@Override
public Answer copyVolumeFromImageCacheToPrimary(CopyCommand cmd) {
VolumeObjectTO srcVolume = (VolumeObjectTO) cmd.getSrcTO();
VolumeObjectTO destVolume = (VolumeObjectTO) cmd.getDestTO();
VmwareContext context = hostService.getServiceContext(cmd);
try {
NfsTO srcStore = (NfsTO) srcVolume.getDataStore();
DataStoreTO destStore = destVolume.getDataStore();
VmwareHypervisorHost hyperHost = hostService.getHyperHost(context, cmd);
String uuid = destStore.getUuid();
ManagedObjectReference morDatastore = HypervisorHostHelper.findDatastoreWithBackwardsCompatibility(hyperHost, uuid);
if (morDatastore == null) {
URI uri = new URI(destStore.getUrl());
morDatastore = hyperHost.mountDatastore(false, uri.getHost(), 0, uri.getPath(), destStore.getUuid().replace("-", ""));
if (morDatastore == null) {
throw new Exception("Unable to mount storage pool on host. storeUrl: " + uri.getHost() + ":/" + uri.getPath());
}
}
Pair<String, String> result = copyVolumeFromSecStorage(hyperHost, srcVolume.getPath(), new DatastoreMO(context, morDatastore), srcStore.getUrl(), (long) cmd.getWait() * 1000, _nfsVersion);
deleteVolumeDirOnSecondaryStorage(result.first(), srcStore.getUrl(), _nfsVersion);
VolumeObjectTO newVolume = new VolumeObjectTO();
newVolume.setPath(result.second());
return new CopyCmdAnswer(newVolume);
} catch (Throwable t) {
if (t instanceof RemoteException) {
hostService.invalidateServiceContext(context);
}
String msg = "Unable to execute CopyVolumeCommand due to exception";
s_logger.error(msg, t);
return new CopyCmdAnswer("copy volume secondary to primary failed due to exception: " + VmwareHelper.getExceptionMessage(t));
}
}
use of org.apache.cloudstack.storage.command.CopyCmdAnswer in project cloudstack by apache.
the class VmwareStorageProcessor method createTemplateFromSnapshot.
@Override
public Answer createTemplateFromSnapshot(CopyCommand cmd) {
SnapshotObjectTO snapshot = (SnapshotObjectTO) cmd.getSrcTO();
TemplateObjectTO template = (TemplateObjectTO) cmd.getDestTO();
DataStoreTO imageStore = template.getDataStore();
String details;
String uniqeName = UUID.randomUUID().toString();
VmwareContext context = hostService.getServiceContext(cmd);
try {
if (!(imageStore instanceof NfsTO)) {
return new CopyCmdAnswer("Only support create template from snapshot, when the dest store is nfs");
}
NfsTO nfsSvr = (NfsTO) imageStore;
Ternary<String, Long, Long> result = createTemplateFromSnapshot(template.getPath(), uniqeName, nfsSvr.getUrl(), snapshot.getPath(), template.getId(), (long) cmd.getWait() * 1000, _nfsVersion);
TemplateObjectTO newTemplate = new TemplateObjectTO();
newTemplate.setPath(result.first());
newTemplate.setPhysicalSize(result.second());
newTemplate.setSize(result.third());
newTemplate.setFormat(ImageFormat.OVA);
newTemplate.setName(uniqeName);
return new CopyCmdAnswer(newTemplate);
} catch (Throwable e) {
if (e instanceof RemoteException) {
hostService.invalidateServiceContext(context);
}
s_logger.error("Unexpecpted exception ", e);
details = "create template from snapshot exception: " + VmwareHelper.getExceptionMessage(e);
return new CopyCmdAnswer(details);
}
}
use of org.apache.cloudstack.storage.command.CopyCmdAnswer in project cloudstack by apache.
the class SnapshotObject method processEvent.
@Override
public void processEvent(ObjectInDataStoreStateMachine.Event event, Answer answer) {
try {
SnapshotDataStoreVO snapshotStore = snapshotStoreDao.findByStoreSnapshot(getDataStore().getRole(), getDataStore().getId(), getId());
if (answer instanceof CreateObjectAnswer) {
SnapshotObjectTO snapshotTO = (SnapshotObjectTO) ((CreateObjectAnswer) answer).getData();
snapshotStore.setInstallPath(snapshotTO.getPath());
snapshotStoreDao.update(snapshotStore.getId(), snapshotStore);
} else if (answer instanceof CopyCmdAnswer) {
SnapshotObjectTO snapshotTO = (SnapshotObjectTO) ((CopyCmdAnswer) answer).getNewData();
snapshotStore.setInstallPath(snapshotTO.getPath());
if (snapshotTO.getPhysicalSize() != null) {
// For S3 delta snapshot, physical size is currently not set
snapshotStore.setPhysicalSize(snapshotTO.getPhysicalSize());
}
if (snapshotTO.getParentSnapshotPath() == null) {
snapshotStore.setParentSnapshotId(0L);
}
snapshotStoreDao.update(snapshotStore.getId(), snapshotStore);
// update side-effect of snapshot operation
if (snapshotTO.getVolume() != null && snapshotTO.getVolume().getPath() != null) {
VolumeVO vol = volumeDao.findByUuid(snapshotTO.getVolume().getUuid());
if (vol != null) {
s_logger.info("Update volume path change due to snapshot operation, volume " + vol.getId() + " path: " + vol.getPath() + "->" + snapshotTO.getVolume().getPath());
vol.setPath(snapshotTO.getVolume().getPath());
volumeDao.update(vol.getId(), vol);
} else {
s_logger.error("Cound't find the original volume with uuid: " + snapshotTO.getVolume().getUuid());
}
}
} else {
throw new CloudRuntimeException("Unknown answer: " + answer.getClass());
}
} catch (RuntimeException ex) {
if (event == ObjectInDataStoreStateMachine.Event.OperationFailed) {
objectInStoreMgr.deleteIfNotReady(this);
}
throw ex;
}
this.processEvent(event);
}
use of org.apache.cloudstack.storage.command.CopyCmdAnswer in project cloudstack by apache.
the class SnapshotServiceImpl method copySnapshotAsyncCallback.
protected Void copySnapshotAsyncCallback(AsyncCallbackDispatcher<SnapshotServiceImpl, CopyCommandResult> callback, CopySnapshotContext<CommandResult> context) {
CopyCommandResult result = callback.getResult();
SnapshotInfo destSnapshot = context.destSnapshot;
SnapshotObject srcSnapshot = (SnapshotObject) context.srcSnapshot;
AsyncCallFuture<SnapshotResult> future = context.future;
SnapshotResult snapResult = new SnapshotResult(destSnapshot, result.getAnswer());
if (result.isFailed()) {
try {
destSnapshot.processEvent(Event.OperationFailed);
//if backup snapshot failed, mark srcSnapshot in snapshot_store_ref as failed also
srcSnapshot.processEvent(Event.DestroyRequested);
srcSnapshot.processEvent(Event.OperationSuccessed);
srcSnapshot.processEvent(Snapshot.Event.OperationFailed);
_snapshotDao.remove(srcSnapshot.getId());
} catch (NoTransitionException e) {
s_logger.debug("Failed to update state: " + e.toString());
}
snapResult.setResult(result.getResult());
future.complete(snapResult);
return null;
}
try {
CopyCmdAnswer copyCmdAnswer = (CopyCmdAnswer) result.getAnswer();
destSnapshot.processEvent(Event.OperationSuccessed, copyCmdAnswer);
srcSnapshot.processEvent(Snapshot.Event.OperationSucceeded);
snapResult = new SnapshotResult(_snapshotFactory.getSnapshot(destSnapshot.getId(), destSnapshot.getDataStore()), copyCmdAnswer);
future.complete(snapResult);
} catch (Exception e) {
s_logger.debug("Failed to update snapshot state", e);
snapResult.setResult(e.toString());
future.complete(snapResult);
}
return null;
}
use of org.apache.cloudstack.storage.command.CopyCmdAnswer in project cloudstack by apache.
the class SnapshotTest method setUp.
@Test(priority = -1)
public void setUp() {
ComponentContext.initComponentsLifeCycle();
host = hostDao.findByGuid(this.getHostGuid());
if (host != null) {
dcId = host.getDataCenterId();
clusterId = host.getClusterId();
podId = host.getPodId();
imageStore = this.imageStoreDao.findByName(imageStoreName);
} else {
// create data center
DataCenterVO dc = new DataCenterVO(UUID.randomUUID().toString(), "test", "8.8.8.8", null, "10.0.0.1", null, "10.0.0.1/24", null, null, NetworkType.Basic, null, null, true, true, null, null);
dc = dcDao.persist(dc);
dcId = dc.getId();
// create pod
HostPodVO pod = new HostPodVO(UUID.randomUUID().toString(), dc.getId(), this.getHostGateway(), this.getHostCidr(), 8, "test");
pod = podDao.persist(pod);
podId = pod.getId();
// create xenserver cluster
ClusterVO cluster = new ClusterVO(dc.getId(), pod.getId(), "devcloud cluster");
cluster.setHypervisorType(this.getHypervisor().toString());
cluster.setClusterType(ClusterType.CloudManaged);
cluster.setManagedState(ManagedState.Managed);
cluster = clusterDao.persist(cluster);
clusterId = cluster.getId();
// create xenserver host
host = new HostVO(this.getHostGuid());
host.setName("devcloud xenserver host");
host.setType(Host.Type.Routing);
host.setPrivateIpAddress(this.getHostIp());
host.setDataCenterId(dc.getId());
host.setVersion("6.0.1");
host.setAvailable(true);
host.setSetup(true);
host.setPodId(podId);
host.setLastPinged(0);
host.setResourceState(ResourceState.Enabled);
host.setHypervisorType(this.getHypervisor());
host.setClusterId(cluster.getId());
host = hostDao.persist(host);
imageStore = new ImageStoreVO();
imageStore.setName(imageStoreName);
imageStore.setDataCenterId(dcId);
imageStore.setProviderName(DataStoreProvider.NFS_IMAGE);
imageStore.setRole(DataStoreRole.Image);
imageStore.setUrl(this.getSecondaryStorage());
imageStore.setUuid(UUID.randomUUID().toString());
imageStore.setProtocol("nfs");
imageStore = imageStoreDao.persist(imageStore);
}
image = new VMTemplateVO();
image.setTemplateType(TemplateType.USER);
image.setUrl(this.getTemplateUrl());
image.setUniqueName(UUID.randomUUID().toString());
image.setName(UUID.randomUUID().toString());
image.setPublicTemplate(true);
image.setFeatured(true);
image.setRequiresHvm(true);
image.setBits(64);
image.setFormat(Storage.ImageFormat.VHD);
image.setEnablePassword(true);
image.setEnableSshKey(true);
image.setGuestOSId(1);
image.setBootable(true);
image.setPrepopulate(true);
image.setCrossZones(true);
image.setExtractable(true);
image = imageDataDao.persist(image);
/*
* TemplateDataStoreVO templateStore = new TemplateDataStoreVO();
*
* templateStore.setDataStoreId(imageStore.getId());
* templateStore.setDownloadPercent(100);
* templateStore.setDownloadState(Status.DOWNLOADED);
* templateStore.setDownloadUrl(imageStore.getUrl());
* templateStore.setInstallPath(this.getImageInstallPath());
* templateStore.setTemplateId(image.getId());
* templateStoreDao.persist(templateStore);
*/
DataStore store = this.dataStoreMgr.getDataStore(imageStore.getId(), DataStoreRole.Image);
TemplateInfo template = templateFactory.getTemplate(image.getId(), DataStoreRole.Image);
DataObject templateOnStore = store.create(template);
TemplateObjectTO to = new TemplateObjectTO();
to.setPath(this.getImageInstallPath());
to.setFormat(ImageFormat.VHD);
to.setSize(1000L);
CopyCmdAnswer answer = new CopyCmdAnswer(to);
templateOnStore.processEvent(Event.CreateOnlyRequested);
templateOnStore.processEvent(Event.OperationSuccessed, answer);
}
Aggregations