Search in sources :

Example 16 with SCPClient

use of com.trilead.ssh2.SCPClient in project cloudstack by apache.

the class BaremetalDhcpdResource method configure.

@Override
public boolean configure(String name, Map<String, Object> params) throws ConfigurationException {
    com.trilead.ssh2.Connection sshConnection = null;
    try {
        super.configure(name, params);
        s_logger.debug(String.format("Trying to connect to DHCP server(IP=%1$s, username=%2$s, password=%3$s)", _ip, _username, "******"));
        sshConnection = SSHCmdHelper.acquireAuthorizedConnection(_ip, _username, _password);
        if (sshConnection == null) {
            throw new ConfigurationException(String.format("Cannot connect to DHCP server(IP=%1$s, username=%2$s, password=%3$s", _ip, _username, "******"));
        }
        if (!SSHCmdHelper.sshExecuteCmd(sshConnection, "[ -f '/usr/sbin/dhcpd' ]")) {
            throw new ConfigurationException("Cannot find dhcpd.conf /etc/dhcpd.conf at  on " + _ip);
        }
        SCPClient scp = new SCPClient(sshConnection);
        String editHosts = "scripts/network/exdhcp/dhcpd_edithosts.py";
        String editHostsPath = Script.findScript("", editHosts);
        if (editHostsPath == null) {
            throw new ConfigurationException("Can not find script dnsmasq_edithosts.sh at " + editHosts);
        }
        scp.put(editHostsPath, "/usr/bin/", "0755");
        String prepareDhcpdScript = "scripts/network/exdhcp/prepare_dhcpd.sh";
        String prepareDhcpdScriptPath = Script.findScript("", prepareDhcpdScript);
        if (prepareDhcpdScriptPath == null) {
            throw new ConfigurationException("Can not find prepare_dhcpd.sh at " + prepareDhcpdScriptPath);
        }
        scp.put(prepareDhcpdScriptPath, "/usr/bin/", "0755");
        // TODO: tooooooooooooooo ugly here!!!
        String[] ips = _ip.split("\\.");
        ips[3] = "0";
        StringBuffer buf = new StringBuffer();
        int i;
        for (i = 0; i < ips.length - 1; i++) {
            buf.append(ips[i]).append(".");
        }
        buf.append(ips[i]);
        String subnet = buf.toString();
        String cmd = String.format("sh /usr/bin/prepare_dhcpd.sh %1$s", subnet);
        if (!SSHCmdHelper.sshExecuteCmd(sshConnection, cmd)) {
            throw new ConfigurationException("prepare Dhcpd at " + _ip + " failed, command:" + cmd);
        }
        s_logger.debug("Dhcpd resource configure successfully");
        return true;
    } catch (Exception e) {
        s_logger.debug("Dhcpd resource configure failed", e);
        throw new ConfigurationException(e.getMessage());
    } finally {
        SSHCmdHelper.releaseSshConnection(sshConnection);
    }
}
Also used : SCPClient(com.trilead.ssh2.SCPClient) ConfigurationException(javax.naming.ConfigurationException) ConfigurationException(javax.naming.ConfigurationException)

Example 17 with SCPClient

use of com.trilead.ssh2.SCPClient in project cloudstack by apache.

the class BaremetalKickStartPxeResource method configure.

