Example usage for com.amazonaws.services.elasticbeanstalk.model DescribeEnvironmentsResult getEnvironments

List of usage examples for com.amazonaws.services.elasticbeanstalk.model DescribeEnvironmentsResult getEnvironments

Introduction

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

Prototype


public java.util.List<EnvironmentDescription> getEnvironments() 

Source Link

Document

Returns an EnvironmentDescription list.

Usage

From source file:br.com.ingenieux.jenkins.plugins.awsebdeployment.cmd.ZeroDowntime.java

License:Apache License

private void terminateEnvironment(String environmentId) {
    final DescribeEnvironmentsResult result = getAwseb().describeEnvironments(
            new DescribeEnvironmentsRequest().withEnvironmentIds(environmentId).withIncludeDeleted(false));

    if (result.getEnvironments().isEmpty()) {
        log("Environment environmentId '%s' was already finished.");
        return;//  w ww .j  ava  2 s . co m
    }

    log("Terminating environment %s", environmentId);

    TerminateEnvironmentRequest request = new TerminateEnvironmentRequest().withEnvironmentId(environmentId);

    getAwseb().terminateEnvironment(request);
}

From source file:br.com.ingenieux.jenkins.plugins.awsebdeployment.cmd.ZeroDowntime.java

License:Apache License

private EnvironmentDescription lookupEnvironmentIds(List<String> environmentNames)
        throws InvalidEnvironmentsSizeException, InvalidDeploymentTypeException {
    DescribeEnvironmentsResult environments = getAwseb().describeEnvironments(new DescribeEnvironmentsRequest()
            .withApplicationName(getApplicationName()).withIncludeDeleted(false));

    for (EnvironmentDescription env : environments.getEnvironments()) {
        if (environmentNames.contains(env.getEnvironmentName())) {
            if (WORKER_ENVIRONMENT_TYPE.equals(env.getTier().getName())) {
                throw new InvalidDeploymentTypeException();
            }//from   w w  w . j  a  v  a2s. c o m

            return env;
        }
    }

    throw new InvalidEnvironmentsSizeException(getApplicationName(), environmentNames.get(0),
            environments.getEnvironments().size());
}

From source file:br.com.ingenieux.jenkins.plugins.awsebdeployment.Deployer.java

License:Apache License

private void updateEnvironments() throws Exception {
    DescribeEnvironmentsResult environments = awseb.describeEnvironments(new DescribeEnvironmentsRequest()
            .withApplicationName(applicationName).withEnvironmentNames(environmentName));

    boolean found = (1 == environments.getEnvironments().size());

    if (found) {// w w  w .java  2s.c  om
        for (int nAttempt = 1; nAttempt <= MAX_ATTEMPTS; nAttempt++) {
            String environmentId = environments.getEnvironments().get(0).getEnvironmentId();

            log("Attempt %d/%s", nAttempt, MAX_ATTEMPTS);

            log("Environment found (environment id=%s). Attempting to update environment to version label %s",
                    environmentId, versionLabel);

            UpdateEnvironmentRequest uavReq = new UpdateEnvironmentRequest()
                    .withEnvironmentName(environmentName).withVersionLabel(versionLabel);

            try {
                awseb.updateEnvironment(uavReq);

                log("q'Apla!");

                return;
            } catch (Exception exc) {
                log("Problem: " + exc.getMessage());

                if (nAttempt == MAX_ATTEMPTS) {
                    log("Giving it up");

                    throw exc;
                }

                log("Reattempting in 90s, up to %d", MAX_ATTEMPTS);

                Thread.sleep(TimeUnit.SECONDS.toMillis(90));
            }
        }
    } else {
        log("Environment not found. Continuing");
    }
}

From source file:br.com.ingenieux.mojo.beanstalk.BeanstalkTestBase.java

License:Apache License

public void clearEnvironments() {
    DescribeEnvironmentsResult environments = service.describeEnvironments();

    for (EnvironmentDescription d : environments.getEnvironments()) {
        service.terminateEnvironment(new TerminateEnvironmentRequest().withEnvironmentId(d.getEnvironmentId())
                .withTerminateResources(true));
    }/*from  w  ww  .j  a va  2s .c om*/
}

From source file:br.com.ingenieux.mojo.beanstalk.env.DescribeEnvironmentsMojo.java

License:Apache License

@Override
protected Object executeInternal() throws MojoExecutionException, MojoFailureException {
    DescribeEnvironmentsRequest req = new DescribeEnvironmentsRequest();

    req.setApplicationName(applicationName);
    req.setIncludeDeleted(includeDeleted);

    // TODO add environmentNames / environmentIds / includeDeletedBackTo

    DescribeEnvironmentsResult result = getService().describeEnvironments(req);

    if (null != outputFile) {
        getLog().info("Writing results into " + outputFile.getName());

        try {/*  ww w . j  a v a  2  s  .c o  m*/
            ObjectMapper objectMapper = new ObjectMapper();

            ObjectWriter writer = objectMapper.writerWithDefaultPrettyPrinter();

            writer.writeValue(outputFile, result.getEnvironments());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

        return null;
    }

    return result;
}

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!");
    }//from  w  w w . j  a v a2  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;
}

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

