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 HostVO createHostVOForConnectedAgent(HostVO host, StartupCommand[] cmd) { StartupCommand firstCmd = cmd[0]; if (!(firstCmd instanceof StartupRoutingCommand)) { return null; } StartupRoutingCommand ssCmd = ((StartupRoutingCommand) firstCmd); if (ssCmd.getHypervisorType() != HypervisorType.KVM) { return null; } /* KVM requires host are the same in cluster */ ClusterVO clusterVO = _clusterDao.findById(host.getClusterId()); List<HostVO> hostsInCluster = _resourceMgr.listAllHostsInCluster(clusterVO.getId()); if (!hostsInCluster.isEmpty()) { HostVO oneHost = hostsInCluster.get(0); _hostDao.loadDetails(oneHost); String hostOsInCluster = oneHost.getDetail("Host.OS"); String hostOs = ssCmd.getHostDetails().get("Host.OS"); if (!hostOsInCluster.equalsIgnoreCase(hostOs)) { throw new IllegalArgumentException( "Can't add host: " + firstCmd.getPrivateIpAddress() + " with hostOS: " + hostOs + " into a cluster," + "in which there are " + hostOsInCluster + " hosts added"); } } _hostDao.loadDetails(host); return _resourceMgr.fillRoutingHostVO(host, ssCmd, HypervisorType.KVM, host.getDetails(), null); }
@Override public Map<? extends ServerResource, Map<String, String>> find( long dcId, Long podId, Long clusterId, URI uri, String username, String password, List<String> hostTags) throws DiscoveryException { ClusterVO cluster = _clusterDao.findById(clusterId); if (cluster == null || cluster.getHypervisorType() != HypervisorType.KVM) { if (s_logger.isInfoEnabled()) s_logger.info("invalid cluster id or cluster is not for KVM hypervisors"); return null; } Map<KvmDummyResourceBase, Map<String, String>> resources = new HashMap<KvmDummyResourceBase, Map<String, String>>(); Map<String, String> details = new HashMap<String, String>(); if (!uri.getScheme().equals("http")) { String msg = "urlString is not http so we're not taking care of the discovery for this: " + uri; s_logger.debug(msg); return null; } com.trilead.ssh2.Connection sshConnection = null; String agentIp = null; try { String hostname = uri.getHost(); InetAddress ia = InetAddress.getByName(hostname); agentIp = ia.getHostAddress(); String guid = UUID.nameUUIDFromBytes(agentIp.getBytes()).toString(); String guidWithTail = guid + "-LibvirtComputingResource"; /*tail added by agent.java*/ if (_resourceMgr.findHostByGuid(guidWithTail) != null) { s_logger.debug( "Skipping " + agentIp + " because " + guidWithTail + " is already in the database."); return null; } sshConnection = new com.trilead.ssh2.Connection(agentIp, 22); sshConnection.connect(null, 60000, 60000); if (!sshConnection.authenticateWithPassword(username, password)) { s_logger.debug("Failed to authenticate"); throw new DiscoveredWithErrorException("Authentication error"); } if (!SSHCmdHelper.sshExecuteCmd(sshConnection, "lsmod|grep kvm", 3)) { s_logger.debug("It's not a KVM enabled machine"); return null; } List<PhysicalNetworkSetupInfo> netInfos = _networkMgr.getPhysicalNetworkInfo(dcId, HypervisorType.KVM); String kvmPrivateNic = _kvmPrivateNic; String kvmPublicNic = _kvmPublicNic; String kvmGuestNic = _kvmGuestNic; for (PhysicalNetworkSetupInfo info : netInfos) { if (info.getPrivateNetworkName() != null) { kvmPrivateNic = info.getPrivateNetworkName(); } if (info.getPublicNetworkName() != null) { kvmPublicNic = info.getPublicNetworkName(); } if (info.getGuestNetworkName() != null) { kvmGuestNic = info.getGuestNetworkName(); } } String parameters = " -m " + _hostIp + " -z " + dcId + " -p " + podId + " -c " + clusterId + " -g " + guid + " -a"; if (kvmPublicNic != null) { parameters += " --pubNic=" + kvmPublicNic; } if (kvmPrivateNic != null) { parameters += " --prvNic=" + kvmPrivateNic; } if (kvmGuestNic != null) { parameters += " --guestNic=" + kvmGuestNic; } SSHCmdHelper.sshExecuteCmd(sshConnection, "cloud-setup-agent " + parameters, 3); KvmDummyResourceBase kvmResource = new KvmDummyResourceBase(); Map<String, Object> params = new HashMap<String, Object>(); params.put("zone", Long.toString(dcId)); params.put("pod", Long.toString(podId)); params.put("cluster", Long.toString(clusterId)); params.put("guid", guid); params.put("agentIp", agentIp); kvmResource.configure("kvm agent", params); resources.put(kvmResource, details); HostVO connectedHost = waitForHostConnect(dcId, podId, clusterId, guidWithTail); if (connectedHost == null) return null; details.put("guid", guidWithTail); // place a place holder guid derived from cluster ID if (cluster.getGuid() == null) { cluster.setGuid(UUID.nameUUIDFromBytes(String.valueOf(clusterId).getBytes()).toString()); _clusterDao.update(clusterId, cluster); } // save user name and password _hostDao.loadDetails(connectedHost); Map<String, String> hostDetails = connectedHost.getDetails(); hostDetails.put("password", password); hostDetails.put("username", username); _hostDao.saveDetails(connectedHost); return resources; } catch (DiscoveredWithErrorException e) { throw e; } catch (Exception e) { String msg = " can't setup agent, due to " + e.toString() + " - " + e.getMessage(); s_logger.warn(msg); } finally { if (sshConnection != null) sshConnection.close(); } return null; }