Search in sources :

Example 1 with HypervDirectConnectResource

use of com.cloud.hypervisor.hyperv.resource.HypervDirectConnectResource in project cloudstack by apache.

the class HypervServerDiscoverer method find.

// End Listener implementation
// Returns server component used by server manager to operate the plugin.
// Server component is a ServerResource. If a connected agent is used, the
// ServerResource is
// ignored in favour of another created in response to
@Override
public final Map<? extends ServerResource, Map<String, String>> find(final long dcId, final Long podId, final Long clusterId, final URI uri, final String username, final String password, final List<String> hostTags) throws DiscoveryException {
    if (s_logger.isInfoEnabled()) {
        s_logger.info("Discover host. dc(zone): " + dcId + ", pod: " + podId + ", cluster: " + clusterId + ", uri host: " + uri.getHost());
    }
    // Assertions
    if (podId == null) {
        if (s_logger.isInfoEnabled()) {
            s_logger.info("No pod is assigned, skipping the discovery in" + " Hyperv discoverer");
        }
        return null;
    }
    // ClusterVO exists
    ClusterVO cluster = _clusterDao.findById(clusterId);
    // database
    if (cluster == null) {
        if (s_logger.isInfoEnabled()) {
            s_logger.info("No cluster in database for cluster id " + clusterId);
        }
        return null;
    }
    if (cluster.getHypervisorType() != HypervisorType.Hyperv) {
        if (s_logger.isInfoEnabled()) {
            s_logger.info("Cluster " + clusterId + "is not for Hyperv hypervisors");
        }
        return null;
    }
    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;
    }
    try {
        String hostname = uri.getHost();
        InetAddress ia = InetAddress.getByName(hostname);
        String agentIp = ia.getHostAddress();
        String uuidSeed = agentIp;
        String guidWithTail = calcServerResourceGuid(uuidSeed) + "-HypervResource";
        if (_resourceMgr.findHostByGuid(guidWithTail) != null) {
            s_logger.debug("Skipping " + agentIp + " because " + guidWithTail + " is already in the database.");
            return null;
        }
        s_logger.info("Creating" + HypervDirectConnectResource.class.getName() + " HypervDummyResourceBase for zone/pod/cluster " + dcId + "/" + podId + "/" + clusterId);
        // This GUID may change.
        if (cluster.getGuid() == null) {
            cluster.setGuid(UUID.nameUUIDFromBytes(String.valueOf(clusterId).getBytes(Charset.forName("UTF-8"))).toString());
            _clusterDao.update(clusterId, cluster);
        }
        // Settings required by all server resources managing a hypervisor
        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", guidWithTail);
        params.put("ipaddress", agentIp);
        // Hyper-V specific settings
        Map<String, String> details = new HashMap<String, String>();
        details.put("url", uri.getHost());
        details.put("username", username);
        details.put("password", password);
        details.put("cluster.guid", cluster.getGuid());
        params.putAll(details);
        params.put("router.aggregation.command.each.timeout", _configDao.getValue(Config.RouterAggregationCommandEachTimeout.toString()));
        HypervDirectConnectResource resource = new HypervDirectConnectResource();
        resource.configure(agentIp, params);
        // Assert
        // TODO: test by using bogus URL and bogus virtual path in URL
        ReadyCommand ping = new ReadyCommand();
        Answer pingAns = resource.executeRequest(ping);
        if (pingAns == null || !pingAns.getResult()) {
            String errMsg = "Agent not running, or no route to agent on at " + uri;
            s_logger.debug(errMsg);
            throw new DiscoveryException(errMsg);
        }
        Map<HypervDirectConnectResource, Map<String, String>> resources = new HashMap<HypervDirectConnectResource, Map<String, String>>();
        resources.put(resource, details);
        // TODO: does the resource have to create a connection?
        return resources;
    } catch (ConfigurationException e) {
        _alertMgr.sendAlert(AlertManager.AlertType.ALERT_TYPE_HOST, dcId, podId, "Unable to add " + uri.getHost(), "Error is " + e.getMessage());
        s_logger.warn("Unable to instantiate " + uri.getHost(), e);
    } catch (UnknownHostException e) {
        _alertMgr.sendAlert(AlertManager.AlertType.ALERT_TYPE_HOST, dcId, podId, "Unable to add " + uri.getHost(), "Error is " + e.getMessage());
        s_logger.warn("Unable to instantiate " + uri.getHost(), e);
    } catch (Exception e) {
        String msg = " can't setup agent, due to " + e.toString() + " - " + e.getMessage();
        s_logger.warn(msg);
    }
    return null;
}
Also used : ClusterVO(com.cloud.dc.ClusterVO) UnknownHostException(java.net.UnknownHostException) HashMap(java.util.HashMap) DiscoveryException(com.cloud.exception.DiscoveryException) ConfigurationException(javax.naming.ConfigurationException) ConnectionException(com.cloud.exception.ConnectionException) AgentUnavailableException(com.cloud.exception.AgentUnavailableException) OperationTimedoutException(com.cloud.exception.OperationTimedoutException) UnknownHostException(java.net.UnknownHostException) UnableDeleteHostException(com.cloud.resource.UnableDeleteHostException) SetupAnswer(com.cloud.agent.api.SetupAnswer) AgentControlAnswer(com.cloud.agent.api.AgentControlAnswer) Answer(com.cloud.agent.api.Answer) ReadyCommand(com.cloud.agent.api.ReadyCommand) ConfigurationException(javax.naming.ConfigurationException) InetAddress(java.net.InetAddress) DiscoveryException(com.cloud.exception.DiscoveryException) HashMap(java.util.HashMap) Map(java.util.Map) HypervDirectConnectResource(com.cloud.hypervisor.hyperv.resource.HypervDirectConnectResource)

Aggregations

AgentControlAnswer (com.cloud.agent.api.AgentControlAnswer)1 Answer (com.cloud.agent.api.Answer)1 ReadyCommand (com.cloud.agent.api.ReadyCommand)1 SetupAnswer (com.cloud.agent.api.SetupAnswer)1 ClusterVO (com.cloud.dc.ClusterVO)1 AgentUnavailableException (com.cloud.exception.AgentUnavailableException)1 ConnectionException (com.cloud.exception.ConnectionException)1 DiscoveryException (com.cloud.exception.DiscoveryException)1 OperationTimedoutException (com.cloud.exception.OperationTimedoutException)1 HypervDirectConnectResource (com.cloud.hypervisor.hyperv.resource.HypervDirectConnectResource)1 UnableDeleteHostException (com.cloud.resource.UnableDeleteHostException)1 InetAddress (java.net.InetAddress)1 UnknownHostException (java.net.UnknownHostException)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 ConfigurationException (javax.naming.ConfigurationException)1