@Override public Status isAgentAlive(HostVO agent) { if (s_logger.isDebugEnabled()) { s_logger.debug("checking if agent (" + agent.getId() + ") is alive"); } if (agent.getPodId() == null) { return null; } List<Long> otherHosts = findHostByPod(agent.getPodId(), agent.getId()); for (Long hostId : otherHosts) { if (s_logger.isDebugEnabled()) { s_logger.debug( "sending ping from (" + hostId + ") to agent's host ip address (" + agent.getPrivateIpAddress() + ")"); } Status hostState = testIpAddress(hostId, agent.getPrivateIpAddress()); if (hostState == null) { continue; } if (hostState == Status.Up) { if (s_logger.isDebugEnabled()) { s_logger.debug( "ping from (" + hostId + ") to agent's host ip address (" + agent.getPrivateIpAddress() + ") successful, returning that agent is disconnected"); } return Status .Disconnected; // the computing host ip is ping-able, but the computing agent is down, // report that the agent is disconnected } else if (hostState == Status.Down) { if (s_logger.isDebugEnabled()) { s_logger.debug("returning host state: " + hostState); } return hostState; } } // could not reach agent, could not reach agent's host, unclear what the problem is but it'll // require more investigation... if (s_logger.isDebugEnabled()) { s_logger.debug( "could not reach agent, could not reach agent's host, returning that we don't have enough information"); } return null; }
@Override public ConsoleProxyInfo assignProxy(long dataCenterId, long userVmId) { UserVmVO userVm = _userVmDao.findById(userVmId); if (userVm == null) { s_logger.warn( "User VM " + userVmId + " no longer exists, return a null proxy for user vm:" + userVmId); return null; } HostVO host = findHost(userVm); if (host != null) { if (s_logger.isDebugEnabled()) { s_logger.debug( "Assign embedded console proxy running at " + host.getName() + " to user vm " + userVmId + " with public IP " + host.getPublicIpAddress()); } // only private IP, public IP, host id have meaningful values, rest // of all are place-holder values String publicIp = host.getPublicIpAddress(); if (publicIp == null) { if (s_logger.isDebugEnabled()) { s_logger.debug( "Host " + host.getName() + "/" + host.getPrivateIpAddress() + " does not have public interface, we will return its private IP for cosole proxy."); } publicIp = host.getPrivateIpAddress(); } int urlPort = _consoleProxyUrlPort; if (host.getProxyPort() != null && host.getProxyPort().intValue() > 0) { urlPort = host.getProxyPort().intValue(); } return new ConsoleProxyInfo( _sslEnabled, publicIp, _consoleProxyPort, urlPort, _consoleProxyUrlDomain); } else { s_logger.warn( "Host that VM is running is no longer available, console access to VM " + userVmId + " will be temporarily unavailable."); } return null; }
protected HashMap<String, Object> buildConfigParams(HostVO host) { HashMap<String, Object> params = new HashMap<String, Object>(host.getDetails().size() + 5); params.putAll(host.getDetails()); params.put("guid", host.getGuid()); params.put("zone", Long.toString(host.getDataCenterId())); if (host.getPodId() != null) { params.put("pod", Long.toString(host.getPodId())); } if (host.getClusterId() != null) { params.put("cluster", Long.toString(host.getClusterId())); String guid = null; ClusterVO cluster = _clusterDao.findById(host.getClusterId()); if (cluster.getGuid() == null) { guid = host.getDetail("pool"); } else { guid = cluster.getGuid(); } if (guid != null && !guid.isEmpty()) { params.put("pool", guid); } } params.put("ipaddress", host.getPrivateIpAddress()); params.put("secondary.storage.vm", "false"); params.put( "max.template.iso.size", _configDao.getValue(Config.MaxTemplateAndIsoSize.toString())); params.put("migratewait", _configDao.getValue(Config.MigrateWait.toString())); return params; }
@Override public BaremetalPxeResponse getApiResponse(BaremetalPxeVO vo) { BaremetalPxeResponse response = new BaremetalPxeResponse(); response.setId(vo.getUuid()); HostVO host = _hostDao.findById(vo.getHostId()); response.setUrl(host.getPrivateIpAddress()); PhysicalNetworkServiceProviderVO providerVO = _physicalNetworkServiceProviderDao.findById(vo.getNetworkServiceProviderId()); response.setPhysicalNetworkId(providerVO.getUuid()); PhysicalNetworkVO nwVO = _physicalNetworkDao.findById(vo.getPhysicalNetworkId()); response.setPhysicalNetworkId(nwVO.getUuid()); response.setObjectName("baremetalpxeserver"); return response; }
@Override protected void injectMockito() { if (host == null) { return; } List<HostVO> results = new ArrayList<HostVO>(); results.add(host); Mockito.when(hostDao.listAll()).thenReturn(results); Mockito.when(hostDao.findById(Matchers.anyLong())).thenReturn(host); Mockito.when(hostDao.findHypervisorHostInCluster(Matchers.anyLong())).thenReturn(results); List<EndPoint> eps = new ArrayList<EndPoint>(); eps.add( RemoteHostEndPoint.getHypervisorHostEndPoint( host.getId(), host.getPrivateIpAddress(), host.getPublicIpAddress())); Mockito.when(selector.selectAll(Matchers.any(DataStore.class))).thenReturn(eps); Mockito.when(selector.select(Matchers.any(DataObject.class))).thenReturn(eps.get(0)); Mockito.when(selector.select(Matchers.any(DataObject.class), Matchers.any(DataObject.class))) .thenReturn(eps.get(0)); }
@Override public Boolean isVmAlive(VirtualMachine vm, Host host) { if (!vm.getType().isUsedBySystem()) { s_logger.debug("Not a System Vm, unable to determine state of " + vm + " returning null"); } if (s_logger.isDebugEnabled()) { s_logger.debug("Testing if " + vm + " is alive"); } if (vm.getHostId() == null) { s_logger.debug("There's no host id for " + vm); return null; } HostVO vmHost = _hostDao.findById(vm.getHostId()); if (vmHost == null) { s_logger.debug("Unable to retrieve the host by using id " + vm.getHostId()); return null; } List<? extends Nic> nics = _networkMgr.getNicsForTraffic(vm.getId(), TrafficType.Management); if (nics.size() == 0) { if (s_logger.isDebugEnabled()) { s_logger.debug( "Unable to find a management nic, cannot ping this system VM, unable to determine state of " + vm + " returning null"); } return null; } for (Nic nic : nics) { if (nic.getIp4Address() == null) { continue; } // get the data center IP address, find a host on the pod, use that host to ping the data // center IP address List<Long> otherHosts = findHostByPod(vmHost.getPodId(), vm.getHostId()); for (Long otherHost : otherHosts) { Status vmState = testIpAddress(otherHost, nic.getIp4Address()); if (vmState == null) { // can't get information from that host, try the next one continue; } if (vmState == Status.Up) { if (s_logger.isDebugEnabled()) { s_logger.debug( "successfully pinged vm's private IP (" + vm.getPrivateIpAddress() + "), returning that the VM is up"); } return Boolean.TRUE; } else if (vmState == Status.Down) { // We can't ping the VM directly...if we can ping the host, then report the VM down. // If we can't ping the host, then we don't have enough information. Status vmHostState = testIpAddress(otherHost, vmHost.getPrivateIpAddress()); if ((vmHostState != null) && (vmHostState == Status.Up)) { if (s_logger.isDebugEnabled()) { s_logger.debug( "successfully pinged vm's host IP (" + vmHost.getPrivateIpAddress() + "), but could not ping VM, returning that the VM is down"); } return Boolean.FALSE; } } } } if (s_logger.isDebugEnabled()) { s_logger.debug("unable to determine state of " + vm + " returning null"); } return null; }
@DB protected void CheckAndCreateTunnel(VirtualMachine instance, DeployDestination dest) { if (!_isEnabled) { return; } if (instance.getType() != VirtualMachine.Type.User && instance.getType() != VirtualMachine.Type.DomainRouter) { return; } long hostId = dest.getHost().getId(); long accountId = instance.getAccountId(); List<UserVmVO> vms = _userVmDao.listByAccountId(accountId); List<DomainRouterVO> routers = _routerDao.findBy(accountId, instance.getDataCenterIdToDeployIn()); List<VMInstanceVO> ins = new ArrayList<VMInstanceVO>(); if (vms != null) { ins.addAll(vms); } if (routers.size() != 0) { ins.addAll(routers); } List<Pair<Long, Integer>> toHosts = new ArrayList<Pair<Long, Integer>>(); List<Pair<Long, Integer>> fromHosts = new ArrayList<Pair<Long, Integer>>(); int key; for (VMInstanceVO v : ins) { Long rh = v.getHostId(); if (rh == null || rh.longValue() == hostId) { continue; } OvsTunnelAccountVO ta = _tunnelAccountDao.getByFromToAccount(hostId, rh.longValue(), accountId); if (ta == null) { key = getGreKey(hostId, rh.longValue(), accountId); if (key == -1) { s_logger.warn( String.format( "Cannot get GRE key for from=%1$s to=%2$s accountId=%3$s, tunnel create failed", hostId, rh.longValue(), accountId)); continue; } Pair<Long, Integer> p = new Pair<Long, Integer>(rh, Integer.valueOf(key)); if (!toHosts.contains(p)) { toHosts.add(p); } } ta = _tunnelAccountDao.getByFromToAccount(rh.longValue(), hostId, accountId); if (ta == null) { key = getGreKey(rh.longValue(), hostId, accountId); if (key == -1) { s_logger.warn( String.format( "Cannot get GRE key for from=%1$s to=%2$s accountId=%3$s, tunnel create failed", rh.longValue(), hostId, accountId)); continue; } Pair<Long, Integer> p = new Pair<Long, Integer>(rh, Integer.valueOf(key)); if (!fromHosts.contains(p)) { fromHosts.add(p); } } } try { String myIp = dest.getHost().getPrivateIpAddress(); for (Pair<Long, Integer> i : toHosts) { HostVO rHost = _hostDao.findById(i.first()); Commands cmds = new Commands( new OvsCreateTunnelCommand( rHost.getPrivateIpAddress(), i.second().toString(), Long.valueOf(hostId), i.first(), accountId, myIp)); s_logger.debug("Ask host " + hostId + " to create gre tunnel to " + i.first()); Answer[] answers = _agentMgr.send(hostId, cmds); handleCreateTunnelAnswer(answers); } for (Pair<Long, Integer> i : fromHosts) { HostVO rHost = _hostDao.findById(i.first()); Commands cmd2s = new Commands( new OvsCreateTunnelCommand( myIp, i.second().toString(), i.first(), Long.valueOf(hostId), accountId, rHost.getPrivateIpAddress())); s_logger.debug("Ask host " + i.first() + " to create gre tunnel to " + hostId); Answer[] answers = _agentMgr.send(i.first(), cmd2s); handleCreateTunnelAnswer(answers); } } catch (Exception e) { s_logger.debug("Ovs Tunnel network created tunnel failed", e); } }
public Long migrate(final HaWorkVO work) { final long vmId = work.getInstanceId(); final VirtualMachineGuru<VMInstanceVO> mgr = findManager(work.getType()); VMInstanceVO vm = mgr.get(vmId); if (vm == null || vm.getRemoved() != null) { s_logger.debug("Unable to find the vm " + vmId); return null; } s_logger.info("Migrating vm: " + vm.toString()); if (vm.getHostId() == null || vm.getHostId() != work.getHostId()) { s_logger.info("VM is not longer running on the current hostId"); return null; } short alertType = AlertManager.ALERT_TYPE_USERVM_MIGRATE; if (VirtualMachine.Type.DomainRouter.equals(vm.getType())) { alertType = AlertManager.ALERT_TYPE_DOMAIN_ROUTER_MIGRATE; } else if (VirtualMachine.Type.ConsoleProxy.equals(vm.getType())) { alertType = AlertManager.ALERT_TYPE_CONSOLE_PROXY_MIGRATE; } HostVO fromHost = _hostDao.findById(vm.getHostId()); String fromHostName = ((fromHost == null) ? "unknown" : fromHost.getName()); HostVO toHost = null; if (work.getStep() == Step.Scheduled) { if (vm.getState() != State.Running) { s_logger.info( "VM's state is not ready for migration. " + vm.toString() + " State is " + vm.getState().toString()); return (System.currentTimeMillis() >> 10) + _migrateRetryInterval; } DataCenterVO dcVO = _dcDao.findById(fromHost.getDataCenterId()); HostPodVO podVO = _podDao.findById(fromHost.getPodId()); try { toHost = mgr.prepareForMigration(vm); if (toHost == null) { if (s_logger.isDebugEnabled()) { s_logger.debug("Unable to find a host for migrating vm " + vmId); } _alertMgr.sendAlert( alertType, vm.getDataCenterId(), vm.getPodId(), "Unable to migrate vm " + vm.getName() + " from host " + fromHostName + " in zone " + dcVO.getName() + " and pod " + podVO.getName(), "Unable to find a suitable host"); } } catch (final InsufficientCapacityException e) { s_logger.warn("Unable to mgirate due to insufficient capacity " + vm.toString()); _alertMgr.sendAlert( alertType, vm.getDataCenterId(), vm.getPodId(), "Unable to migrate vm " + vm.getName() + " from host " + fromHostName + " in zone " + dcVO.getName() + " and pod " + podVO.getName(), "Insufficient capacity"); } catch (final StorageUnavailableException e) { s_logger.warn("Storage is unavailable: " + vm.toString()); _alertMgr.sendAlert( alertType, vm.getDataCenterId(), vm.getPodId(), "Unable to migrate vm " + vm.getName() + " from host " + fromHostName + " in zone " + dcVO.getName() + " and pod " + podVO.getName(), "Storage is gone."); } if (toHost == null) { _agentMgr.maintenanceFailed(vm.getHostId()); return null; } if (s_logger.isDebugEnabled()) { s_logger.debug("Migrating from " + work.getHostId() + " to " + toHost.getId()); } work.setStep(Step.Migrating); work.setHostId(toHost.getId()); _haDao.update(work.getId(), work); } if (work.getStep() == Step.Migrating) { vm = mgr.get(vmId); // let's see if anything has changed. boolean migrated = false; if (vm == null || vm.getRemoved() != null || vm.getHostId() == null || !_itMgr.stateTransitTo(vm, Event.MigrationRequested, vm.getHostId())) { s_logger.info("Migration cancelled because state has changed: " + vm.toString()); } else { try { boolean isWindows = _guestOSCategoryDao .findById(_guestOSDao.findById(vm.getGuestOSId()).getCategoryId()) .getName() .equalsIgnoreCase("Windows"); MigrateCommand cmd = new MigrateCommand(vm.getInstanceName(), toHost.getPrivateIpAddress(), isWindows); Answer answer = _agentMgr.send(fromHost.getId(), cmd); if (answer != null && answer.getResult()) { migrated = true; _storageMgr.unshare(vm, fromHost); work.setStep(Step.Investigating); _haDao.update(work.getId(), work); } } catch (final AgentUnavailableException e) { s_logger.debug("host became unavailable"); } catch (final OperationTimedoutException e) { s_logger.debug("operation timed out"); if (e.isActive()) { scheduleRestart(vm, true); } } } if (!migrated) { s_logger.info("Migration was unsuccessful. Cleaning up: " + vm.toString()); DataCenterVO dcVO = _dcDao.findById(vm.getDataCenterId()); HostPodVO podVO = _podDao.findById(vm.getPodId()); _alertMgr.sendAlert( alertType, fromHost.getDataCenterId(), fromHost.getPodId(), "Unable to migrate vm " + vm.getName() + " from host " + fromHost.getName() + " in zone " + dcVO.getName() + " and pod " + podVO.getName(), "Migrate Command failed. Please check logs."); _itMgr.stateTransitTo(vm, Event.MigrationFailedOnSource, toHost.getId()); _agentMgr.maintenanceFailed(vm.getHostId()); Command cleanup = mgr.cleanup(vm, null); _agentMgr.easySend(toHost.getId(), cleanup); _storageMgr.unshare(vm, toHost); return null; } } if (toHost == null) { toHost = _hostDao.findById(work.getHostId()); } DataCenterVO dcVO = _dcDao.findById(toHost.getDataCenterId()); HostPodVO podVO = _podDao.findById(toHost.getPodId()); try { if (!mgr.completeMigration(vm, toHost)) { _alertMgr.sendAlert( alertType, toHost.getDataCenterId(), toHost.getPodId(), "Unable to migrate " + vmId + " to host " + toHost.getName() + " in zone " + dcVO.getName() + " and pod " + podVO.getName(), "Migration not completed"); s_logger.warn("Unable to complete migration: " + vm.toString()); } else { s_logger.info("Migration is complete: " + vm.toString()); } return null; } catch (final AgentUnavailableException e) { s_logger.warn("Agent is unavailable for " + vm.toString()); } catch (final OperationTimedoutException e) { s_logger.warn("Operation timed outfor " + vm.toString()); } _itMgr.stateTransitTo(vm, Event.MigrationFailedOnDest, toHost.getId()); return (System.currentTimeMillis() >> 10) + _migrateRetryInterval; }
protected void loadResource(Long hostId) { HostVO host = hostDao.findById(hostId); Map<String, Object> params = new HashMap<String, Object>(); params.put("guid", host.getGuid()); params.put("ipaddress", host.getPrivateIpAddress()); params.put("username", "root"); params.put("password", "password"); params.put("zone", String.valueOf(host.getDataCenterId())); params.put("pod", String.valueOf(host.getPodId())); ServerResource resource = null; if (host.getHypervisorType() == HypervisorType.XenServer) { resource = new XcpOssResource(); try { resource.configure(host.getName(), params); } catch (ConfigurationException e) { logger.debug("Failed to load resource:" + e.toString()); } } else if (host.getHypervisorType() == HypervisorType.KVM) { resource = new LibvirtComputingResource(); try { params.put("public.network.device", "cloudbr0"); params.put("private.network.device", "cloudbr0"); resource.configure(host.getName(), params); } catch (ConfigurationException e) { // TODO Auto-generated catch block e.printStackTrace(); } } else if (host.getHypervisorType() == HypervisorType.VMware) { ClusterVO cluster = clusterDao.findById(host.getClusterId()); String url = clusterDetailsDao.findDetail(cluster.getId(), "url").getValue(); URI uri; try { uri = new URI(url); String userName = clusterDetailsDao.findDetail(cluster.getId(), "username").getValue(); String password = clusterDetailsDao.findDetail(cluster.getId(), "password").getValue(); VmwareServerDiscoverer discover = new VmwareServerDiscoverer(); Map<? extends ServerResource, Map<String, String>> resources = discover.find( host.getDataCenterId(), host.getPodId(), host.getClusterId(), uri, userName, password, null); for (Map.Entry<? extends ServerResource, Map<String, String>> entry : resources.entrySet()) { resource = entry.getKey(); } if (resource == null) { throw new CloudRuntimeException("can't find resource"); } } catch (DiscoveryException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (URISyntaxException e) { // TODO Auto-generated catch block e.printStackTrace(); } } hostResourcesMap.put(hostId, resource); HostEnvironment env = new HostEnvironment(); SetupCommand cmd = new SetupCommand(env); cmd.setNeedSetup(true); resource.executeRequest(cmd); }