Search in sources :

Example 1 with Script2

use of com.cloud.utils.script.Script2 in project cloudstack by apache.

the class DiagnosticsHelper method umountSecondaryStorage.

public static void umountSecondaryStorage(String mountPoint) {
    if (StringUtils.isNotBlank(mountPoint)) {
        Script2 umountCmd = new Script2("/bin/bash", LOGGER);
        umountCmd.add("-c");
        String cmdLine = String.format("umount %s", mountPoint);
        umountCmd.add(cmdLine);
        umountCmd.execute();
    }
}
Also used : Script2(com.cloud.utils.script.Script2)

Example 2 with Script2

use of com.cloud.utils.script.Script2 in project cloudstack by apache.

the class BareMetalResourceBase method configure.

@Override
public boolean configure(String name, Map<String, Object> params) throws ConfigurationException {
    setName(name);
    _uuid = (String) params.get("guid");
    try {
        _memCapacity = Long.parseLong((String) params.get(ApiConstants.MEMORY)) * 1024L * 1024L;
        _cpuCapacity = Long.parseLong((String) params.get(ApiConstants.CPU_SPEED));
        _cpuNum = Long.parseLong((String) params.get(ApiConstants.CPU_NUMBER));
    } catch (NumberFormatException e) {
        throw new ConfigurationException(String.format("Unable to parse number of CPU or memory capacity " + "or cpu capacity(cpu number = %1$s memCapacity=%2$s, cpuCapacity=%3$s", params.get(ApiConstants.CPU_NUMBER), params.get(ApiConstants.MEMORY), params.get(ApiConstants.CPU_SPEED)));
    }
    _zone = (String) params.get("zone");
    _pod = (String) params.get("pod");
    _cluster = (String) params.get("cluster");
    hostId = (Long) params.get("hostId");
    _ip = (String) params.get(ApiConstants.PRIVATE_IP);
    _mac = (String) params.get(ApiConstants.HOST_MAC);
    _username = (String) params.get(ApiConstants.USERNAME);
    _password = (String) params.get(ApiConstants.PASSWORD);
    _vmName = (String) params.get("vmName");
    String echoScAgent = (String) params.get(BaremetalManager.EchoSecurityGroupAgent);
    vmDao = (VMInstanceDao) params.get("vmDao");
    configDao = (ConfigurationDao) params.get("configDao");
    if (_pod == null) {
        throw new ConfigurationException("Unable to get the pod");
    }
    if (_cluster == null) {
        throw new ConfigurationException("Unable to get the pod");
    }
    if (_ip == null) {
        throw new ConfigurationException("Unable to get the host address");
    }
    if (_mac.equalsIgnoreCase("unknown")) {
        throw new ConfigurationException("Unable to get the host mac address");
    }
    if (_mac.split(":").length != 6) {
        throw new ConfigurationException("Wrong MAC format(" + _mac + "). It must be in format of for example 00:11:ba:33:aa:dd which is not case sensitive");
    }
    if (_uuid == null) {
        throw new ConfigurationException("Unable to get the uuid");
    }
    if (echoScAgent != null) {
        _isEchoScAgent = Boolean.valueOf(echoScAgent);
    }
    String ipmiIface = "default";
    try {
        ipmiIface = configDao.getValue(Config.BaremetalIpmiLanInterface.key());
    } catch (Exception e) {
        s_logger.debug(e.getMessage(), e);
    }
    try {
        ipmiRetryTimes = Integer.parseInt(configDao.getValue(Config.BaremetalIpmiRetryTimes.key()));
    } catch (Exception e) {
        s_logger.debug(e.getMessage(), e);
    }
    try {
        provisionDoneNotificationOn = Boolean.valueOf(configDao.getValue(Config.BaremetalProvisionDoneNotificationEnabled.key()));
        isProvisionDoneNotificationTimeout = Integer.parseInt(configDao.getValue(Config.BaremetalProvisionDoneNotificationTimeout.key()));
    } catch (Exception e) {
        s_logger.debug(e.getMessage(), e);
    }
    String injectScript = "scripts/util/ipmi.py";
    String scriptPath = Script.findScript("", injectScript);
    if (scriptPath == null) {
        throw new ConfigurationException("Cannot find ping script " + scriptPath);
    }
    String pythonPath = "/usr/bin/python";
    _pingCommand = new Script2(pythonPath, s_logger);
    _pingCommand.add(scriptPath);
    _pingCommand.add("ping");
    _pingCommand.add("interface=" + ipmiIface);
    _pingCommand.add("hostname=" + _ip);
    _pingCommand.add("usrname=" + _username);
    _pingCommand.add("password=" + _password, ParamType.PASSWORD);
    _setPxeBootCommand = new Script2(pythonPath, s_logger);
    _setPxeBootCommand.add(scriptPath);
    _setPxeBootCommand.add("boot_dev");
    _setPxeBootCommand.add("interface=" + ipmiIface);
    _setPxeBootCommand.add("hostname=" + _ip);
    _setPxeBootCommand.add("usrname=" + _username);
    _setPxeBootCommand.add("password=" + _password, ParamType.PASSWORD);
    _setPxeBootCommand.add("dev=pxe");
    _setDiskBootCommand = new Script2(pythonPath, s_logger);
    _setDiskBootCommand.add(scriptPath);
    _setDiskBootCommand.add("boot_dev");
    _setDiskBootCommand.add("interface=" + ipmiIface);
    _setDiskBootCommand.add("hostname=" + _ip);
    _setDiskBootCommand.add("usrname=" + _username);
    _setDiskBootCommand.add("password=" + _password, ParamType.PASSWORD);
    _setDiskBootCommand.add("dev=disk");
    _rebootCommand = new Script2(pythonPath, s_logger);
    _rebootCommand.add(scriptPath);
    _rebootCommand.add("reboot");
    _rebootCommand.add("interface=" + ipmiIface);
    _rebootCommand.add("hostname=" + _ip);
    _rebootCommand.add("usrname=" + _username);
    _rebootCommand.add("password=" + _password, ParamType.PASSWORD);
    _getStatusCommand = new Script2(pythonPath, s_logger);
    _getStatusCommand.add(scriptPath);
    _getStatusCommand.add("ping");
    _getStatusCommand.add("interface=" + ipmiIface);
    _getStatusCommand.add("hostname=" + _ip);
    _getStatusCommand.add("usrname=" + _username);
    _getStatusCommand.add("password=" + _password, ParamType.PASSWORD);
    _powerOnCommand = new Script2(pythonPath, s_logger);
    _powerOnCommand.add(scriptPath);
    _powerOnCommand.add("power");
    _powerOnCommand.add("interface=" + ipmiIface);
    _powerOnCommand.add("hostname=" + _ip);
    _powerOnCommand.add("usrname=" + _username);
    _powerOnCommand.add("password=" + _password, ParamType.PASSWORD);
    _powerOnCommand.add("action=on");
    _powerOffCommand = new Script2(pythonPath, s_logger);
    _powerOffCommand.add(scriptPath);
    _powerOffCommand.add("power");
    _powerOffCommand.add("interface=" + ipmiIface);
    _powerOffCommand.add("hostname=" + _ip);
    _powerOffCommand.add("usrname=" + _username);
    _powerOffCommand.add("password=" + _password, ParamType.PASSWORD);
    _powerOffCommand.add("action=soft");
    _forcePowerOffCommand = new Script2(pythonPath, s_logger);
    _forcePowerOffCommand.add(scriptPath);
    _forcePowerOffCommand.add("power");
    _forcePowerOffCommand.add("interface=" + ipmiIface);
    _forcePowerOffCommand.add("hostname=" + _ip);
    _forcePowerOffCommand.add("usrname=" + _username);
    _forcePowerOffCommand.add("password=" + _password, ParamType.PASSWORD);
    _forcePowerOffCommand.add("action=off");
    _bootOrRebootCommand = new Script2(pythonPath, s_logger);
    _bootOrRebootCommand.add(scriptPath);
    _bootOrRebootCommand.add("boot_or_reboot");
    _bootOrRebootCommand.add("interface=" + ipmiIface);
    _bootOrRebootCommand.add("hostname=" + _ip);
    _bootOrRebootCommand.add("usrname=" + _username);
    _bootOrRebootCommand.add("password=" + _password, ParamType.PASSWORD);
    return true;
}
Also used : ConfigurationException(javax.naming.ConfigurationException) Script2(com.cloud.utils.script.Script2) ConfigurationException(javax.naming.ConfigurationException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException)

Example 3 with Script2

use of com.cloud.utils.script.Script2 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

Script2 (com.cloud.utils.script.Script2)3 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)2 ConfigurationException (javax.naming.ConfigurationException)2 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 InetAddress (java.net.InetAddress)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1