Search in sources :

Example 1 with MigrateAnswer

use of com.cloud.agent.api.MigrateAnswer in project cloudstack by apache.

the class VmwareResource method execute.

protected Answer execute(MigrateCommand cmd) {
    final String vmName = cmd.getVmName();
    try {
        VmwareHypervisorHost hyperHost = getHyperHost(getServiceContext());
        ManagedObjectReference morDc = hyperHost.getHyperHostDatacenter();
        // find VM through datacenter (VM is not at the target host yet)
        VirtualMachineMO vmMo = hyperHost.findVmOnPeerHyperHost(vmName);
        if (vmMo == null) {
            String msg = "VM " + vmName + " does not exist in VMware datacenter";
            s_logger.error(msg);
            throw new Exception(msg);
        }
        VmwareHypervisorHost destHyperHost = getTargetHyperHost(new DatacenterMO(hyperHost.getContext(), morDc), cmd.getDestinationIp());
        ManagedObjectReference morTargetPhysicalHost = destHyperHost.findMigrationTarget(vmMo);
        if (morTargetPhysicalHost == null) {
            throw new Exception("Unable to find a target capable physical host");
        }
        if (!vmMo.migrate(destHyperHost.getHyperHostOwnerResourcePool(), morTargetPhysicalHost)) {
            throw new Exception("Migration failed");
        }
        return new MigrateAnswer(cmd, true, "migration succeeded", null);
    } catch (Throwable e) {
        return new MigrateAnswer(cmd, false, createLogMessageException(e, cmd), null);
    }
}
Also used : MigrateAnswer(com.cloud.agent.api.MigrateAnswer) VirtualMachineMO(com.cloud.hypervisor.vmware.mo.VirtualMachineMO) VmwareHypervisorHost(com.cloud.hypervisor.vmware.mo.VmwareHypervisorHost) ConnectException(java.net.ConnectException) IOException(java.io.IOException) RemoteException(java.rmi.RemoteException) InternalErrorException(com.cloud.exception.InternalErrorException) CloudException(com.cloud.exception.CloudException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ConfigurationException(javax.naming.ConfigurationException) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference) DatacenterMO(com.cloud.hypervisor.vmware.mo.DatacenterMO)

Example 2 with MigrateAnswer

use of com.cloud.agent.api.MigrateAnswer in project cloudstack by apache.

the class Ovm3VmSupport method execute.

/* do migrations of VMs in a simple way just inside a cluster for now */
public MigrateAnswer execute(final MigrateCommand cmd) {
    final String vmName = cmd.getVmName();
    String destUuid = cmd.getHostGuid();
    String destIp = cmd.getDestinationIp();
    State state = State.Error;
    /*
         * TODO: figure out non pooled migration, works from CLI but not from
         * the agent... perhaps pause the VM and then migrate it ? for now just
         * stop the VM.
         */
    String msg = "Migrating " + vmName + " to " + destIp;
    LOGGER.info(msg);
    if (!config.getAgentInOvm3Cluster() && !config.getAgentInOvm3Pool()) {
        try {
            Xen xen = new Xen(c);
            Xen.Vm vm = xen.getRunningVmConfig(vmName);
            HostVO destHost = resourceMgr.findHostByGuid(destUuid);
            if (destHost == null) {
                msg = "Unable to find migration target host in DB " + destUuid + " with ip " + destIp;
                LOGGER.info(msg);
                return new MigrateAnswer(cmd, false, msg, null);
            }
            xen.stopVm(ovmObject.deDash(vm.getVmRootDiskPoolId()), vm.getVmUuid());
            msg = destHost.toString();
            state = State.Stopping;
            return new MigrateAnswer(cmd, false, msg, null);
        } catch (Ovm3ResourceException e) {
            msg = "Unpooled VM Migrate of " + vmName + " to " + destUuid + " failed due to: " + e.getMessage();
            LOGGER.debug(msg, e);
            return new MigrateAnswer(cmd, false, msg, null);
        } finally {
            /* shouldn't we just reinitialize completely as a last resort ? */
            hypervisor.setVmState(vmName, state);
        }
    } else {
        try {
            Xen xen = new Xen(c);
            Xen.Vm vm = xen.getRunningVmConfig(vmName);
            if (vm == null) {
                state = State.Stopped;
                msg = vmName + " is no running on " + config.getAgentHostname();
                return new MigrateAnswer(cmd, false, msg, null);
            }
            /* not a storage migration!!! */
            xen.migrateVm(ovmObject.deDash(vm.getVmRootDiskPoolId()), vm.getVmUuid(), destIp);
            state = State.Stopping;
            msg = "Migration of " + vmName + " successfull";
            return new MigrateAnswer(cmd, true, msg, null);
        } catch (Ovm3ResourceException e) {
            msg = "Pooled VM Migrate" + ": Migration of " + vmName + " to " + destIp + " failed due to " + e.getMessage();
            LOGGER.debug(msg, e);
            return new MigrateAnswer(cmd, false, msg, null);
        } finally {
            hypervisor.setVmState(vmName, state);
        }
    }
}
Also used : Xen(com.cloud.hypervisor.ovm3.objects.Xen) MigrateAnswer(com.cloud.agent.api.MigrateAnswer) State(com.cloud.vm.VirtualMachine.State) Ovm3ResourceException(com.cloud.hypervisor.ovm3.objects.Ovm3ResourceException) HostVO(com.cloud.host.HostVO)

