Search in sources :

Example 56 with DataCenterVO

use of com.cloud.dc.DataCenterVO in project cloudstack by apache.

the class HighAvailabilityManagerImpl method scheduleRestartForVmsOnHost.

@Override
public void scheduleRestartForVmsOnHost(final HostVO host, boolean investigate) {
    if (host.getType() != Host.Type.Routing) {
        return;
    }
    if (host.getHypervisorType() == HypervisorType.VMware || host.getHypervisorType() == HypervisorType.Hyperv) {
        s_logger.info("Don't restart VMs on host " + host.getId() + " as it is a " + host.getHypervisorType().toString() + " host");
        return;
    }
    s_logger.warn("Scheduling restart for VMs on host " + host.getId() + "-" + host.getName());
    final List<VMInstanceVO> vms = _instanceDao.listByHostId(host.getId());
    final DataCenterVO dcVO = _dcDao.findById(host.getDataCenterId());
    // send an email alert that the host is down
    StringBuilder sb = null;
    List<VMInstanceVO> reorderedVMList = new ArrayList<VMInstanceVO>();
    if ((vms != null) && !vms.isEmpty()) {
        sb = new StringBuilder();
        sb.append("  Starting HA on the following VMs:");
        // collect list of vm names for the alert email
        for (int i = 0; i < vms.size(); i++) {
            VMInstanceVO vm = vms.get(i);
            if (vm.getType() == VirtualMachine.Type.User) {
                reorderedVMList.add(vm);
            } else {
                reorderedVMList.add(0, vm);
            }
            if (vm.isHaEnabled()) {
                sb.append(" " + vm.getHostName());
            }
        }
    }
    // send an email alert that the host is down, include VMs
    HostPodVO podVO = _podDao.findById(host.getPodId());
    String hostDesc = "name: " + host.getName() + " (id:" + host.getId() + "), availability zone: " + dcVO.getName() + ", pod: " + podVO.getName();
    _alertMgr.sendAlert(AlertManager.AlertType.ALERT_TYPE_HOST, host.getDataCenterId(), host.getPodId(), "Host is down, " + hostDesc, "Host [" + hostDesc + "] is down." + ((sb != null) ? sb.toString() : ""));
    for (VMInstanceVO vm : reorderedVMList) {
        ServiceOfferingVO vmOffering = _serviceOfferingDao.findById(vm.getServiceOfferingId());
        if (vmOffering.getUseLocalStorage()) {
            if (s_logger.isDebugEnabled()) {
                s_logger.debug("Skipping HA on vm " + vm + ", because it uses local storage. Its fate is tied to the host.");
            }
            continue;
        }
        if (s_logger.isDebugEnabled()) {
            s_logger.debug("Notifying HA Mgr of to restart vm " + vm.getId() + "-" + vm.getInstanceName());
        }
        vm = _instanceDao.findByUuid(vm.getUuid());
        Long hostId = vm.getHostId();
        if (hostId != null && !hostId.equals(host.getId())) {
            s_logger.debug("VM " + vm.getInstanceName() + " is not on down host " + host.getId() + " it is on other host " + hostId + " VM HA is done");
            continue;
        }
        scheduleRestart(vm, investigate);
    }
}
Also used : DataCenterVO(com.cloud.dc.DataCenterVO) ArrayList(java.util.ArrayList) VMInstanceVO(com.cloud.vm.VMInstanceVO) HostPodVO(com.cloud.dc.HostPodVO) ServiceOfferingVO(com.cloud.service.ServiceOfferingVO)

Example 57 with DataCenterVO

use of com.cloud.dc.DataCenterVO in project cloudstack by apache.

the class ExternalFirewallDeviceManagerImpl method applyFirewallRules.

