bug 7441: fix ipassoc when starting router

status 7441: resolved fixed
This commit is contained in:
edison 2010-12-09 20:24:59 -08:00
parent a9ecbd9850
commit 04f3a4baa9
3 changed files with 76 additions and 12 deletions

View File

@ -19,6 +19,7 @@ package com.cloud.network;
import java.util.List;
import com.cloud.agent.manager.Commands;
import com.cloud.dc.Vlan.VlanType;
import com.cloud.deploy.DeployDestination;
import com.cloud.deploy.DeploymentPlan;
@ -115,4 +116,7 @@ public interface NetworkManager extends NetworkService {
boolean applyRules(Ip ip, List<? extends FirewallRule> rules, boolean continueOnError) throws ResourceUnavailableException;
PublicIp fetchNewPublicIp(long dcId, VlanType vlanUse, Account owner, Long networkId, boolean sourceNat) throws InsufficientAddressCapacityException;
Commands getAssociateIPCommands(DomainRouterVO router,
List<String> ipAddrList, boolean add, long vmId, Commands cmds);
}

View File

@ -324,6 +324,55 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag
}
}
@Override
public Commands getAssociateIPCommands(final DomainRouterVO router, final List<String> ipAddrList, final boolean add, long vmId, Commands cmds) {
boolean sourceNat = false;
Map<VlanVO, ArrayList<IPAddressVO>> vlanIpMap = new HashMap<VlanVO, ArrayList<IPAddressVO>>();
for (final String ipAddress: ipAddrList) {
IPAddressVO ip = _ipAddressDao.findById(ipAddress);
VlanVO vlan = _vlanDao.findById(ip.getVlanId());
ArrayList<IPAddressVO> ipList = vlanIpMap.get(vlan.getId());
if (ipList == null) {
ipList = new ArrayList<IPAddressVO>();
}
ipList.add(ip);
vlanIpMap.put(vlan, ipList);
}
for (Map.Entry<VlanVO, ArrayList<IPAddressVO>> vlanAndIp: vlanIpMap.entrySet()) {
boolean firstIP = true;
ArrayList<IPAddressVO> ipList = vlanAndIp.getValue();
Collections.sort(ipList, new Comparator<IPAddressVO>() {
@Override
public int compare(IPAddressVO o1, IPAddressVO o2) {
return o1.getAddress().compareTo(o2.getAddress());
} });
for (final IPAddressVO ip: ipList) {
sourceNat = ip.isSourceNat();
VlanVO vlan = vlanAndIp.getKey();
String vlanId = vlan.getVlanId();
String vlanGateway = vlan.getVlanGateway();
String vlanNetmask = vlan.getVlanNetmask();
String vifMacAddress = null;
if (firstIP && add) {
String[] macAddresses = _dcDao.getNextAvailableMacAddressPair(ip.getDataCenterId());
vifMacAddress = macAddresses[1];
}
String vmGuestAddress = null;
if(vmId!=0){
vmGuestAddress = _vmDao.findById(vmId).getGuestIpAddress();
}
cmds.addCommand(new IPAssocCommand(router.getInstanceName(), router.getPrivateIpAddress(), ip.getAddress(), add, firstIP, sourceNat, vlanId, vlanGateway, vlanNetmask, vifMacAddress, vmGuestAddress));
firstIP = false;
}
}
return cmds;
}
@Override
public boolean associateIP(final DomainRouterVO router, final List<String> ipAddrList, final boolean add, long vmId) {
Commands cmds = new Commands(OnError.Continue);

View File

@ -2244,19 +2244,8 @@ public class DomainRouterManagerImpl implements DomainRouterManager, DomainRoute
NicProfile controlNic = (NicProfile)profile.getParameter("control.nic");
cmds.addCommand("checkSsh", new CheckSshCommand(profile.getInstanceName(), controlNic.getIp4Address(), 3922, 5, 20));
return true;
}
@Override
public boolean finalizeStart(Commands cmds, VirtualMachineProfile<DomainRouterVO> profile, DeployDestination dest, ReservationContext context) {
CheckSshAnswer answer = (CheckSshAnswer)cmds.getAnswer("checkSsh");
if (!answer.getResult()) {
s_logger.warn("Unable to ssh to the VM: " + answer.getDetails());
return false;
}
DomainRouterVO router = profile.getVirtualMachine();
List<NicVO> nics = _nicDao.listBy(router.getId());
for (NicVO nic : nics) {
NetworkVO network = _networkDao.findById(nic.getNetworkId());
@ -2273,6 +2262,28 @@ public class DomainRouterManagerImpl implements DomainRouterManager, DomainRoute
router.setPrivateMacAddress(nic.getMacAddress());
}
}
//source NAT address is stored in /proc/cmdline of the domR and gets
//reassigned upon powerup. Source NAT rule gets configured in StartRouter command
final List<IPAddressVO> ipAddrs = _networkMgr.listPublicIpAddressesInVirtualNetwork(router.getAccountId(), router.getDataCenterId(), null);
final List<String> ipAddrList = new ArrayList<String>();
for (final IPAddressVO ipVO : ipAddrs) {
ipAddrList.add(ipVO.getAddress());
}
if (!ipAddrList.isEmpty()) {
_networkMgr.getAssociateIPCommands(router, ipAddrList, true, 0, cmds);
}
return true;
}
@Override
public boolean finalizeStart(Commands cmds, VirtualMachineProfile<DomainRouterVO> profile, DeployDestination dest, ReservationContext context) {
CheckSshAnswer answer = (CheckSshAnswer)cmds.getAnswer("checkSsh");
if (!answer.getResult()) {
s_logger.warn("Unable to ssh to the VM: " + answer.getDetails());
return false;
}
return true;
}