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);
}
}
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);
}
}
}
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);
}
}
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);
}
}
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);
}
}
Aggregations