Search in sources :

Example 16 with AtsManagerException

use of com.axway.ats.core.atsconfig.exceptions.AtsManagerException 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 17 with AtsManagerException

use of com.axway.ats.core.atsconfig.exceptions.AtsManagerException in project ats-framework by Axway.

the class AgentController method extractAgentZip.

/**
     * Extract the agent zip file in a temporary directory and return its
     * location
     *
     * @param agentZipPath agent zip file path
     * @return the directory where the zip file is unzipped
     * @throws AtsManagerException
     */
private String extractAgentZip(String agentZipPath) throws AtsManagerException {
    File agentZip = new File(agentZipPath);
    if (!agentZip.exists()) {
        throw new AtsManagerException("The agent ZIP file doesn't exist '" + agentZipPath + "'");
    }
    final String tempPath = IoUtils.normalizeDirPath(AtsSystemProperties.SYSTEM_USER_TEMP_DIR + "/ats_tmp/");
    String agentFolderName = tempPath + "agent_" + String.valueOf(agentZip.lastModified());
    File agentFolder = new File(agentFolderName);
    if (!agentFolder.exists()) {
        try {
            IoUtils.unzip(agentZipPath, agentFolderName, true);
        } catch (IOException ioe) {
            throw new AtsManagerException("Unable to unzip the agent ZIP file '" + agentZipPath + "' to the temporary created directory '" + agentFolderName + "'", ioe);
        }
    }
    return agentFolderName;
}
Also used : AtsManagerException(com.axway.ats.core.atsconfig.exceptions.AtsManagerException) IOException(java.io.IOException) File(java.io.File)

Aggregations

AtsManagerException (com.axway.ats.core.atsconfig.exceptions.AtsManagerException)17 JschSshClient (com.axway.ats.core.ssh.JschSshClient)9 ApplicationStatus (com.axway.ats.core.atsconfig.AtsInfrastructureManager.ApplicationStatus)6 AgentInfo (com.axway.ats.core.atsconfig.model.AgentInfo)4 JschSftpClient (com.axway.ats.core.ssh.JschSftpClient)4 File (java.io.File)4 PathInfo (com.axway.ats.core.atsconfig.model.PathInfo)3 IOException (java.io.IOException)3 AtsConfigurationException (com.axway.ats.core.atsconfig.exceptions.AtsConfigurationException)1 AbstractApplicationController (com.axway.ats.core.atsconfig.model.AbstractApplicationController)1 AgentController (com.axway.ats.core.atsconfig.model.AgentController)1 ApplicationController (com.axway.ats.core.atsconfig.model.ApplicationController)1 ApplicationInfo (com.axway.ats.core.atsconfig.model.ApplicationInfo)1 BufferedInputStream (java.io.BufferedInputStream)1 BufferedOutputStream (java.io.BufferedOutputStream)1 FileOutputStream (java.io.FileOutputStream)1 URL (java.net.URL)1 URLConnection (java.net.URLConnection)1