Search in sources :

Example 51 with XenAPIException

use of com.xensource.xenapi.Types.XenAPIException 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 52 with XenAPIException

use of com.xensource.xenapi.Types.XenAPIException in project cloudstack by apache.

the class XenServerStorageProcessor method deleteVolume.

@Override
public Answer deleteVolume(final DeleteCommand cmd) {
    final DataTO volume = cmd.getData();
    final Connection conn = hypervisorResource.getConnection();
    String errorMsg = null;
    try {
        final VDI vdi = VDI.getByUuid(conn, volume.getPath());
        for (VDI svdi : vdi.getSnapshots(conn)) {
            deleteVDI(conn, svdi);
        }
        deleteVDI(conn, vdi);
        return new Answer(null);
    } catch (final BadServerResponse e) {
        s_logger.debug("Failed to delete volume", e);
        errorMsg = e.toString();
    } catch (final XenAPIException e) {
        s_logger.debug("Failed to delete volume", e);
        errorMsg = e.toString();
    } catch (final XmlRpcException e) {
        s_logger.debug("Failed to delete volume", e);
        errorMsg = e.toString();
    }
    return new Answer(null, false, errorMsg);
}
Also used : 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) DataTO(com.cloud.agent.api.to.DataTO) BadServerResponse(com.xensource.xenapi.Types.BadServerResponse) Connection(com.xensource.xenapi.Connection) XenAPIException(com.xensource.xenapi.Types.XenAPIException) VDI(com.xensource.xenapi.VDI) XmlRpcException(org.apache.xmlrpc.XmlRpcException)

Example 53 with XenAPIException

use of com.xensource.xenapi.Types.XenAPIException in project cloudstack by apache.

the class XenServerStorageProcessor method destroySnapshotOnPrimaryStorageExceptThis.

protected boolean destroySnapshotOnPrimaryStorageExceptThis(final Connection conn, final String volumeUuid, final String avoidSnapshotUuid) {
    try {
        final VDI volume = getVDIbyUuid(conn, volumeUuid);
        if (volume == null) {
            throw new InternalErrorException("Could not destroy snapshot on volume " + volumeUuid + " due to can not find it");
        }
        // To avoid deleting snapshots which are still waiting in queue to get backed up.
        VDI avoidSnapshot = getVDIbyUuid(conn, avoidSnapshotUuid);
        if (avoidSnapshot == null) {
            throw new InternalErrorException("Could not find current snapshot " + avoidSnapshotUuid);
        }
        final Set<VDI> snapshots = volume.getSnapshots(conn);
        for (final VDI snapshot : snapshots) {
            try {
                if (!snapshot.getUuid(conn).equals(avoidSnapshotUuid) && snapshot.getSnapshotTime(conn).before(avoidSnapshot.getSnapshotTime(conn)) && snapshot.getVBDs(conn).isEmpty()) {
                    snapshot.destroy(conn);
                }
            } catch (final Exception e) {
                final String msg = "Destroying snapshot: " + snapshot + " on primary storage failed due to " + e.toString();
                s_logger.warn(msg, e);
            }
        }
        s_logger.debug("Successfully destroyed snapshot on volume: " + volumeUuid + " execept this current snapshot " + avoidSnapshotUuid);
        return true;
    } catch (final XenAPIException e) {
        final String msg = "Destroying snapshot on volume: " + volumeUuid + " execept this current snapshot " + avoidSnapshotUuid + " failed due to " + e.toString();
        s_logger.error(msg, e);
    } catch (final Exception e) {
        final String msg = "Destroying snapshot on volume: " + volumeUuid + " execept this current snapshot " + avoidSnapshotUuid + " failed due to " + e.toString();
        s_logger.warn(msg, e);
    }
    return false;
}
Also used : XenAPIException(com.xensource.xenapi.Types.XenAPIException) VDI(com.xensource.xenapi.VDI) InternalErrorException(com.cloud.exception.InternalErrorException) XenAPIException(com.xensource.xenapi.Types.XenAPIException) InternalErrorException(com.cloud.exception.InternalErrorException) XmlRpcException(org.apache.xmlrpc.XmlRpcException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException)

Example 54 with XenAPIException

use of com.xensource.xenapi.Types.XenAPIException in project cloudstack by apache.

the class CitrixScaleVmCommandWrapper method execute.

