Search in sources :

Example 11 with AttachAnswer

use of org.apache.cloudstack.storage.command.AttachAnswer in project cloudstack by apache.

the class NotAValidCommand method testStorageSubSystemCommand.

@Test
public void testStorageSubSystemCommand() {
    final DiskTO disk = Mockito.mock(DiskTO.class);
    final String vmName = "Test";
    final AttachCommand command = new AttachCommand(disk, vmName);
    final StorageSubsystemCommandHandler handler = Mockito.mock(StorageSubsystemCommandHandler.class);
    when(citrixResourceBase.getStorageHandler()).thenReturn(handler);
    when(handler.handleStorageCommands(command)).thenReturn(new AttachAnswer(disk));
    final CitrixRequestWrapper wrapper = CitrixRequestWrapper.getInstance();
    assertNotNull(wrapper);
    final Answer answer = wrapper.execute(command, citrixResourceBase);
    assertTrue(answer.getResult());
}
Also used : RebootAnswer(com.cloud.agent.api.RebootAnswer) CreateAnswer(com.cloud.agent.api.storage.CreateAnswer) AttachAnswer(org.apache.cloudstack.storage.command.AttachAnswer) Answer(com.cloud.agent.api.Answer) StorageSubsystemCommandHandler(com.cloud.storage.resource.StorageSubsystemCommandHandler) AttachCommand(org.apache.cloudstack.storage.command.AttachCommand) DiskTO(com.cloud.agent.api.to.DiskTO) AttachAnswer(org.apache.cloudstack.storage.command.AttachAnswer) Test(org.junit.Test)

Example 12 with AttachAnswer

use of org.apache.cloudstack.storage.command.AttachAnswer in project cloudstack by apache.

the class Ovm3StorageProcessor method attachDetach.

/**
     * Generic disk attach/detach.
     * @param cmd
     * @param vmName
     * @param disk
     * @param isAttach
     * @return
     */
private AttachAnswer attachDetach(Command cmd, String vmName, DiskTO disk, boolean isAttach) {
    Xen xen = new Xen(c);
    String doThis = (isAttach) ? "Attach" : "Dettach";
    LOGGER.debug(doThis + " volume type " + disk.getType() + "  " + vmName);
    String msg = "";
    String path = "";
    try {
        Xen.Vm vm = xen.getVmConfig(vmName);
        /* check running */
        if (vm == null) {
            msg = doThis + " can't find VM " + vmName;
            LOGGER.debug(msg);
            return new AttachAnswer(msg);
        }
        if (disk.getType() == Volume.Type.ISO) {
            path = getIsoPath(disk);
        } else if (disk.getType() == Volume.Type.DATADISK) {
            path = getVirtualDiskPath(disk, vm.getPrimaryPoolUuid());
        }
        if ("".equals(path)) {
            msg = doThis + " can't do anything with an empty path.";
            LOGGER.debug(msg);
            return new AttachAnswer(msg);
        }
        if (isAttach) {
            if (disk.getType() == Volume.Type.ISO) {
                vm.addIso(path);
            } else {
                vm.addDataDisk(path);
            }
        } else {
            if (!vm.removeDisk(path)) {
                msg = doThis + " failed for " + vmName + disk.getType() + "  was not attached " + path;
                LOGGER.debug(msg);
                return new AttachAnswer(msg);
            }
        }
        xen.configureVm(ovmObject.deDash(vm.getPrimaryPoolUuid()), vm.getVmUuid());
        return new AttachAnswer(disk);
    } catch (Ovm3ResourceException e) {
        msg = doThis + " failed for " + vmName + " " + e.getMessage();
        LOGGER.warn(msg, e);
        return new AttachAnswer(msg);
    }
}
Also used : Xen(com.cloud.hypervisor.ovm3.objects.Xen) Ovm3ResourceException(com.cloud.hypervisor.ovm3.objects.Ovm3ResourceException) AttachAnswer(org.apache.cloudstack.storage.command.AttachAnswer)

Example 13 with AttachAnswer

use of org.apache.cloudstack.storage.command.AttachAnswer in project cloudstack by apache.

the class XenServerStorageProcessor method attachIso.

