Search in sources :

Example 6 with AtsManagerException

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

the class AgentController method upgrade.

public ApplicationStatus upgrade(ApplicationStatus previousStatus) throws AtsManagerException {
    // we enter here when the agent is STARTED or STOPPED
    log.info(TOP_LEVEL_ACTION_PREFIX + "Now we will try to perform full upgrade on " + anyApplicationInfo.description);
    String agentZip = sourceProjectInfo.getAgentZip();
    if (StringUtils.isNullOrEmpty(agentZip)) {
        throw new AtsManagerException("The agent zip file is not specified in the configuration");
    }
    // extract agent.zip to a temporary local directory
    String agentFolder = IoUtils.normalizeDirPath(extractAgentZip(agentZip));
    JschSftpClient sftpClient = new JschSftpClient();
    try {
        sftpClient.connect(anyApplicationInfo.systemUser, anyApplicationInfo.systemPassword, anyApplicationInfo.host, anyApplicationInfo.sshPort, anyApplicationInfo.sshPrivateKey, anyApplicationInfo.sshPrivateKeyPassword);
        if (!sftpClient.isRemoteFileOrDirectoryExisting(anyApplicationInfo.sftpHome)) {
            throw new AtsManagerException("The " + anyApplicationInfo.description + " is not installed in " + anyApplicationInfo.sftpHome + ". You must install it first.");
        }
        if (previousStatus == ApplicationStatus.STARTED) {
            // agent is started, stop it before the upgrade
            log.info("We must stop the agent prior to upgrading");
            try {
                stop(false);
            } catch (AtsManagerException e) {
                throw new AtsManagerException("Canceling upgrade as could not stop the agent", e);
            }
        }
        // cleanup the remote directories content
        List<String> preservedPaths = getPreservedPathsList(anyApplicationInfo.paths);
        sftpClient.purgeRemoteDirectoryContents(anyApplicationInfo.sftpHome, preservedPaths);
        anyApplicationInfo.markPathsUnchecked();
        updateAgentFolder(sftpClient, anyApplicationInfo, agentFolder, "");
        for (PathInfo pathInfo : anyApplicationInfo.getUnckeckedPaths()) {
            if (pathInfo.isUpgrade()) {
                if (pathInfo.isFile()) {
                    String fileName = IoUtils.getFileName(pathInfo.getSftpPath());
                    String filePath = sourceProjectInfo.findFile(fileName);
                    if (filePath == null) {
                        log.warn("File '" + fileName + "' can not be found in the source project libraries," + " so we can not upgrade it on the target agent");
                        continue;
                    }
                    int lastSlashIdx = pathInfo.getSftpPath().lastIndexOf('/');
                    if (lastSlashIdx > 0) {
                        sftpClient.makeRemoteDirectories(pathInfo.getSftpPath().substring(0, lastSlashIdx));
                    }
                    sftpClient.uploadFile(filePath, pathInfo.getSftpPath());
                } else {
                // TODO: upgrade directory
                }
            }
        }
        // make agent start file to be executable
        makeScriptsExecutable(anyApplicationInfo);
        // execute post install shell command, if any
        executePostActionShellCommand(anyApplicationInfo, "INSTALL", anyApplicationInfo.postInstallShellCommand);
        if (previousStatus == ApplicationStatus.STARTED) {
            log.info("We stopped the agent while upgrading. We will start it back on");
            ApplicationStatus newStatus = start(false);
            log.info(TOP_LEVEL_ACTION_PREFIX + anyApplicationInfo.description + " is successfully upgraded");
            return newStatus;
        } else {
            // agent status was not changed in this method
            log.info(TOP_LEVEL_ACTION_PREFIX + anyApplicationInfo.description + " is successfully upgraded");
            return ApplicationStatus.STOPPED;
        }
    } finally {
        sftpClient.disconnect();
    }
}
Also used : ApplicationStatus(com.axway.ats.core.atsconfig.AtsInfrastructureManager.ApplicationStatus) AtsManagerException(com.axway.ats.core.atsconfig.exceptions.AtsManagerException) JschSftpClient(com.axway.ats.core.ssh.JschSftpClient)

Example 7 with AtsManagerException

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

the class AgentController method stop.