@Override
public Answer execute(final ScaleVmCommand command, final CitrixResourceBase citrixResourceBase) {
    final VirtualMachineTO vmSpec = command.getVirtualMachine();
    final String vmName = vmSpec.getName();
    try {
        final Connection conn = citrixResourceBase.getConnection();
        final Set<VM> vms = VM.getByNameLabel(conn, vmName);
        final Host host = Host.getByUuid(conn, citrixResourceBase.getHost().getUuid());
        // If DMC is not enable then don't execute this command.
        if (!citrixResourceBase.isDmcEnabled(conn, host)) {
            throw new CloudRuntimeException("Unable to scale the vm: " + vmName + " as DMC - Dynamic memory control is not enabled for the XenServer:" + citrixResourceBase.getHost().getUuid() + " ,check your license and hypervisor version.");
        }
        if (vms == null || vms.size() == 0) {
            s_logger.info("No running VM " + vmName + " exists on XenServer" + citrixResourceBase.getHost().getUuid());
            return new ScaleVmAnswer(command, false, "VM does not exist");
        }
        // stop vm which is running on this host or is in halted state
        final Iterator<VM> iter = vms.iterator();
        while (iter.hasNext()) {
            final VM vm = iter.next();
            final VM.Record vmr = vm.getRecord(conn);
            if (vmr.powerState == VmPowerState.HALTED || vmr.powerState == VmPowerState.RUNNING && !citrixResourceBase.isRefNull(vmr.residentOn) && !vmr.residentOn.getUuid(conn).equals(citrixResourceBase.getHost().getUuid())) {
                iter.remove();
            }
        }
        for (final VM vm : vms) {
            vm.getRecord(conn);
            try {
                citrixResourceBase.scaleVM(conn, vm, vmSpec, host);
            } catch (final Exception e) {
                final String msg = "Catch exception " + e.getClass().getName() + " when scaling VM:" + vmName + " due to " + e.toString();
                s_logger.debug(msg);
                return new ScaleVmAnswer(command, false, msg);
            }
        }
        final String msg = "scaling VM " + vmName + " is successful on host " + host;
        s_logger.debug(msg);
        return new ScaleVmAnswer(command, true, msg);
    } catch (final XenAPIException e) {
        final String msg = "Upgrade Vm " + vmName + " fail due to " + e.toString();
        s_logger.warn(msg, e);
        return new ScaleVmAnswer(command, false, msg);
    } catch (final XmlRpcException e) {
        final String msg = "Upgrade Vm " + vmName + " fail due to " + e.getMessage();
        s_logger.warn(msg, e);
        return new ScaleVmAnswer(command, false, msg);
    } catch (final Exception e) {
        final String msg = "Unable to upgrade " + vmName + " due to " + e.getMessage();
        s_logger.warn(msg, e);
        return new ScaleVmAnswer(command, false, msg);
    }
}
Also used : Connection(com.xensource.xenapi.Connection) XenAPIException(com.xensource.xenapi.Types.XenAPIException) Host(com.xensource.xenapi.Host) VirtualMachineTO(com.cloud.agent.api.to.VirtualMachineTO) XmlRpcException(org.apache.xmlrpc.XmlRpcException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) XenAPIException(com.xensource.xenapi.Types.XenAPIException) ScaleVmAnswer(com.cloud.agent.api.ScaleVmAnswer) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) VM(com.xensource.xenapi.VM) XmlRpcException(org.apache.xmlrpc.XmlRpcException)

Example 55 with XenAPIException

use of com.xensource.xenapi.Types.XenAPIException in project cloudstack by apache.

the class CitrixStopCommandWrapper method execute.

