Package org.glassfish.cluster.ssh.sftp

Examples of org.glassfish.cluster.ssh.sftp.SFTPClient


        openConnection();
        logger.fine("Connection settings valid");
        String testPath = installDir;
        if (StringUtils.ok(testPath)) {
            // Validate if installDir exists
            SFTPClient sftpClient = new SFTPClient(connection);
            if (sftpClient.exists(testPath)) {
                // installDir exists. Now check for landmark if provided
                if (StringUtils.ok(landmarkPath)) {                   
                    testPath = installDir + "/" + landmarkPath;
                }
                validInstallDir = sftpClient.exists(testPath);
            } else {
                validInstallDir = false;
            }
            SSHUtil.unregister(connection);
            connection = null;
View Full Code Here


                             installDir, null, logger);
    }

    public SFTPClient getSFTPClient() throws IOException {
        openConnection();
        SFTPClient sftpClient = new SFTPClient(connection);
        return sftpClient;
    }
View Full Code Here

        if (!ret) {
            throw new IOException("SSH password authentication failed for user " + userName + " on host " + node);
        }
        //initiate scp client
        SCPClient scp = new SCPClient(conn);
        SFTPClient sftp = new SFTPClient(connection);

        if (key.exists()) {

            //fixes .ssh file mode
            setupSSHDir();

            if (pubKeyFile == null) {
                pubKeyFile = keyFile + ".pub";
            }

            File pubKey = new File(pubKeyFile);
            if(!pubKey.exists()) {
                throw new IOException("Public key file " + pubKeyFile + " does not exist.");
            }

            try {
                if(!sftp.exists(SSH_DIR)) {
                    if(logger.isLoggable(Level.FINER)) {
                        logger.fine(SSH_DIR + " does not exist");
                    }
                    sftp.mkdirs(".ssh", 0700);
                }
            } catch (Exception e) {
                if(logger.isLoggable(Level.FINER)) {
                    e.printStackTrace();
                }
                throw new IOException("Error while creating .ssh directory on remote host:" + e.getMessage());
            }

            //copy over the public key to remote host
            scp.put(pubKey.getAbsolutePath(), "key.tmp", ".ssh", "0600");           

            //append the public key file contents to authorized_keys file on remote host
            String mergeCommand = "cd .ssh; cat key.tmp >> " + AUTH_KEY_FILE;
            if(logger.isLoggable(Level.FINER)) {
                logger.finer("mergeCommand = " + mergeCommand);
            }
            if(conn.exec(mergeCommand, new ByteArrayOutputStream())!=0) {
                throw new IOException("Failed to propogate the public key " + pubKeyFile + " to " + host);
            }
            logger.info("Copied keyfile " + pubKeyFile + " to " + userName + "@" + host);

            //remove the public key file on remote host
            if(conn.exec("rm .ssh/key.tmp", new ByteArrayOutputStream())!=0) {
                logger.warning("WARNING: Failed to remove the public key file key.tmp on remote host " + host);
            }
            if(logger.isLoggable(Level.FINER)) {
                logger.finer("Removed the temporary key file on remote host");
            }
           
            //Lets fix all the permissions
            //On MKS, chmod doesn't work as expected. StrictMode needs to be disabled
            //for connection to go through
            logger.info("Fixing file permissions for home(755), .ssh(700) and authorized_keys file(644)");
            sftp.chmod(".", 0755);
            sftp.chmod(SSH_DIR, 0700);
            sftp.chmod(SSH_DIR + AUTH_KEY_FILE, 0644);
            //release the connections
            sftp.close();
            conn.close();
            conn = null;
        }
    }
