Search in sources :

Example 1 with BareMetalResourceBase

use of com.cloud.baremetal.networkservice.BareMetalResourceBase in project cloudstack by apache.

the class BareMetalDiscoverer method find.

@Override
public Map<? extends ServerResource, Map<String, String>> find(long dcId, Long podId, Long clusterId, URI url, String username, String password, List<String> hostTags) throws DiscoveryException {
    /* Enable this after we decide to use addBaremetalHostCmd instead of addHostCmd
        String discoverName = _params.get(ApiConstants.BAREMETAL_DISCOVER_NAME);
        if (!this.getClass().getName().equals(discoverName)) {
            return null;
        } */
    Map<BareMetalResourceBase, Map<String, String>> resources = new HashMap<BareMetalResourceBase, Map<String, String>>();
    Map<String, String> details = new HashMap<String, String>();
    if (!url.getScheme().equals("http")) {
        String msg = "urlString is not http so we're not taking care of the discovery for this: " + url;
        s_logger.debug(msg);
        return null;
    }
    if (clusterId == null) {
        String msg = "must specify cluster Id when add host";
        s_logger.debug(msg);
        throw new RuntimeException(msg);
    }
    if (podId == null) {
        String msg = "must specify pod Id when add host";
        s_logger.debug(msg);
        throw new RuntimeException(msg);
    }
    ClusterVO cluster = _clusterDao.findById(clusterId);
    if (cluster == null || (cluster.getHypervisorType() != HypervisorType.BareMetal)) {
        if (s_logger.isInfoEnabled())
            s_logger.info("invalid cluster id or cluster is not for Bare Metal hosts");
        return null;
    }
    DataCenterVO zone = _dcDao.findById(dcId);
    if (zone == null) {
        throw new RuntimeException("Cannot find zone " + dcId);
    }
    try {
        String hostname = url.getHost();
        InetAddress ia = InetAddress.getByName(hostname);
        String ipmiIp = ia.getHostAddress();
        String guid = UUID.nameUUIDFromBytes(ipmiIp.getBytes()).toString();
        String injectScript = "scripts/util/ipmi.py";
        String scriptPath = Script.findScript("", injectScript);
        if (scriptPath == null) {
            throw new CloudRuntimeException("Unable to find key ipmi script " + injectScript);
        }
        final Script2 command = new Script2(scriptPath, s_logger);
        command.add("ping");
        command.add("hostname=" + ipmiIp);
        command.add("usrname=" + username);
        command.add("password=" + password, ParamType.PASSWORD);
        final String result = command.execute();
        if (result != null) {
            s_logger.warn(String.format("Can not set up ipmi connection(ip=%1$s, username=%2$s, password=%3$s, args) because %4$s", ipmiIp, username, "******", result));
            return null;
        }
        ClusterVO clu = _clusterDao.findById(clusterId);
        if (clu.getGuid() == null) {
            clu.setGuid(UUID.randomUUID().toString());
            _clusterDao.update(clusterId, clu);
        }
        Map<String, Object> params = new HashMap<String, Object>();
        params.putAll(_params);
        params.put("zone", Long.toString(dcId));
        params.put("pod", Long.toString(podId));
        params.put("cluster", Long.toString(clusterId));
        params.put("guid", guid);
        params.put(ApiConstants.PRIVATE_IP, ipmiIp);
        params.put(ApiConstants.USERNAME, username);
        params.put(ApiConstants.PASSWORD, password);
        params.put("vmDao", _vmDao);
        params.put("configDao", _configDao);
        String resourceClassName = _configDao.getValue(Config.ExternalBaremetalResourceClassName.key());
        BareMetalResourceBase resource = null;
        if (resourceClassName != null) {
            Class<?> clazz = Class.forName(resourceClassName);
            resource = (BareMetalResourceBase) clazz.newInstance();
            String externalUrl = _configDao.getValue(Config.ExternalBaremetalSystemUrl.key());
            if (externalUrl == null) {
                throw new IllegalArgumentException(String.format("You must specify ExternalBaremetalSystemUrl in global config page as ExternalBaremetalResourceClassName is not null"));
            }
            details.put(BaremetalManager.ExternalBaremetalSystemUrl, externalUrl);
        } else {
            resource = new BareMetalResourceBase();
        }
        resource.configure("Bare Metal Agent", params);
        String memCapacity = (String) params.get(ApiConstants.MEMORY);
        String cpuCapacity = (String) params.get(ApiConstants.CPU_SPEED);
        String cpuNum = (String) params.get(ApiConstants.CPU_NUMBER);
        String mac = (String) params.get(ApiConstants.HOST_MAC);
        if (hostTags != null && hostTags.size() != 0) {
            details.put("hostTag", hostTags.get(0));
        }
        details.put(ApiConstants.MEMORY, memCapacity);
        details.put(ApiConstants.CPU_SPEED, cpuCapacity);
        details.put(ApiConstants.CPU_NUMBER, cpuNum);
        details.put(ApiConstants.HOST_MAC, mac);
        details.put(ApiConstants.USERNAME, username);
        details.put(ApiConstants.PASSWORD, password);
        details.put(ApiConstants.PRIVATE_IP, ipmiIp);
        String vmIp = (String) params.get(ApiConstants.IP_ADDRESS);
        if (vmIp != null) {
            details.put(ApiConstants.IP_ADDRESS, vmIp);
        }
        String isEchoScAgent = _configDao.getValue(Config.EnableBaremetalSecurityGroupAgentEcho.key());
        details.put(BaremetalManager.EchoSecurityGroupAgent, isEchoScAgent);
        resources.put(resource, details);
        resource.start();
        zone.setGatewayProvider(Network.Provider.ExternalGateWay.getName());
        zone.setDnsProvider(Network.Provider.ExternalDhcpServer.getName());
        zone.setDhcpProvider(Network.Provider.ExternalDhcpServer.getName());
        _dcDao.update(zone.getId(), zone);
        s_logger.debug(String.format("Discover Bare Metal host successfully(ip=%1$s, username=%2$s, password=%3%s," + "cpuNum=%4$s, cpuCapacity-%5$s, memCapacity=%6$s)", ipmiIp, username, "******", cpuNum, cpuCapacity, memCapacity));
        return resources;
    } catch (Exception e) {
        s_logger.warn("Can not set up bare metal agent", e);
    }
    return null;
}
Also used : DataCenterVO(com.cloud.dc.DataCenterVO) ClusterVO(com.cloud.dc.ClusterVO) HashMap(java.util.HashMap) Script2(com.cloud.utils.script.Script2) DiscoveryException(com.cloud.exception.DiscoveryException) ConfigurationException(javax.naming.ConfigurationException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) UnableDeleteHostException(com.cloud.resource.UnableDeleteHostException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) BareMetalResourceBase(com.cloud.baremetal.networkservice.BareMetalResourceBase) HashMap(java.util.HashMap) Map(java.util.Map) InetAddress(java.net.InetAddress)

Aggregations

BareMetalResourceBase (com.cloud.baremetal.networkservice.BareMetalResourceBase)1 ClusterVO (com.cloud.dc.ClusterVO)1 DataCenterVO (com.cloud.dc.DataCenterVO)1 DiscoveryException (com.cloud.exception.DiscoveryException)1 UnableDeleteHostException (com.cloud.resource.UnableDeleteHostException)1 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)1 Script2 (com.cloud.utils.script.Script2)1 InetAddress (java.net.InetAddress)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 ConfigurationException (javax.naming.ConfigurationException)1