Example usage for com.amazonaws.services.elasticbeanstalk.model EnvironmentDescription getDateUpdated

List of usage examples for com.amazonaws.services.elasticbeanstalk.model EnvironmentDescription getDateUpdated

Introduction

In this page you can find the example usage for com.amazonaws.services.elasticbeanstalk.model EnvironmentDescription getDateUpdated.

Prototype


public java.util.Date getDateUpdated() 

Source Link

Document

The last modified date for this environment.

Usage

From source file:br.com.ingenieux.mojo.beanstalk.version.CleanPreviousVersionsMojo.java

License:Apache License

@Override
protected Object executeInternal() throws MojoExecutionException, MojoFailureException {
    boolean bVersionsToKeepDefined = (null != versionsToKeep);
    boolean bDaysToKeepDefined = (null != daysToKeep);

    if (!(bVersionsToKeepDefined ^ bDaysToKeepDefined)) {
        throw new MojoFailureException("Declare either versionsToKeep or daysToKeep, but not both nor none!");
    }/* w  w  w.j  a  v a 2 s  .c o m*/

    DescribeApplicationVersionsRequest describeApplicationVersionsRequest = new DescribeApplicationVersionsRequest()
            .withApplicationName(applicationName);

    DescribeApplicationVersionsResult appVersions = getService()
            .describeApplicationVersions(describeApplicationVersionsRequest);

    DescribeEnvironmentsResult environments = getService().describeEnvironments();

    List<ApplicationVersionDescription> appVersionList = new ArrayList<ApplicationVersionDescription>(
            appVersions.getApplicationVersions());

    deletedVersionsCount = 0;

    for (EnvironmentDescription d : environments.getEnvironments()) {
        boolean bActiveEnvironment = (d.getStatus().equals("Running") || d.getStatus().equals("Launching")
                || d.getStatus().equals("Ready"));

        for (ListIterator<ApplicationVersionDescription> appVersionIterator = appVersionList
                .listIterator(); appVersionIterator.hasNext();) {
            ApplicationVersionDescription appVersion = appVersionIterator.next();

            boolean bMatchesVersion = appVersion.getVersionLabel().equals(d.getVersionLabel());

            if (bActiveEnvironment && bMatchesVersion) {
                getLog().info("VersionLabel " + appVersion.getVersionLabel() + " is bound to environment "
                        + d.getEnvironmentName() + " - Skipping it");

                appVersionIterator.remove();
            }
        }
    }

    filterAppVersionListByVersionLabelPattern(appVersionList, cleanFilter);

    Collections.sort(appVersionList, new Comparator<ApplicationVersionDescription>() {
        @Override
        public int compare(ApplicationVersionDescription o1, ApplicationVersionDescription o2) {
            return new CompareToBuilder().append(o1.getDateUpdated(), o2.getDateUpdated()).toComparison();
        }
    });

    if (bDaysToKeepDefined) {
        Date now = new Date();

        for (ApplicationVersionDescription d : appVersionList) {
            long delta = now.getTime() - d.getDateUpdated().getTime();

            delta /= 1000;
            delta /= 86400;

            boolean shouldDeleteP = (delta > daysToKeep);

            if (getLog().isDebugEnabled()) {
                getLog().debug("Version " + d.getVersionLabel() + " was from " + delta
                        + " days ago. Should we delete? " + shouldDeleteP);
            }

            if (shouldDeleteP) {
                deleteVersion(d);
            }
        }
    } else {
        while (appVersionList.size() > versionsToKeep) {
            deleteVersion(appVersionList.remove(0));
        }
    }

    getLog().info("Deleted " + deletedVersionsCount + " versions.");

    return null;
}