Search in sources :

Example 6 with JschSshClient

use of com.axway.ats.core.ssh.JschSshClient in project ats-framework by Axway.

the class AtsInfrastructureManager method makeScriptsExecutable.

/**
     * Make script files executable
     *
     * @param agentInfo agent information
     * @throws AtsManagerException
     */
private void makeScriptsExecutable(AgentInfo agentInfo) throws AtsManagerException {
    // set executable privileges to the script files
    if (agentInfo.isUnix()) {
        log.info("Set executable priviledges on all 'sh' files in " + agentInfo.getHome());
        JschSshClient sshClient = new JschSshClient();
        try {
            sshClient.connect(agentInfo.getSystemUser(), agentInfo.getSystemPassword(), agentInfo.getHost(), agentInfo.getSSHPort(), agentInfo.getSSHPrivateKey(), agentInfo.getSSHPrivateKeyPassword());
            int exitCode = sshClient.execute("chmod a+x " + agentInfo.getHome() + "/*.sh", true);
            if (exitCode != 0) {
                throw new AtsManagerException("Unable to set execute privileges to the shell script files in '" + agentInfo.getHome() + "'");
            }
        } finally {
            sshClient.disconnect();
        }
    }
}
Also used : AtsManagerException(com.axway.ats.core.atsconfig.exceptions.AtsManagerException) JschSshClient(com.axway.ats.core.ssh.JschSshClient)

Example 7 with JschSshClient

use of com.axway.ats.core.ssh.JschSshClient in project ats-framework by Axway.

the class AtsInfrastructureManager method executePostActionShellCommand.

/**
     * Execute post install/upgrade(full) shell command, if any
     *
     * @param agentInfo agent information
     * @throws AtsManagerException
     */
private void executePostActionShellCommand(AgentInfo agentInfo, String actionName, String shellCommand) throws AtsManagerException {
    if (shellCommand != null) {
        log.info("Executing post '" + actionName + "' shell command: " + shellCommand);
        JschSshClient sshClient = new JschSshClient();
        try {
            sshClient.connect(agentInfo.getSystemUser(), agentInfo.getSystemPassword(), agentInfo.getHost(), agentInfo.getSSHPort(), agentInfo.getSSHPrivateKey(), agentInfo.getSSHPrivateKeyPassword());
            int exitCode = sshClient.execute(shellCommand, true);
            if (exitCode != 0) {
                throw new AtsManagerException("Unable to execute the post '" + actionName + "' shell command '" + shellCommand + "' on agent '" + agentInfo.getAlias() + "'. The error output is" + (StringUtils.isNullOrEmpty(sshClient.getErrorOutput()) ? " empty." : ":\n" + sshClient.getErrorOutput()));
            }
            log.info("The output of shell command \"" + shellCommand + "\" is" + (StringUtils.isNullOrEmpty(sshClient.getStandardOutput()) ? " empty." : ":\n" + sshClient.getStandardOutput()));
        } finally {
            sshClient.disconnect();
        }
    }
}
Also used : AtsManagerException(com.axway.ats.core.atsconfig.exceptions.AtsManagerException) JschSshClient(com.axway.ats.core.ssh.JschSshClient)

Example 8 with JschSshClient

use of com.axway.ats.core.ssh.JschSshClient in project ats-framework by Axway.

the class ApplicationController method start.

@Override
public ApplicationStatus start(boolean isTopLevelAction) throws AtsManagerException {
    JschSshClient sshClient = new JschSshClient();
    try {
        // it must be stopped, before we try to start it
        ApplicationStatus status = getStatus(sshClient, false);
        if (status != ApplicationStatus.STOPPED) {
            log.error((isTopLevelAction ? TOP_LEVEL_ACTION_PREFIX : "") + "We will not try to start " + anyApplicationInfo.description + " as it is currently " + status.name());
            return status;
        }
        // connect
        String command = anyApplicationInfo.getStartCommand();
        log.info((isTopLevelAction ? TOP_LEVEL_ACTION_PREFIX : "") + "Now we will try to start " + anyApplicationInfo.description + " with: " + command);
        sshClient.connect(anyApplicationInfo.systemUser, anyApplicationInfo.systemPassword, anyApplicationInfo.host, anyApplicationInfo.sshPort);
        // execute shell command
        int commandExitCode = sshClient.execute(command, true);
        // check exit code
        // check if there is something in STD ERR
        boolean isStartedOk = false;
        if (commandExitCode == 0 && StringUtils.isNullOrEmpty(sshClient.getErrorOutput())) {
            String stdoutSearchToken = anyApplicationInfo.getStartCommandStdOutSearchToken();
            if (stdoutSearchToken != null) {
                // we confirm the success by a token in STD OUT
                if (Pattern.compile(stdoutSearchToken, Pattern.DOTALL).matcher(sshClient.getStandardOutput()).matches()) {
                    isStartedOk = true;
                } else {
                    log.error("Execution of '" + command + "' completed with exit code " + commandExitCode + " and empty STD ERR, but we did not get the expected '" + stdoutSearchToken + "' in STD OUT");
                }
            } else {
                // we confirm the success by checking the status again
                if (getStatus(sshClient, false) == ApplicationStatus.STARTED) {
                    isStartedOk = true;
                }
            }
        }
        if (isStartedOk) {
            log.info((isTopLevelAction ? TOP_LEVEL_ACTION_PREFIX : "") + anyApplicationInfo.description + " is successfully started");
            executePostActionShellCommand(anyApplicationInfo, "START", anyApplicationInfo.getPostStartShellCommand());
            return ApplicationStatus.STARTED;
        } else {
            throw new AtsManagerException("Can't start " + anyApplicationInfo.description + "\n" + sshClient.getLastCommandExecutionResult());
        }
    } finally {
        sshClient.disconnect();
    }
}
Also used : ApplicationStatus(com.axway.ats.core.atsconfig.AtsInfrastructureManager.ApplicationStatus) AtsManagerException(com.axway.ats.core.atsconfig.exceptions.AtsManagerException) JschSshClient(com.axway.ats.core.ssh.JschSshClient)