@Override
public Answer execute(final StopCommand command, final CitrixResourceBase citrixResourceBase) {
    final String vmName = command.getVmName();
    String platformstring = null;
    try {
        final Connection conn = citrixResourceBase.getConnection();
        final Set<VM> vms = VM.getByNameLabel(conn, vmName);
        // stop vm which is running on this host or is in halted state
        final Iterator<VM> iter = vms.iterator();
        while (iter.hasNext()) {
            final VM vm = iter.next();
            final VM.Record vmr = vm.getRecord(conn);
            if (vmr.powerState != VmPowerState.RUNNING) {
                continue;
            }
            if (citrixResourceBase.isRefNull(vmr.residentOn)) {
                continue;
            }
            if (vmr.residentOn.getUuid(conn).equals(citrixResourceBase.getHost().getUuid())) {
                continue;
            }
            iter.remove();
        }
        if (vms.size() == 0) {
            return new StopAnswer(command, "VM does not exist", true);
        }
        for (final VM vm : vms) {
            final VM.Record vmr = vm.getRecord(conn);
            platformstring = StringUtils.mapToString(vmr.platform);
            if (vmr.isControlDomain) {
                final String msg = "Tring to Shutdown control domain";
                s_logger.warn(msg);
                return new StopAnswer(command, msg, false);
            }
            if (vmr.powerState == VmPowerState.RUNNING && !citrixResourceBase.isRefNull(vmr.residentOn) && !vmr.residentOn.getUuid(conn).equals(citrixResourceBase.getHost().getUuid())) {
                final String msg = "Stop Vm " + vmName + " failed due to this vm is not running on this host: " + citrixResourceBase.getHost().getUuid() + " but host:" + vmr.residentOn.getUuid(conn);
                s_logger.warn(msg);
                return new StopAnswer(command, msg, platformstring, false);
            }
            if (command.checkBeforeCleanup() && vmr.powerState == VmPowerState.RUNNING) {
                final String msg = "Vm " + vmName + " is running on host and checkBeforeCleanup flag is set, so bailing out";
                s_logger.debug(msg);
                return new StopAnswer(command, msg, false);
            }
            s_logger.debug("9. The VM " + vmName + " is in Stopping state");
            try {
                if (vmr.powerState == VmPowerState.RUNNING) {
                    /* when stop a vm, set affinity to current xenserver */
                    vm.setAffinity(conn, vm.getResidentOn(conn));
                    if (citrixResourceBase.canBridgeFirewall()) {
                        final String result = citrixResourceBase.callHostPlugin(conn, "vmops", "destroy_network_rules_for_vm", "vmName", command.getVmName());
                        if (result == null || result.isEmpty() || !Boolean.parseBoolean(result)) {
                            s_logger.warn("Failed to remove  network rules for vm " + command.getVmName());
                        } else {
                            s_logger.info("Removed  network rules for vm " + command.getVmName());
                        }
                    }
                    citrixResourceBase.shutdownVM(conn, vm, vmName);
                }
            } catch (final Exception e) {
                final String msg = "Catch exception " + e.getClass().getName() + " when stop VM:" + command.getVmName() + " due to " + e.toString();
                s_logger.debug(msg);
                return new StopAnswer(command, msg, platformstring, false);
            } finally {
                try {
                    if (vm.getPowerState(conn) == VmPowerState.HALTED) {
                        Set<VGPU> vGPUs = null;
                        // Get updated GPU details
                        try {
                            vGPUs = vm.getVGPUs(conn);
                        } catch (final XenAPIException e2) {
                            s_logger.debug("VM " + vmName + " does not have GPU support.");
                        }
                        if (vGPUs != null && !vGPUs.isEmpty()) {
                            final HashMap<String, HashMap<String, VgpuTypesInfo>> groupDetails = citrixResourceBase.getGPUGroupDetails(conn);
                            command.setGpuDevice(new GPUDeviceTO(null, null, groupDetails));
                        }
                        final Set<VIF> vifs = vm.getVIFs(conn);
                        final List<Network> networks = new ArrayList<Network>();
                        for (final VIF vif : vifs) {
                            networks.add(vif.getNetwork(conn));
                        }
                        vm.destroy(conn);
                        final SR sr = citrixResourceBase.getISOSRbyVmName(conn, command.getVmName());
                        citrixResourceBase.removeSR(conn, sr);
                        // anymore
                        for (final Network network : networks) {
                            try {
                                if (network.getNameLabel(conn).startsWith("VLAN")) {
                                    citrixResourceBase.disableVlanNetwork(conn, network);
                                }
                            } catch (final Exception e) {
                            // network might be destroyed by other host
                            }
                        }
                        return new StopAnswer(command, "Stop VM " + vmName + " Succeed", platformstring, true);
                    }
                } catch (final Exception e) {
                    final String msg = "VM destroy failed in Stop " + vmName + " Command due to " + e.getMessage();
                    s_logger.warn(msg, e);
                } finally {
                    s_logger.debug("10. The VM " + vmName + " is in Stopped state");
                }
            }
        }
    } catch (final Exception e) {
        final String msg = "Stop Vm " + vmName + " fail due to " + e.toString();
        s_logger.warn(msg, e);
        return new StopAnswer(command, msg, platformstring, false);
    }
    return new StopAnswer(command, "Stop VM failed", platformstring, false);
}
Also used : HashMap(java.util.HashMap) Connection(com.xensource.xenapi.Connection) ArrayList(java.util.ArrayList) XenAPIException(com.xensource.xenapi.Types.XenAPIException) GPUDeviceTO(com.cloud.agent.api.to.GPUDeviceTO) XenAPIException(com.xensource.xenapi.Types.XenAPIException) VIF(com.xensource.xenapi.VIF) VM(com.xensource.xenapi.VM) Network(com.xensource.xenapi.Network) StopAnswer(com.cloud.agent.api.StopAnswer) VGPU(com.xensource.xenapi.VGPU) SR(com.xensource.xenapi.SR)

Aggregations

XenAPIException (com.xensource.xenapi.Types.XenAPIException)101 XmlRpcException (org.apache.xmlrpc.XmlRpcException)93 Connection (com.xensource.xenapi.Connection)56 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)53 Answer (com.cloud.agent.api.Answer)33 InternalErrorException (com.cloud.exception.InternalErrorException)31 SR (com.xensource.xenapi.SR)30 Host (com.xensource.xenapi.Host)29 Network (com.xensource.xenapi.Network)27 BadServerResponse (com.xensource.xenapi.Types.BadServerResponse)26 VDI (com.xensource.xenapi.VDI)25 IOException (java.io.IOException)25 HashMap (java.util.HashMap)25 AttachAnswer (org.apache.cloudstack.storage.command.AttachAnswer)25 Test (org.junit.Test)25 ConfigurationException (javax.naming.ConfigurationException)22 XsLocalNetwork (com.cloud.hypervisor.xenserver.resource.XsLocalNetwork)21 MalformedURLException (java.net.MalformedURLException)21 URISyntaxException (java.net.URISyntaxException)21 TimeoutException (java.util.concurrent.TimeoutException)21