Search in sources :

Example 26 with SCPClient

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

the class CitrixResourceBase method copyConfigDriveIsoToHost.

public boolean copyConfigDriveIsoToHost(final Connection conn, final SR sr, final String vmName) {
    final String vmIso = "/tmp/" + vmName + "/configDrive/" + vmName + ".iso";
    // scp file into the host
    final com.trilead.ssh2.Connection sshConnection = new com.trilead.ssh2.Connection(_host.getIp(), 22);
    try {
        sshConnection.connect(null, 60000, 60000);
        if (!sshConnection.authenticateWithPassword(_username, _password.peek())) {
            throw new CloudRuntimeException("Unable to authenticate");
        }
        s_logger.debug("scp config drive iso file " + vmIso + " to host " + _host.getIp() + " path " + _configDriveIsopath);
        final SCPClient scp = new SCPClient(sshConnection);
        final String p = "0755";
        scp.put(vmIso, _configDriveIsopath, p);
        sr.scan(conn);
        s_logger.debug("copied config drive iso to host " + _host);
    } catch (final IOException e) {
        s_logger.debug("failed to copy configdrive iso " + vmIso + " to host " + _host, e);
        return false;
    } catch (final XmlRpcException e) {
        s_logger.debug("Failed to scan config drive iso SR " + _configDriveSRName + _host.getIp() + " in host " + _host, e);
        return false;
    } finally {
        sshConnection.close();
        // clean up the config drive files
        final String configDir = "/tmp/" + vmName;
        try {
            deleteLocalFolder(configDir);
            s_logger.debug("Successfully cleaned up config drive directory " + configDir + " after copying it to host ");
        } catch (final Exception e) {
            s_logger.debug("Failed to delete config drive folder :" + configDir + " for VM " + vmName + " " + e.getMessage());
        }
    }
    return true;
}
Also used : SCPClient(com.trilead.ssh2.SCPClient) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) Connection(com.xensource.xenapi.Connection) URLConnection(java.net.URLConnection) IOException(java.io.IOException) XmlRpcException(org.apache.xmlrpc.XmlRpcException) XenAPIException(com.xensource.xenapi.Types.XenAPIException) XmlRpcException(org.apache.xmlrpc.XmlRpcException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) TimeoutException(java.util.concurrent.TimeoutException) SAXException(org.xml.sax.SAXException) InternalErrorException(com.cloud.exception.InternalErrorException) ConfigurationException(javax.naming.ConfigurationException) MalformedURLException(java.net.MalformedURLException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException)

Example 27 with SCPClient

use of com.trilead.ssh2.SCPClient in project cosmic by MissionCriticalCloud.

the class CitrixResourceBase method copyConfigDriveIsoToHost.

public boolean copyConfigDriveIsoToHost(final Connection conn, final SR sr, final String vmName) {
    final String vmIso = "/tmp/" + vmName + "/configDrive/" + vmName + ".iso";
    // scp file into the host
    final com.trilead.ssh2.Connection sshConnection = new com.trilead.ssh2.Connection(_host.getIp(), 22);
    try {
        sshConnection.connect(null, 60000, 60000);
        if (!sshConnection.authenticateWithPassword(_username, _password.peek())) {
            throw new CloudRuntimeException("Unable to authenticate");
        }
        s_logger.debug("scp config drive iso file " + vmIso + " to host " + _host.getIp() + " path " + _configDriveIsopath);
        final SCPClient scp = new SCPClient(sshConnection);
        final String p = "0755";
        scp.put(vmIso, _configDriveIsopath, p);
        sr.scan(conn);
        s_logger.debug("copied config drive iso to host " + _host);
    } catch (final IOException e) {
        s_logger.debug("failed to copy configdrive iso " + vmIso + " to host " + _host, e);
        return false;
    } catch (final XmlRpcException e) {
        s_logger.debug("Failed to scan config drive iso SR " + _configDriveSRName + _host.getIp() + " in host " + _host, e);
        return false;
    } finally {
        sshConnection.close();
        // clean up the config drive files
        final String configDir = "/tmp/" + vmName;
        try {
            deleteLocalFolder(configDir);
            s_logger.debug("Successfully cleaned up config drive directory " + configDir + " after copying it to host ");
        } catch (final Exception e) {
            s_logger.debug("Failed to delete config drive folder :" + configDir + " for VM " + vmName + " " + e.getMessage());
        }
    }
    return true;
}
Also used : SCPClient(com.trilead.ssh2.SCPClient) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) Connection(com.xensource.xenapi.Connection) URLConnection(java.net.URLConnection) IOException(java.io.IOException) XmlRpcException(org.apache.xmlrpc.XmlRpcException) XenAPIException(com.xensource.xenapi.Types.XenAPIException) XmlRpcException(org.apache.xmlrpc.XmlRpcException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) TimeoutException(java.util.concurrent.TimeoutException) SAXException(org.xml.sax.SAXException) ConfigurationException(javax.naming.ConfigurationException) MalformedURLException(java.net.MalformedURLException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException)