Example 9 with JschSshClient

use of com.axway.ats.core.ssh.JschSshClient in project ats-framework by Axway.

the class ApplicationController method stop.

@Override
public ApplicationStatus stop(boolean isTopLevelAction) throws AtsManagerException {
    JschSshClient sshClient = new JschSshClient();
    try {
        // it must be started, before we try to stop it
        ApplicationStatus status = getStatus(sshClient, false);
        if (status != ApplicationStatus.STARTED) {
            log.error((isTopLevelAction ? TOP_LEVEL_ACTION_PREFIX : "") + "We will not try to stop " + anyApplicationInfo.description + " as it is currently " + status.name());
            return status;
        }
        // connect
        String command = anyApplicationInfo.getStopCommand();
        log.info((isTopLevelAction ? TOP_LEVEL_ACTION_PREFIX : "") + "Now we will try to stop " + anyApplicationInfo.description + " with: " + command);
        sshClient.connect(anyApplicationInfo.systemUser, anyApplicationInfo.systemPassword, anyApplicationInfo.host, anyApplicationInfo.sshPort);
        // execute shell command
        int commandExitCode = sshClient.execute(command, true);
        // check exit code
        // check if there is something in STD ERR
        boolean isStoppedOk = false;
        if (commandExitCode == 0 && StringUtils.isNullOrEmpty(sshClient.getErrorOutput())) {
            String stdoutSearchToken = anyApplicationInfo.getStopCommandStdOutSearchToken();
            if (stdoutSearchToken != null) {
                // we confirm the success by a token in STD OUT
                if (Pattern.compile(stdoutSearchToken, Pattern.DOTALL).matcher(sshClient.getStandardOutput()).matches()) {
                    isStoppedOk = true;
                } else {
                    log.error("Execution of '" + command + "' completed with exit code " + commandExitCode + " and empty STD ERR, but we did not get the expected '" + stdoutSearchToken + "' in STD OUT");
                }
            } else {
                // we confirm the success by checking the status again
                if (getStatus(sshClient, false) == ApplicationStatus.STOPPED) {
                    isStoppedOk = true;
                }
            }
        }
        if (isStoppedOk) {
            log.info((isTopLevelAction ? TOP_LEVEL_ACTION_PREFIX : "") + anyApplicationInfo.description + " is successfully stopped");
            executePostActionShellCommand(anyApplicationInfo, "STOP", anyApplicationInfo.getPostStopShellCommand());
            return ApplicationStatus.STOPPED;
        } else {
            throw new AtsManagerException("Can't stop " + anyApplicationInfo.description + "\n" + sshClient.getLastCommandExecutionResult());
        }
    } finally {
        sshClient.disconnect();
    }
}
Also used : ApplicationStatus(com.axway.ats.core.atsconfig.AtsInfrastructureManager.ApplicationStatus) AtsManagerException(com.axway.ats.core.atsconfig.exceptions.AtsManagerException) JschSshClient(com.axway.ats.core.ssh.JschSshClient)

Example 10 with JschSshClient

use of com.axway.ats.core.ssh.JschSshClient in project ats-framework by Axway.

the class AbstractApplicationController method executeShellCommand.

public JschSshClient executeShellCommand(AbstractApplicationInfo info, String command) {
    if (!info.isUnix()) {
        command = "cmd.exe /c \"" + command + "\"";
    }
    JschSshClient sshClient = new JschSshClient();
    log.info("Run '" + command + "' on " + info.alias);
    sshClient.connect(info.systemUser, info.systemPassword, info.host, info.sshPort);
    sshClient.execute(command, false);
    return sshClient;
}
Also used : JschSshClient(com.axway.ats.core.ssh.JschSshClient)

Aggregations

JschSshClient (com.axway.ats.core.ssh.JschSshClient)10 AtsManagerException (com.axway.ats.core.atsconfig.exceptions.AtsManagerException)9 ApplicationStatus (com.axway.ats.core.atsconfig.AtsInfrastructureManager.ApplicationStatus)5