View Full Code Here

        if (node.getType().equals("SSH")) {

            sshL.init(node, logger);

            SFTPClient sftpClient = sshL.getSFTPClient();

            File logFileDirectoryOnServer = makingDirectory(domainRoot + File.separator + "logs"
                    + File.separator + instanceName);

            boolean noFileFound = true;

            String loggingDir = getLoggingDirectoryForNode(instanceLogFileName, node, sNode, instanceName);

            try {
                Vector instanceLogFileNames = sftpClient.ls(loggingDir);

                for (int i = 0; i < instanceLogFileNames.size(); i++) {
                    SFTPv3DirectoryEntry file = (SFTPv3DirectoryEntry) instanceLogFileNames.get(i);
                    String fileName = file.filename;
                    // code to remove . and .. file which is return from sftpclient ls method
                    if (!file.attributes.isDirectory() && !fileName.equals(".") && !fileName.equals("..")
                            && fileName.contains(".log") && !fileName.contains(".log.")) {
                        noFileFound = false;
                        break;
                    }
                }
            } catch (Exception e) {
                // if directory doesn't present or missing on remote machine. It happens due to bug 16451
                noFileFound = true;
            }

            if (noFileFound) {
                // this loop is used when user has changed value for server.log but not restarted the server.
                loggingDir = getLoggingDirectoryForNodeWhenNoFilesFound(instanceLogFileName, node, sNode, instanceName);
            }

            String loggingFile = loggingDir + File.separator + logFileName;
            if (!sftpClient.exists(loggingFile)) {
                loggingFile = loggingDir + File.separator + "server.log";
            } else if (!sftpClient.exists(loggingFile)) {
                loggingFile = instanceLogFileName;
            }

            // creating local file name on DAS
            long instanceLogFileSize = 0;
            instanceLogFile = new File(logFileDirectoryOnServer.getAbsolutePath() + File.separator
                    + loggingFile.substring(loggingFile.lastIndexOf(File.separator), loggingFile.length()));

            // getting size of the file on DAS
            if (instanceLogFile.exists())
                instanceLogFileSize = instanceLogFile.length();

            SFTPv3FileAttributes sftPv3FileAttributes = sftpClient._stat(loggingFile);

            // getting size of the file on instance machine
            long fileSizeOnNode = sftPv3FileAttributes.size;

            // if differ both size then downloading
            if (instanceLogFileSize != fileSizeOnNode) {
                BufferedInputStream in = null;
                FileOutputStream file = null;
                BufferedOutputStream out = null;
                try {
                InputStream inputStream = sftpClient.read(loggingFile);
                in = new BufferedInputStream(inputStream);
                file = new FileOutputStream(instanceLogFile);
                out = new BufferedOutputStream(file);
                int i;
                while ((i = in.read()) != -1) {
                    out.write(i);
                }
                out.flush();
                } finally {
                    if (out != null) try { out.close(); } catch (IOException ex) {}
                    if (in != null) try { in.close(); } catch (IOException ex) {}
                }
            }

            sftpClient.close();
        } else if (node.getType().equals("DCOM")) {

            File logFileDirectoryOnServer = makingDirectory(domainRoot + File.separator + "logs"
                    + File.separator + instanceName);
View Full Code Here

            Vector allInstanceLogFileName = getInstanceLogFileNames(habitat, targetServer, domain, logger, instanceName, instanceLogFileDirectory);

            boolean noFileFound = true;
            String sourceDir = getLoggingDirectoryForNode(instanceLogFileDirectory, node, sNode, instanceName);
            SFTPClient sftpClient = sshL.getSFTPClient();

            try {
                Vector instanceLogFileNames = sftpClient.ls(sourceDir);

                for (int i = 0; i < instanceLogFileNames.size(); i++) {
                    SFTPv3DirectoryEntry file = (SFTPv3DirectoryEntry) instanceLogFileNames.get(i);
                    String fileName = file.filename;
                    // code to remove . and .. file which is return from sftpclient ls method
                    if (!file.attributes.isDirectory() && !fileName.equals(".") && !fileName.equals("..")
                            && fileName.contains(".log") && !fileName.contains(".log.")) {
                        noFileFound = false;
                        break;
                    }
                }
            } catch (Exception e) {
                // if directory doesn't present or missing on remote machine. It happens due to bug 16451
                noFileFound = true;
            }

            if (noFileFound) {
                // this loop is used when user has changed value for server.log but not restarted the server.
                sourceDir = getLoggingDirectoryForNodeWhenNoFilesFound(instanceLogFileDirectory, node, sNode, instanceName);
            }

            String[] remoteFileNames = new String[allInstanceLogFileName.size()];
            for (int i = 0; i < allInstanceLogFileName.size(); i++) {
                remoteFileNames[i] = sourceDir + File.separator + allInstanceLogFileName.get(i);
            }

            sftpClient.close();

            SCPClient scpClient = sshL.getSCPClient();
            scpClient.get(remoteFileNames, tempDirectoryOnServer);
        } else if (node.getType().equals("DCOM")) {
View Full Code Here

            }
        } else if (node.getType().equals("SSH")) {
            // this code is used if DAS and instance are running on different machine
            SSHLauncher sshL = getSSHL(habitat);
            sshL.init(node, logger);
            SFTPClient sftpClient = sshL.getSFTPClient();

            boolean noFileFound = true;

            String loggingDir = getLoggingDirectoryForNode(instanceLogFileDetails, node, sNode, instanceName);

            try {
                instanceLogFileNames = sftpClient.ls(loggingDir);
                for (int i = 0; i < instanceLogFileNames.size(); i++) {
                    SFTPv3DirectoryEntry file = (SFTPv3DirectoryEntry) instanceLogFileNames.get(i);
                    String fileName = file.filename;
                    // code to remove . and .. file which is return from sftpclient ls method
                    if (!file.attributes.isDirectory() && !fileName.equals(".") && !fileName.equals("..")
                            && fileName.contains(".log") && !fileName.contains(".log.")) {
                        instanceLogFileNamesAsString.add(fileName);
                        noFileFound = false;
                    }
                }
            } catch (Exception ex) {
                // if directory doesn't present or missing on remote machine. It happens due to bug 16451
                noFileFound = true;
            }

            if (noFileFound) {
                // this loop is used when user has changed value for server.log but not restarted the server.
                loggingDir = getLoggingDirectoryForNodeWhenNoFilesFound(instanceLogFileDetails, node, sNode, instanceName);
                instanceLogFileNames = sftpClient.ls(loggingDir);


                for (int i = 0; i < instanceLogFileNames.size(); i++) {
                    SFTPv3DirectoryEntry file = (SFTPv3DirectoryEntry) instanceLogFileNames.get(i);
                    String fileName = file.filename;
                    // code to remove . and .. file which is return from sftpclient ls method
                    if (!file.attributes.isDirectory() && !fileName.equals(".") && !fileName.equals("..")
                            && fileName.contains(".log") && !fileName.contains(".log.")) {
                        instanceLogFileNamesAsString.add(fileName);
                    }
                }
            }

            sftpClient.close();
        } else if (node.getType().equals("DCOM")) {

            String loggingDir = getLoggingDirectoryForNode(instanceLogFileDetails, node, sNode, instanceName);

            try {
View Full Code Here

        openConnection();
        logger.fine("Connection settings valid");
        String testPath = installDir;
        if (StringUtils.ok(testPath)) {
            // Validate if installDir exists
            SFTPClient sftpClient = new SFTPClient(connection);
            if (sftpClient.exists(testPath)) {
                // installDir exists. Now check for landmark if provided
                if (StringUtils.ok(landmarkPath)) {                   
                    testPath = installDir + "/" + landmarkPath;
                }
                validInstallDir = sftpClient.exists(testPath);
            } else {
                validInstallDir = false;
            }
            SSHUtil.unregister(connection);
            connection = null;
View Full Code Here

                             installDir, null, logger);
    }

    public SFTPClient getSFTPClient() throws IOException {
        openConnection();
        SFTPClient sftpClient = new SFTPClient(connection);
        return sftpClient;
    }
View Full Code Here

        if (!ret) {
            throw new IOException("SSH password authentication failed for user " + userName + " on host " + node);
        }
        //initiate scp client
        SCPClient scp = new SCPClient(conn);
        SFTPClient sftp = new SFTPClient(connection);

        if (key.exists()) {

            //fixes .ssh file mode
            setupSSHDir();

            if (pubKeyFile == null) {
                pubKeyFile = keyFile + ".pub";
            }

            File pubKey = new File(pubKeyFile);
            if(!pubKey.exists()) {
                throw new IOException("Public key file " + pubKeyFile + " does not exist.");
            }

            try {
                if(!sftp.exists(SSH_DIR)) {
                    if(logger.isLoggable(Level.FINER)) {
                        logger.fine(SSH_DIR + " does not exist");
                    }
                    sftp.mkdirs(".ssh", 0700);
                }
            } catch (Exception e) {
                if(logger.isLoggable(Level.FINER)) {
                    e.printStackTrace();
                }
                throw new IOException("Error while creating .ssh directory on remote host:" + e.getMessage());
            }

            //copy over the public key to remote host
            scp.put(pubKey.getAbsolutePath(), "key.tmp", ".ssh", "0600");           

            //append the public key file contents to authorized_keys file on remote host
            String mergeCommand = "cd .ssh; cat key.tmp >> " + AUTH_KEY_FILE;
            if(logger.isLoggable(Level.FINER)) {
                logger.finer("mergeCommand = " + mergeCommand);
            }
            if(conn.exec(mergeCommand, new ByteArrayOutputStream())!=0) {
                throw new IOException("Failed to propogate the public key " + pubKeyFile + " to " + host);
            }
            logger.info("Copied keyfile " + pubKeyFile + " to " + userName + "@" + host);

            //remove the public key file on remote host
            if(conn.exec("rm .ssh/key.tmp", new ByteArrayOutputStream())!=0) {
                logger.warning("WARNING: Failed to remove the public key file key.tmp on remote host " + host);
            }
            if(logger.isLoggable(Level.FINER)) {
                logger.finer("Removed the temporary key file on remote host");
            }
           
            //Lets fix all the permissions
            //On MKS, chmod doesn't work as expected. StrictMode needs to be disabled
            //for connection to go through
            logger.info("Fixing file permissions for home(755), .ssh(700) and authorized_keys file(644)");
            sftp.chmod(".", 0755);
            sftp.chmod(SSH_DIR, 0700);
            sftp.chmod(SSH_DIR + AUTH_KEY_FILE, 0644);
            //release the connections
            sftp.close();
            conn.close();
        }
    }