@Override
public ApplicationStatus stop(boolean isTopLevelAction) throws AtsManagerException {
    JschSshClient sshClient = new JschSshClient();
    try {
        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;
        }
        log.info((isTopLevelAction ? TOP_LEVEL_ACTION_PREFIX : "") + "Now we will try to stop " + anyApplicationInfo.description);
        sshClient.connect(anyApplicationInfo.systemUser, anyApplicationInfo.systemPassword, anyApplicationInfo.host, anyApplicationInfo.sshPort, anyApplicationInfo.sshPrivateKey, anyApplicationInfo.sshPrivateKeyPassword);
        String agentStopCommand = anyApplicationInfo.getStopCommand();
        log.info(TOP_LEVEL_ACTION_PREFIX + anyApplicationInfo.description + " stop with: " + agentStopCommand);
        final int stopCommandExitCode = sshClient.execute(agentStopCommand, true);
        final String stopCommandExecutionResult = sshClient.getLastCommandExecutionResult();
        if (stopCommandExitCode == 0 && StringUtils.isNullOrEmpty(sshClient.getErrorOutput())) {
            log.info(anyApplicationInfo.description + " is probably stopped, but we will do a quick check");
            if (getStatus(sshClient, false) == ApplicationStatus.STOPPED) {
                log.info((isTopLevelAction ? TOP_LEVEL_ACTION_PREFIX : "") + anyApplicationInfo.description + " is successfully stopped");
                executePostActionShellCommand(anyApplicationInfo, "STOP", anyApplicationInfo.getPostStopShellCommand());
                return ApplicationStatus.STOPPED;
            }
        }
        throw new AtsManagerException("Can't stop " + anyApplicationInfo.description + "\n" + stopCommandExecutionResult + "\nYou can check the nohup.out file for details");
    } 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 8 with AtsManagerException

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

the class AtsInfrastructureManager method upgradeAgent.

/**
     * Upgrade the ATS Agent by alias
     *
     * @param agentAlias the agent alias declared in the configuration
     * @throws AtsManagerException
     */
public ApplicationStatus upgradeAgent(String agentAlias, ApplicationStatus previousStatus) throws AtsManagerException {
    // we enter here when the agent is STARTED or STOPPED
    AgentInfo agentInfo = getAgentInfo(agentAlias);
    log.info(TOP_LEVEL_ACTION_PREFIX + "Now we will try to perform full upgrade on " + agentInfo.getDescription());
    String agentZip = projectConfiguration.getSourceProject().getAgentZip();
    if (StringUtils.isNullOrEmpty(agentZip)) {
        throw new AtsManagerException("The agent zip file is not specified in the configuration");
    }
    // extract agent.zip to a temporary local directory
    String agentFolder = IoUtils.normalizeDirPath(extractAgentZip(agentZip));
    JschSftpClient sftpClient = new JschSftpClient();
    try {
        sftpClient.connect(agentInfo.getSystemUser(), agentInfo.getSystemPassword(), agentInfo.getHost(), agentInfo.getSSHPort(), agentInfo.getSSHPrivateKey(), agentInfo.getSSHPrivateKeyPassword());
        if (!sftpClient.isRemoteFileOrDirectoryExisting(agentInfo.getSftpHome())) {
            throw new AtsManagerException("The " + agentInfo.getDescription() + " is not installed in " + agentInfo.getSftpHome() + ". You must install it first.");
        }
        if (previousStatus == ApplicationStatus.STARTED) {
            // agent is started, stop it before the upgrade
            log.info("We must stop the agent prior to upgrading");
            try {
                stopAnyApplication(agentAlias, false);
            } catch (AtsManagerException e) {
                throw new AtsManagerException("Canceling upgrade as could not stop the agent", e);
            }
        }
        // cleanup the remote directories content
        List<String> preservedPaths = getPreservedPathsList(agentInfo.getPaths());
        sftpClient.purgeRemoteDirectoryContents(agentInfo.getSftpHome(), preservedPaths);
        agentInfo.markPathsUnchecked();
        updateAgentFolder(sftpClient, agentInfo, agentFolder, "");
        for (PathInfo pathInfo : agentInfo.getUnckeckedPaths()) {
            if (pathInfo.isUpgrade()) {
                if (pathInfo.isFile()) {
                    String fileName = IoUtils.getFileName(pathInfo.getSftpPath());
                    String filePath = projectConfiguration.getSourceProject().findFile(fileName);
                    if (filePath == null) {
                        log.warn("File '" + fileName + "' can not be found in the source project libraries," + " so we can not upgrade it on the target agent");
                        continue;
                    }
                    int lastSlashIdx = pathInfo.getSftpPath().lastIndexOf('/');
                    if (lastSlashIdx > 0) {
                        sftpClient.makeRemoteDirectories(pathInfo.getSftpPath().substring(0, lastSlashIdx));
                    }
                    sftpClient.uploadFile(filePath, pathInfo.getSftpPath());
                } else {
                // TODO: upgrade directory
                }
            }
        }
        // make agent start file to be executable
        makeScriptsExecutable(agentInfo);
        // execute post install shell command, if any
        executePostActionShellCommand(agentInfo, "install", agentInfo.getPostInstallShellCommand());
        if (previousStatus == ApplicationStatus.STARTED) {
            log.info("We stopped the agent while upgrading. Now we will start it back on");
            ApplicationStatus newStatus = startAnyApplication(agentAlias, false);
            log.info(TOP_LEVEL_ACTION_PREFIX + agentInfo.getDescription() + " is successfully upgraded");
            return newStatus;
        } else {
            // agent status was not changed in this method
            log.info(TOP_LEVEL_ACTION_PREFIX + agentInfo.getDescription() + " is successfully upgraded");
            return ApplicationStatus.STOPPED;
        }
    } finally {
        sftpClient.disconnect();
    }
}
Also used : AtsManagerException(com.axway.ats.core.atsconfig.exceptions.AtsManagerException) JschSftpClient(com.axway.ats.core.ssh.JschSftpClient) AgentInfo(com.axway.ats.core.atsconfig.model.AgentInfo) PathInfo(com.axway.ats.core.atsconfig.model.PathInfo)

