Search in sources :

Example 1 with CommandException

use of com.iwave.ext.command.CommandException in project coprhd-controller by CoprHD.

the class AixHostDiscoveryAdapter method setNativeGuid.

@Override
protected void setNativeGuid(Host host) {
    AixSystem aix = getCli(host);
    try {
        String macAddress = aix.getNetworkAdapterMacAddress(ENT0);
        if (macAddress != null && !host.getNativeGuid().equalsIgnoreCase(macAddress)) {
            checkDuplicateHost(host, macAddress);
            info("Setting nativeGuid for " + host.getId() + " as " + macAddress);
            host.setNativeGuid(macAddress);
            save(host);
        }
    } catch (CommandException ex) {
        LOG.warn("Failed to get MAC address of adapter during discovery");
    }
}
Also used : AixSystem(com.emc.aix.AixSystem) CommandException(com.iwave.ext.command.CommandException)

Example 2 with CommandException

use of com.iwave.ext.command.CommandException in project coprhd-controller by CoprHD.

the class LinuxHostDiscoveryAdapter method getVersions.

protected List<LinuxVersion> getVersions(Host host) {
    LinuxSystemCLI cli = createLinuxCLI(host);
    List<LinuxVersion> versions = new ArrayList<LinuxVersion>();
    LinuxVersionCommand[] commands = { new GetSuSEVersionCommand(), new GetRedhatVersionCommandOne(), new GetRedhatVersionCommandTwo(), new GetRedhatVersionCommandThree(), new GetLinuxVersionLSBReleaseCommand() };
    for (LinuxVersionCommand command : commands) {
        try {
            cli.executeCommand(command);
            LinuxVersion version = command.getResults();
            if (version != null) {
                versions.add(version);
            }
        } catch (CommandException e) {
            warn("Could not retrieve linux version", e);
        }
    }
    if (versions.isEmpty()) {
        error("Could not determine version of linux host %s", host.getLabel());
        versions.add(new LinuxVersion(LinuxDistribution.UNKNOWN, ""));
    }
    return versions;
}
Also used : GetSuSEVersionCommand(com.iwave.ext.linux.command.version.GetSuSEVersionCommand) GetRedhatVersionCommandOne(com.iwave.ext.linux.command.version.GetRedhatVersionCommandOne) LinuxSystemCLI(com.iwave.ext.linux.LinuxSystemCLI) LinuxVersionCommand(com.iwave.ext.linux.command.version.LinuxVersionCommand) ArrayList(java.util.ArrayList) GetRedhatVersionCommandThree(com.iwave.ext.linux.command.version.GetRedhatVersionCommandThree) GetRedhatVersionCommandTwo(com.iwave.ext.linux.command.version.GetRedhatVersionCommandTwo) CommandException(com.iwave.ext.command.CommandException) LinuxVersion(com.iwave.ext.linux.model.LinuxVersion) GetLinuxVersionLSBReleaseCommand(com.iwave.ext.linux.command.version.GetLinuxVersionLSBReleaseCommand)

Example 3 with CommandException

use of com.iwave.ext.command.CommandException in project coprhd-controller by CoprHD.

the class SSHCommandExecutor method executeCommand.

@Override
public CommandOutput executeCommand(Command command) throws CommandException {
    try {
        if (!isConnected()) {
            connect();
        }
        ChannelExec channel = (ChannelExec) session.openChannel("exec");
        try {
            StreamConsumer stdout = new StreamConsumer(channel.getInputStream());
            StreamConsumer stderr = new StreamConsumer(channel.getErrStream());
            connect(command, channel);
            waitForDone(channel);
            stdout.close();
            stderr.close();
            int exitCode = channel.getExitStatus();
            return new CommandOutput(stdout.toString(), stderr.toString(), exitCode);
        } finally {
            channel.disconnect();
        }
    } catch (JSchException | IOException | InterruptedException | SSHException e) {
        log.error(String.format("SSH '%s' command failed: ", command.getCommand()), e);
        throw new CommandException(e);
    } finally {
        if (isAutoDisconnect()) {
            disconnect();
        }
    }
}
Also used : JSchException(com.jcraft.jsch.JSchException) CommandOutput(com.iwave.ext.command.CommandOutput) IOException(java.io.IOException) CommandException(com.iwave.ext.command.CommandException) ChannelExec(com.jcraft.jsch.ChannelExec)