@Override
public boolean configure(String name, Map<String, Object> params) throws ConfigurationException {
    super.configure(name, params);
    _tftpDir = (String) params.get(BaremetalPxeService.PXE_PARAM_TFTP_DIR);
    if (_tftpDir == null) {
        throw new ConfigurationException("No tftp directory specified");
    }
    com.trilead.ssh2.Connection sshConnection = new com.trilead.ssh2.Connection(_ip, 22);
    s_logger.debug(String.format("Trying to connect to kickstart PXE server(IP=%1$s, username=%2$s, password=%3$s", _ip, _username, "******"));
    try {
        sshConnection.connect(null, 60000, 60000);
        if (!sshConnection.authenticateWithPassword(_username, _password)) {
            s_logger.debug("SSH Failed to authenticate");
            throw new ConfigurationException(String.format("Cannot connect to kickstart PXE server(IP=%1$s, username=%2$s, password=%3$s", _ip, _username, "******"));
        }
        String cmd = String.format("[ -f /%1$s/pxelinux.0 ]", _tftpDir);
        if (!SSHCmdHelper.sshExecuteCmd(sshConnection, cmd)) {
            throw new ConfigurationException("Miss files in TFTP directory at " + _tftpDir + " check if pxelinux.0 are here");
        }
        SCPClient scp = new SCPClient(sshConnection);
        String prepareScript = "scripts/network/ping/prepare_kickstart_bootfile.py";
        String prepareScriptPath = Script.findScript("", prepareScript);
        if (prepareScriptPath == null) {
            throw new ConfigurationException("Can not find prepare_kickstart_bootfile.py at " + prepareScript);
        }
        scp.put(prepareScriptPath, "/usr/bin/", "0755");
        String cpScript = "scripts/network/ping/prepare_kickstart_kernel_initrd.py";
        String cpScriptPath = Script.findScript("", cpScript);
        if (cpScriptPath == null) {
            throw new ConfigurationException("Can not find prepare_kickstart_kernel_initrd.py at " + cpScript);
        }
        scp.put(cpScriptPath, "/usr/bin/", "0755");
        String userDataScript = "scripts/network/ping/baremetal_user_data.py";
        String userDataScriptPath = Script.findScript("", userDataScript);
        if (userDataScriptPath == null) {
            throw new ConfigurationException("Can not find baremetal_user_data.py at " + userDataScript);
        }
        scp.put(userDataScriptPath, "/usr/bin/", "0755");
        return true;
    } catch (Exception e) {
        throw new CloudRuntimeException(e);
    } finally {
        if (sshConnection != null) {
            sshConnection.close();
        }
    }
}
Also used : SCPClient(com.trilead.ssh2.SCPClient) ConfigurationException(javax.naming.ConfigurationException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) ConfigurationException(javax.naming.ConfigurationException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException)

Example 18 with SCPClient

use of com.trilead.ssh2.SCPClient in project cloudstack by apache.

the class BaremetalPingPxeResource method configure.

