Search in sources :

Example 1 with SFTPClient

use of org.glassfish.cluster.ssh.sftp.SFTPClient in project Payara by payara.

the class InstallNodeSshCommand method copyToHostsInternal.

private void copyToHostsInternal(File zipFile, ArrayList<String> binDirFiles) throws IOException, InterruptedException, CommandException {
    ByteArrayOutputStream outStream = new ByteArrayOutputStream();
    boolean prompt = promptPass;
    for (String host : hosts) {
        sshLauncher.init(getRemoteUser(), host, getRemotePort(), sshpassword, getSshKeyFile(), sshkeypassphrase, logger);
        if (getSshKeyFile() != null && !sshLauncher.checkConnection()) {
            // key auth failed, so use password auth
            prompt = true;
        }
        if (prompt) {
            String sshpass = null;
            if (sshPasswords.containsKey(host))
                sshpass = String.valueOf(sshPasswords.get(host));
            else
                sshpass = getSSHPassword(host);
            // re-initialize
            sshLauncher.init(getRemoteUser(), host, getRemotePort(), sshpass, getSshKeyFile(), sshkeypassphrase, logger);
            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));
            if (logger.isLoggable(Level.FINER))
                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());
            }
            if (logger.isLoggable(Level.FINER))
                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());
            if (logger.isLoggable(Level.FINER))
                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);
                }
            }
            if (logger.isLoggable(Level.FINER))
                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);
                if (logger.isLoggable(Level.FINER))
                    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);
            }
        }
        sftpClient.close();
    }
}
Also used : SCPClient(com.trilead.ssh2.SCPClient) SFTPClient(org.glassfish.cluster.ssh.sftp.SFTPClient) CommandException(org.glassfish.api.admin.CommandException)

Example 2 with SFTPClient

use of org.glassfish.cluster.ssh.sftp.SFTPClient in project Payara by payara.

the class InstallNodeSshCommand method precopy.

@Override
final void precopy() throws CommandException {
    if (getForce())
        return;
    boolean prompt = promptPass;
    for (String host : hosts) {
        sshLauncher.init(getRemoteUser(), host, getRemotePort(), sshpassword, getSshKeyFile(), sshkeypassphrase, logger);
        if (getSshKeyFile() != null && !sshLauncher.checkConnection()) {
            // key auth failed, so use password auth
            prompt = true;
        }
        if (prompt) {
            String sshpass = getSSHPassword(host);
            sshPasswords.put(host, sshpass.toCharArray());
            // re-initialize
            sshLauncher.init(getRemoteUser(), host, getRemotePort(), sshpass, getSshKeyFile(), sshkeypassphrase, logger);
            prompt = false;
        }
        String sshInstallDir = getInstallDir().replaceAll("\\\\", "/");
        try {
            SFTPClient sftpClient = sshLauncher.getSFTPClient();
            if (sftpClient.exists(sshInstallDir)) {
                checkIfAlreadyInstalled(host, sshInstallDir);
            }
            sftpClient.close();
        } catch (IOException ex) {
            throw new CommandException(ex);
        } catch (InterruptedException ex) {
            throw new CommandException(ex);
        }
    }
}
Also used : SFTPClient(org.glassfish.cluster.ssh.sftp.SFTPClient) CommandException(org.glassfish.api.admin.CommandException)

Example 3 with SFTPClient

use of org.glassfish.cluster.ssh.sftp.SFTPClient in project Payara by payara.

the class UninstallNodeSshCommand method deleteFromHosts.

@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();
        }
    }
}
Also used : SFTPClient(org.glassfish.cluster.ssh.sftp.SFTPClient) IOException(java.io.IOException) CommandException(org.glassfish.api.admin.CommandException) CommandException(org.glassfish.api.admin.CommandException) IOException(java.io.IOException)

Example 4 with SFTPClient

use of org.glassfish.cluster.ssh.sftp.SFTPClient in project Payara by payara.

the class LogFilterForInstance method downloadGivenInstanceLogFile.

