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;
}
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;
}
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);
}
}
}
Aggregations