diff --git a/plugins/hypervisors/xenserver/src/com/cloud/hypervisor/xenserver/resource/CitrixResourceBase.java b/plugins/hypervisors/xenserver/src/com/cloud/hypervisor/xenserver/resource/CitrixResourceBase.java index 30c96ee5273..2f7ba8b8398 100644 --- a/plugins/hypervisors/xenserver/src/com/cloud/hypervisor/xenserver/resource/CitrixResourceBase.java +++ b/plugins/hypervisors/xenserver/src/com/cloud/hypervisor/xenserver/resource/CitrixResourceBase.java @@ -1013,7 +1013,7 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe } } - protected Network getNetwork(final Connection conn, final NicTO nic) throws XenAPIException, XmlRpcException { + public Network getNetwork(final Connection conn, final NicTO nic) throws XenAPIException, XmlRpcException { final String name = nic.getName(); final XsLocalNetwork network = getNativeNetworkForTraffic(conn, nic.getType(), name); if (network == null) { @@ -1125,7 +1125,7 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe return vif; } - protected void prepareISO(final Connection conn, final String vmName) throws XmlRpcException, XenAPIException { + public void prepareISO(final Connection conn, final String vmName) throws XmlRpcException, XenAPIException { final Set vms = VM.getByNameLabel(conn, vmName); if (vms == null || vms.size() != 1) { @@ -3355,7 +3355,7 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe } } - private void migrateVM(final Connection conn, final Host destHost, final VM vm, final String vmName) throws Exception { + public void migrateVM(final Connection conn, final Host destHost, final VM vm, final String vmName) throws Exception { Task task = null; try { final Map other = new HashMap(); diff --git a/plugins/hypervisors/xenserver/src/com/cloud/hypervisor/xenserver/resource/wrapper/CitrixMigrateCommandWrapper.java b/plugins/hypervisors/xenserver/src/com/cloud/hypervisor/xenserver/resource/wrapper/CitrixMigrateCommandWrapper.java new file mode 100644 index 00000000000..6c5e55c071a --- /dev/null +++ b/plugins/hypervisors/xenserver/src/com/cloud/hypervisor/xenserver/resource/wrapper/CitrixMigrateCommandWrapper.java @@ -0,0 +1,84 @@ +// +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. +// + +package com.cloud.hypervisor.xenserver.resource.wrapper; + +import java.util.Set; + +import org.apache.log4j.Logger; + +import com.cloud.agent.api.Answer; +import com.cloud.agent.api.MigrateAnswer; +import com.cloud.agent.api.MigrateCommand; +import com.cloud.hypervisor.xenserver.resource.CitrixResourceBase; +import com.cloud.resource.CommandWrapper; +import com.xensource.xenapi.Connection; +import com.xensource.xenapi.Host; +import com.xensource.xenapi.Types; +import com.xensource.xenapi.VBD; +import com.xensource.xenapi.VM; + +public final class CitrixMigrateCommandWrapper extends CommandWrapper { + + private static final Logger s_logger = Logger.getLogger(CitrixMigrateCommandWrapper.class); + + @Override + public Answer execute(final MigrateCommand command, final CitrixResourceBase citrixResourceBase) { + final Connection conn = citrixResourceBase.getConnection(); + final String vmName = command.getVmName(); + + try { + final Set vms = VM.getByNameLabel(conn, vmName); + + final String ipaddr = command.getDestinationIp(); + + final Set hosts = Host.getAll(conn); + Host dsthost = null; + if(hosts != null) { + for (final Host host : hosts) { + if (host.getAddress(conn).equals(ipaddr)) { + dsthost = host; + break; + } + } + } + if (dsthost == null) { + final String msg = "Migration failed due to unable to find host " + ipaddr + " in XenServer pool " + citrixResourceBase.getHost().getPool(); + s_logger.warn(msg); + return new MigrateAnswer(command, false, msg, null); + } + for (final VM vm : vms) { + final Set 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); + break; + } + } + citrixResourceBase.migrateVM(conn, dsthost, vm, vmName); + vm.setAffinity(conn, dsthost); + } + 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); + } + } +} \ No newline at end of file diff --git a/plugins/hypervisors/xenserver/src/com/cloud/hypervisor/xenserver/resource/wrapper/CitrixPrepareForMigrationCommandWrapper.java b/plugins/hypervisors/xenserver/src/com/cloud/hypervisor/xenserver/resource/wrapper/CitrixPrepareForMigrationCommandWrapper.java new file mode 100644 index 00000000000..a8bc18248be --- /dev/null +++ b/plugins/hypervisors/xenserver/src/com/cloud/hypervisor/xenserver/resource/wrapper/CitrixPrepareForMigrationCommandWrapper.java @@ -0,0 +1,61 @@ +// +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. +// + +package com.cloud.hypervisor.xenserver.resource.wrapper; + +import org.apache.log4j.Logger; + +import com.cloud.agent.api.Answer; +import com.cloud.agent.api.PrepareForMigrationAnswer; +import com.cloud.agent.api.PrepareForMigrationCommand; +import com.cloud.agent.api.to.NicTO; +import com.cloud.agent.api.to.VirtualMachineTO; +import com.cloud.hypervisor.xenserver.resource.CitrixResourceBase; +import com.cloud.resource.CommandWrapper; +import com.xensource.xenapi.Connection; + +public final class CitrixPrepareForMigrationCommandWrapper extends CommandWrapper { + + private static final Logger s_logger = Logger.getLogger(CitrixPrepareForMigrationCommandWrapper.class); + + @Override + public Answer execute(final PrepareForMigrationCommand command, final CitrixResourceBase citrixResourceBase) { + final Connection conn = citrixResourceBase.getConnection(); + + final VirtualMachineTO vm = command.getVirtualMachine(); + if (s_logger.isDebugEnabled()) { + s_logger.debug("Preparing host for migrating " + vm); + } + + final NicTO[] nics = vm.getNics(); + try { + citrixResourceBase.prepareISO(conn, vm.getName()); + + for (final NicTO nic : nics) { + citrixResourceBase.getNetwork(conn, nic); + } + s_logger.debug("4. The VM " + vm.getName() + " is in Migrating state"); + + return new PrepareForMigrationAnswer(command); + } catch (final Exception e) { + s_logger.warn("Catch Exception " + e.getClass().getName() + " prepare for migration failed due to " + e.toString(), e); + return new PrepareForMigrationAnswer(command, e); + } + } +} \ No newline at end of file diff --git a/plugins/hypervisors/xenserver/src/com/cloud/hypervisor/xenserver/resource/wrapper/CitrixRequestWrapper.java b/plugins/hypervisors/xenserver/src/com/cloud/hypervisor/xenserver/resource/wrapper/CitrixRequestWrapper.java index 77879f988ec..ca0b7d03114 100644 --- a/plugins/hypervisors/xenserver/src/com/cloud/hypervisor/xenserver/resource/wrapper/CitrixRequestWrapper.java +++ b/plugins/hypervisors/xenserver/src/com/cloud/hypervisor/xenserver/resource/wrapper/CitrixRequestWrapper.java @@ -28,6 +28,8 @@ import com.cloud.agent.api.Command; import com.cloud.agent.api.GetHostStatsCommand; import com.cloud.agent.api.GetVmDiskStatsCommand; import com.cloud.agent.api.GetVmStatsCommand; +import com.cloud.agent.api.MigrateCommand; +import com.cloud.agent.api.PrepareForMigrationCommand; import com.cloud.agent.api.ReadyCommand; import com.cloud.agent.api.RebootCommand; import com.cloud.agent.api.RebootRouterCommand; @@ -69,6 +71,8 @@ public class CitrixRequestWrapper extends RequestWrapper { map.put(StopCommand.class, new CitrixStopCommandWrapper()); map.put(RebootCommand.class, new CitrixRebootCommandWrapper()); map.put(CheckVirtualMachineCommand.class, new CitrixCheckVirtualMachineCommandWrapper()); + map.put(PrepareForMigrationCommand.class, new CitrixPrepareForMigrationCommandWrapper()); + map.put(MigrateCommand.class, new CitrixMigrateCommandWrapper()); } public static CitrixRequestWrapper getInstance() { diff --git a/plugins/hypervisors/xenserver/test/com/cloud/hypervisor/xenserver/resource/wrapper/CitrixRequestWrapperTest.java b/plugins/hypervisors/xenserver/test/com/cloud/hypervisor/xenserver/resource/wrapper/CitrixRequestWrapperTest.java index 2237cf11aa1..e895581767f 100644 --- a/plugins/hypervisors/xenserver/test/com/cloud/hypervisor/xenserver/resource/wrapper/CitrixRequestWrapperTest.java +++ b/plugins/hypervisors/xenserver/test/com/cloud/hypervisor/xenserver/resource/wrapper/CitrixRequestWrapperTest.java @@ -21,6 +21,8 @@ import com.cloud.agent.api.Command; import com.cloud.agent.api.GetHostStatsCommand; import com.cloud.agent.api.GetVmDiskStatsCommand; import com.cloud.agent.api.GetVmStatsCommand; +import com.cloud.agent.api.MigrateCommand; +import com.cloud.agent.api.PrepareForMigrationCommand; import com.cloud.agent.api.ReadyCommand; import com.cloud.agent.api.RebootAnswer; import com.cloud.agent.api.RebootCommand; @@ -30,6 +32,7 @@ import com.cloud.agent.api.proxy.CheckConsoleProxyLoadCommand; import com.cloud.agent.api.proxy.WatchConsoleProxyLoadCommand; import com.cloud.agent.api.storage.CreateAnswer; import com.cloud.agent.api.storage.CreateCommand; +import com.cloud.agent.api.to.VirtualMachineTO; import com.cloud.hypervisor.xenserver.resource.CitrixResourceBase; import com.cloud.vm.DiskProfile; @@ -37,11 +40,11 @@ import com.cloud.vm.DiskProfile; public class CitrixRequestWrapperTest { @Mock - protected CitrixResourceBase citrixResourceBase; + private CitrixResourceBase citrixResourceBase; @Mock - protected RebootAnswer rebootAnswer; + private RebootAnswer rebootAnswer; @Mock - protected CreateAnswer createAnswer; + private CreateAnswer createAnswer; @Test public void testWrapperInstance() { @@ -114,7 +117,7 @@ public class CitrixRequestWrapperTest { } @Test - public void testReadyCommandCommand() { + public void testReadyCommand() { final ReadyCommand readyCommand = new ReadyCommand(); final CitrixRequestWrapper wrapper = CitrixRequestWrapper.getInstance(); @@ -126,7 +129,7 @@ public class CitrixRequestWrapperTest { } @Test - public void testGetHostStatsCommandCommand() { + public void testGetHostStatsCommand() { final GetHostStatsCommand statsCommand = new GetHostStatsCommand(null, null, 0); final CitrixRequestWrapper wrapper = CitrixRequestWrapper.getInstance(); @@ -138,7 +141,7 @@ public class CitrixRequestWrapperTest { } @Test - public void testGetVmStatsCommandCommand() { + public void testGetVmStatsCommand() { final GetVmStatsCommand statsCommand = new GetVmStatsCommand(new ArrayList(), null, null); final CitrixRequestWrapper wrapper = CitrixRequestWrapper.getInstance(); @@ -150,7 +153,7 @@ public class CitrixRequestWrapperTest { } @Test - public void testGetVmDiskStatsCommandCommand() { + public void testGetVmDiskStatsCommand() { final GetVmDiskStatsCommand statsCommand = new GetVmDiskStatsCommand(new ArrayList(), null, null); final CitrixRequestWrapper wrapper = CitrixRequestWrapper.getInstance(); @@ -162,7 +165,7 @@ public class CitrixRequestWrapperTest { } @Test - public void testCheckHealthCommandCommand() { + public void testCheckHealthCommand() { final CheckHealthCommand statsCommand = new CheckHealthCommand(); final CitrixRequestWrapper wrapper = CitrixRequestWrapper.getInstance(); @@ -200,16 +203,40 @@ public class CitrixRequestWrapperTest { } @Test - public void testCheckVirtualMachineCommandCommand() { - final CheckVirtualMachineCommand statsCommand = new CheckVirtualMachineCommand("Test"); + public void testCheckVirtualMachineCommand() { + final CheckVirtualMachineCommand virtualMachineCommand = new CheckVirtualMachineCommand("Test"); final CitrixRequestWrapper wrapper = CitrixRequestWrapper.getInstance(); assertNotNull(wrapper); - final Answer answer = wrapper.execute(statsCommand, citrixResourceBase); + final Answer answer = wrapper.execute(virtualMachineCommand, citrixResourceBase); assertTrue(answer.getResult()); } + + @Test + public void testPrepareForMigrationCommand() { + final PrepareForMigrationCommand prepareCommand = new PrepareForMigrationCommand(new VirtualMachineTO(0, "Test", null, 2, 200, 256, 512, null, "CentOS", true, false, "123")); + + final CitrixRequestWrapper wrapper = CitrixRequestWrapper.getInstance(); + assertNotNull(wrapper); + + final Answer answer = wrapper.execute(prepareCommand, citrixResourceBase); + + assertFalse(answer.getResult()); + } + + @Test + public void testMigrateCommand() { + final MigrateCommand migrateCommand = new MigrateCommand("Test", "127.0.0.1", false, new VirtualMachineTO(0, "Test", null, 2, 200, 256, 512, null, "CentOS", true, false, "123"), false); + + final CitrixRequestWrapper wrapper = CitrixRequestWrapper.getInstance(); + assertNotNull(wrapper); + + final Answer answer = wrapper.execute(migrateCommand, citrixResourceBase); + + assertFalse(answer.getResult()); + } } class NotAValidCommand extends Command {