@Override
public boolean applyFirewallRules(Network network, List<? extends FirewallRule> rules) throws ResourceUnavailableException {
    // Find the external firewall in this zone
    long zoneId = network.getDataCenterId();
    DataCenterVO zone = _dcDao.findById(zoneId);
    ExternalFirewallDeviceVO fwDeviceVO = getExternalFirewallForNetwork(network);
    // During destroy, device reference may already been clean up, then we just return true
    if (fwDeviceVO == null) {
        return true;
    }
    HostVO externalFirewall = _hostDao.findById(fwDeviceVO.getHostId());
    assert (externalFirewall != null);
    if (network.getState() == Network.State.Allocated) {
        s_logger.debug("External firewall was asked to apply firewall rules for network with ID " + network.getId() + "; this network is not implemented. Skipping backend commands.");
        return true;
    }
    List<FirewallRuleTO> rulesTO = new ArrayList<FirewallRuleTO>();
    NetworkVO networkVO = _networkDao.findById(network.getId());
    NetworkOfferingVO offering = _networkOfferingDao.findById(networkVO.getNetworkOfferingId());
    Boolean defaultEgressPolicy = offering.getEgressDefaultPolicy();
    for (FirewallRule rule : rules) {
        if (rule.getSourceCidrList() == null && (rule.getPurpose() == Purpose.Firewall || rule.getPurpose() == Purpose.NetworkACL)) {
            _fwRulesDao.loadSourceCidrs((FirewallRuleVO) rule);
        }
        FirewallRuleTO ruleTO;
        if (rule.getPurpose() == Purpose.Firewall && rule.getTrafficType() == FirewallRule.TrafficType.Egress) {
            String guestVlanTag = BroadcastDomainType.getValue(network.getBroadcastUri());
            String guestCidr = network.getCidr();
            ruleTO = new FirewallRuleTO(rule, guestVlanTag, rule.getTrafficType(), guestCidr, defaultEgressPolicy, rule.getType());
        } else {
            IpAddress sourceIp = _networkModel.getIp(rule.getSourceIpAddressId());
            Vlan vlan = _vlanDao.findById(sourceIp.getVlanId());
            ruleTO = new FirewallRuleTO(rule, vlan.getVlanTag(), sourceIp.getAddress().addr());
        }
        rulesTO.add(ruleTO);
    }
    //Firewall rules configured for staticNAT/PF
    sendFirewallRules(rulesTO, zone, externalFirewall.getId());
    return true;
}
Also used : DataCenterVO(com.cloud.dc.DataCenterVO) ExternalFirewallDeviceVO(com.cloud.network.dao.ExternalFirewallDeviceVO) PhysicalNetworkVO(com.cloud.network.dao.PhysicalNetworkVO) NetworkVO(com.cloud.network.dao.NetworkVO) ArrayList(java.util.ArrayList) Vlan(com.cloud.dc.Vlan) FirewallRuleTO(com.cloud.agent.api.to.FirewallRuleTO) HostVO(com.cloud.host.HostVO) NetworkOfferingVO(com.cloud.offerings.NetworkOfferingVO) FirewallRule(com.cloud.network.rules.FirewallRule)

Example 58 with DataCenterVO

use of com.cloud.dc.DataCenterVO in project cloudstack by apache.

the class VirtualNetworkApplianceManagerImpl method finalizeStop.

@Override
public void finalizeStop(final VirtualMachineProfile profile, final Answer answer) {
    if (answer != null) {
        final VirtualMachine vm = profile.getVirtualMachine();
        final DomainRouterVO domR = _routerDao.findById(vm.getId());
        processStopOrRebootAnswer(domR, answer);
        final List<? extends Nic> routerNics = _nicDao.listByVmId(profile.getId());
        for (final Nic nic : routerNics) {
            final Network network = _networkModel.getNetwork(nic.getNetworkId());
            final DataCenterVO dcVO = _dcDao.findById(network.getDataCenterId());
            if (network.getTrafficType() == TrafficType.Guest && nic.getBroadcastUri() != null && nic.getBroadcastUri().getScheme().equals("pvlan")) {
                final NicProfile nicProfile = new NicProfile(nic, network, nic.getBroadcastUri(), nic.getIsolationUri(), 0, false, "pvlan-nic");
                final NetworkTopology networkTopology = _networkTopologyContext.retrieveNetworkTopology(dcVO);
                try {
                    networkTopology.setupDhcpForPvlan(false, domR, domR.getHostId(), nicProfile);
                } catch (final ResourceUnavailableException e) {
                    s_logger.debug("ERROR in finalizeStop: ", e);
                }
            }
        }
    }
}
Also used : DataCenterVO(com.cloud.dc.DataCenterVO) Network(com.cloud.network.Network) NetworkTopology(org.apache.cloudstack.network.topology.NetworkTopology) ResourceUnavailableException(com.cloud.exception.ResourceUnavailableException) Nic(com.cloud.vm.Nic) NicProfile(com.cloud.vm.NicProfile) DomainRouterVO(com.cloud.vm.DomainRouterVO) VirtualMachine(com.cloud.vm.VirtualMachine)