License:Apache License

@Override
protected Object executeInternal() throws MojoExecutionException, MojoFailureException {
    // TODO: Deal with withVersionLabels
    DescribeApplicationVersionsRequest describeApplicationVersionsRequest = new DescribeApplicationVersionsRequest()
            .withApplicationName(applicationName);

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

    DescribeEnvironmentsRequest describeEnvironmentsRequest = new DescribeEnvironmentsRequest()
            .withApplicationName(applicationName).withEnvironmentIds(curEnv.getEnvironmentId())
            .withEnvironmentNames(curEnv.getEnvironmentName()).withIncludeDeleted(false);

    DescribeEnvironmentsResult environments = getService().describeEnvironments(describeEnvironmentsRequest);

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

    List<EnvironmentDescription> environmentList = environments.getEnvironments();

    if (environmentList.isEmpty()) {
        throw new MojoFailureException("No environments were found");
    }/*from  ww  w  .  j a  v  a  2  s  .  c  o m*/

    EnvironmentDescription d = environmentList.get(0);

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

    Collections.reverse(appVersionList);

    if (latestVersionInstead) {
        ApplicationVersionDescription latestVersionDescription = appVersionList.get(0);

        return changeToVersion(d, latestVersionDescription);
    }

    ListIterator<ApplicationVersionDescription> versionIterator = appVersionList.listIterator();

    String curVersionLabel = d.getVersionLabel();

    while (versionIterator.hasNext()) {
        ApplicationVersionDescription versionDescription = versionIterator.next();

        String versionLabel = versionDescription.getVersionLabel();

        if (curVersionLabel.equals(versionLabel) && versionIterator.hasNext()) {
            return changeToVersion(d, versionIterator.next());
        }
    }

    throw new MojoFailureException("No previous version was found (current version: " + curVersionLabel);
}

From source file:jp.classmethod.aws.gradle.elasticbeanstalk.AwsBeanstalkPluginExtension.java

License:Apache License

public String getEbEnvironmentCNAME(String environmentName) {
    DescribeEnvironmentsResult der = getClient().describeEnvironments(new DescribeEnvironmentsRequest()
            .withApplicationName(appName).withEnvironmentNames(environmentName));
    EnvironmentDescription env = der.getEnvironments().get(0);
    return env.getCNAME();
}

From source file:jp.classmethod.aws.gradle.elasticbeanstalk.AwsBeanstalkPluginExtension.java

License:Apache License

public List<EnvironmentDescription> getEnvironmentDescs(List<String> environmentNames) {
    DescribeEnvironmentsRequest req = new DescribeEnvironmentsRequest().withApplicationName(appName);
    if (environmentNames.isEmpty() == false) {
        req.setEnvironmentNames(environmentNames);
    }//from w w w  . j  a v  a 2  s  .  co m
    DescribeEnvironmentsResult der = getClient().describeEnvironments(req);
    return der.getEnvironments();
}

From source file:jp.classmethod.aws.gradle.elasticbeanstalk.AWSElasticBeanstalkCleanupApplicationVersionTask.java

License:Apache License

@TaskAction
public void deleteVersion() {
    // to enable conventionMappings feature
    String appName = getAppName();
    boolean deleteSourceBundle = isDeleteSourceBundle();

    AwsBeanstalkPluginExtension ext = getProject().getExtensions().getByType(AwsBeanstalkPluginExtension.class);
    AWSElasticBeanstalk eb = ext.getClient();

    DescribeEnvironmentsResult der = eb
            .describeEnvironments(new DescribeEnvironmentsRequest().withApplicationName(appName));
    List<String> usingVersions = der.getEnvironments().stream().map(ed -> ed.getVersionLabel())
            .collect(Collectors.toList());

    DescribeApplicationVersionsResult davr = eb
            .describeApplicationVersions(new DescribeApplicationVersionsRequest().withApplicationName(appName));
    List<String> versionLabelsToDelete = davr.getApplicationVersions().stream()
            .filter(avd -> usingVersions.contains(avd.getVersionLabel()) == false
                    && avd.getVersionLabel().contains("-SNAPSHOT-"))
            .map(avd -> avd.getVersionLabel()).collect(Collectors.toList());

    versionLabelsToDelete.forEach(versionLabel -> {
        getLogger().info("version " + versionLabel + " deleted");
        eb.deleteApplicationVersion(new DeleteApplicationVersionRequest().withApplicationName(appName)
                .withVersionLabel(versionLabel).withDeleteSourceBundle(deleteSourceBundle));
    });/*from  w  ww.ja va  2 s .  c om*/
}