use of com.jcraft.jsch.ChannelSftp in project incubator-gobblin by apache.
the class SftpLightWeightFileSystem method open.
@Override
public FSDataInputStream open(Path path, int bufferSize) throws IOException {
SftpGetMonitor monitor = new SftpGetMonitor();
try {
ChannelSftp channelSftp = this.fsHelper.getSftpChannel();
InputStream is = channelSftp.get(HadoopUtils.toUriPath(path), monitor);
return new FSDataInputStream(new BufferedFSInputStream(new SftpFsHelper.SftpFsFileInputStream(is, channelSftp), bufferSize));
} catch (SftpException e) {
throw new IOException(e);
}
}
use of com.jcraft.jsch.ChannelSftp in project incubator-gobblin by apache.
the class SftpLightWeightFileSystem method getFileStatus.
@Override
public FileStatus getFileStatus(Path path) throws IOException {
ChannelSftp channelSftp = null;
ChannelExec channelExec1 = null;
ChannelExec channelExec2 = null;
try {
channelSftp = this.fsHelper.getSftpChannel();
SftpATTRS sftpAttrs = channelSftp.stat(HadoopUtils.toUriPath(path));
FsPermission permission = new FsPermission((short) sftpAttrs.getPermissions());
channelExec1 = this.fsHelper.getExecChannel("id " + sftpAttrs.getUId());
String userName = IOUtils.toString(channelExec1.getInputStream());
channelExec2 = this.fsHelper.getExecChannel("id " + sftpAttrs.getGId());
String groupName = IOUtils.toString(channelExec2.getInputStream());
FileStatus fs = new FileStatus(sftpAttrs.getSize(), sftpAttrs.isDir(), 1, 0l, sftpAttrs.getMTime(), sftpAttrs.getATime(), permission, StringUtils.trimToEmpty(userName), StringUtils.trimToEmpty(groupName), path);
return fs;
} catch (SftpException e) {
throw new IOException(e);
} finally {
safeDisconnect(channelSftp);
safeDisconnect(channelExec1);
safeDisconnect(channelExec2);
}
}
use of com.jcraft.jsch.ChannelSftp in project incubator-gobblin by apache.
the class SftpLightWeightFileSystem method setWorkingDirectory.
@Override
public void setWorkingDirectory(Path path) {
ChannelSftp channelSftp = null;
try {
channelSftp = this.fsHelper.getSftpChannel();
channelSftp.lcd(HadoopUtils.toUriPath(path));
} catch (SftpException e) {
throw new RuntimeException("Failed to set working directory", e);
} finally {
safeDisconnect(channelSftp);
}
}
use of com.jcraft.jsch.ChannelSftp in project incubator-gobblin by apache.
the class SftpLightWeightFileSystem method getWorkingDirectory.
@Override
public Path getWorkingDirectory() {
ChannelSftp channelSftp = null;
try {
channelSftp = this.fsHelper.getSftpChannel();
Path workingDir = new Path(channelSftp.pwd());
return workingDir;
} catch (SftpException e) {
return null;
} finally {
safeDisconnect(channelSftp);
}
}
use of com.jcraft.jsch.ChannelSftp in project acceptance-test-harness by jenkinsci.
the class GitRepo method transferToDockerContainer.
/**
* Zip bare repository, copy to Docker container using sftp, then unzip.
* The repo is now accessible over "ssh://git@ip:port/home/git/gitRepo.git"
*
* @param host IP of Docker container
* @param port SSH port of Docker container
*/
public void transferToDockerContainer(String host, int port) {
try {
Path zipPath = Files.createTempFile("git", "zip");
File zippedRepo = zipPath.toFile();
String zippedFilename = zipPath.getFileName().toString();
ZipUtil.pack(new File(dir.getPath()), zippedRepo);
Properties props = new Properties();
props.put("StrictHostKeyChecking", "no");
JSch jSch = new JSch();
jSch.addIdentity(privateKey.getAbsolutePath());
Session session = jSch.getSession("git", host, port);
session.setConfig(props);
session.connect();
ChannelSftp channel = (ChannelSftp) session.openChannel("sftp");
channel.connect();
channel.cd("/home/git");
channel.put(new FileInputStream(zippedRepo), zippedFilename);
ChannelExec channelExec = (ChannelExec) session.openChannel("exec");
InputStream in = channelExec.getInputStream();
channelExec.setCommand("unzip " + zippedFilename + " -d " + REPO_NAME);
channelExec.connect();
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
String line;
int index = 0;
while ((line = reader.readLine()) != null) {
System.out.println(++index + " : " + line);
}
channelExec.disconnect();
channel.disconnect();
session.disconnect();
Files.delete(zipPath);
} catch (IOException | JSchException | SftpException e) {
throw new AssertionError("Can't transfer git repository to docker container", e);
}
}
Aggregations