@Override
public AttachAnswer attachIso(final AttachCommand cmd) {
    final DiskTO disk = cmd.getDisk();
    final DataTO data = disk.getData();
    final DataStoreTO store = data.getDataStore();
    String isoURL = null;
    if (store == null) {
        final TemplateObjectTO iso = (TemplateObjectTO) disk.getData();
        isoURL = iso.getName();
    } else {
        if (!(store instanceof NfsTO)) {
            s_logger.debug("Can't attach a iso which is not created on nfs: ");
            return new AttachAnswer("Can't attach a iso which is not created on nfs: ");
        }
        final NfsTO nfsStore = (NfsTO) store;
        isoURL = nfsStore.getUrl() + nfsStore.getPathSeparator() + data.getPath();
    }
    final String vmName = cmd.getVmName();
    try {
        final Connection conn = hypervisorResource.getConnection();
        VBD isoVBD = null;
        // Find the VM
        final VM vm = hypervisorResource.getVM(conn, vmName);
        // Find the ISO VDI
        final VDI isoVDI = hypervisorResource.getIsoVDIByURL(conn, vmName, isoURL);
        // Find the VM's CD-ROM VBD
        final Set<VBD> vbds = vm.getVBDs(conn);
        for (final VBD vbd : vbds) {
            final String userDevice = vbd.getUserdevice(conn);
            final Types.VbdType type = vbd.getType(conn);
            if (userDevice.equals("3") && type == Types.VbdType.CD) {
                isoVBD = vbd;
                break;
            }
        }
        if (isoVBD == null) {
            throw new CloudRuntimeException("Unable to find CD-ROM VBD for VM: " + vmName);
        } else {
            // If an ISO is already inserted, eject it
            if (!isoVBD.getEmpty(conn)) {
                isoVBD.eject(conn);
            }
            // Insert the new ISO
            isoVBD.insert(conn, isoVDI);
        }
        return new AttachAnswer(disk);
    } catch (final XenAPIException e) {
        s_logger.warn("Failed to attach iso" + ": " + e.toString(), e);
        return new AttachAnswer(e.toString());
    } catch (final Exception e) {
        s_logger.warn("Failed to attach iso" + ": " + e.toString(), e);
        return new AttachAnswer(e.toString());
    }
}
Also used : Types(com.xensource.xenapi.Types) PrimaryDataStoreTO(org.apache.cloudstack.storage.to.PrimaryDataStoreTO) DataStoreTO(com.cloud.agent.api.to.DataStoreTO) Connection(com.xensource.xenapi.Connection) XenAPIException(com.xensource.xenapi.Types.XenAPIException) NfsTO(com.cloud.agent.api.to.NfsTO) XenAPIException(com.xensource.xenapi.Types.XenAPIException) InternalErrorException(com.cloud.exception.InternalErrorException) XmlRpcException(org.apache.xmlrpc.XmlRpcException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) DataTO(com.cloud.agent.api.to.DataTO) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) VM(com.xensource.xenapi.VM) VBD(com.xensource.xenapi.VBD) VDI(com.xensource.xenapi.VDI) TemplateObjectTO(org.apache.cloudstack.storage.to.TemplateObjectTO) DiskTO(com.cloud.agent.api.to.DiskTO) AttachAnswer(org.apache.cloudstack.storage.command.AttachAnswer)

Example 14 with AttachAnswer

use of org.apache.cloudstack.storage.command.AttachAnswer in project cloudstack by apache.

the class VmwareStorageProcessor method attachIso.

