use of org.apache.cloudstack.storage.command.CopyCmdAnswer in project cloudstack by apache.
the class LocalNfsSecondaryStorageResourceTest method testExecuteRequest.
@Test
public void testExecuteRequest() throws Exception {
TemplateObjectTO template = Mockito.mock(TemplateObjectTO.class);
NfsTO cacheStore = Mockito.mock(NfsTO.class);
Mockito.when(cacheStore.getUrl()).thenReturn("nfs://nfs2.lab.vmops.com/export/home/edison/");
SwiftTO swift = Mockito.mock(SwiftTO.class);
Mockito.when(swift.getEndPoint()).thenReturn("https://objects.dreamhost.com/auth");
Mockito.when(swift.getAccount()).thenReturn("cloudstack");
Mockito.when(swift.getUserName()).thenReturn("images");
Mockito.when(swift.getKey()).thenReturn("oxvELQaOD1U5_VyosGfA-wpZ7uBWEff-CUBGCM0u");
Mockito.when(template.getDataStore()).thenReturn(swift);
Mockito.when(template.getPath()).thenReturn("template/1/1/");
Mockito.when(template.isRequiresHvm()).thenReturn(true);
Mockito.when(template.getId()).thenReturn(1L);
Mockito.when(template.getFormat()).thenReturn(Storage.ImageFormat.VHD);
Mockito.when(template.getOrigUrl()).thenReturn("http://nfs1.lab.vmops.com/templates/test.bz2");
Mockito.when(template.getName()).thenReturn(UUID.randomUUID().toString());
Mockito.when(template.getObjectType()).thenReturn(DataObjectType.TEMPLATE);
DownloadCommand cmd = new DownloadCommand(template, 100000L);
cmd.setCacheStore(cacheStore);
DownloadAnswer answer = (DownloadAnswer) resource.executeRequest(cmd);
Assert.assertTrue(answer.getResult());
Mockito.when(template.getPath()).thenReturn(answer.getInstallPath());
Mockito.when(template.getDataStore()).thenReturn(swift);
//download swift:
Mockito.when(cacheStore.getRole()).thenReturn(DataStoreRole.ImageCache);
TemplateObjectTO destTemplate = Mockito.mock(TemplateObjectTO.class);
Mockito.when(destTemplate.getPath()).thenReturn("template/1/2");
Mockito.when(destTemplate.getDataStore()).thenReturn(cacheStore);
Mockito.when(destTemplate.getObjectType()).thenReturn(DataObjectType.TEMPLATE);
CopyCommand cpyCmd = new CopyCommand(template, destTemplate, 10000, true);
CopyCmdAnswer copyCmdAnswer = (CopyCmdAnswer) resource.executeRequest(cpyCmd);
Assert.assertTrue(copyCmdAnswer.getResult());
//list template
ListTemplateCommand listCmd = new ListTemplateCommand(swift);
ListTemplateAnswer listAnswer = (ListTemplateAnswer) resource.executeRequest(listCmd);
Assert.assertTrue(listAnswer.getTemplateInfo().size() > 0);
}
use of org.apache.cloudstack.storage.command.CopyCmdAnswer in project cloudstack by apache.
the class Ovm3StorageProcessorTest method copyCommandTemplateToTemplateTest.
/**
* Copy template from secondary to primary template
*
* @throws ConfigurationException
*/
@Test
public void copyCommandTemplateToTemplateTest() throws ConfigurationException {
con = prepare();
con.setMethodResponse("storage_plugin_mount", results.simpleResponseWrapWrapper(storageplugin.getNfsFileSystemInfo()));
/*
* because the template requires a reference to the name for the uuid...
* -sigh-
*/
String templateid = ovmObject.newUuid();
String targetid = ovmObject.newUuid();
String templatedir = "template/tmpl/1/11" + templateid + ".raw";
String storeUrl = "nfs://" + linux.getRemoteHost() + "/" + linux.getRemoteDir();
TemplateObjectTO src = template(templateid, linux.getRepoId(), storeUrl, templatedir);
TemplateObjectTO dest = template(targetid, linux.getRepoId(), linux.getRepoId(), linux.getTemplatesDir());
CopyCommand copy = new CopyCommand(src, dest, 0, true);
CopyCmdAnswer ra = (CopyCmdAnswer) hypervisor.executeRequest(copy);
TemplateObjectTO vol = (TemplateObjectTO) ra.getNewData();
results.basicStringTest(vol.getUuid(), targetid);
results.basicStringTest(vol.getPath(), targetid);
results.basicBooleanTest(ra.getResult());
}
use of org.apache.cloudstack.storage.command.CopyCmdAnswer in project cloudstack by apache.
the class Ovm3StorageProcessorTest method copyCommandTemplateToVolumeTest.
/**
* Copy template from primary to primary volume
*
* @throws ConfigurationException
*/
@Test
public void copyCommandTemplateToVolumeTest() throws ConfigurationException {
con = prepare();
String voluuid = ovmObject.newUuid();
TemplateObjectTO src = template(ovmObject.newUuid(), ovmObject.newUuid(), linux.getRepoId(), linux.getTemplatesDir());
VolumeObjectTO dest = volume(voluuid, ovmObject.newUuid(), linux.getRepoId(), linux.getVirtualDisksDir());
CopyCommand copy = new CopyCommand(src, dest, 0, true);
CopyCmdAnswer ra = (CopyCmdAnswer) hypervisor.executeRequest(copy);
VolumeObjectTO vol = (VolumeObjectTO) ra.getNewData();
results.basicStringTest(vol.getUuid(), voluuid);
results.basicStringTest(vol.getPath(), voluuid);
results.basicBooleanTest(ra.getResult());
}
use of org.apache.cloudstack.storage.command.CopyCmdAnswer in project cloudstack by apache.
the class XenServerStorageProcessor method directDownloadHttpTemplate.
protected Answer directDownloadHttpTemplate(final CopyCommand cmd, final DecodedDataObject srcObj, final DecodedDataObject destObj) {
final Connection conn = hypervisorResource.getConnection();
SR poolsr = null;
VDI vdi = null;
boolean result = false;
try {
if (destObj.getPath() == null) {
//need to create volume at first
}
vdi = VDI.getByUuid(conn, destObj.getPath());
if (vdi == null) {
throw new CloudRuntimeException("can't find volume: " + destObj.getPath());
}
final String destStoreUuid = destObj.getStore().getUuid();
final Set<SR> srs = SR.getByNameLabel(conn, destStoreUuid);
if (srs.size() != 1) {
throw new CloudRuntimeException("storage uuid: " + destStoreUuid + " is not unique");
}
poolsr = srs.iterator().next();
final VDI.Record vdir = vdi.getRecord(conn);
final String vdiLocation = vdir.location;
String pbdLocation = null;
if (destObj.getStore().getScheme().equalsIgnoreCase(DataStoreProtocol.NFS.toString())) {
pbdLocation = "/run/sr-mount/" + poolsr.getUuid(conn);
} else {
final Set<PBD> pbds = poolsr.getPBDs(conn);
if (pbds.size() != 1) {
throw new CloudRuntimeException("Don't how to handle multiple pbds:" + pbds.size() + " for sr: " + poolsr.getUuid(conn));
}
final PBD pbd = pbds.iterator().next();
final Map<String, String> deviceCfg = pbd.getDeviceConfig(conn);
pbdLocation = deviceCfg.get("location");
}
if (pbdLocation == null) {
throw new CloudRuntimeException("Can't get pbd location");
}
final String vdiPath = pbdLocation + "/" + vdiLocation + ".vhd";
//download a url into vdipath
//downloadHttpToLocalFile(vdiPath, template.getPath());
hypervisorResource.callHostPlugin(conn, "storagePlugin", "downloadTemplateFromUrl", "destPath", vdiPath, "srcUrl", srcObj.getPath());
result = true;
//return new CopyCmdAnswer(cmd, vdi.getUuid(conn));
} catch (final BadServerResponse e) {
s_logger.debug("Failed to download template", e);
} catch (final XenAPIException e) {
s_logger.debug("Failed to download template", e);
} catch (final XmlRpcException e) {
s_logger.debug("Failed to download template", e);
} catch (final Exception e) {
s_logger.debug("Failed to download template", e);
} finally {
if (!result && vdi != null) {
try {
vdi.destroy(conn);
} catch (final BadServerResponse e) {
s_logger.debug("Failed to cleanup newly created vdi");
} catch (final XenAPIException e) {
s_logger.debug("Failed to cleanup newly created vdi");
} catch (final XmlRpcException e) {
s_logger.debug("Failed to cleanup newly created vdi");
}
}
}
return new Answer(cmd, false, "Failed to download template");
}
use of org.apache.cloudstack.storage.command.CopyCmdAnswer in project cloudstack by apache.
the class XenServerStorageProcessor method createTemplateFromVolume.
@Override
public Answer createTemplateFromVolume(final CopyCommand cmd) {
final Connection conn = hypervisorResource.getConnection();
final VolumeObjectTO volume = (VolumeObjectTO) cmd.getSrcTO();
final TemplateObjectTO template = (TemplateObjectTO) cmd.getDestTO();
final NfsTO destStore = (NfsTO) cmd.getDestTO().getDataStore();
final int wait = cmd.getWait();
final String secondaryStoragePoolURL = destStore.getUrl();
final String volumeUUID = volume.getPath();
final String userSpecifiedName = template.getName();
String details = null;
SR tmpltSR = null;
boolean result = false;
String secondaryStorageMountPath = null;
String installPath = null;
try {
final URI uri = new URI(secondaryStoragePoolURL);
secondaryStorageMountPath = uri.getHost() + ":" + uri.getPath();
installPath = template.getPath();
if (!hypervisorResource.createSecondaryStorageFolder(conn, secondaryStorageMountPath, installPath)) {
details = " Filed to create folder " + installPath + " in secondary storage";
s_logger.warn(details);
return new CopyCmdAnswer(details);
}
final VDI vol = getVDIbyUuid(conn, volumeUUID);
// create template SR
final URI tmpltURI = new URI(secondaryStoragePoolURL + "/" + installPath);
tmpltSR = hypervisorResource.createNfsSRbyURI(conn, tmpltURI, false);
// copy volume to template SR
final VDI tmpltVDI = hypervisorResource.cloudVDIcopy(conn, vol, tmpltSR, wait);
// scan makes XenServer pick up VDI physicalSize
tmpltSR.scan(conn);
if (userSpecifiedName != null) {
tmpltVDI.setNameLabel(conn, userSpecifiedName);
}
final String tmpltUUID = tmpltVDI.getUuid(conn);
final String tmpltFilename = tmpltUUID + ".vhd";
final long virtualSize = tmpltVDI.getVirtualSize(conn);
final long physicalSize = tmpltVDI.getPhysicalUtilisation(conn);
// create the template.properties file
final String templatePath = secondaryStorageMountPath + "/" + installPath;
result = hypervisorResource.postCreatePrivateTemplate(conn, templatePath, tmpltFilename, tmpltUUID, userSpecifiedName, null, physicalSize, virtualSize, template.getId());
if (!result) {
throw new CloudRuntimeException("Could not create the template.properties file on secondary storage dir: " + tmpltURI);
}
installPath = installPath + "/" + tmpltFilename;
hypervisorResource.removeSR(conn, tmpltSR);
tmpltSR = null;
final TemplateObjectTO newTemplate = new TemplateObjectTO();
newTemplate.setPath(installPath);
newTemplate.setFormat(ImageFormat.VHD);
newTemplate.setSize(virtualSize);
newTemplate.setPhysicalSize(physicalSize);
newTemplate.setName(tmpltUUID);
final CopyCmdAnswer answer = new CopyCmdAnswer(newTemplate);
return answer;
} catch (final Exception e) {
if (tmpltSR != null) {
hypervisorResource.removeSR(conn, tmpltSR);
}
if (secondaryStorageMountPath != null) {
hypervisorResource.deleteSecondaryStorageFolder(conn, secondaryStorageMountPath, installPath);
}
details = "Creating template from volume " + volumeUUID + " failed due to " + e.toString();
s_logger.error(details, e);
}
return new CopyCmdAnswer(details);
}
Aggregations