Search in sources :

Example 1 with SSHApiException

use of org.apache.airavata.gfac.core.SSHApiException in project airavata by apache.

the class BESJobSubmissionTask method copyInputFilesToLocal.

private void copyInputFilesToLocal(TaskContext taskContext) throws GFacException {
    ProcessContext pc = taskContext.getParentProcessContext();
    StorageResourceDescription storageResource = pc.getStorageResource();
    if (storageResource != null) {
        hostName = storageResource.getHostName();
    } else {
        throw new GFacException("Storage Resource is null");
    }
    inputPath = pc.getStorageFileSystemRootLocation();
    inputPath = (inputPath.endsWith(File.separator) ? inputPath : inputPath + File.separator);
    String remoteFilePath = null, fileName = null, localFilePath = null;
    URI remoteFileURI = null;
    try {
        authenticationInfo = Factory.getStorageSSHKeyAuthentication(pc);
        ServerInfo serverInfo = pc.getStorageResourceServerInfo();
        Session sshSession = Factory.getSSHSession(authenticationInfo, serverInfo);
        List<InputDataObjectType> processInputs = pc.getProcessModel().getProcessInputs();
        for (InputDataObjectType input : processInputs) {
            if (input.getType() == DataType.URI) {
                remoteFileURI = new URI(input.getValue());
                remoteFilePath = remoteFileURI.getPath();
                fileName = remoteFilePath.substring(remoteFilePath.lastIndexOf("/") + 1);
                localFilePath = pc.getInputDir() + File.separator + fileName;
                log.info("SCP remote file :{} -> to local :{}", remoteFilePath, localFilePath);
                SSHUtils.scpFrom(remoteFilePath, localFilePath, sshSession);
                input.setValue("file:/" + localFilePath);
            }
        }
    } catch (IOException | JSchException | SSHApiException | URISyntaxException e) {
        log.error("Error while coping remote file " + remoteFilePath + " to local " + localFilePath, e);
        throw new GFacException("Error while scp input files to local file location", e);
    } catch (CredentialStoreException e) {
        String msg = "Authentication issue, make sure you are passing valid credential token";
        log.error(msg, e);
        throw new GFacException(msg, e);
    }
}
Also used : JSchException(com.jcraft.jsch.JSchException) ServerInfo(org.apache.airavata.gfac.core.cluster.ServerInfo) InputDataObjectType(org.apache.airavata.model.application.io.InputDataObjectType) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) CredentialStoreException(org.apache.airavata.credential.store.store.CredentialStoreException) URI(java.net.URI) SSHApiException(org.apache.airavata.gfac.core.SSHApiException) ProcessContext(org.apache.airavata.gfac.core.context.ProcessContext) StorageResourceDescription(org.apache.airavata.model.appcatalog.storageresource.StorageResourceDescription) GFacException(org.apache.airavata.gfac.core.GFacException) Session(com.jcraft.jsch.Session)

Example 2 with SSHApiException

use of org.apache.airavata.gfac.core.SSHApiException in project airavata by apache.

the class SSHUtils method scpFrom.

/**
 * This method will copy a remote file to a local directory
 *
 * @param remoteFile remote file path, this has to be a full qualified path
 * @param localFile  This is the local file to copy, this can be a directory too
 * @return returns the final local file path of the new file came from the remote resource
 */
