incubator-cloudstack-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mlsoren...@apache.org
Subject git commit: Summary: Add/remove nic capability while VM is running
Date Wed, 23 Jan 2013 22:22:34 GMT
Updated Branches:
  refs/heads/add_remove_nics 68b236cd2 -> ed12b2304


Summary: Add/remove nic capability while VM is running

Submitted-by: Brian Angus <blangus@betterservers.com>
Signed-off-by: Marcus Sorensen <marcus@betterservers.com> 1358979747 -0700


Project: http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/commit/ed12b230
Tree: http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/tree/ed12b230
Diff: http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/diff/ed12b230

Branch: refs/heads/add_remove_nics
Commit: ed12b2304f9b34884fdaf1be339d02299ed89876
Parents: 68b236c
Author: Marcus Sorensen <marcus@betterservers.com>
Authored: Wed Jan 23 15:22:27 2013 -0700
Committer: Marcus Sorensen <marcus@betterservers.com>
Committed: Wed Jan 23 15:22:27 2013 -0700

----------------------------------------------------------------------
 server/src/com/cloud/network/NetworkManager.java   |    2 +-
 .../src/com/cloud/network/NetworkManagerImpl.java  |    4 +-
 .../network/element/VirtualRouterElement.java      |    2 +-
 server/src/com/cloud/vm/UserVmManagerImpl.java     |   73 +++++++++++----
 server/src/com/cloud/vm/VirtualMachineManager.java |    2 +-
 .../com/cloud/vm/VirtualMachineManagerImpl.java    |   34 ++++++-
 .../com/cloud/network/MockNetworkManagerImpl.java  |    2 +-
 .../cloud/vm/MockVirtualMachineManagerImpl.java    |    2 +-
 .../test/com/cloud/vpc/MockNetworkManagerImpl.java |    2 +-
 9 files changed, 91 insertions(+), 32 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/ed12b230/server/src/com/cloud/network/NetworkManager.java
