Search in sources :

Example 6 with SCPClient

use of ch.ethz.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 7 with SCPClient

use of ch.ethz.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 8 with SCPClient

use of ch.ethz.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 9 with SCPClient

use of ch.ethz.ssh2.SCPClient in project cloudstack by apache.

the class Ovm3HypervisorSupport method setupServer.

/**
 * setupServer:
 * Add the cloudstack plugin and setup the agent.
 * Add the ssh keys to the host.
 *
 * @param c
 * @throws IOException
 */
public Boolean setupServer(String key) throws IOException {
    LOGGER.debug("Setup all bits on agent: " + config.getAgentHostname());
    /* version dependent patching ? */
    try {
        com.trilead.ssh2.Connection sshConnection = SSHCmdHelper.acquireAuthorizedConnection(config.getAgentIp(), config.getAgentSshUserName(), config.getAgentSshPassword());
        if (sshConnection == null) {
            throw new ConfigurationException(String.format("Unable to " + "connect to server(IP=%1$s, username=%2$s, " + "password=%3$s", config.getAgentIp(), config.getAgentSshUserName(), config.getAgentSshPassword()));
        }
        SCPClient scp = new SCPClient(sshConnection);
        String userDataScriptDir = "scripts/vm/hypervisor/ovm3/";
        String userDataScriptPath = Script.findScript("", userDataScriptDir);
        if (userDataScriptPath == null) {
            throw new ConfigurationException("Can not find " + userDataScriptDir);
        }
        String mkdir = "mkdir -p " + config.getAgentScriptsDir();
        if (!SSHCmdHelper.sshExecuteCmd(sshConnection, mkdir)) {
            throw new ConfigurationException("Failed " + mkdir + " on " + config.getAgentHostname());
        }
        for (String script : config.getAgentScripts()) {
            script = userDataScriptPath + "/" + script;
            scp.put(script, config.getAgentScriptsDir(), "0755");
        }
        String prepareCmd = String.format(config.getAgentScriptsDir() + "/" + config.getAgentScript() + " --ssl=" + c.getUseSsl() + " " + "--port=" + c.getPort());
        if (!SSHCmdHelper.sshExecuteCmd(sshConnection, prepareCmd)) {
            throw new ConfigurationException("Failed to insert module on " + config.getAgentHostname());
        } else {
            /* because of OVM 3.3.1 (might be 2000) */
            Thread.sleep(5000);
        }
        CloudstackPlugin cSp = new CloudstackPlugin(c);
        cSp.ovsUploadSshKey(config.getAgentSshKeyFileName(), FileUtils.readFileToString(getSystemVMKeyFile(key)));
        cSp.dom0CheckStorageHealthCheck(config.getAgentScriptsDir(), config.getAgentCheckStorageScript(), config.getCsHostGuid(), config.getAgentStorageCheckTimeout(), config.getAgentStorageCheckInterval());
    } catch (Exception es) {
        LOGGER.error("Unexpected exception ", es);
        String msg = "Unable to install module in agent";
        throw new CloudRuntimeException(msg);
    }
    return true;
}
Also used : SCPClient(com.trilead.ssh2.SCPClient) ConfigurationException(javax.naming.ConfigurationException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) CloudstackPlugin(com.cloud.hypervisor.ovm3.objects.CloudstackPlugin) ConfigurationException(javax.naming.ConfigurationException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) IOException(java.io.IOException) Ovm3ResourceException(com.cloud.hypervisor.ovm3.objects.Ovm3ResourceException)

Example 10 with SCPClient

use of ch.ethz.ssh2.SCPClient in project cloudstack by apache.

the class CitrixResourceBase method setupServer.

