Search in sources :

Example 1 with JschSftpClient

use of com.axway.ats.core.ssh.JschSftpClient 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 2 with JschSftpClient

use of com.axway.ats.core.ssh.JschSftpClient 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 3 with JschSftpClient

use of com.axway.ats.core.ssh.JschSftpClient 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)

Example 4 with JschSftpClient

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

the class AtsInfrastructureManager method installAgent.

/**
     * Install the ATS Agent by alias
     *
     * @param agentAlias the agent alias declared in the configuration
     * @throws AtsManagerException
     */
public ApplicationStatus installAgent(String agentAlias) throws AtsManagerException {
    AgentInfo agentInfo = getAgentInfo(agentAlias);
    log.info(TOP_LEVEL_ACTION_PREFIX + "Now we will try to install " + agentInfo.getDescription());
    String agentZip = projectConfiguration.getSourceProject().getAgentZip();
    if (StringUtils.isNullOrEmpty(agentZip)) {
        throw new AtsManagerException("The agent zip source 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 {
        // upload clean agent
        log.info("Upload clean " + agentInfo.getDescription());
        sftpClient.connect(agentInfo.getSystemUser(), agentInfo.getSystemPassword(), agentInfo.getHost(), agentInfo.getSSHPort(), agentInfo.getSSHPrivateKey(), agentInfo.getSSHPrivateKeyPassword());
        if (sftpClient.isRemoteFileOrDirectoryExisting(agentInfo.getSftpHome())) {
            sftpClient.purgeRemoteDirectoryContents(agentInfo.getSftpHome());
        }
        sftpClient.uploadDirectory(agentFolder, agentInfo.getSftpHome(), true);
        // upload custom agent dependencies
        log.info("Upload custom agent dependencies");
        for (PathInfo pathInfo : agentInfo.getPaths()) {
            if (pathInfo.isFile()) {
                if (!sftpClient.isRemoteFileOrDirectoryExisting(pathInfo.getSftpPath())) {
                    String fileName = IoUtils.getFileName(pathInfo.getSftpPath());
                    String filePath = projectConfiguration.getSourceProject().findFile(fileName);
                    if (filePath == null) {
                        log.warn("File '" + fileName + "' can't be found in the source project libraries," + " so it can't be uploaded to " + agentInfo.getDescription());
                        continue;
                    }
                    if (!new File(filePath).exists()) {
                        log.warn("Local file '" + filePath + "' does not exist on the local system," + " so it can't be uploaded to " + agentInfo.getDescription());
                        continue;
                    }
                    int lastSlashIdx = pathInfo.getSftpPath().lastIndexOf('/');
                    if (lastSlashIdx > 0) {
                        sftpClient.makeRemoteDirectories(pathInfo.getSftpPath().substring(0, lastSlashIdx));
                    }
                    sftpClient.uploadFile(filePath, pathInfo.getSftpPath());
                }
            } else {
                log.warn("Uploading directories into ATS agent is still not supported");
            }
        }
        // make agent start file to be executable
        makeScriptsExecutable(agentInfo);
        // execute post install shell command, if any
        executePostActionShellCommand(agentInfo, "install", agentInfo.getPostInstallShellCommand());
        log.info(TOP_LEVEL_ACTION_PREFIX + "Successfully installed " + agentInfo.getDescription());
        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) File(java.io.File)

Example 5 with JschSftpClient

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

the class ApplicationController method isApplicationInstalled.

private boolean isApplicationInstalled(ApplicationInfo anyApplicationInfo) {
    JschSftpClient sftpClient = new JschSftpClient();
    try {
        sftpClient.connect(anyApplicationInfo.systemUser, anyApplicationInfo.systemPassword, anyApplicationInfo.host, anyApplicationInfo.sshPort);
        boolean isDeployed = sftpClient.isRemoteFileOrDirectoryExisting(anyApplicationInfo.getSftpHome());
        log.info(anyApplicationInfo.description + " seems " + (isDeployed ? "" : "not ") + "deployed in " + anyApplicationInfo.getSftpHome());
        return isDeployed;
    } finally {
        sftpClient.disconnect();
    }
}
Also used : JschSftpClient(com.axway.ats.core.ssh.JschSftpClient)

Aggregations

JschSftpClient (com.axway.ats.core.ssh.JschSftpClient)6 AtsManagerException (com.axway.ats.core.atsconfig.exceptions.AtsManagerException)4 AgentInfo (com.axway.ats.core.atsconfig.model.AgentInfo)3 PathInfo (com.axway.ats.core.atsconfig.model.PathInfo)3 ApplicationStatus (com.axway.ats.core.atsconfig.AtsInfrastructureManager.ApplicationStatus)1 File (java.io.File)1