Search in sources :

Example 11 with CommandException

use of org.glassfish.api.admin.CommandException in project Payara by payara.

the class CreateLocalInstanceCommand method getInstanceHostName.

private String getInstanceHostName(boolean isCanonical) throws CommandException {
    String instanceHostName = null;
    InetAddress localHost = null;
    try {
        localHost = InetAddress.getLocalHost();
    } catch (UnknownHostException ex) {
        throw new CommandException(Strings.get("cantGetHostName", ex));
    }
    if (localHost != null) {
        if (isCanonical) {
            instanceHostName = localHost.getCanonicalHostName();
        } else {
            instanceHostName = localHost.getHostName();
        }
    }
    return instanceHostName;
}
Also used : UnknownHostException(java.net.UnknownHostException) CommandException(org.glassfish.api.admin.CommandException) InetAddress(java.net.InetAddress)

Example 12 with CommandException

use of org.glassfish.api.admin.CommandException in project Payara by payara.

the class InstallNodeDcomCommand method unpackOnHosts.

private void unpackOnHosts(String host, String remotePassword, String unpackScript) throws WindowsException, CommandException {
    String domain = windowsDomain;
    if (!ok(domain))
        domain = host;
    WindowsCredentials bonafides = new WindowsCredentials(host, domain, getRemoteUser(), remotePassword);
    WindowsRemoteScripter scripter = new WindowsRemoteScripter(bonafides);
    String out = scripter.run(unpackScript);
    if (out == null || out.length() < 50)
        throw new CommandException(Strings.get("dcom.error.unpacking", unpackScript, out));
    if (logger.isLoggable(Level.FINE))
        logger.fine("Output from Windows Unpacker:\n" + out);
}
Also used : WindowsRemoteScripter(com.sun.enterprise.util.cluster.windows.process.WindowsRemoteScripter) CommandException(org.glassfish.api.admin.CommandException) WindowsCredentials(com.sun.enterprise.util.cluster.windows.process.WindowsCredentials)

Example 13 with CommandException

use of org.glassfish.api.admin.CommandException 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 14 with CommandException

use of org.glassfish.api.admin.CommandException 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 15 with CommandException

use of org.glassfish.api.admin.CommandException 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)

Aggregations

CommandException (org.glassfish.api.admin.CommandException)61 File (java.io.File)20 CommandValidationException (org.glassfish.api.admin.CommandValidationException)16 IOException (java.io.IOException)13 ParameterMap (org.glassfish.api.admin.ParameterMap)11 InvalidCommandException (org.glassfish.api.admin.InvalidCommandException)10 ActionReport (org.glassfish.api.ActionReport)9 ArrayList (java.util.ArrayList)6 RemoteCLICommand (com.sun.enterprise.admin.cli.remote.RemoteCLICommand)5 MiniXmlParserException (com.sun.enterprise.universal.xml.MiniXmlParserException)5 HostAndPort (com.sun.enterprise.util.HostAndPort)4 BackupException (com.sun.enterprise.backup.BackupException)3 BackupWarningException (com.sun.enterprise.backup.BackupWarningException)3 SmartFile (com.sun.enterprise.universal.io.SmartFile)3 FileNotFoundException (java.io.FileNotFoundException)3 UnknownHostException (java.net.UnknownHostException)3 Logger (java.util.logging.Logger)3 SFTPClient (org.glassfish.cluster.ssh.sftp.SFTPClient)3 CLIProcessExecutor (com.sun.enterprise.admin.cli.CLIProcessExecutor)2 PEDomainsManager (com.sun.enterprise.admin.servermgmt.pe.PEDomainsManager)2