/* return : if setup is needed */
public boolean setupServer(final Connection conn, final Host host) {
    final String packageVersion = CitrixResourceBase.class.getPackage().getImplementationVersion();
    final String version = this.getClass().getName() + "-" + (packageVersion == null ? Long.toString(System.currentTimeMillis()) : packageVersion);
    try {
        /* push patches to XenServer */
        final Host.Record hr = host.getRecord(conn);
        final Iterator<String> it = hr.tags.iterator();
        while (it.hasNext()) {
            final String tag = it.next();
            if (tag.startsWith("vmops-version-")) {
                if (tag.contains(version)) {
                    s_logger.info(logX(host, "Host " + hr.address + " is already setup."));
                    return false;
                } else {
                    it.remove();
                }
            }
        }
        final com.trilead.ssh2.Connection sshConnection = new com.trilead.ssh2.Connection(hr.address, 22);
        try {
            sshConnection.connect(null, 60000, 60000);
            if (!sshConnection.authenticateWithPassword(_username, _password.peek())) {
                throw new CloudRuntimeException("Unable to authenticate");
            }
            final String cmd = "mkdir -p /opt/cloud/bin /var/log/cloud";
            if (!SSHCmdHelper.sshExecuteCmd(sshConnection, cmd)) {
                throw new CloudRuntimeException("Cannot create directory /opt/cloud/bin on XenServer hosts");
            }
            final SCPClient scp = new SCPClient(sshConnection);
            final List<File> files = getPatchFiles();
            if (files == null || files.isEmpty()) {
                throw new CloudRuntimeException("Can not find patch file");
            }
            for (final File file : files) {
                final String path = file.getParentFile().getAbsolutePath() + "/";
                final Properties props = PropertiesUtil.loadFromFile(file);
                for (final Map.Entry<Object, Object> entry : props.entrySet()) {
                    final String k = (String) entry.getKey();
                    final String v = (String) entry.getValue();
                    assert k != null && k.length() > 0 && v != null && v.length() > 0 : "Problems with " + k + "=" + v;
                    final String[] tokens = v.split(",");
                    String f = null;
                    if (tokens.length == 3 && tokens[0].length() > 0) {
                        if (tokens[0].startsWith("/")) {
                            f = tokens[0];
                        } else if (tokens[0].startsWith("~")) {
                            final String homedir = System.getenv("HOME");
                            f = homedir + tokens[0].substring(1) + k;
                        } else {
                            f = path + tokens[0] + '/' + k;
                        }
                    } else {
                        f = path + k;
                    }
                    final String directoryPath = tokens[tokens.length - 1];
                    f = f.replace('/', File.separatorChar);
                    String permissions = "0755";
                    if (tokens.length == 3) {
                        permissions = tokens[1];
                    } else if (tokens.length == 2) {
                        permissions = tokens[0];
                    }
                    if (!new File(f).exists()) {
                        s_logger.warn("We cannot locate " + f);
                        continue;
                    }
                    if (s_logger.isDebugEnabled()) {
                        s_logger.debug("Copying " + f + " to " + directoryPath + " on " + hr.address + " with permission " + permissions);
                    }
                    if (!SSHCmdHelper.sshExecuteCmd(sshConnection, "mkdir -m 700 -p " + directoryPath)) {
                        s_logger.debug("Unable to create destination path: " + directoryPath + " on " + hr.address + ".");
                    }
                    try {
                        scp.put(f, directoryPath, permissions);
                    } catch (final IOException e) {
                        final String msg = "Unable to copy file " + f + " to path " + directoryPath + " with permissions  " + permissions;
                        s_logger.debug(msg);
                        throw new CloudRuntimeException("Unable to setup the server: " + msg, e);
                    }
                }
            }
        } catch (final IOException e) {
            throw new CloudRuntimeException("Unable to setup the server correctly", e);
        } finally {
            sshConnection.close();
        }
        hr.tags.add("vmops-version-" + version);
        host.setTags(conn, hr.tags);
        return true;
    } catch (final XenAPIException e) {
        final String msg = "XenServer setup failed due to " + e.toString();
        s_logger.warn(msg, e);
        throw new CloudRuntimeException("Unable to get host information " + e.toString(), e);
    } catch (final XmlRpcException e) {
        final String msg = "XenServer setup failed due to " + e.getMessage();
        s_logger.warn(msg, e);
        throw new CloudRuntimeException("Unable to get host information ", e);
    }
}
Also used : SCPClient(com.trilead.ssh2.SCPClient) Connection(com.xensource.xenapi.Connection) URLConnection(java.net.URLConnection) XenAPIException(com.xensource.xenapi.Types.XenAPIException) Host(com.xensource.xenapi.Host) IOException(java.io.IOException) Properties(java.util.Properties) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) XenAPIObject(com.xensource.xenapi.XenAPIObject) File(java.io.File) Map(java.util.Map) HashMap(java.util.HashMap) XmlRpcException(org.apache.xmlrpc.XmlRpcException)

Aggregations

SCPClient (com.trilead.ssh2.SCPClient)21 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)13 IOException (java.io.IOException)12 ConfigurationException (javax.naming.ConfigurationException)7 Connection (com.trilead.ssh2.Connection)6 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 Connection (ch.ethz.ssh2.Connection)2 SCPClient (ch.ethz.ssh2.SCPClient)2 Host (com.xensource.xenapi.Host)2 XenAPIObject (com.xensource.xenapi.XenAPIObject)2 MalformedURLException (java.net.MalformedURLException)2