Package org.glassfish.cluster.ssh.sftp

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


        }
    }

    @Override
    void deleteFromHosts() throws CommandException {
        SFTPClient sftpClient = null;
        try {
            List<String> files = getListOfInstallFiles(getInstallDir());

            for (String host : hosts) {
                sshLauncher.init(getRemoteUser(), host, getRemotePort(), sshpassword, sshkeyfile, sshkeypassphrase, logger);

                if (sshkeyfile != null && !sshLauncher.checkConnection()) {
                    //key auth failed, so use password auth
                    promptPass = true;
                }

                if (promptPass) {
                    sshpassword = getSSHPassword(host);
                    //re-initialize
                    sshLauncher.init(getRemoteUser(), host, getRemotePort(), sshpassword, sshkeyfile, sshkeypassphrase, logger);
                }

                sftpClient = sshLauncher.getSFTPClient();

                if (!sftpClient.exists(getInstallDir())) {
                    throw new IOException(getInstallDir() + " Directory does not exist");
                }

                deleteRemoteFiles(sftpClient, files, getInstallDir(), getForce());

                if (isRemoteDirectoryEmpty(sftpClient, getInstallDir())) {
                    sftpClient.rmdir(getInstallDir());
                }
                sftpClient.close();
            }
        }
        catch (CommandException ce) {
            throw ce;
        }
        catch (Exception ex) {
            throw new CommandException(ex);
        } finally {
            if (sftpClient != null) {
                sftpClient.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);
            }

            if (checkIfAlreadyInstalled(host, sshInstallDir))
                continue;

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


            logger.info("Fixing file permissions of all files under " + host + ":" + sshInstallDir + "/bin");
            try {
                if (binDirFiles.isEmpty()) {
                    //binDirFiles can be empty if the archive isn't a fresh one
                    String cmd = "cd '" + sshInstallDir + "/" + SystemPropertyConstants.getComponentName() + "/bin'; chmod 0755 *";
                    int status = sshLauncher.runCommand(cmd, outStream);
                    if (status != 0) {
                        logger.info(Strings.get("jar.failed", host, outStream.toString()));
                        throw new CommandException("Remote command output: " + outStream.toString());
                    }
                }
                else {
                    for (String binDirFile : binDirFiles) {
                        sftpClient.chmod((sshInstallDir + "/" + binDirFile), 0755);
                    }
                }
                logger.finer("Fixed file permissions of all files under " + host + ":" + sshInstallDir + "/bin");
            }
            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 + "/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

            }

            String sshInstallDir = getInstallDir().replaceAll("\\\\", "/");

            try {
                SFTPClient sftpClient = sshLauncher.getSFTPClient();
                if (sftpClient.exists(sshInstallDir) && checkIfAlreadyInstalled(host, sshInstallDir)){
                    createZip = false;
                } else {
                    //no point in continuing if we know we have to create zip for at least one host
                    break;
                }
View Full Code Here

                    sshpassword = getSSHPassword(host);
                    //re-initialize
                    sshLauncher.init(getRemoteUser(), host, getRemotePort(), sshpassword, sshkeyfile, sshkeypassphrase, logger);
                }

                SFTPClient sftpClient = sshLauncher.getSFTPClient();

                if (!sftpClient.exists(getInstallDir())) {
                    throw new IOException(getInstallDir() + " Directory does not exist");
                }

                deleteRemoteFiles(sftpClient, files, getInstallDir(), getForce());

                if (sftpClient.ls(getInstallDir()).isEmpty()) {
                    sftpClient.rmdir(getInstallDir());
                }
            }
        }
        catch (CommandException ce) {
            throw ce;
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

        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);
            connection.close();
            conn.close();
        }
    }
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.