public File downloadGivenInstanceLogFile(ServiceLocator habitat, Server targetServer, Domain domain, Logger logger, String instanceName, String domainRoot, String logFileName, String instanceLogFileName) throws IOException {
    File instanceLogFile = null;
    // method is used from logviewer back end code logfilter.
    // for Instance it's going through this loop. This will use ssh utility to get file from instance machine(remote machine) and
    // store in domains/domain1/logs/<instance name> which is used to get LogFile object.
    // Right now user needs to go through this URL to setup and configure ssh http://wikis.sun.com/display/GlassFish/3.1SSHSetup
    SSHLauncher sshL = getSSHL(habitat);
    String sNode = targetServer.getNodeRef();
    Nodes nodes = domain.getNodes();
    Node node = nodes.getNode(sNode);
    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 {
            List 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);
        String loggingDir = getLoggingDirectoryForNode(instanceLogFileName, node, sNode, instanceName);
        try {
            DcomInfo info = new DcomInfo(node);
            WindowsRemoteFileSystem wrfs = new WindowsRemoteFileSystem(info.getHost(), info.getUser(), info.getPassword());
            if (logFileName == null || logFileName.equals("")) {
                logFileName = "server.log";
            }
            WindowsRemoteFile wrf = new WindowsRemoteFile(wrfs, loggingDir + File.separator + logFileName);
            instanceLogFile = new File(logFileDirectoryOnServer + File.separator + logFileName);
            wrf.copyTo(instanceLogFile);
        } catch (WindowsException ex) {
            throw new IOException("Unable to download instance log file from DCOM Instance Node");
        }
    }
    return instanceLogFile;
}
Also used : SSHLauncher(org.glassfish.cluster.ssh.launcher.SSHLauncher) Node(com.sun.enterprise.config.serverbeans.Node) SFTPClient(org.glassfish.cluster.ssh.sftp.SFTPClient) WindowsException(com.sun.enterprise.util.cluster.windows.process.WindowsException) Nodes(com.sun.enterprise.config.serverbeans.Nodes) WindowsException(com.sun.enterprise.util.cluster.windows.process.WindowsException) DcomInfo(org.glassfish.cluster.ssh.util.DcomInfo) SFTPv3DirectoryEntry(com.trilead.ssh2.SFTPv3DirectoryEntry) WindowsRemoteFile(com.sun.enterprise.util.cluster.windows.io.WindowsRemoteFile) WindowsRemoteFileSystem(com.sun.enterprise.util.cluster.windows.io.WindowsRemoteFileSystem) ArrayList(java.util.ArrayList) List(java.util.List) SFTPv3FileAttributes(com.trilead.ssh2.SFTPv3FileAttributes) WindowsRemoteFile(com.sun.enterprise.util.cluster.windows.io.WindowsRemoteFile)

Example 5 with SFTPClient

use of org.glassfish.cluster.ssh.sftp.SFTPClient in project Payara by payara.

the class SSHLauncher method getSFTPClient.

public SFTPClient getSFTPClient() throws IOException {
    openConnection();
    SFTPClient sftpClient = new SFTPClient(connection);
    return sftpClient;
}
Also used : SFTPClient(org.glassfish.cluster.ssh.sftp.SFTPClient)

Aggregations

SFTPClient (org.glassfish.cluster.ssh.sftp.SFTPClient)9 Node (com.sun.enterprise.config.serverbeans.Node)3 WindowsRemoteFile (com.sun.enterprise.util.cluster.windows.io.WindowsRemoteFile)3 WindowsRemoteFileSystem (com.sun.enterprise.util.cluster.windows.io.WindowsRemoteFileSystem)3 WindowsException (com.sun.enterprise.util.cluster.windows.process.WindowsException)3 SCPClient (com.trilead.ssh2.SCPClient)3 SFTPv3DirectoryEntry (com.trilead.ssh2.SFTPv3DirectoryEntry)3 ArrayList (java.util.ArrayList)3 List (java.util.List)3 CommandException (org.glassfish.api.admin.CommandException)3 SSHLauncher (org.glassfish.cluster.ssh.launcher.SSHLauncher)3 DcomInfo (org.glassfish.cluster.ssh.util.DcomInfo)3 Nodes (com.sun.enterprise.config.serverbeans.Nodes)2 FileNotFoundException (java.io.FileNotFoundException)2 IOException (java.io.IOException)2 ProcessManagerException (com.sun.enterprise.universal.process.ProcessManagerException)1 Connection (com.trilead.ssh2.Connection)1 SFTPv3FileAttributes (com.trilead.ssh2.SFTPv3FileAttributes)1 File (java.io.File)1