Example 59 with DataCenterVO

use of com.cloud.dc.DataCenterVO in project cloudstack by apache.

the class VirtualNetworkApplianceManagerImpl method finalizeVirtualMachineProfile.

@Override
public boolean finalizeVirtualMachineProfile(final VirtualMachineProfile profile, final DeployDestination dest, final ReservationContext context) {
    boolean dnsProvided = true;
    boolean dhcpProvided = true;
    boolean publicNetwork = false;
    final DataCenterVO dc = _dcDao.findById(dest.getDataCenter().getId());
    _dcDao.loadDetails(dc);
    // 1) Set router details
    final DomainRouterVO router = _routerDao.findById(profile.getVirtualMachine().getId());
    final Map<String, String> details = _vmDetailsDao.listDetailsKeyPairs(router.getId());
    router.setDetails(details);
    // 2) Prepare boot loader elements related with Control network
    final StringBuilder buf = profile.getBootArgsBuilder();
    buf.append(" template=domP");
    buf.append(" name=").append(profile.getHostName());
    if (Boolean.valueOf(_configDao.getValue("system.vm.random.password"))) {
        buf.append(" vmpassword=").append(_configDao.getValue("system.vm.password"));
    }
    NicProfile controlNic = null;
    String defaultDns1 = null;
    String defaultDns2 = null;
    String defaultIp6Dns1 = null;
    String defaultIp6Dns2 = null;
    for (final NicProfile nic : profile.getNics()) {
        final int deviceId = nic.getDeviceId();
        boolean ipv4 = false, ipv6 = false;
        if (nic.getIPv4Address() != null) {
            ipv4 = true;
            buf.append(" eth").append(deviceId).append("ip=").append(nic.getIPv4Address());
            buf.append(" eth").append(deviceId).append("mask=").append(nic.getIPv4Netmask());
        }
        if (nic.getIPv6Address() != null) {
            ipv6 = true;
            buf.append(" eth").append(deviceId).append("ip6=").append(nic.getIPv6Address());
            buf.append(" eth").append(deviceId).append("ip6prelen=").append(NetUtils.getIp6CidrSize(nic.getIPv6Cidr()));
        }
        if (nic.isDefaultNic()) {
            if (ipv4) {
                buf.append(" gateway=").append(nic.getIPv4Gateway());
            }
            if (ipv6) {
                buf.append(" ip6gateway=").append(nic.getIPv6Gateway());
            }
            defaultDns1 = nic.getIPv4Dns1();
            defaultDns2 = nic.getIPv4Dns2();
            defaultIp6Dns1 = nic.getIPv6Dns1();
            defaultIp6Dns2 = nic.getIPv6Dns2();
        }
        if (nic.getTrafficType() == TrafficType.Management) {
            buf.append(" localgw=").append(dest.getPod().getGateway());
        } else if (nic.getTrafficType() == TrafficType.Control) {
            controlNic = nic;
            buf.append(createRedundantRouterArgs(controlNic, router));
            // DOMR control command is sent over management server in VMware
            if (dest.getHost().getHypervisorType() == HypervisorType.VMware || dest.getHost().getHypervisorType() == HypervisorType.Hyperv) {
                s_logger.info("Check if we need to add management server explicit route to DomR. pod cidr: " + dest.getPod().getCidrAddress() + "/" + dest.getPod().getCidrSize() + ", pod gateway: " + dest.getPod().getGateway() + ", management host: " + ApiServiceConfiguration.ManagementHostIPAdr.value());
                if (s_logger.isInfoEnabled()) {
                    s_logger.info("Add management server explicit route to DomR.");
                }
                // always add management explicit route, for basic
                // networking setup, DomR may have two interfaces while both
                // are on the same subnet
                _mgmtCidr = _configDao.getValue(Config.ManagementNetwork.key());
                if (NetUtils.isValidCIDR(_mgmtCidr)) {
                    buf.append(" mgmtcidr=").append(_mgmtCidr);
                    buf.append(" localgw=").append(dest.getPod().getGateway());
                }
                if (dc.getNetworkType() == NetworkType.Basic) {
                    // ask domR to setup SSH on guest network
                    buf.append(" sshonguest=true");
                }
            }
        } else if (nic.getTrafficType() == TrafficType.Guest) {
            dnsProvided = _networkModel.isProviderSupportServiceInNetwork(nic.getNetworkId(), Service.Dns, Provider.VirtualRouter);
            dhcpProvided = _networkModel.isProviderSupportServiceInNetwork(nic.getNetworkId(), Service.Dhcp, Provider.VirtualRouter);
            // build bootloader parameter for the guest
            buf.append(createGuestBootLoadArgs(nic, defaultDns1, defaultDns2, router));
        } else if (nic.getTrafficType() == TrafficType.Public) {
            publicNetwork = true;
        }
    }
    if (controlNic == null) {
        throw new CloudRuntimeException("Didn't start a control port");
    }
    final String rpValue = _configDao.getValue(Config.NetworkRouterRpFilter.key());
    if (rpValue != null && rpValue.equalsIgnoreCase("true")) {
        _disableRpFilter = true;
    } else {
        _disableRpFilter = false;
    }
    String rpFilter = " ";
    String type = null;
    if (router.getVpcId() != null) {
        type = "vpcrouter";
        if (_disableRpFilter) {
            rpFilter = " disable_rp_filter=true";
        }
    } else if (!publicNetwork) {
        type = "dhcpsrvr";
    } else {
        type = "router";
        if (_disableRpFilter) {
            rpFilter = " disable_rp_filter=true";
        }
    }
    if (_disableRpFilter) {
        rpFilter = " disable_rp_filter=true";
    }
    buf.append(" type=" + type + rpFilter);
    final String domain_suffix = dc.getDetail(ZoneConfig.DnsSearchOrder.getName());
    if (domain_suffix != null) {
        buf.append(" dnssearchorder=").append(domain_suffix);
    }
    if (profile.getHypervisorType() == HypervisorType.VMware || profile.getHypervisorType() == HypervisorType.Hyperv) {
        buf.append(" extra_pubnics=" + _routerExtraPublicNics);
    }
    /*
         * If virtual router didn't provide DNS service but provide DHCP
         * service, we need to override the DHCP response to return DNS server
         * rather than virtual router itself.
         */
    if (dnsProvided || dhcpProvided) {
        if (defaultDns1 != null) {
            buf.append(" dns1=").append(defaultDns1);
        }
        if (defaultDns2 != null) {
            buf.append(" dns2=").append(defaultDns2);
        }
        if (defaultIp6Dns1 != null) {
            buf.append(" ip6dns1=").append(defaultIp6Dns1);
        }
        if (defaultIp6Dns2 != null) {
            buf.append(" ip6dns2=").append(defaultIp6Dns2);
        }
        boolean useExtDns = !dnsProvided;
        /* For backward compatibility */
        useExtDns = useExtDns || UseExternalDnsServers.valueIn(dc.getId());
        if (useExtDns) {
            buf.append(" useextdns=true");
        }
    }
    if (Boolean.valueOf(_configDao.getValue(Config.BaremetalProvisionDoneNotificationEnabled.key()))) {
        final QueryBuilder<UserVO> acntq = QueryBuilder.create(UserVO.class);
        acntq.and(acntq.entity().getUsername(), SearchCriteria.Op.EQ, "baremetal-system-account");
        final UserVO user = acntq.find();
        if (user == null) {
            s_logger.warn(String.format("global setting[baremetal.provision.done.notification] is enabled but user baremetal-system-account is not found. Baremetal provision done notification will not be enabled"));
        } else {
            buf.append(String.format(" baremetalnotificationsecuritykey=%s", user.getSecretKey()));
            buf.append(String.format(" baremetalnotificationapikey=%s", user.getApiKey()));
            buf.append(" host=").append(ApiServiceConfiguration.ManagementHostIPAdr.value());
            buf.append(" port=").append(_configDao.getValue(Config.BaremetalProvisionDoneNotificationPort.key()));
        }
    }
    if (s_logger.isDebugEnabled()) {
        s_logger.debug("Boot Args for " + profile + ": " + buf.toString());
    }
    return true;
}
Also used : DataCenterVO(com.cloud.dc.DataCenterVO) UserVO(com.cloud.user.UserVO) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) NicProfile(com.cloud.vm.NicProfile) DomainRouterVO(com.cloud.vm.DomainRouterVO)

