Search in sources :

Example 86 with ChannelSftp

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);
    }
}
Also used : BufferedFSInputStream(org.apache.hadoop.fs.BufferedFSInputStream) ChannelSftp(com.jcraft.jsch.ChannelSftp) BufferedFSInputStream(org.apache.hadoop.fs.BufferedFSInputStream) FSDataInputStream(org.apache.hadoop.fs.FSDataInputStream) SeekableFSInputStream(org.apache.gobblin.util.io.SeekableFSInputStream) InputStream(java.io.InputStream) SftpException(com.jcraft.jsch.SftpException) FSDataInputStream(org.apache.hadoop.fs.FSDataInputStream) SftpGetMonitor(org.apache.gobblin.source.extractor.extract.sftp.SftpFsHelper.SftpGetMonitor) IOException(java.io.IOException)

Example 87 with ChannelSftp

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);
    }
}
Also used : ChannelSftp(com.jcraft.jsch.ChannelSftp) FileStatus(org.apache.hadoop.fs.FileStatus) SftpATTRS(com.jcraft.jsch.SftpATTRS) SftpException(com.jcraft.jsch.SftpException) FsPermission(org.apache.hadoop.fs.permission.FsPermission) IOException(java.io.IOException) ChannelExec(com.jcraft.jsch.ChannelExec)

Example 88 with ChannelSftp

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);
    }
}
Also used : ChannelSftp(com.jcraft.jsch.ChannelSftp) SftpException(com.jcraft.jsch.SftpException)

Example 89 with 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);
    }
}
Also used : Path(org.apache.hadoop.fs.Path) ChannelSftp(com.jcraft.jsch.ChannelSftp) SftpException(com.jcraft.jsch.SftpException)

Example 90 with 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);
    }
}
Also used : Path(java.nio.file.Path) JSchException(com.jcraft.jsch.JSchException) InputStreamReader(java.io.InputStreamReader) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) SftpException(com.jcraft.jsch.SftpException) IOException(java.io.IOException) Properties(java.util.Properties) JSch(com.jcraft.jsch.JSch) FileInputStream(java.io.FileInputStream) ChannelExec(com.jcraft.jsch.ChannelExec) ChannelSftp(com.jcraft.jsch.ChannelSftp) BufferedReader(java.io.BufferedReader) File(java.io.File) Session(com.jcraft.jsch.Session)

Aggregations

ChannelSftp (com.jcraft.jsch.ChannelSftp)99 SftpException (com.jcraft.jsch.SftpException)61 IOException (java.io.IOException)36 JSchException (com.jcraft.jsch.JSchException)28 Session (com.jcraft.jsch.Session)25 JSch (com.jcraft.jsch.JSch)20 Channel (com.jcraft.jsch.Channel)17 LsEntry (com.jcraft.jsch.ChannelSftp.LsEntry)17 File (java.io.File)16 Test (org.junit.Test)14 InputStream (java.io.InputStream)12 SftpATTRS (com.jcraft.jsch.SftpATTRS)11 FileNotFoundException (java.io.FileNotFoundException)9 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)8 FileInputStream (java.io.FileInputStream)7 OutputStream (java.io.OutputStream)7 Path (org.apache.hadoop.fs.Path)7 CoreException (org.eclipse.core.runtime.CoreException)7 IStatus (org.eclipse.core.runtime.IStatus)7 Status (org.eclipse.core.runtime.Status)7