Example 28 with SCPClient

use of com.trilead.ssh2.SCPClient in project cosmic by MissionCriticalCloud.

the class ScpTemplateDownloader method download.

@Override
public long download(final boolean resume, final DownloadCompleteCallback callback) {
    if (_status == Status.ABORTED || _status == Status.UNRECOVERABLE_ERROR || _status == Status.DOWNLOAD_FINISHED) {
        return 0;
    }
    _resume = resume;
    _start = System.currentTimeMillis();
    final URI uri;
    try {
        uri = new URI(_downloadUrl);
    } catch (final URISyntaxException e1) {
        _status = Status.UNRECOVERABLE_ERROR;
        return 0;
    }
    final String username = uri.getUserInfo();
    final String queries = uri.getQuery();
    String password = null;
    if (queries != null) {
        final String[] qs = queries.split("&");
        for (final String q : qs) {
            final String[] tokens = q.split("=");
            if (tokens[0].equalsIgnoreCase("password")) {
                password = tokens[1];
                break;
            }
        }
    }
    int port = uri.getPort();
    if (port == -1) {
        port = 22;
    }
    final File file = new File(_toFile);
    final com.trilead.ssh2.Connection sshConnection = new com.trilead.ssh2.Connection(uri.getHost(), port);
    try {
        if (_storage != null) {
            file.createNewFile();
            _storage.setWorldReadableAndWriteable(file);
        }
        sshConnection.connect(null, 60000, 60000);
        if (!sshConnection.authenticateWithPassword(username, password)) {
            throw new CloudRuntimeException("Unable to authenticate");
        }
        final SCPClient scp = new SCPClient(sshConnection);
        final String src = uri.getPath();
        _status = Status.IN_PROGRESS;
        scp.get(src, _toDir);
        if (!file.exists()) {
            _status = Status.UNRECOVERABLE_ERROR;
            s_logger.debug("unable to scp the file " + _downloadUrl);
            return 0;
        }
        _status = Status.DOWNLOAD_FINISHED;
        _totalBytes = file.length();
        final String downloaded = "(download complete)";
        _errorString = "Downloaded " + _remoteSize + " bytes " + downloaded;
        _downloadTime += System.currentTimeMillis() - _start;
        return _totalBytes;
    } catch (final Exception e) {
        s_logger.warn("Unable to download " + _downloadUrl, e);
        _status = TemplateDownloader.Status.UNRECOVERABLE_ERROR;
        _errorString = e.getMessage();
        return 0;
    } finally {
        sshConnection.close();
        if (_status == Status.UNRECOVERABLE_ERROR && file.exists()) {
            file.delete();
        }
        if (callback != null) {
            callback.downloadComplete(_status);
        }
    }
}
Also used : SCPClient(com.trilead.ssh2.SCPClient) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) URISyntaxException(java.net.URISyntaxException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) 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