Search in sources :

Example 1 with RemoteCLICommand

use of com.sun.enterprise.admin.cli.remote.RemoteCLICommand in project Payara by payara.

the class CLICommand method prevalidate.

/**
 * The prevalidate method supplies missing options from
 * the environment.  It also supplies passwords from the password
 * file or prompts for them if interactive.
 *
 * @throws CommandException           if execution of the command fails
 * @throws CommandValidationException if there's something wrong
 *                                    with the options or arguments
 */
protected void prevalidate() throws CommandException {
    /*
         * First, check that the command has the proper scope.
         * (Could check this in getCommand(), but at that point we
         * don't have the CommandModel yet.)
         * Remote commands are checked on the server.
         */
    if (!(this instanceof RemoteCommand) && !(this instanceof RemoteCLICommand)) {
        Class<? extends Annotation> myScope = getScope(this.getClass());
        if (myScope == null) {
            throw new CommandException(strings.get("NoScope", name));
        } else if (Singleton.class.equals(myScope)) {
            // check that there are no parameters for this command
            if (commandModel.getParameters().size() > 0) {
                throw new CommandException(strings.get("HasParams", name));
            }
        }
    }
    /*
         * Check for missing options and operands.
         */
    int operandMin = 0;
    int operandMax = 0;
    ParamModel operandParam = getOperandModel();
    if (operandParam != null) {
        operandMin = operandParam.getParam().optional() ? 0 : 1;
        operandMax = operandParam.getParam().multiple() ? Integer.MAX_VALUE : 1;
    }
    if (programOpts.isInteractive()) {
        try {
            buildTerminal();
            buildLineReader();
            boolean missingOption = false;
            for (ParamModel opt : commandModel.getParameters()) {
                if (opt.getParam().password()) {
                    // passwords are handled later
                    continue;
                }
                if (opt.getParam().obsolete() && getOption(opt.getName()) != null) {
                    logger.info(strings.get("ObsoleteOption", opt.getName()));
                }
                if (opt.getParam().optional()) {
                    continue;
                }
                if (opt.getParam().primary()) {
                    continue;
                }
                // if option isn't set, prompt for it (if interactive)
                if (getOption(opt.getName()) == null && lineReader != null && !missingOption) {
                    String val = lineReader.readLine(strings.get("optionPrompt", lc(opt.getName())));
                    if (ok(val)) {
                        options.set(opt.getName(), val);
                    }
                }
                // if it's still not set, that's an error
                if (getOption(opt.getName()) == null) {
                    missingOption = true;
                    logger.log(Level.INFO, strings.get("missingOption", "--" + opt.getName()));
                }
                if (opt.getParam().obsolete()) {
                    // a required obsolete option?
                    logger.log(Level.INFO, strings.get("ObsoleteOption", opt.getName()));
                }
            }
            if (missingOption) {
                throw new CommandValidationException(strings.get("missingOptions", name));
            }
            if (operands.size() < operandMin && lineReader != null) {
                String val = null;
                if (programOpts.isAutoName()) {
                    val = NameGenerator.generateName();
                }
                if (!ok(val)) {
                    val = lineReader.readLine(strings.get("operandPrompt", operandParam.getName()));
                }
                if (ok(val)) {
                    operands = new ArrayList<>();
                    operands.add(val);
                }
            }
        } catch (UserInterruptException | EndOfFileException e) {
        // Ignore
        } finally {
            closeTerminal();
        }
    } else {
        // Check if we're missing an operand even if not interactive in case we want to generate it.
        if (operands.size() < operandMin) {
            String val = null;
            if (programOpts.isAutoName()) {
                val = NameGenerator.generateName();
            }
            if (ok(val)) {
                operands = new ArrayList<>();
                operands.add(val);
            }
        }
    }
    // Validate that we have the required operands
    if (operands.size() < operandMin) {
        throw new CommandValidationException(strings.get("notEnoughOperands", name, operandParam.getType()));
    }
    if (operands.size() > operandMax) {
        switch(operandMax) {
            case 0:
                throw new CommandValidationException(strings.get("noOperandsAllowed", name));
            case 1:
                throw new CommandValidationException(strings.get("tooManyOperands1", name));
            default:
                throw new CommandValidationException(strings.get("tooManyOperands", name, operandMax));
        }
    }
    initializeCommandPassword();
}
Also used : EndOfFileException(org.jline.reader.EndOfFileException) CommandValidationException(org.glassfish.api.admin.CommandValidationException) ParamModel(org.glassfish.api.admin.CommandModel.ParamModel) CommandException(org.glassfish.api.admin.CommandException) UserInterruptException(org.jline.reader.UserInterruptException) RemoteCLICommand(com.sun.enterprise.admin.cli.remote.RemoteCLICommand) Singleton(javax.inject.Singleton) RemoteCommand(com.sun.enterprise.admin.cli.remote.RemoteCommand)