private Answer attachIso(DiskTO disk, boolean isAttach, String vmName) {
    try {
        VmwareContext context = hostService.getServiceContext(null);
        VmwareHypervisorHost hyperHost = hostService.getHyperHost(context, null);
        VirtualMachineMO vmMo = hyperHost.findVmOnHyperHost(vmName);
        if (vmMo == null) {
            String msg = "Unable to find VM in vSphere to execute AttachIsoCommand, vmName: " + vmName;
            s_logger.error(msg);
            throw new Exception(msg);
        }
        TemplateObjectTO iso = (TemplateObjectTO) disk.getData();
        NfsTO nfsImageStore = (NfsTO) iso.getDataStore();
        String storeUrl = null;
        if (nfsImageStore != null) {
            storeUrl = nfsImageStore.getUrl();
        }
        if (storeUrl == null) {
            if (!iso.getName().equalsIgnoreCase("vmware-tools.iso")) {
                String msg = "ISO store root url is not found in AttachIsoCommand";
                s_logger.error(msg);
                throw new Exception(msg);
            } else {
                if (isAttach) {
                    vmMo.mountToolsInstaller();
                } else {
                    try {
                        if (!vmMo.unmountToolsInstaller()) {
                            return new AttachAnswer("Failed to unmount vmware-tools installer ISO as the corresponding CDROM device is locked by VM. Please unmount the CDROM device inside the VM and ret-try.");
                        }
                    } catch (Throwable e) {
                        vmMo.detachIso(null);
                    }
                }
                return new AttachAnswer(disk);
            }
        }
        ManagedObjectReference morSecondaryDs = prepareSecondaryDatastoreOnHost(storeUrl);
        String isoPath = nfsImageStore.getUrl() + File.separator + iso.getPath();
        if (!isoPath.startsWith(storeUrl)) {
            assert (false);
            String msg = "ISO path does not start with the secondary storage root";
            s_logger.error(msg);
            throw new Exception(msg);
        }
        int isoNameStartPos = isoPath.lastIndexOf('/');
        String isoFileName = isoPath.substring(isoNameStartPos + 1);
        String isoStorePathFromRoot = isoPath.substring(storeUrl.length(), isoNameStartPos);
        // TODO, check if iso is already attached, or if there is a previous
        // attachment
        DatastoreMO secondaryDsMo = new DatastoreMO(context, morSecondaryDs);
        String storeName = secondaryDsMo.getName();
        String isoDatastorePath = String.format("[%s] %s/%s", storeName, isoStorePathFromRoot, isoFileName);
        if (isAttach) {
            vmMo.attachIso(isoDatastorePath, morSecondaryDs, true, false);
        } else {
            vmMo.detachIso(isoDatastorePath);
        }
        return new AttachAnswer(disk);
    } catch (Throwable e) {
        if (e instanceof RemoteException) {
            s_logger.warn("Encounter remote exception to vCenter, invalidate VMware session context");
            hostService.invalidateServiceContext(null);
        }
        if (isAttach) {
            String msg = "AttachIsoCommand(attach) failed due to " + VmwareHelper.getExceptionMessage(e);
            msg = msg + " Also check if your guest os is a supported version";
            s_logger.error(msg, e);
            return new AttachAnswer(msg);
        } else {
            String msg = "AttachIsoCommand(detach) failed due to " + VmwareHelper.getExceptionMessage(e);
            msg = msg + " Also check if your guest os is a supported version";
            s_logger.warn(msg, e);
            return new AttachAnswer(msg);
        }
    }
}
Also used : VirtualMachineMO(com.cloud.hypervisor.vmware.mo.VirtualMachineMO) VmwareHypervisorHost(com.cloud.hypervisor.vmware.mo.VmwareHypervisorHost) NfsTO(com.cloud.agent.api.to.NfsTO) RemoteException(java.rmi.RemoteException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) DatastoreMO(com.cloud.hypervisor.vmware.mo.DatastoreMO) VmwareContext(com.cloud.hypervisor.vmware.util.VmwareContext) TemplateObjectTO(org.apache.cloudstack.storage.to.TemplateObjectTO) RemoteException(java.rmi.RemoteException) AttachAnswer(org.apache.cloudstack.storage.command.AttachAnswer) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference)

Aggregations

AttachAnswer (org.apache.cloudstack.storage.command.AttachAnswer)14 DiskTO (com.cloud.agent.api.to.DiskTO)11 DataStoreTO (com.cloud.agent.api.to.DataStoreTO)7 NfsTO (com.cloud.agent.api.to.NfsTO)7 TemplateObjectTO (org.apache.cloudstack.storage.to.TemplateObjectTO)7 Answer (com.cloud.agent.api.Answer)6 InternalErrorException (com.cloud.exception.InternalErrorException)6 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)6 PrimaryDataStoreTO (org.apache.cloudstack.storage.to.PrimaryDataStoreTO)6 DettachAnswer (org.apache.cloudstack.storage.command.DettachAnswer)5 DataTO (com.cloud.agent.api.to.DataTO)4 CopyCmdAnswer (org.apache.cloudstack.storage.command.CopyCmdAnswer)4 CreateObjectAnswer (org.apache.cloudstack.storage.command.CreateObjectAnswer)4 ResignatureAnswer (org.apache.cloudstack.storage.command.ResignatureAnswer)4 SnapshotAndCopyAnswer (org.apache.cloudstack.storage.command.SnapshotAndCopyAnswer)4 Connection (com.xensource.xenapi.Connection)3 XenAPIException (com.xensource.xenapi.Types.XenAPIException)3 VBD (com.xensource.xenapi.VBD)3 VDI (com.xensource.xenapi.VDI)3 VM (com.xensource.xenapi.VM)3