Example 4 with CommandException

use of com.iwave.ext.command.CommandException in project coprhd-controller by CoprHD.

the class ShellCommandExecutor method sendCommand.

/**
 * Sends a command to the remote host. This returns a CommandOutput, but the exit value is
 * always 0 since it is being handled by the remote shell.
 *
 * @param command the command to send.
 * @return the command output.
 */
protected CommandOutput sendCommand(String command) {
    try {
        send(command);
        String matched = waitFor(promptPattern);
        String stdout = getCurrentCommandOutput();
        stdoutPos += StringUtils.length(stdout);
        // Strip the command from the start of the output (ignoring any inserted line breaks)
        stdout = IWaveStringUtils.removeStartIgnoringWhiteSpace(stdout, command);
        if (StringUtils.startsWith(stdout, "\r\n")) {
            stdout = StringUtils.removeStart(stdout, "\r\n");
        } else if (StringUtils.startsWith(stdout, "\r") || StringUtils.startsWith(stdout, "\n")) {
            stdout = StringUtils.substring(stdout, 1);
        }
        // Strip the prompt from the end of the output
        stdout = StringUtils.removeEnd(stdout, matched);
        return new CommandOutput(stdout, null, 0);
    } catch (Exception e) {
        CommandException ce = new CommandException(e);
        ce.setOutput(tryGetCommandOutput());
        throw ce;
    }
}
Also used : CommandOutput(com.iwave.ext.command.CommandOutput) CommandException(com.iwave.ext.command.CommandException) IOException(java.io.IOException) CommandException(com.iwave.ext.command.CommandException) JSchException(com.jcraft.jsch.JSchException)

Example 5 with CommandException

use of com.iwave.ext.command.CommandException in project coprhd-controller by CoprHD.

the class AixVioDiscoveryAdapter method setNativeGuid.

@Override
protected void setNativeGuid(Host host) {
    AixVioCLI cli = getCli(host);
    try {
        String macAddress = cli.getNetworkAdapterMacAddress(ENT0);
        if (macAddress != null && !host.getNativeGuid().equalsIgnoreCase(macAddress)) {
            checkDuplicateHost(host, macAddress);
            info("Setting nativeGuid for " + host.getId() + " as " + macAddress);
            host.setNativeGuid(macAddress);
            save(host);
        }
    } catch (CommandException ex) {
        LOG.warn("Failed to get MAC address of adapter during discovery");
    }
}
Also used : AixVioCLI(com.emc.aix.AixVioCLI) CommandException(com.iwave.ext.command.CommandException)

Aggregations

CommandException (com.iwave.ext.command.CommandException)6 CommandOutput (com.iwave.ext.command.CommandOutput)2 JSchException (com.jcraft.jsch.JSchException)2 IOException (java.io.IOException)2 AixSystem (com.emc.aix.AixSystem)1 AixVioCLI (com.emc.aix.AixVioCLI)1 HpuxSystem (com.emc.hpux.HpuxSystem)1 LinuxSystemCLI (com.iwave.ext.linux.LinuxSystemCLI)1 GetLinuxVersionLSBReleaseCommand (com.iwave.ext.linux.command.version.GetLinuxVersionLSBReleaseCommand)1 GetRedhatVersionCommandOne (com.iwave.ext.linux.command.version.GetRedhatVersionCommandOne)1 GetRedhatVersionCommandThree (com.iwave.ext.linux.command.version.GetRedhatVersionCommandThree)1 GetRedhatVersionCommandTwo (com.iwave.ext.linux.command.version.GetRedhatVersionCommandTwo)1 GetSuSEVersionCommand (com.iwave.ext.linux.command.version.GetSuSEVersionCommand)1 LinuxVersionCommand (com.iwave.ext.linux.command.version.LinuxVersionCommand)1 LinuxVersion (com.iwave.ext.linux.model.LinuxVersion)1 ChannelExec (com.jcraft.jsch.ChannelExec)1 ArrayList (java.util.ArrayList)1