Example 9 with AtsManagerException

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

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

the class AtsInfrastructureManager method lightUpgradeAgent.

/**
     * Light upgrade the ATS Agent by alias. Which means upgrade of specific
     * directories only
     *
     * @param agentAlias the agent alias declared in the configuration
     * @throws AtsManagerException
     */
public ApplicationStatus lightUpgradeAgent(String agentAlias, ApplicationStatus previousStatus) throws AtsManagerException {
    // we enter here when the agent is STARTED or STOPPED
    ApplicationStatus newStatus = previousStatus;
    AgentInfo agentInfo = getAgentInfo(agentAlias);
    log.info(TOP_LEVEL_ACTION_PREFIX + "Now we will try to perform light upgrade on " + agentInfo.getDescription());
    JschSftpClient sftpClient = new JschSftpClient();
    try {
        sftpClient.connect(agentInfo.getSystemUser(), agentInfo.getSystemPassword(), agentInfo.getHost(), agentInfo.getSSHPort(), agentInfo.getSSHPrivateKey(), agentInfo.getSSHPrivateKeyPassword());
        // Stop the agent if at least one of the file upgrades requires it
        if (newStatus == ApplicationStatus.STARTED) {
            for (PathInfo pathInfo : agentInfo.getPaths()) {
                if (pathInfo.isUpgrade() && mustUpgradeOnStoppedAgent(pathInfo.getSftpPath())) {
                    log.info("We must stop the agent prior to upgrading " + pathInfo.getPath());
                    try {
                        newStatus = stopAnyApplication(agentAlias, false);
                        break;
                    } catch (AtsManagerException e) {
                        throw new AtsManagerException("Canceling upgrade as could not stop the agent", e);
                    }
                }
            }
        }
        // Do the actual upgrade
        for (PathInfo pathInfo : agentInfo.getPaths()) {
            if (pathInfo.isUpgrade()) {
                if (pathInfo.isFile()) {
                    String fileName = IoUtils.getFileName(pathInfo.getSftpPath());
                    String filePath = projectConfiguration.getSourceProject().findFile(fileName);
                    if (filePath == null) {
                        log.warn("File '" + fileName + "' can not be found in the source project libraries," + " so we can not upgrade it on the target agent");
                        continue;
                    }
                    // create directories to the file, only if not exist
                    int lastSlashIdx = pathInfo.getSftpPath().lastIndexOf('/');
                    if (lastSlashIdx > 0) {
                        sftpClient.makeRemoteDirectories(pathInfo.getSftpPath().substring(0, lastSlashIdx));
                    }
                    sftpClient.uploadFile(filePath, pathInfo.getSftpPath());
                } else {
                // TODO: upgrade directory
                }
            }
        }
        // Start the agent if we stopped it
        if (previousStatus == ApplicationStatus.STARTED && newStatus == ApplicationStatus.STOPPED) {
            log.info("We stopped the agent while upgrading. Now we will start it back on");
            newStatus = startAnyApplication(agentAlias, false);
            log.info(TOP_LEVEL_ACTION_PREFIX + agentInfo.getDescription() + " is successfully upgraded");
            return newStatus;
        } else {
            // agent status was not changed in this method
            log.info(TOP_LEVEL_ACTION_PREFIX + agentInfo.getDescription() + " is successfully upgraded");
            return previousStatus;
        }
    } finally {
        sftpClient.disconnect();
    }
}
Also used : JschSftpClient(com.axway.ats.core.ssh.JschSftpClient) AtsManagerException(com.axway.ats.core.atsconfig.exceptions.AtsManagerException) AgentInfo(com.axway.ats.core.atsconfig.model.AgentInfo) PathInfo(com.axway.ats.core.atsconfig.model.PathInfo)

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