Search in sources :

Example 56 with Zone

use of com.cloud.db.model.Zone in project cosmic by MissionCriticalCloud.

the class CommandSetupHelper method createVmOverviewFromRouter.

public VMOverviewTO createVmOverviewFromRouter(final VirtualRouter router) {
    final VMOverviewTO vmOverviewTO = new VMOverviewTO();
    final Map<UserVmVO, List<NicVO>> vmsAndNicsMap = new HashMap<>();
    final List<NicVO> routerNics = _nicDao.listByVmId(router.getId());
    for (final NicVO routerNic : routerNics) {
        final Network network = _networkModel.getNetwork(routerNic.getNetworkId());
        if (TrafficType.Guest.equals(network.getTrafficType()) && !GuestType.Sync.equals(network.getGuestType())) {
            _userVmDao.listByNetworkIdAndStates(network.getId(), VirtualMachine.State.Starting, VirtualMachine.State.Running, VirtualMachine.State.Paused, VirtualMachine.State.Migrating, VirtualMachine.State.Stopping).forEach(vm -> {
                final NicVO nic = _nicDao.findByNtwkIdAndInstanceId(network.getId(), vm.getId());
                if (nic != null) {
                    if (!vmsAndNicsMap.containsKey(vm)) {
                        vmsAndNicsMap.put(vm, new ArrayList<NicVO>() {

                            {
                                add(nic);
                            }
                        });
                    } else {
                        vmsAndNicsMap.get(vm).add(nic);
                    }
                }
            });
        }
    }
    final List<VMOverviewTO.VMTO> vmsTO = new ArrayList<>();
    vmsAndNicsMap.forEach((vm, nics) -> {
        _userVmDao.loadDetails(vm);
        final VMOverviewTO.VMTO vmTO = new VMOverviewTO.VMTO(vm.getHostName());
        final List<VMOverviewTO.VMTO.InterfaceTO> interfacesTO = new ArrayList<>();
        final ServiceOfferingVO serviceOffering = _serviceOfferingDao.findByIdIncludingRemoved(vm.getId(), vm.getServiceOfferingId());
        final Zone zone = zoneRepository.findById(router.getDataCenterId()).orElse(null);
        nics.forEach(nic -> {
            final VMOverviewTO.VMTO.InterfaceTO interfaceTO = new VMOverviewTO.VMTO.InterfaceTO(nic.getIPv4Address(), nic.getMacAddress(), nic.isDefaultNic());
            final NetworkVO networkVO = _networkDao.findById(nic.getNetworkId());
            final String vmNameFQDN = networkVO != null ? vm.getHostName() + "." + networkVO.getNetworkDomain() : vm.getHostName();
            final Map<String, String> metadata = interfaceTO.getMetadata();
            metadata.put("service-offering", StringUtils.unicodeEscape(serviceOffering.getDisplayText()));
            metadata.put("availability-zone", StringUtils.unicodeEscape(zone.getName()));
            metadata.put("local-ipv4", nic.getIPv4Address());
            metadata.put("local-hostname", StringUtils.unicodeEscape(vmNameFQDN));
            metadata.put("public-ipv4", router.getPublicIpAddress() != null ? router.getPublicIpAddress() : nic.getIPv4Address());
            metadata.put("public-hostname", router.getPublicIpAddress());
            metadata.put("instance-id", vm.getUuid() != null ? vm.getUuid() : vm.getInstanceName());
            metadata.put("vm-id", vm.getUuid() != null ? vm.getUuid() : String.valueOf(vm.getId()));
            metadata.put("public-keys", vm.getDetail("SSH.PublicKey"));
            final String cloudIdentifier = _configDao.getValue("cloud.identifier");
            metadata.put("cloud-identifier", cloudIdentifier != null ? "CloudStack-{" + cloudIdentifier + "}" : "");
            final Map<String, String> userData = interfaceTO.getUserData();
            userData.put("user-data", vm.getUserData());
            interfacesTO.add(interfaceTO);
        });
        vmTO.setInterfaces(interfacesTO.toArray(new VMOverviewTO.VMTO.InterfaceTO[interfacesTO.size()]));
        vmsTO.add(vmTO);
    });
    vmOverviewTO.setVms(vmsTO.toArray(new VMOverviewTO.VMTO[vmsTO.size()]));
    return vmOverviewTO;
}
Also used : UserVmVO(com.cloud.vm.UserVmVO) NetworkVO(com.cloud.network.dao.NetworkVO) HashMap(java.util.HashMap) Zone(com.cloud.db.model.Zone) ArrayList(java.util.ArrayList) ServiceOfferingVO(com.cloud.service.ServiceOfferingVO) VMOverviewTO(com.cloud.legacymodel.to.VMOverviewTO) Network(com.cloud.legacymodel.network.Network) ArrayList(java.util.ArrayList) List(java.util.List) NicVO(com.cloud.vm.NicVO)

