Search in sources :

Example 56 with CopyCmdAnswer

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);
}
Also used : SwiftTO(com.cloud.agent.api.to.SwiftTO) DownloadCommand(org.apache.cloudstack.storage.command.DownloadCommand) CopyCommand(org.apache.cloudstack.storage.command.CopyCommand) ListTemplateAnswer(com.cloud.agent.api.storage.ListTemplateAnswer) ListTemplateCommand(com.cloud.agent.api.storage.ListTemplateCommand) TemplateObjectTO(org.apache.cloudstack.storage.to.TemplateObjectTO) DownloadAnswer(com.cloud.agent.api.storage.DownloadAnswer) NfsTO(com.cloud.agent.api.to.NfsTO) CopyCmdAnswer(org.apache.cloudstack.storage.command.CopyCmdAnswer) Test(org.junit.Test)

Example 57 with CopyCmdAnswer

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());
}
Also used : CopyCommand(org.apache.cloudstack.storage.command.CopyCommand) TemplateObjectTO(org.apache.cloudstack.storage.to.TemplateObjectTO) CopyCmdAnswer(org.apache.cloudstack.storage.command.CopyCmdAnswer) ConnectionTest(com.cloud.hypervisor.ovm3.objects.ConnectionTest) LinuxTest(com.cloud.hypervisor.ovm3.objects.LinuxTest) Test(org.junit.Test) XenTest(com.cloud.hypervisor.ovm3.objects.XenTest) StoragePluginTest(com.cloud.hypervisor.ovm3.objects.StoragePluginTest) Ovm3SupportTest(com.cloud.hypervisor.ovm3.support.Ovm3SupportTest) XmlTestResultTest(com.cloud.hypervisor.ovm3.objects.XmlTestResultTest) Ovm3ConfigurationTest(com.cloud.hypervisor.ovm3.resources.helpers.Ovm3ConfigurationTest)

Example 58 with CopyCmdAnswer

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());
}
Also used : CopyCommand(org.apache.cloudstack.storage.command.CopyCommand) VolumeObjectTO(org.apache.cloudstack.storage.to.VolumeObjectTO) TemplateObjectTO(org.apache.cloudstack.storage.to.TemplateObjectTO) CopyCmdAnswer(org.apache.cloudstack.storage.command.CopyCmdAnswer) ConnectionTest(com.cloud.hypervisor.ovm3.objects.ConnectionTest) LinuxTest(com.cloud.hypervisor.ovm3.objects.LinuxTest) Test(org.junit.Test) XenTest(com.cloud.hypervisor.ovm3.objects.XenTest) StoragePluginTest(com.cloud.hypervisor.ovm3.objects.StoragePluginTest) Ovm3SupportTest(com.cloud.hypervisor.ovm3.support.Ovm3SupportTest) XmlTestResultTest(com.cloud.hypervisor.ovm3.objects.XmlTestResultTest) Ovm3ConfigurationTest(com.cloud.hypervisor.ovm3.resources.helpers.Ovm3ConfigurationTest)

Example 59 with CopyCmdAnswer

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");
}
Also used : BadServerResponse(com.xensource.xenapi.Types.BadServerResponse) Connection(com.xensource.xenapi.Connection) XenAPIException(com.xensource.xenapi.Types.XenAPIException) XenAPIException(com.xensource.xenapi.Types.XenAPIException) InternalErrorException(com.cloud.exception.InternalErrorException) XmlRpcException(org.apache.xmlrpc.XmlRpcException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) CreateObjectAnswer(org.apache.cloudstack.storage.command.CreateObjectAnswer) Answer(com.cloud.agent.api.Answer) ResignatureAnswer(org.apache.cloudstack.storage.command.ResignatureAnswer) IntroduceObjectAnswer(org.apache.cloudstack.storage.command.IntroduceObjectAnswer) AttachPrimaryDataStoreAnswer(org.apache.cloudstack.storage.command.AttachPrimaryDataStoreAnswer) DettachAnswer(org.apache.cloudstack.storage.command.DettachAnswer) SnapshotAndCopyAnswer(org.apache.cloudstack.storage.command.SnapshotAndCopyAnswer) CopyCmdAnswer(org.apache.cloudstack.storage.command.CopyCmdAnswer) AttachAnswer(org.apache.cloudstack.storage.command.AttachAnswer) PBD(com.xensource.xenapi.PBD) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) VDI(com.xensource.xenapi.VDI) XmlRpcException(org.apache.xmlrpc.XmlRpcException) SR(com.xensource.xenapi.SR)

Example 60 with CopyCmdAnswer

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);
}
Also used : Connection(com.xensource.xenapi.Connection) NfsTO(com.cloud.agent.api.to.NfsTO) URI(java.net.URI) XenAPIException(com.xensource.xenapi.Types.XenAPIException) InternalErrorException(com.cloud.exception.InternalErrorException) XmlRpcException(org.apache.xmlrpc.XmlRpcException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) VolumeObjectTO(org.apache.cloudstack.storage.to.VolumeObjectTO) VDI(com.xensource.xenapi.VDI) TemplateObjectTO(org.apache.cloudstack.storage.to.TemplateObjectTO) CopyCmdAnswer(org.apache.cloudstack.storage.command.CopyCmdAnswer) SR(com.xensource.xenapi.SR)

Aggregations

CopyCmdAnswer (org.apache.cloudstack.storage.command.CopyCmdAnswer)78 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)49 NfsTO (com.cloud.agent.api.to.NfsTO)40 DataStoreTO (com.cloud.agent.api.to.DataStoreTO)38 DataTO (com.cloud.agent.api.to.DataTO)37 VolumeObjectTO (org.apache.cloudstack.storage.to.VolumeObjectTO)37 TemplateObjectTO (org.apache.cloudstack.storage.to.TemplateObjectTO)34 PrimaryDataStoreTO (org.apache.cloudstack.storage.to.PrimaryDataStoreTO)30 InternalErrorException (com.cloud.exception.InternalErrorException)27 SnapshotObjectTO (org.apache.cloudstack.storage.to.SnapshotObjectTO)25 Connection (com.xensource.xenapi.Connection)18 XenAPIException (com.xensource.xenapi.Types.XenAPIException)18 XmlRpcException (org.apache.xmlrpc.XmlRpcException)18 SR (com.xensource.xenapi.SR)17 VDI (com.xensource.xenapi.VDI)17 URI (java.net.URI)16 UnsupportedEncodingException (java.io.UnsupportedEncodingException)10 ConfigurationException (javax.naming.ConfigurationException)10 IOException (java.io.IOException)9 CopyCommand (org.apache.cloudstack.storage.command.CopyCommand)9