@Override
public boolean configure(String name, Map<String, Object> params) throws ConfigurationException {
    super.configure(name, params);
    _storageServer = (String) params.get(BaremetalPxeService.PXE_PARAM_PING_STORAGE_SERVER_IP);
    _pingDir = (String) params.get(BaremetalPxeService.PXE_PARAM_PING_ROOT_DIR);
    _tftpDir = (String) params.get(BaremetalPxeService.PXE_PARAM_TFTP_DIR);
    _cifsUserName = (String) params.get(BaremetalPxeService.PXE_PARAM_PING_STORAGE_SERVER_USERNAME);
    _cifsPassword = (String) params.get(BaremetalPxeService.PXE_PARAM_PING_STORAGE_SERVER_PASSWORD);
    if (_podId == null) {
        throw new ConfigurationException("No Pod specified");
    }
    if (_storageServer == null) {
        throw new ConfigurationException("No stroage server specified");
    }
    if (_tftpDir == null) {
        throw new ConfigurationException("No tftp directory specified");
    }
    if (_pingDir == null) {
        throw new ConfigurationException("No PING directory specified");
    }
    if (_cifsUserName == null || _cifsUserName.equalsIgnoreCase("")) {
        _cifsUserName = "xxx";
    }
    if (_cifsPassword == null || _cifsPassword.equalsIgnoreCase("")) {
        _cifsPassword = "xxx";
    }
    String[] pingDirs = _pingDir.split("/");
    if (pingDirs.length != 2) {
        throw new ConfigurationException("PING dir should have format like myshare/direcotry, eg: windows/64bit");
    }
    _share = pingDirs[0];
    _dir = pingDirs[1];
    com.trilead.ssh2.Connection sshConnection = new com.trilead.ssh2.Connection(_ip, 22);
    s_logger.debug(String.format("Trying to connect to PING PXE server(IP=%1$s, username=%2$s, password=%3$s", _ip, _username, "******"));
    try {
        sshConnection.connect(null, 60000, 60000);
        if (!sshConnection.authenticateWithPassword(_username, _password)) {
            s_logger.debug("SSH Failed to authenticate");
            throw new ConfigurationException(String.format("Cannot connect to PING PXE server(IP=%1$s, username=%2$s, password=%3$s", _ip, _username, "******"));
        }
        String cmd = String.format("[ -f /%1$s/pxelinux.0 ] && [ -f /%2$s/kernel ] && [ -f /%3$s/initrd.gz ] ", _tftpDir, _tftpDir, _tftpDir);
        if (!SSHCmdHelper.sshExecuteCmd(sshConnection, cmd)) {
            throw new ConfigurationException("Miss files in TFTP directory at " + _tftpDir + " check if pxelinux.0, kernel initrd.gz are here");
        }
        SCPClient scp = new SCPClient(sshConnection);
        String prepareScript = "scripts/network/ping/prepare_tftp_bootfile.py";
        String prepareScriptPath = Script.findScript("", prepareScript);
        if (prepareScriptPath == null) {
            throw new ConfigurationException("Can not find prepare_tftp_bootfile.py at " + prepareScriptPath);
        }
        scp.put(prepareScriptPath, "/usr/bin/", "0755");
        String userDataScript = "scripts/network/ping/baremetal_user_data.py";
        String userDataScriptPath = Script.findScript("", userDataScript);
        if (userDataScriptPath == null) {
            throw new ConfigurationException("Can not find baremetal_user_data.py at " + userDataScriptPath);
        }
        scp.put(userDataScriptPath, "/usr/bin/", "0755");
        return true;
    } catch (Exception e) {
        throw new ConfigurationException(e.getMessage());
    } finally {
        if (sshConnection != null) {
            sshConnection.close();
        }
    }
}
Also used : SCPClient(com.trilead.ssh2.SCPClient) ConfigurationException(javax.naming.ConfigurationException) ConfigurationException(javax.naming.ConfigurationException)

Example 19 with SCPClient

use of com.trilead.ssh2.SCPClient in project cloudstack by apache.

the class BaremetalDnsmasqResource method configure.

@Override
public boolean configure(String name, Map<String, Object> params) throws ConfigurationException {
    com.trilead.ssh2.Connection sshConnection = null;
    try {
        super.configure(name, params);
        s_logger.debug(String.format("Trying to connect to DHCP server(IP=%1$s, username=%2$s, password=%3$s)", _ip, _username, _password));
        sshConnection = SSHCmdHelper.acquireAuthorizedConnection(_ip, _username, _password);
        if (sshConnection == null) {
            throw new ConfigurationException(String.format("Cannot connect to DHCP server(IP=%1$s, username=%2$s, password=%3$s", _ip, _username, _password));
        }
        if (!SSHCmdHelper.sshExecuteCmd(sshConnection, "[ -f '/usr/sbin/dnsmasq' ]")) {
            throw new ConfigurationException("Cannot find dnsmasq at /usr/sbin/dnsmasq on " + _ip);
        }
        SCPClient scp = new SCPClient(sshConnection);
        String editHosts = "scripts/network/exdhcp/dnsmasq_edithosts.sh";
        String editHostsPath = Script.findScript("", editHosts);
        if (editHostsPath == null) {
            throw new ConfigurationException("Can not find script dnsmasq_edithosts.sh at " + editHosts);
        }
        scp.put(editHostsPath, "/usr/bin/", "0755");
        String prepareDnsmasq = "scripts/network/exdhcp/prepare_dnsmasq.sh";
        String prepareDnsmasqPath = Script.findScript("", prepareDnsmasq);
        if (prepareDnsmasqPath == null) {
            throw new ConfigurationException("Can not find script prepare_dnsmasq.sh at " + prepareDnsmasq);
        }
        scp.put(prepareDnsmasqPath, "/usr/bin/", "0755");
        /*
            String prepareCmd = String.format("sh /usr/bin/prepare_dnsmasq.sh %1$s %2$s %3$s", _gateway, _dns, _ip);
            if (!SSHCmdHelper.sshExecuteCmd(sshConnection, prepareCmd)) {
                throw new ConfigurationException("prepare dnsmasq at " + _ip + " failed");
            }
            */
        s_logger.debug("Dnsmasq resource configure successfully");
        return true;
    } catch (Exception e) {
        s_logger.debug("Dnsmasq resorce configure failed", e);
        throw new ConfigurationException(e.getMessage());
    } finally {
        SSHCmdHelper.releaseSshConnection(sshConnection);
    }
}
Also used : SCPClient(com.trilead.ssh2.SCPClient) ConfigurationException(javax.naming.ConfigurationException) ConfigurationException(javax.naming.ConfigurationException)