Example 57 with Zone

use of com.cloud.db.model.Zone in project cosmic by MissionCriticalCloud.

the class CommandSetupHelper method createApplyStaticNatRulesCommands.

public void createApplyStaticNatRulesCommands(final List<? extends StaticNatRule> rules, final VirtualRouter router, final Commands cmds, final long guestNetworkId) {
    final List<StaticNatRuleTO> rulesTO = new ArrayList<>();
    if (rules != null) {
        for (final StaticNatRule rule : rules) {
            final IpAddress sourceIp = _networkModel.getIp(rule.getSourceIpAddressId());
            final StaticNatRuleTO ruleTO = new StaticNatRuleTO(rule, null, sourceIp.getAddress().addr(), rule.getDestIpAddress());
            rulesTO.add(ruleTO);
        }
    }
    final SetStaticNatRulesCommand cmd = new SetStaticNatRulesCommand(rulesTO, router.getVpcId());
    cmd.setAccessDetail(NetworkElementCommand.ROUTER_IP, _routerControlHelper.getRouterControlIp(router.getId()));
    cmd.setAccessDetail(NetworkElementCommand.ROUTER_NAME, router.getInstanceName());
    final Zone zone = zoneRepository.findById(router.getDataCenterId()).orElse(null);
    cmd.setAccessDetail(NetworkElementCommand.ZONE_NETWORK_TYPE, zone.getNetworkType().toString());
    cmds.addCommand(cmd);
}
Also used : StaticNatRuleTO(com.cloud.legacymodel.to.StaticNatRuleTO) SetStaticNatRulesCommand(com.cloud.legacymodel.communication.command.SetStaticNatRulesCommand) Zone(com.cloud.db.model.Zone) ArrayList(java.util.ArrayList) IpAddress(com.cloud.network.IpAddress) PublicIpAddress(com.cloud.network.PublicIpAddress) StaticNatRule(com.cloud.legacymodel.network.StaticNatRule)

Example 58 with Zone

use of com.cloud.db.model.Zone in project cosmic by MissionCriticalCloud.

the class AgentManagerImpl method handleDisconnectWithInvestigation.

