Search in sources :

Example 61 with DomainRouterVO

use of com.cloud.vm.DomainRouterVO in project cloudstack by apache.

the class NetworkHelperImpl method destroyRouter.

@Override
public VirtualRouter destroyRouter(final long routerId, final Account caller, final Long callerUserId) throws ResourceUnavailableException, ConcurrentOperationException {
    if (s_logger.isDebugEnabled()) {
        s_logger.debug("Attempting to destroy router " + routerId);
    }
    final DomainRouterVO router = _routerDao.findById(routerId);
    if (router == null) {
        return null;
    }
    _accountMgr.checkAccess(caller, null, true, router);
    _itMgr.expunge(router.getUuid());
    _routerDao.remove(router.getId());
    return router;
}
Also used : DomainRouterVO(com.cloud.vm.DomainRouterVO)

Example 62 with DomainRouterVO

use of com.cloud.vm.DomainRouterVO in project cloudstack by apache.

the class NetworkHelperImpl method deployRouter.

@Override
public DomainRouterVO deployRouter(final RouterDeploymentDefinition routerDeploymentDefinition, final boolean startRouter) throws InsufficientAddressCapacityException, InsufficientServerCapacityException, InsufficientCapacityException, StorageUnavailableException, ResourceUnavailableException {
    final ServiceOfferingVO routerOffering = _serviceOfferingDao.findById(routerDeploymentDefinition.getServiceOfferingId());
    final Account owner = routerDeploymentDefinition.getOwner();
    // Router is the network element, we don't know the hypervisor type yet.
    // Try to allocate the domR twice using diff hypervisors, and when
    // failed both times, throw the exception up
    final List<HypervisorType> hypervisors = getHypervisors(routerDeploymentDefinition);
    int allocateRetry = 0;
    int startRetry = 0;
    DomainRouterVO router = null;
    for (final Iterator<HypervisorType> iter = hypervisors.iterator(); iter.hasNext(); ) {
        final HypervisorType hType = iter.next();
        try {
            final long id = _routerDao.getNextInSequence(Long.class, "id");
            if (s_logger.isDebugEnabled()) {
                s_logger.debug(String.format("Allocating the VR with id=%s in datacenter %s with the hypervisor type %s", id, routerDeploymentDefinition.getDest().getDataCenter(), hType));
            }
            final String templateName = retrieveTemplateName(hType, routerDeploymentDefinition.getDest().getDataCenter().getId());
            final VMTemplateVO template = _templateDao.findRoutingTemplate(hType, templateName);
            if (template == null) {
                s_logger.debug(hType + " won't support system vm, skip it");
                continue;
            }
            final boolean offerHA = routerOffering.getOfferHA();
            // routerDeploymentDefinition.getVpc().getId() ==> do not use
            // VPC because it is not a VPC offering.
            final Long vpcId = routerDeploymentDefinition.getVpc() != null ? routerDeploymentDefinition.getVpc().getId() : null;
            long userId = CallContext.current().getCallingUserId();
            if (CallContext.current().getCallingAccount().getId() != owner.getId()) {
                final List<UserVO> userVOs = _userDao.listByAccount(owner.getAccountId());
                if (!userVOs.isEmpty()) {
                    userId = userVOs.get(0).getId();
                }
            }
            router = new DomainRouterVO(id, routerOffering.getId(), routerDeploymentDefinition.getVirtualProvider().getId(), VirtualMachineName.getRouterName(id, s_vmInstanceName), template.getId(), template.getHypervisorType(), template.getGuestOSId(), owner.getDomainId(), owner.getId(), userId, routerDeploymentDefinition.isRedundant(), RedundantState.UNKNOWN, offerHA, false, vpcId);
            router.setDynamicallyScalable(template.isDynamicallyScalable());
            router.setRole(Role.VIRTUAL_ROUTER);
            router = _routerDao.persist(router);
            reallocateRouterNetworks(routerDeploymentDefinition, router, template, null);
            router = _routerDao.findById(router.getId());
        } catch (final InsufficientCapacityException ex) {
            if (allocateRetry < 2 && iter.hasNext()) {
                s_logger.debug("Failed to allocate the VR with hypervisor type " + hType + ", retrying one more time");
                continue;
            } else {
                throw ex;
            }
        } finally {
            allocateRetry++;
        }
        if (startRouter) {
            try {
                router = startVirtualRouter(router, _accountMgr.getSystemUser(), _accountMgr.getSystemAccount(), routerDeploymentDefinition.getParams());
                break;
            } catch (final InsufficientCapacityException ex) {
                if (startRetry < 2 && iter.hasNext()) {
                    s_logger.debug("Failed to start the VR  " + router + " with hypervisor type " + hType + ", " + "destroying it and recreating one more time");
                    // destroy the router
                    destroyRouter(router.getId(), _accountMgr.getAccount(Account.ACCOUNT_ID_SYSTEM), User.UID_SYSTEM);
                    continue;
                } else {
                    throw ex;
                }
            } finally {
                startRetry++;
            }
        } else {
            // return stopped router
            return router;
        }
    }
    return router;
}
Also used : Account(com.cloud.user.Account) VMTemplateVO(com.cloud.storage.VMTemplateVO) ServiceOfferingVO(com.cloud.service.ServiceOfferingVO) HypervisorType(com.cloud.hypervisor.Hypervisor.HypervisorType) UserVO(com.cloud.user.UserVO) InsufficientCapacityException(com.cloud.exception.InsufficientCapacityException) DomainRouterVO(com.cloud.vm.DomainRouterVO)