View Full Code Here

                prompt = false;
            }

            String sshInstallDir = getInstallDir().replace('\\', '/');

            SFTPClient sftpClient = sshLauncher.getSFTPClient();
            SCPClient scpClient = sshLauncher.getSCPClient();
            try {
                if (!sftpClient.exists(sshInstallDir)) {
                    sftpClient.mkdirs(sshInstallDir, 0755);
                }
            }
            catch (IOException ioe) {
                logger.info(Strings.get("mkdir.failed", sshInstallDir, host));
                throw new IOException(ioe);
            }

            //delete the sshInstallDir contents if non-empty
            try {
                //get list of file in DAS sshInstallDir
                List<String> files = getListOfInstallFiles(sshInstallDir);
                deleteRemoteFiles(sftpClient, files, sshInstallDir, getForce());
            }
            catch (IOException ex) {
                logger.finer("Failed to remove sshInstallDir contents");
                throw new IOException(ex);
            }

            String zip = zipFile.getCanonicalPath();
            try {
                logger.info("Copying " + zip + " (" + zipFile.length() + " bytes)"
                        + " to " + host + ":" + sshInstallDir);
                // Looks like we need to quote the paths to scp in case they
                // contain spaces.
                scpClient.put(zipFile.getAbsolutePath(), FileUtils.quoteString(sshInstallDir));
                logger.finer("Copied " + zip + " to " + host + ":" + sshInstallDir);
            }
            catch (IOException ex) {
                logger.info(Strings.get("cannot.copy.zip.file", zip, host));
                throw new IOException(ex);
            }

            try {
                logger.info("Installing " + getArchiveName() + " into " + host + ":" + sshInstallDir);
                String unzipCommand = "cd '" + sshInstallDir + "'; jar -xvf " + getArchiveName();
                int status = sshLauncher.runCommand(unzipCommand, outStream);
                if (status != 0) {
                    logger.info(Strings.get("jar.failed", host, outStream.toString()));
                    throw new CommandException("Remote command output: " + outStream.toString());
                }
                logger.finer("Installed " + getArchiveName() + " into " + host + ":" + sshInstallDir);
            }
            catch (IOException ioe) {
                logger.info(Strings.get("jar.failed", host, outStream.toString()));
                throw new IOException(ioe);
            }

            try {
                logger.info("Removing " + host + ":" + sshInstallDir + "/" + getArchiveName());
                sftpClient.rm(sshInstallDir + "/" + getArchiveName());
                logger.finer("Removed " + host + ":" + sshInstallDir + "/" + getArchiveName());
            }
            catch (IOException ioe) {
                logger.info(Strings.get("remove.glassfish.failed", host, sshInstallDir));
                throw new IOException(ioe);
            }

            // unjarring doesn't retain file permissions, hence executables need
            // to be fixed with proper permissions
            logger.info("Fixing file permissions of all bin files under " + host + ":" + sshInstallDir);
            try {
                if (binDirFiles.isEmpty()) {
                    //binDirFiles can be empty if the archive isn't a fresh one
                    searchAndFixBinDirectoryFiles(sshInstallDir, sftpClient);
                }
                else {
                    for (String binDirFile : binDirFiles) {
                        sftpClient.chmod((sshInstallDir + "/" + binDirFile), 0755);
                    }
                }
                logger.finer("Fixed file permissions of all bin files under " + host + ":" + sshInstallDir);
            }
            catch (IOException ioe) {
                logger.info(Strings.get("fix.permissions.failed", host, sshInstallDir));
                throw new IOException(ioe);
            }

            if (Constants.v4) {
                logger.info("Fixing file permissions for nadmin file under " + host + ":"
                            + sshInstallDir + "/" + SystemPropertyConstants.getComponentName() + "/lib");
                try {
                    sftpClient.chmod((sshInstallDir + "/" + SystemPropertyConstants.getComponentName() + "/lib/nadmin"), 0755);
                    logger.finer("Fixed file permission for nadmin under " + host + ":" + sshInstallDir + "/" + SystemPropertyConstants.getComponentName() + "/lib/nadmin");
                }
                catch (IOException ioe) {
                    logger.info(Strings.get("fix.permissions.failed", host, sshInstallDir));
                    throw new IOException(ioe);
View Full Code Here

TOP

Related Classes of org.glassfish.cluster.ssh.sftp.SFTPClient

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.