Example 20 with SCPClient

use of com.trilead.ssh2.SCPClient in project cloudstack by apache.

the class OvmResourceBase method setupServer.

protected void setupServer() throws IOException {
    com.trilead.ssh2.Connection sshConnection = new com.trilead.ssh2.Connection(_ip, 22);
    sshConnection.connect(null, 60000, 60000);
    if (!sshConnection.authenticateWithPassword(_username, _password)) {
        throw new CloudRuntimeException("Unable to authenticate");
    }
    SCPClient scp = new SCPClient(sshConnection);
    String configScriptName = "scripts/vm/hypervisor/ovm/configureOvm.sh";
    String configScriptPath = Script.findScript("", configScriptName);
    if (configScriptPath == null) {
        throw new CloudRuntimeException("Unable to find " + configScriptName);
    }
    scp.put(configScriptPath, "/usr/bin/", "0755");
    if (!SSHCmdHelper.sshExecuteCmd(sshConnection, "sh /usr/bin/configureOvm.sh preSetup")) {
        throw new CloudRuntimeException("Execute configureOvm.sh preSetup failed on " + _ip);
    }
    File tmp = new File(configScriptPath);
    File scriptDir = new File(tmp.getParent());
    File[] scripts = scriptDir.listFiles();
    for (int i = 0; i < scripts.length; i++) {
        File script = scripts[i];
        if (script.getName().equals("configureOvm.sh")) {
            continue;
        }
        if (s_logger.isDebugEnabled()) {
            s_logger.debug("Copying " + script.getPath() + " to " + s_ovsAgentPath + " on " + _ip + " with permission 0644");
        }
        scp.put(script.getPath(), s_ovsAgentPath, "0644");
    }
    sshConnection = SSHCmdHelper.acquireAuthorizedConnection(_ip, _username, _password);
    if (sshConnection == null) {
        throw new CloudRuntimeException(String.format("Cannot connect to ovm host(IP=%1$s, username=%2$s, password=%3$s", _ip, _username, _password));
    }
    if (!SSHCmdHelper.sshExecuteCmd(sshConnection, "sh /usr/bin/configureOvm.sh postSetup")) {
        throw new CloudRuntimeException("Execute configureOvm.sh postSetup failed on " + _ip);
    }
}
Also used : SCPClient(com.trilead.ssh2.SCPClient) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) Connection(com.cloud.ovm.object.Connection) File(java.io.File)

Aggregations

SCPClient (com.trilead.ssh2.SCPClient)22 IOException (java.io.IOException)15 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)13 Connection (com.trilead.ssh2.Connection)12 ConfigurationException (javax.naming.ConfigurationException)7 Session (com.trilead.ssh2.Session)6 File (java.io.File)6 InputStream (java.io.InputStream)6 Connection (com.xensource.xenapi.Connection)4 XenAPIException (com.xensource.xenapi.Types.XenAPIException)4 URISyntaxException (java.net.URISyntaxException)4 URLConnection (java.net.URLConnection)4 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)4 HttpException (org.apache.commons.httpclient.HttpException)4 XmlRpcException (org.apache.xmlrpc.XmlRpcException)4 SFTPClient (org.glassfish.cluster.ssh.sftp.SFTPClient)3 Host (com.xensource.xenapi.Host)2 XenAPIObject (com.xensource.xenapi.XenAPIObject)2 MalformedURLException (java.net.MalformedURLException)2 URI (java.net.URI)2