public static void scpFrom(String remoteFile, String localFile, Session session) throws IOException, JSchException, SSHApiException {
    FileOutputStream fos = null;
    try {
        String prefix = null;
        if (new File(localFile).isDirectory()) {
            prefix = localFile + File.separator;
        }
        // exec 'scp -f remotefile' remotely
        String command = "scp -f " + remoteFile;
        Channel channel = session.openChannel("exec");
        ((ChannelExec) channel).setCommand(command);
        StandardOutReader stdOutReader = new StandardOutReader();
        ((ChannelExec) channel).setErrStream(stdOutReader.getStandardError());
        // get I/O streams for remote scp
        OutputStream out = channel.getOutputStream();
        InputStream in = channel.getInputStream();
        if (!channel.isClosed()) {
            channel.connect();
        }
        byte[] buf = new byte[1024];
        // send '\0'
        buf[0] = 0;
        out.write(buf, 0, 1);
        out.flush();
        while (true) {
            int c = checkAck(in);
            if (c != 'C') {
                break;
            }
            // read '0644 '
            in.read(buf, 0, 5);
            long filesize = 0L;
            while (true) {
                if (in.read(buf, 0, 1) < 0) {
                    // error
                    break;
                }
                if (buf[0] == ' ')
                    break;
                filesize = filesize * 10L + (long) (buf[0] - '0');
            }
            String file = null;
            for (int i = 0; ; i++) {
                in.read(buf, i, 1);
                if (buf[i] == (byte) 0x0a) {
                    file = new String(buf, 0, i);
                    break;
                }
            }
            // System.out.println("filesize="+filesize+", file="+file);
            // send '\0'
            buf[0] = 0;
            out.write(buf, 0, 1);
            out.flush();
            // read a content of lfile
            fos = new FileOutputStream(prefix == null ? localFile : prefix + file);
            int foo;
            while (true) {
                if (buf.length < filesize)
                    foo = buf.length;
                else
                    foo = (int) filesize;
                foo = in.read(buf, 0, foo);
                if (foo < 0) {
                    // error
                    break;
                }
                fos.write(buf, 0, foo);
                filesize -= foo;
                if (filesize == 0L)
                    break;
            }
            fos.close();
            fos = null;
            if (checkAck(in) != 0) {
                String error = "Error transfering the file content";
                log.error(error);
                throw new SSHApiException(error);
            }
            // send '\0'
            buf[0] = 0;
            out.write(buf, 0, 1);
            out.flush();
        }
        stdOutReader.onOutput(channel);
        if (stdOutReader.getStdErrorString().contains("scp:")) {
            throw new SSHApiException(stdOutReader.getStdErrorString());
        }
    } catch (Exception e) {
        log.error(e.getMessage(), e);
    } finally {
        try {
            if (fos != null)
                fos.close();
        } catch (Exception ee) {
        }
    }
}
Also used : FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) Channel(com.jcraft.jsch.Channel) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) SSHApiException(org.apache.airavata.gfac.core.SSHApiException) ChannelExec(com.jcraft.jsch.ChannelExec) GFacException(org.apache.airavata.gfac.core.GFacException) SSHApiException(org.apache.airavata.gfac.core.SSHApiException) IOException(java.io.IOException) JSchException(com.jcraft.jsch.JSchException) FileOutputStream(java.io.FileOutputStream) File(java.io.File)

Example 3 with SSHApiException

use of org.apache.airavata.gfac.core.SSHApiException in project airavata by apache.

the class SSHUtils method scpTo.

/**
 * This will copy a local file to a remote location
 *
 * @param remoteFile remote location you want to transfer the file, this cannot be a directory, if user pass
 *                   a dirctory we do copy it to that directory but we simply return the directory name
 *                   todo handle the directory name as input and return the proper final output file name
 * @param localFile  Local file to transfer, this can be a directory
 * @return returns the final remote file path, so that users can use the new file location
 */
public static String scpTo(String localFile, String remoteFile, Session session) throws IOException, JSchException, SSHApiException {
    FileInputStream fis = null;
    String prefix = null;
    if (new File(localFile).isDirectory()) {
        prefix = localFile + File.separator;
    }
    boolean ptimestamp = true;
    // exec 'scp -t rfile' remotely
    String command = "scp " + (ptimestamp ? "-p" : "") + " -t " + remoteFile;
    Channel channel = session.openChannel("exec");
    StandardOutReader stdOutReader = new StandardOutReader();
    ((ChannelExec) channel).setErrStream(stdOutReader.getStandardError());
    ((ChannelExec) channel).setCommand(command);
    // get I/O streams for remote scp
    OutputStream out = channel.getOutputStream();
    InputStream in = channel.getInputStream();
    channel.connect();
    if (checkAck(in) != 0) {
        String error = "Error Reading input Stream";
        log.error(error);
        throw new SSHApiException(error);
    }
    File _lfile = new File(localFile);
    if (ptimestamp) {
        command = "T" + (_lfile.lastModified() / 1000) + " 0";
        // The access time should be sent here,
        // but it is not accessible with JavaAPI ;-<
        command += (" " + (_lfile.lastModified() / 1000) + " 0\n");
        out.write(command.getBytes());
        out.flush();
        if (checkAck(in) != 0) {
            String error = "Error Reading input Stream";
            log.error(error);
            throw new SSHApiException(error);
        }
    }
    // send "C0644 filesize filename", where filename should not include '/'
    long filesize = _lfile.length();
    command = "C0644 " + filesize + " ";
    if (localFile.lastIndexOf('/') > 0) {
        command += localFile.substring(localFile.lastIndexOf('/') + 1);
    } else {
        command += localFile;
    }
    command += "\n";
    out.write(command.getBytes());
    out.flush();
    if (checkAck(in) != 0) {
        String error = "Error Reading input Stream";
        log.error(error);
        throw new SSHApiException(error);
    }
    // send a content of localFile
    fis = new FileInputStream(localFile);
    byte[] buf = new byte[1024];
    while (true) {
        int len = fis.read(buf, 0, buf.length);
        if (len <= 0)
            break;
        // out.flush();
        out.write(buf, 0, len);
    }
    fis.close();
    fis = null;
    // send '\0'
    buf[0] = 0;
    out.write(buf, 0, 1);
    out.flush();
    if (checkAck(in) != 0) {
        String error = "Error Reading input Stream";
        log.error(error);
        throw new SSHApiException(error);
    }
    out.close();
    stdOutReader.onOutput(channel);
    channel.disconnect();
    if (stdOutReader.getStdErrorString().contains("scp:")) {
        throw new SSHApiException(stdOutReader.getStdErrorString());
    }
    // since remote file is always a file  we just return the file
    return remoteFile;
}
Also used : FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) Channel(com.jcraft.jsch.Channel) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) SSHApiException(org.apache.airavata.gfac.core.SSHApiException) FileInputStream(java.io.FileInputStream) ChannelExec(com.jcraft.jsch.ChannelExec) File(java.io.File)