Example 60 with DataCenterVO

use of com.cloud.dc.DataCenterVO in project cloudstack by apache.

the class VirtualNetworkApplianceManagerImpl method createGuestBootLoadArgs.

protected StringBuilder createGuestBootLoadArgs(final NicProfile guestNic, final String defaultDns1, final String defaultDns2, final DomainRouterVO router) {
    final long guestNetworkId = guestNic.getNetworkId();
    final NetworkVO guestNetwork = _networkDao.findById(guestNetworkId);
    String dhcpRange = null;
    final DataCenterVO dc = _dcDao.findById(guestNetwork.getDataCenterId());
    final StringBuilder buf = new StringBuilder();
    final boolean isRedundant = router.getIsRedundantRouter();
    if (isRedundant) {
        buf.append(createRedundantRouterArgs(guestNic, router));
        final Network net = _networkModel.getNetwork(guestNic.getNetworkId());
        buf.append(" guestgw=").append(net.getGateway());
        final String brd = NetUtils.long2Ip(NetUtils.ip2Long(guestNic.getIPv4Address()) | ~NetUtils.ip2Long(guestNic.getIPv4Netmask()));
        buf.append(" guestbrd=").append(brd);
        buf.append(" guestcidrsize=").append(NetUtils.getCidrSize(guestNic.getIPv4Netmask()));
        final int advertInt = NumbersUtil.parseInt(_configDao.getValue(Config.RedundantRouterVrrpInterval.key()), 1);
        buf.append(" advert_int=").append(advertInt);
    }
    // setup network domain
    final String domain = guestNetwork.getNetworkDomain();
    if (domain != null) {
        buf.append(" domain=" + domain);
    }
    long cidrSize = 0;
    // setup dhcp range
    if (dc.getNetworkType() == NetworkType.Basic) {
        if (guestNic.isDefaultNic()) {
            cidrSize = NetUtils.getCidrSize(guestNic.getIPv4Netmask());
            final String cidr = NetUtils.getCidrSubNet(guestNic.getIPv4Gateway(), cidrSize);
            if (cidr != null) {
                dhcpRange = NetUtils.getIpRangeStartIpFromCidr(cidr, cidrSize);
            }
        }
    } else if (dc.getNetworkType() == NetworkType.Advanced) {
        final String cidr = guestNetwork.getCidr();
        if (cidr != null) {
            cidrSize = NetUtils.getCidrSize(NetUtils.getCidrNetmask(cidr));
            dhcpRange = NetUtils.getDhcpRange(cidr);
        }
    }
    if (dhcpRange != null) {
        // To limit DNS to the cidr range
        buf.append(" cidrsize=" + String.valueOf(cidrSize));
        buf.append(" dhcprange=" + dhcpRange);
    }
    return buf;
}
Also used : DataCenterVO(com.cloud.dc.DataCenterVO) NetworkVO(com.cloud.network.dao.NetworkVO) Network(com.cloud.network.Network)

Aggregations

DataCenterVO (com.cloud.dc.DataCenterVO)214 ArrayList (java.util.ArrayList)60 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)54 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)53 HostVO (com.cloud.host.HostVO)42 Account (com.cloud.user.Account)37 NetworkVO (com.cloud.network.dao.NetworkVO)35 DomainRouterVO (com.cloud.vm.DomainRouterVO)33 HostPodVO (com.cloud.dc.HostPodVO)32 ResourceUnavailableException (com.cloud.exception.ResourceUnavailableException)30 ClusterVO (com.cloud.dc.ClusterVO)27 NetworkTopology (org.apache.cloudstack.network.topology.NetworkTopology)27 DB (com.cloud.utils.db.DB)26 Network (com.cloud.network.Network)25 HashMap (java.util.HashMap)25 ConfigurationException (javax.naming.ConfigurationException)25 PhysicalNetworkVO (com.cloud.network.dao.PhysicalNetworkVO)20 Test (org.junit.Test)20 NicProfile (com.cloud.vm.NicProfile)19 ActionEvent (com.cloud.event.ActionEvent)18