Example 3 with MigrateAnswer

use of com.cloud.agent.api.MigrateAnswer in project cloudstack by apache.

the class OvmResourceBase method execute.

protected MigrateAnswer execute(final MigrateCommand cmd) {
    final String vmName = cmd.getVmName();
    try {
        OvmVm.Details vm = OvmVm.getDetails(_conn, vmName);
        String destIp = cmd.getDestinationIp();
        OvmVm.migrate(_conn, vmName, destIp);
        cleanup(vm);
        return new MigrateAnswer(cmd, true, "migration succeeded", null);
    } catch (Exception e) {
        String msg = "Catch Exception " + e.getClass().getName() + ": Migration failed due to " + e.toString();
        s_logger.debug(msg, e);
        return new MigrateAnswer(cmd, false, msg, null);
    }
}
Also used : MigrateAnswer(com.cloud.agent.api.MigrateAnswer) OvmVm(com.cloud.ovm.object.OvmVm) XmlRpcException(org.apache.xmlrpc.XmlRpcException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) ConfigurationException(javax.naming.ConfigurationException)

Example 4 with MigrateAnswer

use of com.cloud.agent.api.MigrateAnswer in project cloudstack by apache.

the class CitrixMigrateCommandWrapper method execute.

@Override
public Answer execute(final MigrateCommand command, final CitrixResourceBase citrixResourceBase) {
    final Connection conn = citrixResourceBase.getConnection();
    final String vmName = command.getVmName();
    final String dstHostIpAddr = command.getDestinationIp();
    try {
        final Set<VM> vms = VM.getByNameLabel(conn, vmName);
        final Set<Host> hosts = Host.getAll(conn);
        Host dsthost = null;
        if (hosts != null) {
            for (final Host host : hosts) {
                if (host.getAddress(conn).equals(dstHostIpAddr)) {
                    dsthost = host;
                    break;
                }
            }
        }
        if (dsthost == null) {
            final String msg = "Migration failed due to unable to find host " + dstHostIpAddr + " in XenServer pool " + citrixResourceBase.getHost().getPool();
            s_logger.warn(msg);
            return new MigrateAnswer(command, false, msg, null);
        }
        for (final VM vm : vms) {
            final Set<VBD> vbds = vm.getVBDs(conn);
            for (final VBD vbd : vbds) {
                final VBD.Record vbdRec = vbd.getRecord(conn);
                if (vbdRec.type.equals(Types.VbdType.CD) && !vbdRec.empty) {
                    vbd.eject(conn);
                    // for config drive vbd destroy the vbd.
                    if (!vbdRec.userdevice.equals(citrixResourceBase._attachIsoDeviceNum)) {
                        if (vbdRec.currentlyAttached) {
                            vbd.destroy(conn);
                        }
                    }
                    continue;
                }
            }
            citrixResourceBase.migrateVM(conn, dsthost, vm, vmName);
            vm.setAffinity(conn, dsthost);
            destroyMigratedVmNetworkRulesOnSourceHost(command, citrixResourceBase, conn, dsthost);
        }
        // The iso can be attached to vm only once the vm is (present in the host) migrated.
        // Attach the config drive iso device to VM
        VM vm = vms.iterator().next();
        if (!citrixResourceBase.attachConfigDriveIsoToVm(conn, vm)) {
            s_logger.debug("Config drive ISO attach failed after migration for vm " + vmName);
        }
        return new MigrateAnswer(command, true, "migration succeeded", null);
    } catch (final Exception e) {
        s_logger.warn(e.getMessage(), e);
        return new MigrateAnswer(command, false, e.getMessage(), null);
    }
}
Also used : MigrateAnswer(com.cloud.agent.api.MigrateAnswer) VM(com.xensource.xenapi.VM) Connection(com.xensource.xenapi.Connection) VBD(com.xensource.xenapi.VBD) Host(com.xensource.xenapi.Host) XmlRpcException(org.apache.xmlrpc.XmlRpcException) XenAPIException(com.xensource.xenapi.Types.XenAPIException)

