@Override public List<EnvironmentDescription> getEnvironments(AbstractBuild<?, ?> build, BuildListener listener, AWSElasticBeanstalk awseb, String applicationName) { DescribeEnvironmentsRequest request = new DescribeEnvironmentsRequest(); request.withApplicationName(applicationName); request.withIncludeDeleted(false); DescribeEnvironmentsResult result = awseb.describeEnvironments(request); List<EnvironmentDescription> environments = new ArrayList<EnvironmentDescription>(); List<String> resolvedUrls = new ArrayList<String>(urlList.size()); for (String url : urlList) { resolvedUrls.add(AWSEBUtils.replaceMacros(build, listener, url)); } for (EnvironmentDescription environment : result.getEnvironments()) { String envUrl = environment.getCNAME(); if (resolvedUrls.contains(envUrl)) { environments.add(environment); } } return environments; }
public void execute() { checkParams(); AWSElasticBeanstalkClient bcClient = getOrCreateClient(AWSElasticBeanstalkClient.class); DescribeEnvironmentsRequest deRequest = new DescribeEnvironmentsRequest() .withEnvironmentNames(environmentName); DescribeEnvironmentsResult result = bcClient .describeEnvironments(deRequest); if (result.getEnvironments().size() < 1) { throw new BuildException( "No environments found with the specified name " + environmentName); } try { AWSTestUtils.waitForEnvironmentToTransitionToStateAndHealth( environmentName, EnvironmentStatus.Ready, null, bcClient); } catch (InterruptedException e) { throw new BuildException(e.getMessage()); } }
/** * Get environment description of this. * @return The description */ private EnvironmentDescription description() { final DescribeEnvironmentsResult res = this.client.describeEnvironments( new DescribeEnvironmentsRequest() .withEnvironmentIds(this.eid) ); if (res.getEnvironments().isEmpty()) { throw new DeploymentException( String.format("environment '%s' not found", this.eid) ); } final EnvironmentDescription desc = res.getEnvironments().get(0); Logger.debug( this, // @checkstyle LineLength (1 line) "ID=%s, env=%s, app=%s, CNAME=%s, label=%s, template=%s, status=%s, health=%s", desc.getEnvironmentId(), desc.getEnvironmentName(), desc.getApplicationName(), desc.getCNAME(), desc.getVersionLabel(), desc.getTemplateName(), desc.getStatus(), desc.getHealth() ); return desc; }
public static List<EnvironmentDescription> getEnvironments(AWSCredentialsProvider credentials, Regions region, String appName) { AWSElasticBeanstalk awseb = getElasticBeanstalk(credentials, Region.getRegion(region)); DescribeEnvironmentsRequest request = new DescribeEnvironmentsRequest().withApplicationName(appName); DescribeEnvironmentsResult result = awseb.describeEnvironments(request); return result.getEnvironments(); }
@Override public List<EnvironmentDescription> getEnvironments(AbstractBuild<?, ?> build, BuildListener listener, AWSElasticBeanstalk awseb, String applicationName) { DescribeEnvironmentsRequest request = new DescribeEnvironmentsRequest(); request.withApplicationName(applicationName); request.withIncludeDeleted(false); List<String> escaped = new ArrayList<String>(envNameList.size()); for (String env : envNameList) { escaped.add(AWSEBUtils.replaceMacros(build, listener, env)); } request.withEnvironmentNames(escaped); return awseb.describeEnvironments(request).getEnvironments(); }
public AWSEBEnvironmentUpdaterThread(AWSElasticBeanstalk awseb, EnvironmentDescription envd, BuildListener listener, String versionLabel) { this.awseb = awseb; this.envd = envd; this.listener = listener; this.versionLabel = versionLabel; this.lastEvent = new EventDescription(); lastEvent.setEventDate(new Date()); // We can make our requests and, hopefully, safely assume the environmentId won't change under us. envRequest = new DescribeEnvironmentsRequest().withEnvironmentIds(envd.getEnvironmentId()); eventRequest = new DescribeEventsRequest().withEnvironmentId(envd.getEnvironmentId()); // Hack to acknowledge that the time of the Jenkins box may not match AWS. try { DescribeEventsResult lastEntry = awseb.describeEvents(new DescribeEventsRequest() .withEnvironmentId(envd.getEnvironmentId()) .withMaxRecords(1)); lastEvent = lastEntry.getEvents().get(0); } catch (Exception e) { log("'%s': Unable to get last event, using system current timestamp for event logs", envd.getEnvironmentName()); } eventRequest.withStartTime(lastEvent.getEventDate()); // Initialize to the right start time. this.environmentId = envd.getEnvironmentId(); nAttempt = 0; }
public static void waitForEnvironmentToTransitionToStateAndHealth( String environmentName, EnvironmentStatus state, EnvironmentHealth health, AWSElasticBeanstalkClient bcClient) throws InterruptedException { System.out.println("Waiting for instance " + environmentName + " to transition to " + state + "/" + health); int count = 0; while (true) { Thread.sleep(1000 * 30); if (count++ > 100) { throw new RuntimeException("Environment " + environmentName + " never transitioned to " + state + "/" + health); } List<EnvironmentDescription> environments = bcClient .describeEnvironments( new DescribeEnvironmentsRequest() .withEnvironmentNames(environmentName)) .getEnvironments(); if (environments.size() == 0) { System.out .println("No environments with that name were found."); return; } EnvironmentDescription environment = environments.get(0); System.out.println(" - " + environment.getStatus() + "/" + environment.getHealth()); if (environment.getStatus().equalsIgnoreCase(state.toString()) == false) continue; if (health != null && environment.getHealth().equalsIgnoreCase( health.toString()) == false) continue; return; } }
/** * Get all environments in this app. * @return Collection of envs */ @SuppressWarnings("PMD.AvoidInstantiatingObjectsInLoops") private Collection<Environment> environments() { final DescribeEnvironmentsResult res = this.client.describeEnvironments( new DescribeEnvironmentsRequest().withApplicationName(this.name) ); final Collection<Environment> envs = new LinkedList<Environment>(); for (final EnvironmentDescription desc : res.getEnvironments()) { envs.add(new Environment(this.client, desc.getEnvironmentId())); } return envs; }
/** * Environment can check readiness of environment. * @throws Exception If something is wrong */ @Test public void checksReadinessOfEnvironment() throws Exception { final String eid = "some-env-id"; final AWSElasticBeanstalk ebt = Mockito.mock(AWSElasticBeanstalk.class); Mockito.doReturn( new DescribeConfigurationSettingsResult().withConfigurationSettings( new ArrayList<ConfigurationSettingsDescription>(0) ) ).when(ebt) .describeConfigurationSettings( Mockito.any(DescribeConfigurationSettingsRequest.class) ); Mockito.doReturn( new DescribeEnvironmentsResult().withEnvironments( Arrays.asList( new EnvironmentDescription() .withStatus("Ready") .withHealth("Red") ) ) ).when(ebt) .describeEnvironments( Mockito.any(DescribeEnvironmentsRequest.class) ); final Environment env = new Environment(ebt, eid); MatcherAssert.assertThat( env.green(), Matchers.equalTo(false) ); }
@Override public boolean perform() throws Exception { DescribeEnvironmentsRequest req = new DescribeEnvironmentsRequest(). withApplicationName(getApplicationName()). withEnvironmentNames(Lists.<String>newArrayList(getEnvironmentName().replaceAll("\\s", "").split(","))). withIncludeDeleted(false); DescribeEnvironmentsResult result = getAwseb().describeEnvironments(req); if (result.getEnvironments().size() < 1) { log("Unable to lookup environmentId. Skipping Update."); return true; } StringBuilder csEnvironmentIds = new StringBuilder(); for (ListIterator<EnvironmentDescription> i = result.getEnvironments().listIterator(); i.hasNext(); ) { EnvironmentDescription element = i.next(); final String environmentLabel = element.getVersionLabel(); if (null != environmentLabel && environmentLabel.equals(getVersionLabel())) { log("The version to deploy and currently used are the same. Even if you overwrite, AWSEB won't allow you to update." + "Skipping."); csEnvironmentIds.append(element.getEnvironmentId()); if (i.hasNext()) { csEnvironmentIds.append(","); } continue; } final String environmentId = element.getEnvironmentId(); log("Using environmentId '%s'", environmentId); csEnvironmentIds.append(environmentId); if (i.hasNext()) { csEnvironmentIds.append(","); } } setEnvironmentId(csEnvironmentIds.toString()); return false; }
@Override public boolean perform() throws Exception { final DescribeEnvironmentsRequest req = new DescribeEnvironmentsRequest(). withApplicationName(getApplicationName()). withEnvironmentIds(Lists.<String>newArrayList(getEnvironmentId().split(","))). withIncludeDeleted(false); final DescribeEnvironmentsResult result = getAwseb().describeEnvironments(req); if (result.getEnvironments().size() < 1) { log("Environment w/ environmentId '%s' not found. Aborting.", getEnvironmentId()); return true; } // If there are any abortable environment updates set to true. boolean abort = false; for (Integer i = 0; i < result.getEnvironments().size(); i++) { String resultingStatus = result.getEnvironments().get(i).getStatus(); boolean abortableP = result.getEnvironments().get(i).getAbortableOperationInProgress(); String environmentId = result.getEnvironments().get(i).getEnvironmentId(); if (!STATUS_READY.equals(resultingStatus)) { if (abortableP) { log("AWS Abortable Environment Update Found. Calling abort on AWSEB Service"); getAwseb().abortEnvironmentUpdate(new AbortEnvironmentUpdateRequest().withEnvironmentId(environmentId)); log("Environment Update Aborted. Proceeding."); abort = true; } } else { log("No pending Environment Updates. Proceeding."); } } // Call wait for status if found an abortable env update. if (abort) { WaitForEnvironment waitForStatus = new WaitForEnvironment(WaitFor.Status).withoutVersionCheck(); waitForStatus.setDeployerContext(c); return waitForStatus.perform(); } return false; }
/** * Application can create a new environment. * @throws Exception If something is wrong */ @Test public void createsNewEnvironment() throws Exception { final String name = "some-app-name"; final String template = "some-template"; final Version version = Mockito.mock(Version.class); final AWSElasticBeanstalk ebt = Mockito.mock(AWSElasticBeanstalk.class); Mockito.doReturn( new CheckDNSAvailabilityResult().withAvailable(true) ).when(ebt) .checkDNSAvailability( Mockito.any(CheckDNSAvailabilityRequest.class) ); Mockito.doReturn( new CreateEnvironmentResult() .withApplicationName(name) .withEnvironmentId("f4g5h6j7") .withEnvironmentName(name) ).when(ebt) .createEnvironment( Mockito.any(CreateEnvironmentRequest.class) ); Mockito.doReturn( new DescribeConfigurationSettingsResult().withConfigurationSettings( new ArrayList<ConfigurationSettingsDescription>(0) ) ).when(ebt) .describeConfigurationSettings( Mockito.any(DescribeConfigurationSettingsRequest.class) ); Mockito.doReturn( new DescribeEnvironmentsResult().withEnvironments( Arrays.asList( new EnvironmentDescription() .withCNAME("") .withEnvironmentName("some-env") .withEnvironmentId("a1b2c3d4") .withStatus("Ready") ) ) ).when(ebt) .describeEnvironments( Mockito.any(DescribeEnvironmentsRequest.class) ); Mockito.doReturn(new TerminateEnvironmentResult()) .when(ebt) .terminateEnvironment( Mockito.any(TerminateEnvironmentRequest.class) ); final Application app = new Application(ebt, name); app.clean(false); MatcherAssert.assertThat( app.candidate(version, template), Matchers.notNullValue() ); }