Example 2 with RemoteCLICommand

use of com.sun.enterprise.admin.cli.remote.RemoteCLICommand in project Payara by payara.

the class CLIUtil method getRemoteCommands.

/**
 * Get the list of commands from the remote server.
 *
 * @param container
 * @param po Options to get the command i.e. admin port
 * @param env
 * @return the commands as a String array, sorted
 * @throws CommandException
 * @throws CommandValidationException
 */
public static String[] getRemoteCommands(CLIContainer container, ProgramOptions po, Environment env) throws CommandException, CommandValidationException {
    /*
         * In order to eliminate all local command names from the list
         * of remote commands, we collect the local command names into
         * a Set that we check later when collecting remote command
         * names.
         */
    Set<String> localnames = container.getLocalCommandsNames();
    /*
         * Now get the list of remote commands.
         */
    po.removeDetach();
    RemoteCLICommand cmd = new RemoteCLICommand("list-commands", po, env);
    ActionReport report = cmd.executeAndReturnActionReport("list-commands");
    List<MessagePart> children = report.getTopMessagePart().getChildren();
    List<String> rcmds = new ArrayList<String>(children.size());
    for (ActionReport.MessagePart msg : children) {
        if (!localnames.contains(msg.getMessage())) {
            rcmds.add(msg.getMessage());
        }
    }
    Collections.sort(rcmds);
    String[] remoteCommands = rcmds.toArray(new String[rcmds.size()]);
    Arrays.sort(remoteCommands);
    return remoteCommands;
}
Also used : MessagePart(org.glassfish.api.ActionReport.MessagePart) ActionReport(org.glassfish.api.ActionReport) RemoteCLICommand(com.sun.enterprise.admin.cli.remote.RemoteCLICommand) MessagePart(org.glassfish.api.ActionReport.MessagePart)

Example 3 with RemoteCLICommand

use of com.sun.enterprise.admin.cli.remote.RemoteCLICommand in project Payara by payara.

the class ListDomainsCommand method getStatus.

private DomainInfo getStatus(String dn) throws IOException, CommandException {
    setDomainName(dn);
    initDomain();
    DomainInfo di = new DomainInfo();
    di.adminAddr = getAdminAddress();
    programOpts.setHostAndPort(di.adminAddr);
    di.status = isThisDAS(getDomainRootDir());
    if (di.status) {
        di.statusMsg = STRINGS.get("list.domains.StatusRunning", dn);
        try {
            RemoteCLICommand cmd = new RemoteCLICommand("_get-restart-required", programOpts, env);
            String restartRequired = cmd.executeAndReturnOutput("_get-restart-required");
            di.restartRequired = Boolean.parseBoolean(restartRequired.trim());
            if (di.restartRequired) {
                di.statusMsg = STRINGS.get("list.domains.StatusRestartRequired", dn);
            }
        } catch (Exception ex) {
        }
    } else {
        di.statusMsg = STRINGS.get("list.domains.StatusNotRunning", dn);
    }
    return di;
}
Also used : RemoteCLICommand(com.sun.enterprise.admin.cli.remote.RemoteCLICommand) IOException(java.io.IOException) DomainException(com.sun.enterprise.admin.servermgmt.DomainException)