protected boolean handleDisconnectWithInvestigation(final AgentAttache attache, Status.Event event) {
    final long hostId = attache.getId();
    HostVO host = _hostDao.findById(hostId);
    if (host != null) {
        Status nextStatus = null;
        try {
            nextStatus = host.getStatus().getNextStatus(event);
        } catch (final NoTransitionException ne) {
            /*
                 * Agent may be currently in status of Down, Alert, Removed, namely there is no next status for some events. Why this can happen? Ask God not me. I hate there was
                 * no piece of comment for code handling race condition. God knew what race condition the code dealt with!
                 */
            s_logger.debug("Caught exception while getting agent's next status", ne);
        }
        if (nextStatus == Status.Alert) {
            /* OK, we are going to the bad status, let's see what happened */
            s_logger.info("Investigating why host " + hostId + " has disconnected with event " + event);
            Status determinedState = investigate(attache);
            // if state cannot be determined do nothing and bail out
            if (determinedState == null) {
                if ((System.currentTimeMillis() >> 10) - host.getLastPinged() > AlertWait.value()) {
                    s_logger.warn("Agent " + hostId + " state cannot be determined for more than " + AlertWait + "(" + AlertWait.value() + ") seconds, will go to Alert state");
                    determinedState = Status.Alert;
                } else {
                    s_logger.warn("Agent " + hostId + " state cannot be determined, do nothing");
                    return false;
                }
            }
            final Status currentStatus = host.getStatus();
            s_logger.info("The agent from host " + hostId + " state determined is " + determinedState);
            if (determinedState == Status.Down) {
                final String message = "Host is down: " + host.getId() + "-" + host.getName() + ". Starting HA on the VMs";
                s_logger.error(message);
                if (host.getType() != Host.Type.SecondaryStorage && host.getType() != Host.Type.ConsoleProxy) {
                    _alertMgr.sendAlert(AlertManager.AlertType.ALERT_TYPE_HOST, host.getDataCenterId(), host.getPodId(), "Host down, " + host.getId(), message);
                }
                event = Status.Event.HostDown;
            } else if (determinedState == Status.Up) {
                /* Got ping response from host, bring it back */
                s_logger.info("Agent is determined to be up and running");
                agentStatusTransitTo(host, Status.Event.Ping, _nodeId);
                return false;
            } else if (determinedState == Status.Disconnected) {
                s_logger.warn("Agent is disconnected but the host is still up: " + host.getId() + "-" + host.getName());
                if (currentStatus == Status.Disconnected) {
                    if ((System.currentTimeMillis() >> 10) - host.getLastPinged() > AlertWait.value()) {
                        s_logger.warn("Host " + host.getId() + " has been disconnected past the wait time it should be disconnected.");
                        event = Status.Event.WaitedTooLong;
                    } else {
                        s_logger.debug("Host " + host.getId() + " has been determined to be disconnected but it hasn't passed the wait time yet.");
                        return false;
                    }
                } else if (currentStatus == Status.Up) {
                    final Zone zone = _zoneRepository.findOne(host.getDataCenterId());
                    final HostPodVO podVO = _podDao.findById(host.getPodId());
                    final String hostDesc = "name: " + host.getName() + " (id:" + host.getId() + "), availability zone: " + zone.getName() + ", pod: " + podVO.getName();
                    if (host.getType() != Host.Type.SecondaryStorage && host.getType() != Host.Type.ConsoleProxy) {
                        _alertMgr.sendAlert(AlertManager.AlertType.ALERT_TYPE_HOST, host.getDataCenterId(), host.getPodId(), "Host disconnected, " + hostDesc, "If the agent for host [" + hostDesc + "] is not restarted within " + AlertWait + " seconds, host will go to Alert state");
                    }
                    event = Status.Event.AgentDisconnected;
                }
            } else {
                // if we end up here we are in alert state, send an alert
                final Zone zone = _zoneRepository.findOne(host.getDataCenterId());
                final HostPodVO podVO = _podDao.findById(host.getPodId());
                final String podName = podVO != null ? podVO.getName() : "NO POD";
                final String hostDesc = "name: " + host.getName() + " (id:" + host.getId() + "), availability zone: " + zone.getName() + ", pod: " + podName;
                _alertMgr.sendAlert(AlertManager.AlertType.ALERT_TYPE_HOST, host.getDataCenterId(), host.getPodId(), "Host in ALERT state, " + hostDesc, "In availability zone " + host.getDataCenterId() + ", host is in alert state: " + host.getId() + "-" + host.getName());
            }
        } else {
            s_logger.debug("The next status of agent " + host.getId() + " is not Alert, no need to investigate what happened");
        }
    }
    handleDisconnectWithoutInvestigation(attache, event, true, true);
    // Maybe the host magically reappeared?
    host = _hostDao.findById(hostId);
    if (host != null && host.getStatus() == Status.Down) {
        _haMgr.scheduleRestartForVmsOnHost(host, true);
    }
    return true;
}
Also used : Status(com.cloud.host.Status) Zone(com.cloud.db.model.Zone) NoTransitionException(com.cloud.utils.fsm.NoTransitionException) HostPodVO(com.cloud.dc.HostPodVO) HostVO(com.cloud.host.HostVO)