----------------------------------------------------------------------
diff --git a/server/src/com/cloud/network/NetworkManager.java b/server/src/com/cloud/network/NetworkManager.java
index 546f1bf..c0065dd 100755
--- a/server/src/com/cloud/network/NetworkManager.java
+++ b/server/src/com/cloud/network/NetworkManager.java
@@ -262,7 +262,7 @@ public interface NetworkManager  {
      * @throws InsufficientCapacityException
      * @throws ResourceUnavailableException
      */
-    NicProfile createNicForVm(Network network, NicProfile requested, ReservationContext context,
VirtualMachineProfileImpl<VMInstanceVO> vmProfile, boolean prepare) throws InsufficientVirtualNetworkCapcityException,
+    NicProfile createNicForVm(Network network, NicProfile requested, ReservationContext context,
VirtualMachineProfile<? extends VMInstanceVO> vmProfile, boolean prepare) throws InsufficientVirtualNetworkCapcityException,
             InsufficientAddressCapacityException, ConcurrentOperationException, InsufficientCapacityException,
ResourceUnavailableException;
 
 

http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/ed12b230/server/src/com/cloud/network/NetworkManagerImpl.java
----------------------------------------------------------------------
diff --git a/server/src/com/cloud/network/NetworkManagerImpl.java b/server/src/com/cloud/network/NetworkManagerImpl.java
index bb60dcf..241c07f 100755
--- a/server/src/com/cloud/network/NetworkManagerImpl.java
+++ b/server/src/com/cloud/network/NetworkManagerImpl.java
@@ -3355,12 +3355,11 @@ public class NetworkManagerImpl implements NetworkManager, Manager,
Listener {
     }
     
     @Override
-    public NicProfile createNicForVm(Network network, NicProfile requested, ReservationContext
context, VirtualMachineProfileImpl<VMInstanceVO> vmProfile, boolean prepare)
+    public NicProfile createNicForVm(Network network, NicProfile requested, ReservationContext
context, VirtualMachineProfile<? extends VMInstanceVO> vmProfile, boolean prepare)
             throws InsufficientVirtualNetworkCapcityException, InsufficientAddressCapacityException,
             ConcurrentOperationException, InsufficientCapacityException, ResourceUnavailableException
{
                 
                 VirtualMachine vm = vmProfile.getVirtualMachine();
-                NetworkVO networkVO = _networksDao.findById(network.getId());
                 DataCenter dc = _configMgr.getZone(network.getDataCenterId());
                 Host host = _hostDao.findById(vm.getHostId()); 
                 DeployDestination dest = new DeployDestination(dc, null, null, host);
@@ -3383,6 +3382,7 @@ public class NetworkManagerImpl implements NetworkManager, Manager,
Listener {
                 
                 //2) prepare nic
                 if (prepare) {
+                    NetworkVO networkVO = _networksDao.findById(network.getId());
                     nic = prepareNic(vmProfile, dest, context, nic.getId(), networkVO);
                     s_logger.debug("Nic is prepared successfully for vm " + vm + " in network
" + network);
                 }

http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/ed12b230/server/src/com/cloud/network/element/VirtualRouterElement.java
----------------------------------------------------------------------
diff --git a/server/src/com/cloud/network/element/VirtualRouterElement.java b/server/src/com/cloud/network/element/VirtualRouterElement.java
index 2b54ae0..cc64c15 100755
--- a/server/src/com/cloud/network/element/VirtualRouterElement.java
+++ b/server/src/com/cloud/network/element/VirtualRouterElement.java
@@ -860,8 +860,8 @@ public class VirtualRouterElement extends AdapterBase implements VirtualRouterEl
 
         // for Basic zone, add all Running routers - we have to send Dhcp/vmData/password
info to them when
         // network.dns.basiczone.updates is set to "all"
-        Long podId = dest.getPod().getId();
         if (isPodBased && _routerMgr.getDnsBasicZoneUpdate().equalsIgnoreCase("all"))
{
+            Long podId = dest.getPod().getId();
             List<DomainRouterVO> allRunningRoutersOutsideThePod = _routerDao.findByNetworkOutsideThePod(network.getId(),
                     podId, State.Running, Role.VIRTUAL_ROUTER);
             routers.addAll(allRunningRoutersOutsideThePod);

http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/ed12b230/server/src/com/cloud/vm/UserVmManagerImpl.java
----------------------------------------------------------------------
diff --git a/server/src/com/cloud/vm/UserVmManagerImpl.java b/server/src/com/cloud/vm/UserVmManagerImpl.java
index 26841e1..b1fa323 100644
--- a/server/src/com/cloud/vm/UserVmManagerImpl.java
+++ b/server/src/com/cloud/vm/UserVmManagerImpl.java
@@ -17,11 +17,16 @@
 package com.cloud.vm;
 
 import com.cloud.agent.AgentManager;
+import com.cloud.agent.AgentManager.OnError;
 import com.cloud.agent.api.*;
 import com.cloud.agent.api.storage.CreatePrivateTemplateAnswer;
 import com.cloud.agent.api.to.NicTO;
 import com.cloud.agent.api.to.VirtualMachineTO;
 import com.cloud.agent.api.to.VolumeTO;
+import com.cloud.agent.api.PlugNicAnswer;
+import com.cloud.agent.api.PlugNicCommand;
+import com.cloud.agent.api.UnPlugNicAnswer;
+import com.cloud.agent.api.UnPlugNicCommand;
 import com.cloud.agent.manager.Commands;
 import com.cloud.alert.AlertManager;
 import com.cloud.api.ApiDBUtils;
@@ -951,11 +956,6 @@ public class UserVmManagerImpl implements UserVmManager, UserVmService,
Manager
             }
         }
         
-        if (vmInstance.getState() != State.Stopped) {
-            s_logger.warn("VM is running, needs to be stopped in order to add nic");
-            throw new CloudRuntimeException(vmInstance + " currently in " + vmInstance.getState()
+ " state, operation can only execute when VM is Stopped");
-        }
-
         //todo: any security group related checks
         //todo: ensure network belongs in zone
         //todo: check other nics for VPC networks (can only belong to one?)
@@ -969,7 +969,7 @@ public class UserVmManagerImpl implements UserVmManager, UserVmService,
Manager
         NicProfile guestNic = null;
 
         try {
-            guestNic = _itMgr.addUserVmToNetwork(vmInstance, network, profile);
+            guestNic = _itMgr.addUserVmToNetwork(vmInstance,_vmDao.findById(vmInstance.getId()),
network, profile);
         } catch (ResourceUnavailableException e) {
             throw new CloudRuntimeException("Unable to add NIC to " + vmInstance + ": " +
e);
         } catch (InsufficientCapacityException e) {
@@ -1014,11 +1014,6 @@ public class UserVmManagerImpl implements UserVmManager, UserVmService,
Manager
             }
         }
         
-        if (vmInstance.getState() != State.Stopped) {
-            s_logger.warn("VM is running, needs to be stopped in order to remove nic");
-            throw new CloudRuntimeException(vmInstance + " currently in " + vmInstance.getState()
+ " state, operation can only execute when VM is Stopped");
-        }
-
         //todo: any security group related checks
         //todo: ensure network belongs in zone
         //todo: check other nics for VPC networks (can only belong to one?)
@@ -1065,11 +1060,6 @@ public class UserVmManagerImpl implements UserVmManager, UserVmService,
Manager
         // Perform permission check on VM
         _accountMgr.checkAccess(caller, null, true, vmInstance);
 
-        if(vmInstance.getState() != State.Stopped){
-            s_logger.warn("VM is running, needs to be stopped in order to change default
nic");
-            throw new CloudRuntimeException(vmInstance + " currently in " + vmInstance.getState()
+ " state, operation can only execute when VM is Stopped");
-        }
-
         // no need to check permissions for network, we'll enumerate the ones they already
have access to
         Network existingdefaultnet = _networkModel.getDefaultNetworkForVm(vmId);
         
@@ -3897,16 +3887,59 @@ public class UserVmManagerImpl implements UserVmManager, UserVmService,
Manager
     public boolean plugNic(Network network, NicTO nic, VirtualMachineTO vm,
             ReservationContext context, DeployDestination dest) throws ConcurrentOperationException,
ResourceUnavailableException,
             InsufficientCapacityException {
-        //not supported
-        throw new UnsupportedOperationException("Plug nic is not supported for vm of type
" + vm.getType());
+        UserVmVO vmVO = _vmDao.findById(vm.getId());
+        if (vmVO.getState() == State.Running) {
+            s_logger.warn("plugNic called need to plug in the NIC!!!! vm " + vmVO);
+            try {
+                PlugNicCommand plugNicCmd = new PlugNicCommand(nic,vm.getName());
+                Commands cmds = new Commands(OnError.Stop);
+                cmds.addCommand("plugnic",plugNicCmd);
+                _agentMgr.send(dest.getHost().getId(),cmds);
+                PlugNicAnswer plugNicAnswer = cmds.getAnswer(PlugNicAnswer.class);
+                if (!(plugNicAnswer != null && plugNicAnswer.getResult())) {
+                    s_logger.warn("Unable to plug nic for " + vmVO);
+                    return false;
+                }
+            } catch (OperationTimedoutException e) {
+                throw new AgentUnavailableException("Unable to plug nic for " + vmVO + "
in network " + network, dest.getHost().getId(), e);
+            }
+        } else if (vmVO.getState() == State.Stopped || vmVO.getState() == State.Stopping)
{
+            s_logger.warn(vmVO + " is Stopped, not sending PlugNicCommand.  Currently " +
vmVO.getState());
+        } else {
+            s_logger.warn("Unable to plug nic, " + vmVO + " is not in the right state " +
vmVO.getState());
+            throw new ResourceUnavailableException("Unable to plug nic on the backend," +
+                    vmVO + " is not in the right state", DataCenter.class, vmVO.getDataCenterIdToDeployIn());
+        }
+        return true;
     }
 
 
     @Override
     public boolean unplugNic(Network network, NicTO nic, VirtualMachineTO vm,
             ReservationContext context, DeployDestination dest) throws ConcurrentOperationException,
ResourceUnavailableException {
-        //not supported
-        throw new UnsupportedOperationException("Unplug nic is not supported for vm of type
" + vm.getType());
+        UserVmVO vmVO = _vmDao.findById(vm.getId());
+        if (vmVO.getState() == State.Running) {
+            try {
+                UnPlugNicCommand unplugNicCmd = new UnPlugNicCommand(nic,vm.getName());
+                Commands cmds = new Commands(OnError.Stop);
+                cmds.addCommand("unplugnic",unplugNicCmd);
+                _agentMgr.send(dest.getHost().getId(),cmds);
+                UnPlugNicAnswer unplugNicAnswer = cmds.getAnswer(UnPlugNicAnswer.class);
+                if (!(unplugNicAnswer != null && unplugNicAnswer.getResult())) {
+                    s_logger.warn("Unable to unplug nic for " + vmVO);
+                    return false;
+                }
+            } catch (OperationTimedoutException e) {
+                throw new AgentUnavailableException("Unable to unplug nic for " + vmVO +
" in network " + network, dest.getHost().getId(), e);
+            }
+        } else if (vmVO.getState() == State.Stopped || vmVO.getState() == State.Stopping)
{
+            s_logger.warn(vmVO + " is Stopped, not sending UnPlugNicCommand.  Currently "
+ vmVO.getState());
+        } else {
+            s_logger.warn("Unable to unplug nic, " + vmVO + " is not in the right state "
+ vmVO.getState());
+            throw new ResourceUnavailableException("Unable to unplug nic on the backend,"
+
+                    vmVO + " is not in the right state", DataCenter.class, vmVO.getDataCenterIdToDeployIn());
+        }
+        return true;
     }
 
     @Override

http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/ed12b230/server/src/com/cloud/vm/VirtualMachineManager.java
----------------------------------------------------------------------
diff --git a/server/src/com/cloud/vm/VirtualMachineManager.java b/server/src/com/cloud/vm/VirtualMachineManager.java
index aa7e166..a9c161e 100644
--- a/server/src/com/cloud/vm/VirtualMachineManager.java
+++ b/server/src/com/cloud/vm/VirtualMachineManager.java
@@ -161,7 +161,7 @@ public interface VirtualMachineManager extends Manager {
      * @throws ResourceUnavailableException
      * @throws InsufficientCapacityException
      */
-    NicProfile addUserVmToNetwork(VirtualMachine vm, Network network, NicProfile requested)
throws ConcurrentOperationException,
+    NicProfile addUserVmToNetwork(VirtualMachine vm,UserVmVO vmVO, Network network, NicProfile
requested) throws ConcurrentOperationException,
                 ResourceUnavailableException, InsufficientCapacityException;
 
     /**

http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/ed12b230/server/src/com/cloud/vm/VirtualMachineManagerImpl.java
----------------------------------------------------------------------
diff --git a/server/src/com/cloud/vm/VirtualMachineManagerImpl.java b/server/src/com/cloud/vm/VirtualMachineManagerImpl.java
index 626bbb4..8546467 100755
--- a/server/src/com/cloud/vm/VirtualMachineManagerImpl.java
+++ b/server/src/com/cloud/vm/VirtualMachineManagerImpl.java
@@ -2507,18 +2507,44 @@ public class VirtualMachineManagerImpl implements VirtualMachineManager,
Listene
     }
 
     @Override
-    public NicProfile addUserVmToNetwork(VirtualMachine vm, Network network, NicProfile requested)
throws ConcurrentOperationException,
+    public NicProfile addUserVmToNetwork(VirtualMachine vm, UserVmVO vmVO, Network network,
NicProfile requested) throws ConcurrentOperationException,
                                                     ResourceUnavailableException, InsufficientCapacityException
{
 
         s_logger.debug("Adding vm " + vm + " to network " + network + "; requested nic profile
" + requested);
-        VMInstanceVO vmVO = _vmDao.findById(vm.getId());
         ReservationContext context = new ReservationContextImpl(null, null, _accountMgr.getActiveUser(User.UID_SYSTEM),
                 _accountMgr.getAccount(Account.ACCOUNT_ID_SYSTEM));
 
-        VirtualMachineProfileImpl<VMInstanceVO> vmProfile = new VirtualMachineProfileImpl<VMInstanceVO>(vmVO,
null,
+        VirtualMachineProfileImpl<UserVmVO> vmProfile = new VirtualMachineProfileImpl<UserVmVO>(vmVO,
null,
                 null, null, null);
 
-        if (vm.getState() == State.Stopped) {
+        DataCenter dc = _configMgr.getZone(network.getDataCenterId());
+        Host host = _hostDao.findById(vm.getHostId()); 
+        DeployDestination dest = new DeployDestination(dc, null, null, host);
+        
+        //check vm state
+        if (vm.getState() == State.Running) {
+            //1) allocate and prepare nic
+            NicProfile nic = _networkMgr.createNicForVm(network, requested, context, vmProfile,
true);
+            
+            //2) Convert vmProfile to vmTO
+            HypervisorGuru hvGuru = _hvGuruMgr.getGuru(vmProfile.getVirtualMachine().getHypervisorType());
+            VirtualMachineTO vmTO = hvGuru.implement(vmProfile);
+            
+            //3) Convert nicProfile to NicTO
+            NicTO nicTO = toNicTO(nic, vmProfile.getVirtualMachine().getHypervisorType());
+            
+            //4) plug the nic to the vm
+            VirtualMachineGuru<UserVmVO> vmGuru = getVmGuru(vmVO);
+            
+            s_logger.debug("Plugging nic for vm " + vm + " in network " + network);
+            if (vmGuru.plugNic(network, nicTO, vmTO, context, dest)) {
+                s_logger.debug("Nic is plugged successfully for vm " + vm + " in network
" + network + ". Vm  is a part of network now");
+                return nic;
+            } else {
+                s_logger.warn("Failed to plug nic to the vm " + vm + " in network " + network);
+                return null;
+            }
+        } else if (vm.getState() == State.Stopped) {
             //1) allocate nic
             return _networkMgr.createNicForVm(network, requested, context, vmProfile, false);
         } else {

http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/ed12b230/server/test/com/cloud/network/MockNetworkManagerImpl.java
----------------------------------------------------------------------
diff --git a/server/test/com/cloud/network/MockNetworkManagerImpl.java b/server/test/com/cloud/network/MockNetworkManagerImpl.java
index ef5b9c9..c9446bb 100755
--- a/server/test/com/cloud/network/MockNetworkManagerImpl.java
+++ b/server/test/com/cloud/network/MockNetworkManagerImpl.java
@@ -733,7 +733,7 @@ public class MockNetworkManagerImpl implements NetworkManager, Manager,
NetworkS
      */
     @Override
     public NicProfile createNicForVm(Network network, NicProfile requested, ReservationContext
context,
-            VirtualMachineProfileImpl<VMInstanceVO> vmProfile, boolean prepare)
+            VirtualMachineProfile<? extends VMInstanceVO> vmProfile, boolean prepare)
             throws InsufficientVirtualNetworkCapcityException, InsufficientAddressCapacityException,
             ConcurrentOperationException, InsufficientCapacityException, ResourceUnavailableException
{
         // TODO Auto-generated method stub

http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/ed12b230/server/test/com/cloud/vm/MockVirtualMachineManagerImpl.java
----------------------------------------------------------------------
diff --git a/server/test/com/cloud/vm/MockVirtualMachineManagerImpl.java b/server/test/com/cloud/vm/MockVirtualMachineManagerImpl.java
index 75d90aa..6ff0a53 100755
--- a/server/test/com/cloud/vm/MockVirtualMachineManagerImpl.java
+++ b/server/test/com/cloud/vm/MockVirtualMachineManagerImpl.java
@@ -274,7 +274,7 @@ public class MockVirtualMachineManagerImpl implements VirtualMachineManager
{
      * @see com.cloud.vm.VirtualMachineManager#addVmToNetwork(com.cloud.vm.VirtualMachine,
com.cloud.network.Network, com.cloud.vm.NicProfile)
      */
     @Override
-    public NicProfile addUserVmToNetwork(VirtualMachine vm, Network network, NicProfile requested)
throws ConcurrentOperationException, ResourceUnavailableException, InsufficientCapacityException
{
+    public NicProfile addUserVmToNetwork(VirtualMachine vm, UserVmVO vmVO,  Network network,
NicProfile requested) throws ConcurrentOperationException, ResourceUnavailableException, InsufficientCapacityException
{
         // TODO Auto-generated method stub
         return null;
     }

http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/ed12b230/server/test/com/cloud/vpc/MockNetworkManagerImpl.java
----------------------------------------------------------------------
diff --git a/server/test/com/cloud/vpc/MockNetworkManagerImpl.java b/server/test/com/cloud/vpc/MockNetworkManagerImpl.java
index b4e1794..63bc752 100644
--- a/server/test/com/cloud/vpc/MockNetworkManagerImpl.java
+++ b/server/test/com/cloud/vpc/MockNetworkManagerImpl.java
@@ -1127,7 +1127,7 @@ public class MockNetworkManagerImpl implements NetworkManager, NetworkService,
M
      */
     @Override
     public NicProfile createNicForVm(Network network, NicProfile requested, ReservationContext
context,
-            VirtualMachineProfileImpl<VMInstanceVO> vmProfile, boolean prepare)
+            VirtualMachineProfile<? extends VMInstanceVO> vmProfile, boolean prepare)
             throws InsufficientVirtualNetworkCapcityException, InsufficientAddressCapacityException,
             ConcurrentOperationException, InsufficientCapacityException, ResourceUnavailableException
{
         // TODO Auto-generated method stub


Mime
View raw message