Example 63 with DomainRouterVO

use of com.cloud.vm.DomainRouterVO in project cloudstack by apache.

the class NetworkHelperImpl method waitRouter.

protected DomainRouterVO waitRouter(final DomainRouterVO router) {
    DomainRouterVO vm = _routerDao.findById(router.getId());
    if (s_logger.isDebugEnabled()) {
        s_logger.debug("Router " + router.getInstanceName() + " is not fully up yet, we will wait");
    }
    while (vm.getState() == State.Starting) {
        try {
            Thread.sleep(1000);
        } catch (final InterruptedException e) {
        }
        // reload to get the latest state info
        vm = _routerDao.findById(router.getId());
    }
    if (vm.getState() == State.Running) {
        if (s_logger.isDebugEnabled()) {
            s_logger.debug("Router " + router.getInstanceName() + " is now fully up");
        }
        return router;
    }
    s_logger.warn("Router " + router.getInstanceName() + " failed to start. current state: " + vm.getState());
    return null;
}
Also used : DomainRouterVO(com.cloud.vm.DomainRouterVO)

Example 64 with DomainRouterVO

use of com.cloud.vm.DomainRouterVO in project cloudstack by apache.

the class VirtualNetworkApplianceManagerImpl method finalizeCommandsOnStart.

@Override
public boolean finalizeCommandsOnStart(final Commands cmds, final VirtualMachineProfile profile) {
    final DomainRouterVO router = _routerDao.findById(profile.getId());
    final NicProfile controlNic = getControlNic(profile);
    if (controlNic == null) {
        s_logger.error("Control network doesn't exist for the router " + router);
        return false;
    }
    finalizeSshAndVersionAndNetworkUsageOnStart(cmds, profile, router, controlNic);
    // restart network if restartNetwork = false is not specified in profile
    // parameters
    boolean reprogramGuestNtwks = true;
    if (profile.getParameter(Param.ReProgramGuestNetworks) != null && (Boolean) profile.getParameter(Param.ReProgramGuestNetworks) == false) {
        reprogramGuestNtwks = false;
    }
    final VirtualRouterProvider vrProvider = _vrProviderDao.findById(router.getElementId());
    if (vrProvider == null) {
        throw new CloudRuntimeException("Cannot find related virtual router provider of router: " + router.getHostName());
    }
    final Provider provider = Network.Provider.getProvider(vrProvider.getType().toString());
    if (provider == null) {
        throw new CloudRuntimeException("Cannot find related provider of virtual router provider: " + vrProvider.getType().toString());
    }
    final List<Long> routerGuestNtwkIds = _routerDao.getRouterNetworks(router.getId());
    for (final Long guestNetworkId : routerGuestNtwkIds) {
        final AggregationControlCommand startCmd = new AggregationControlCommand(Action.Start, router.getInstanceName(), controlNic.getIPv4Address(), _routerControlHelper.getRouterIpInNetwork(guestNetworkId, router.getId()));
        cmds.addCommand(startCmd);
        if (reprogramGuestNtwks) {
            finalizeIpAssocForNetwork(cmds, router, provider, guestNetworkId, null);
            finalizeNetworkRulesForNetwork(cmds, router, provider, guestNetworkId);
            final NetworkOffering offering = _networkOfferingDao.findById(_networkDao.findById(guestNetworkId).getNetworkOfferingId());
            // service monitoring is currently not added in RVR
            if (!offering.getRedundantRouter()) {
                final String serviceMonitringSet = _configDao.getValue(Config.EnableServiceMonitoring.key());
                if (serviceMonitringSet != null && serviceMonitringSet.equalsIgnoreCase("true")) {
                    finalizeMonitorServiceOnStrat(cmds, profile, router, provider, guestNetworkId, true);
                } else {
                    finalizeMonitorServiceOnStrat(cmds, profile, router, provider, guestNetworkId, false);
                }
            }
        }
        finalizeUserDataAndDhcpOnStart(cmds, router, provider, guestNetworkId);
        final AggregationControlCommand finishCmd = new AggregationControlCommand(Action.Finish, router.getInstanceName(), controlNic.getIPv4Address(), _routerControlHelper.getRouterIpInNetwork(guestNetworkId, router.getId()));
        cmds.addCommand(finishCmd);
    }
    return true;
}
Also used : NetworkOffering(com.cloud.offering.NetworkOffering) VirtualRouterProvider(com.cloud.network.VirtualRouterProvider) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) AggregationControlCommand(com.cloud.agent.api.routing.AggregationControlCommand) NicProfile(com.cloud.vm.NicProfile) DomainRouterVO(com.cloud.vm.DomainRouterVO) VirtualRouterProvider(com.cloud.network.VirtualRouterProvider) Provider(com.cloud.network.Network.Provider)