Example 4 with RemoteCLICommand

use of com.sun.enterprise.admin.cli.remote.RemoteCLICommand in project Payara by payara.

the class LocalServerCommand method isThisServer.

/**
 * See if the server is alive and is the one at the specified directory.
 *
 * @param ourDir       the directory to check if the server is alive agains
 * @param directoryKey the key for the directory
 * @return true if it's the DAS at this domain directory
 */
protected final boolean isThisServer(File ourDir, String directoryKey) {
    if (!ok(directoryKey))
        throw new NullPointerException();
    ourDir = getUniquePath(ourDir);
    logger.log(Level.FINER, "Check if server is at location {0}", ourDir);
    try {
        programOpts.setHostAndPort(getAdminAddress());
        RemoteCLICommand cmd = new RemoteCLICommand("__locations", programOpts, env);
        ActionReport report = cmd.executeAndReturnActionReport("__locations");
        String theirDirPath = report.findProperty(directoryKey);
        logger.log(Level.FINER, "Remote server has root directory {0}", theirDirPath);
        if (ok(theirDirPath)) {
            File theirDir = getUniquePath(new File(theirDirPath));
            return theirDir.equals(ourDir);
        }
        return false;
    } catch (Exception ex) {
        return false;
    }
}
Also used : ActionReport(org.glassfish.api.ActionReport) SmartFile(com.sun.enterprise.universal.io.SmartFile) File(java.io.File) RemoteCLICommand(com.sun.enterprise.admin.cli.remote.RemoteCLICommand) MiniXmlParserException(com.sun.enterprise.universal.xml.MiniXmlParserException) XMLStreamException(javax.xml.stream.XMLStreamException) CommandException(org.glassfish.api.admin.CommandException) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXException(org.xml.sax.SAXException)

Example 5 with RemoteCLICommand

use of com.sun.enterprise.admin.cli.remote.RemoteCLICommand in project Payara by payara.

the class LocalServerCommand method getUptime.

/**
 * Get uptime from the server.
 *
 * @return uptime in milliseconds
 * @throws CommandException if the server is not running
 */
protected final long getUptime() throws CommandException {
    RemoteCLICommand cmd = new RemoteCLICommand("uptime", programOpts, env);
    String up = cmd.executeAndReturnOutput("uptime", "--milliseconds").trim();
    long uptimeMillis = parseUptime(up);
    if (uptimeMillis <= 0) {
        throw new CommandException(STRINGS.get("restart.dasNotRunning"));
    }
    logger.log(Level.FINER, "server uptime: {0}", uptimeMillis);
    return uptimeMillis;
}
Also used : CommandException(org.glassfish.api.admin.CommandException) RemoteCLICommand(com.sun.enterprise.admin.cli.remote.RemoteCLICommand)

Aggregations

RemoteCLICommand (com.sun.enterprise.admin.cli.remote.RemoteCLICommand)28 CommandException (org.glassfish.api.admin.CommandException)12 ArrayList (java.util.ArrayList)4 ActionReport (org.glassfish.api.ActionReport)4 IOException (java.io.IOException)3 DomainException (com.sun.enterprise.admin.servermgmt.DomainException)2 BackupException (com.sun.enterprise.backup.BackupException)2 BackupWarningException (com.sun.enterprise.backup.BackupWarningException)2 File (java.io.File)2 CommandValidationException (org.glassfish.api.admin.CommandValidationException)2 MultiException (org.glassfish.hk2.api.MultiException)2 RemoteCommand (com.sun.enterprise.admin.cli.remote.RemoteCommand)1 SmartFile (com.sun.enterprise.universal.io.SmartFile)1 MiniXmlParserException (com.sun.enterprise.universal.xml.MiniXmlParserException)1 SyncRequest (com.sun.enterprise.util.cluster.SyncRequest)1 ConnectException (java.net.ConnectException)1 Singleton (javax.inject.Singleton)1 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)1 XMLStreamException (javax.xml.stream.XMLStreamException)1 MessagePart (org.glassfish.api.ActionReport.MessagePart)1