Example 5 with MigrateAnswer

use of com.cloud.agent.api.MigrateAnswer in project cosmic by MissionCriticalCloud.

the class CitrixMigrateCommandWrapper method execute.

@Override
public Answer execute(final MigrateCommand command, final CitrixResourceBase citrixResourceBase) {
    final Connection conn = citrixResourceBase.getConnection();
    final String vmName = command.getVmName();
    final String dstHostIpAddr = command.getDestinationIp();
    try {
        final Set<VM> vms = VM.getByNameLabel(conn, vmName);
        final Set<Host> hosts = Host.getAll(conn);
        Host dsthost = null;
        if (hosts != null) {
            for (final Host host : hosts) {
                if (host.getAddress(conn).equals(dstHostIpAddr)) {
                    dsthost = host;
                    break;
                }
            }
        }
        if (dsthost == null) {
            final String msg = "Migration failed due to unable to find host " + dstHostIpAddr + " in XenServer pool " + citrixResourceBase.getHost().getPool();
            s_logger.warn(msg);
            return new MigrateAnswer(command, false, msg, null);
        }
        for (final VM vm : vms) {
            final Set<VBD> vbds = vm.getVBDs(conn);
            for (final VBD vbd : vbds) {
                final VBD.Record vbdRec = vbd.getRecord(conn);
                if (vbdRec.type.equals(Types.VbdType.CD) && !vbdRec.empty) {
                    vbd.eject(conn);
                    // for config drive vbd destroy the vbd.
                    if (!vbdRec.userdevice.equals(citrixResourceBase._attachIsoDeviceNum)) {
                        if (vbdRec.currentlyAttached) {
                            vbd.destroy(conn);
                        }
                    }
                    continue;
                }
            }
            citrixResourceBase.migrateVM(conn, dsthost, vm, vmName);
            vm.setAffinity(conn, dsthost);
        }
        // Attach the config drive iso device to VM
        if (!citrixResourceBase.attachConfigDriveToMigratedVm(conn, vmName, dstHostIpAddr)) {
            s_logger.debug("Config drive ISO attach failed after migration for vm " + vmName);
        }
        return new MigrateAnswer(command, true, "migration succeeded", null);
    } catch (final Exception e) {
        s_logger.warn(e.getMessage(), e);
        return new MigrateAnswer(command, false, e.getMessage(), null);
    }
}
Also used : MigrateAnswer(com.cloud.agent.api.MigrateAnswer) VM(com.xensource.xenapi.VM) Connection(com.xensource.xenapi.Connection) VBD(com.xensource.xenapi.VBD) Host(com.xensource.xenapi.Host)

Aggregations

MigrateAnswer (com.cloud.agent.api.MigrateAnswer)11 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)5 IOException (java.io.IOException)4 ConfigurationException (javax.naming.ConfigurationException)4 URISyntaxException (java.net.URISyntaxException)3 ExecutionException (java.util.concurrent.ExecutionException)3 Connect (org.libvirt.Connect)3 Domain (org.libvirt.Domain)3 LibvirtException (org.libvirt.LibvirtException)3 MigrateDiskInfo (com.cloud.agent.api.MigrateCommand.MigrateDiskInfo)2 InternalErrorException (com.cloud.exception.InternalErrorException)2 VifDriver (com.cloud.hypervisor.kvm.resource.VifDriver)2 MockHost (com.cloud.simulator.MockHost)2 MockVMVO (com.cloud.simulator.MockVMVO)2 Ternary (com.cloud.utils.Ternary)2 State (com.cloud.vm.VirtualMachine.State)2 Connection (com.xensource.xenapi.Connection)2 Host (com.xensource.xenapi.Host)2 VBD (com.xensource.xenapi.VBD)2 VM (com.xensource.xenapi.VM)2