Example 65 with DomainRouterVO

use of com.cloud.vm.DomainRouterVO in project cloudstack by apache.

the class VirtualNetworkApplianceManagerImpl method createRedundantRouterArgs.

protected StringBuilder createRedundantRouterArgs(final NicProfile nic, final DomainRouterVO router) {
    final StringBuilder buf = new StringBuilder();
    final boolean isRedundant = router.getIsRedundantRouter();
    if (isRedundant) {
        buf.append(" redundant_router=1");
        final int advertInt = NumbersUtil.parseInt(_configDao.getValue(Config.RedundantRouterVrrpInterval.key()), 1);
        buf.append(" advert_int=").append(advertInt);
        final Long vpcId = router.getVpcId();
        final List<DomainRouterVO> routers;
        if (vpcId != null) {
            routers = _routerDao.listByVpcId(vpcId);
            // For a redundant VPC router, both shall have the same router id. It will be used by the VRRP virtural_router_id attribute.
            // So we use the VPC id to avoid group problems.
            buf.append(" router_id=").append(vpcId);
            // Will build the routers password based on the VPC ID and UUID.
            final Vpc vpc = _vpcDao.findById(vpcId);
            try {
                final MessageDigest digest = MessageDigest.getInstance("SHA-512");
                final byte[] rawDigest = vpc.getUuid().getBytes(Charset.defaultCharset());
                digest.update(rawDigest);
                final BigInteger password = new BigInteger(1, digest.digest());
                buf.append(" router_password=").append(password);
            } catch (final NoSuchAlgorithmException e) {
                s_logger.error("Failed to pssword! Will use the plan B instead.");
                buf.append(" router_password=").append(vpc.getUuid());
            }
        } else {
            routers = _routerDao.listByNetworkAndRole(nic.getNetworkId(), Role.VIRTUAL_ROUTER);
        }
        String redundantState = RedundantState.BACKUP.toString();
        router.setRedundantState(RedundantState.BACKUP);
        if (routers.size() == 0) {
            redundantState = RedundantState.MASTER.toString();
            router.setRedundantState(RedundantState.MASTER);
        } else {
            final DomainRouterVO router0 = routers.get(0);
            if (router.getId() == router0.getId()) {
                redundantState = RedundantState.MASTER.toString();
                router.setRedundantState(RedundantState.MASTER);
            }
        }
        buf.append(" redundant_state=").append(redundantState);
    }
    return buf;
}
Also used : Vpc(com.cloud.network.vpc.Vpc) BigInteger(java.math.BigInteger) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) MessageDigest(java.security.MessageDigest) DomainRouterVO(com.cloud.vm.DomainRouterVO)

Aggregations

DomainRouterVO (com.cloud.vm.DomainRouterVO)148 ArrayList (java.util.ArrayList)39 DataCenterVO (com.cloud.dc.DataCenterVO)33 ResourceUnavailableException (com.cloud.exception.ResourceUnavailableException)33 NetworkTopology (org.apache.cloudstack.network.topology.NetworkTopology)27 Test (org.junit.Test)26 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)24 Network (com.cloud.network.Network)20 NicProfile (com.cloud.vm.NicProfile)17 Account (com.cloud.user.Account)12 NetworkVO (com.cloud.network.dao.NetworkVO)11 Answer (com.cloud.agent.api.Answer)10 ConcurrentOperationException (com.cloud.exception.ConcurrentOperationException)10 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)10 VirtualMachineProfile (com.cloud.vm.VirtualMachineProfile)10 UserVmVO (com.cloud.vm.UserVmVO)9 Vpc (com.cloud.network.vpc.Vpc)8 ServiceOfferingVO (com.cloud.service.ServiceOfferingVO)8 UserVO (com.cloud.user.UserVO)8 HashMap (java.util.HashMap)8