Example 59 with Zone

use of com.cloud.db.model.Zone in project cosmic by MissionCriticalCloud.

the class PublicNetworkGuru method allocate.

@Override
public NicProfile allocate(final Network network, NicProfile nic, final VirtualMachineProfile vm) throws InsufficientVirtualNetworkCapacityException, InsufficientAddressCapacityException, ConcurrentOperationException {
    final Zone zone = zoneRepository.findById(network.getDataCenterId()).orElse(null);
    if (nic != null && nic.getRequestedIPv4() != null) {
        throw new CloudRuntimeException("Does not support custom ip allocation at this time: " + nic);
    }
    if (nic == null) {
        nic = new NicProfile(ReservationStrategy.Create, null, null, null, null);
    }
    getIp(nic, zone, vm, network);
    if (nic.getIPv4Address() == null) {
        nic.setReservationStrategy(ReservationStrategy.Start);
    } else if (vm.getVirtualMachine().getType() == VirtualMachineType.DomainRouter) {
        nic.setReservationStrategy(ReservationStrategy.Managed);
    } else {
        nic.setReservationStrategy(ReservationStrategy.Create);
    }
    return nic;
}
Also used : Zone(com.cloud.db.model.Zone) CloudRuntimeException(com.cloud.legacymodel.exceptions.CloudRuntimeException) NicProfile(com.cloud.vm.NicProfile)

Example 60 with Zone

use of com.cloud.db.model.Zone in project cosmic by MissionCriticalCloud.

the class CommandSetupHelper method createVRConfigCommands.

public void createVRConfigCommands(final Vpc vpc, final DomainRouterVO router, final Commands cmds) {
    final SetupVRCommand cmd = new SetupVRCommand(vpc);
    cmd.setAccessDetail(NetworkElementCommand.ROUTER_IP, _routerControlHelper.getRouterControlIp(router.getId()));
    cmd.setAccessDetail(NetworkElementCommand.ROUTER_NAME, router.getInstanceName());
    final Zone zone = zoneRepository.findById(router.getDataCenterId()).orElse(null);
    cmd.setAccessDetail(NetworkElementCommand.ZONE_NETWORK_TYPE, zone.getNetworkType().toString());
    cmds.addCommand(cmd);
}
Also used : Zone(com.cloud.db.model.Zone) SetupVRCommand(com.cloud.legacymodel.communication.command.SetupVRCommand)

Aggregations

Zone (com.cloud.db.model.Zone)109 CloudRuntimeException (com.cloud.legacymodel.exceptions.CloudRuntimeException)34 ArrayList (java.util.ArrayList)34 DomainRouterVO (com.cloud.vm.DomainRouterVO)28 ResourceUnavailableException (com.cloud.legacymodel.exceptions.ResourceUnavailableException)26 Network (com.cloud.legacymodel.network.Network)25 InvalidParameterValueException (com.cloud.legacymodel.exceptions.InvalidParameterValueException)23 Account (com.cloud.legacymodel.user.Account)23 NetworkVO (com.cloud.network.dao.NetworkVO)23 NetworkTopology (com.cloud.network.topology.NetworkTopology)23 DeployDestination (com.cloud.deploy.DeployDestination)18 NicProfile (com.cloud.vm.NicProfile)17 List (java.util.List)17 HostPodVO (com.cloud.dc.HostPodVO)16 HostVO (com.cloud.host.HostVO)16 ConcurrentOperationException (com.cloud.legacymodel.exceptions.ConcurrentOperationException)14 InsufficientCapacityException (com.cloud.legacymodel.exceptions.InsufficientCapacityException)14 DB (com.cloud.utils.db.DB)14 PhysicalNetworkVO (com.cloud.network.dao.PhysicalNetworkVO)12 NetworkOffering (com.cloud.offering.NetworkOffering)11