Search in sources :

Example 1 with PathInfo

use of com.axway.ats.core.atsconfig.model.PathInfo 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 2 with PathInfo

use of com.axway.ats.core.atsconfig.model.PathInfo 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 3 with PathInfo

use of com.axway.ats.core.atsconfig.model.PathInfo in project ats-framework by Axway.

the class AtsInfrastructureManager method updateAgentFolder.

/**
     *
     * @param sftpClient {@link JschSftpClient} instance
     * @param agentInfo the current agent information
     * @param localAgentFolder local agent folder
     * @param relativeFolderPath the relative path of the current folder for update
     * @throws AtsManagerException
     */
private void updateAgentFolder(JschSftpClient sftpClient, AgentInfo agentInfo, String localAgentFolder, String relativeFolderPath) throws AtsManagerException {
    String remoteFolderPath = agentInfo.getSftpHome() + relativeFolderPath;
    if (!sftpClient.isRemoteFileOrDirectoryExisting(remoteFolderPath)) {
        sftpClient.uploadDirectory(localAgentFolder + relativeFolderPath, remoteFolderPath, true);
        return;
    }
    File localFolder = new File(localAgentFolder + relativeFolderPath);
    File[] localEntries = localFolder.listFiles();
    if (localEntries != null && localEntries.length > 0) {
        for (File localEntry : localEntries) {
            String remoteFilePath = remoteFolderPath + localEntry.getName();
            PathInfo pathInfo = agentInfo.getPathInfo(remoteFilePath, localEntry.isFile(), true);
            if (pathInfo != null) {
                pathInfo.setChecked(true);
                if (!pathInfo.isUpgrade()) {
                    log.info("Skipping upgrade of '" + remoteFilePath + "', because its 'upgrade' flag is 'false'");
                    continue;
                }
            }
            if (localEntry.isDirectory()) {
                updateAgentFolder(sftpClient, agentInfo, localAgentFolder, relativeFolderPath + localEntry.getName() + "/");
            } else {
                String localFilePath = localAgentFolder + relativeFolderPath + localEntry.getName();
                sftpClient.uploadFile(localFilePath, remoteFilePath);
            }
        }
    }
}
Also used : PathInfo(com.axway.ats.core.atsconfig.model.PathInfo) File(java.io.File)

Example 4 with PathInfo

use of com.axway.ats.core.atsconfig.model.PathInfo 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)

Aggregations

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