Example 4 with SSHApiException

use of org.apache.airavata.gfac.core.SSHApiException in project airavata by apache.

the class BESJobSubmissionTask method copyOutputFilesToStorage.

private void copyOutputFilesToStorage(TaskContext taskContext, List<OutputDataObjectType> copyOutput) throws GFacException {
    ProcessContext pc = taskContext.getParentProcessContext();
    String remoteFilePath = null, fileName = null, localFilePath = null;
    try {
        authenticationInfo = Factory.getStorageSSHKeyAuthentication(pc);
        ServerInfo serverInfo = pc.getComputeResourceServerInfo();
        Session sshSession = Factory.getSSHSession(authenticationInfo, serverInfo);
        for (OutputDataObjectType output : copyOutput) {
            switch(output.getType()) {
                case STDERR:
                case STDOUT:
                case STRING:
                case URI:
                    localFilePath = output.getValue();
                    if (localFilePath.contains("://")) {
                        localFilePath = localFilePath.substring(localFilePath.indexOf("://") + 2, localFilePath.length());
                    }
                    fileName = localFilePath.substring(localFilePath.lastIndexOf("/") + 1);
                    URI destinationURI = TaskUtils.getDestinationURI(taskContext, hostName, inputPath, fileName);
                    remoteFilePath = destinationURI.getPath();
                    log.info("SCP local file :{} -> from remote :{}", localFilePath, remoteFilePath);
                    SSHUtils.scpTo(localFilePath, remoteFilePath, sshSession);
                    output.setValue(destinationURI.toString());
                    break;
                default:
                    break;
            }
        }
    } catch (IOException | JSchException | SSHApiException | URISyntaxException | CredentialStoreException e) {
        log.error("Error while coping local file " + localFilePath + " to remote " + remoteFilePath, e);
        throw new GFacException("Error while scp output files to remote storage file location", e);
    }
}
Also used : JSchException(com.jcraft.jsch.JSchException) ServerInfo(org.apache.airavata.gfac.core.cluster.ServerInfo) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) CredentialStoreException(org.apache.airavata.credential.store.store.CredentialStoreException) URI(java.net.URI) SSHApiException(org.apache.airavata.gfac.core.SSHApiException) ProcessContext(org.apache.airavata.gfac.core.context.ProcessContext) OutputDataObjectType(org.apache.airavata.model.application.io.OutputDataObjectType) GFacException(org.apache.airavata.gfac.core.GFacException) Session(com.jcraft.jsch.Session)

Aggregations

SSHApiException (org.apache.airavata.gfac.core.SSHApiException)4 JSchException (com.jcraft.jsch.JSchException)3 IOException (java.io.IOException)3 GFacException (org.apache.airavata.gfac.core.GFacException)3 Channel (com.jcraft.jsch.Channel)2 ChannelExec (com.jcraft.jsch.ChannelExec)2 Session (com.jcraft.jsch.Session)2 File (java.io.File)2 FileInputStream (java.io.FileInputStream)2 FileOutputStream (java.io.FileOutputStream)2 InputStream (java.io.InputStream)2 OutputStream (java.io.OutputStream)2 URI (java.net.URI)2 URISyntaxException (java.net.URISyntaxException)2 CredentialStoreException (org.apache.airavata.credential.store.store.CredentialStoreException)2 ServerInfo (org.apache.airavata.gfac.core.cluster.ServerInfo)2 ProcessContext (org.apache.airavata.gfac.core.context.ProcessContext)2 StorageResourceDescription (org.apache.airavata.model.appcatalog.storageresource.StorageResourceDescription)1 InputDataObjectType (org.apache.airavata.model.application.io.InputDataObjectType)1 OutputDataObjectType (org.apache.airavata.model.application.io.OutputDataObjectType)1