@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; }
/** * 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 boolean isEnvironmentEligible( EnvironmentDescription environment, String environmentNamePrefix, String environmentNameRegex ) { boolean eligible = true; if ( !environment.getStatus().equalsIgnoreCase( "Ready" ) ) { eligible = false; } else if ( !isEnvironmentEligible( environment.getEnvironmentName(), environmentNamePrefix, environmentNameRegex ) ) { eligible = false; } return eligible; }
public boolean updateEnvironments() throws InterruptedException { List<EnvironmentDescription> envList = new ArrayList<EnvironmentDescription>(10); for (AWSEBSetup extension : envSetup.getEnvLookup()) { if (extension instanceof EnvLookup){ EnvLookup envLookup = (EnvLookup) extension; envList.addAll(envLookup.getEnvironments(build, listener, awseb, applicationName)); } } if (envList.size() <= 0) { AWSEBUtils.log(listener, "No environments found matching applicationName:%s", applicationName); if (envSetup.getFailOnError()) { listener.finished(Result.FAILURE); return false; } else { listener.finished(Result.SUCCESS); return true; } } ExecutorService pool = Executors.newFixedThreadPool(MAX_THREAD_COUNT); List<AWSEBEnvironmentUpdaterThread> updaters = new ArrayList<AWSEBEnvironmentUpdaterThread>(); for (EnvironmentDescription envd : envList) { AWSEBUtils.log(listener, "Environment found (environment id='%s', name='%s'). " + "Attempting to update environment to version label '%s'", envd.getEnvironmentId(), envd.getEnvironmentName(), versionLabel); updaters.add(new AWSEBEnvironmentUpdaterThread(awseb, envd, listener, versionLabel)); } List<Future<AWSEBEnvironmentUpdaterThread>> results = pool.invokeAll(updaters); return printResults(results); }
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(); }
public static String getEnvironmentCnamesListAsString(AWSEBCredentials credentials, Regions region, String appName) { AWSCredentialsProvider awsCredentials = null; if (credentials != null) { awsCredentials = credentials.getAwsCredentials(); } List<EnvironmentDescription> environments = AWSEBUtils.getEnvironments(awsCredentials, region, appName); StringBuilder sb = new StringBuilder(); for (EnvironmentDescription env : environments) { sb.append(env.getCNAME()); sb.append("\n"); } return sb.toString(); }
@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(); }
private String getEnvironmentsListAsString(AWSEBCredentials credentials, Regions region, String appName) { AWSCredentialsProvider awsCredentials = null; if (credentials != null) { awsCredentials = credentials.getAwsCredentials(); } List<EnvironmentDescription> environments = AWSEBUtils.getEnvironments(awsCredentials, region, appName); StringBuilder sb = new StringBuilder(); for (EnvironmentDescription env : environments) { sb.append(env.getEnvironmentName()); sb.append("\n"); } return sb.toString(); }
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; } }
/** * Public ctor. * @param clnt The client * @param idnt Environment ID */ protected Environment(@NotNull final AWSElasticBeanstalk clnt, @NotNull final String idnt) { this.client = clnt; this.eid = idnt; final EnvironmentDescription desc = this.description(); final String template = desc.getTemplateName(); if (template != null) { final DescribeConfigurationSettingsResult res = this.client.describeConfigurationSettings( new DescribeConfigurationSettingsRequest() .withApplicationName(desc.getApplicationName()) .withTemplateName(template) ); for (final ConfigurationSettingsDescription config : res.getConfigurationSettings()) { Logger.debug( Environment.class, "Environment '%s/%s/%s' settings:", config.getApplicationName(), config.getEnvironmentName() ); for (final ConfigurationOptionSetting opt : config.getOptionSettings()) { Logger.debug( Environment.class, " %s/%s: %s", opt.getNamespace(), opt.getOptionName(), opt.getValue() ); } } } }
/** * {@inheritDoc} */ @Override public String toString() { final EnvironmentDescription desc = this.description(); return String.format( "%s/%s/%s", desc.getEnvironmentName(), desc.getEnvironmentId(), desc.getCNAME() ); }
/** * Wait for stable state, and return TRUE if achieved or FALSE if not. * @return TRUE if environment is stable */ public boolean stable() { return this.until( new Environment.Barrier() { @Override public String message() { return "stable state"; } @Override public boolean allow(final EnvironmentDescription desc) { return !desc.getStatus().matches(".*ing$"); } } ); }
/** * 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) ); }
private String getBeanstalkEnvironmentResponse() throws JsonProcessingException{ List<Environment> envs = new ArrayList<Environment>(); for(EnvironmentDescription desc : elasticBeanstalkService.describeEnvironments()){ Environment env = new Environment(); BeanUtils.copyProperties(desc, env); env.setEnvResources(elasticBeanstalkService.describeApplicationResources(desc.getEnvironmentId())); env.setVersions(elasticBeanstalkService.getVersions(desc.getApplicationName())); envs.add(env); } return mapper.writeValueAsString(envs); }
@Override public List<HostGroup> lookupHostsToMonitor() throws CandlestackPropertiesException { List<HostGroup> hostGroups = new ArrayList<>(); // Figure out the minimum launch age for the environment to monitored Date minLaunchAge = new Date( System.currentTimeMillis() - newResourceMonitorDelayMillis ); // Go ahead and fetch the EC2 instances in bulk rather than making calls for the individual environments Map<String, List<Instance>> environmentInstanceMap = EBUtil.lookupInstances( ec2Client, environmentNamePrefix, environmentNameRegex ); // Look through the environments for eligible ones for ( EnvironmentDescription environment : beanstalkClient.describeEnvironments().getEnvironments() ) { // Skip over ineligible environments if ( !EBUtil.isEnvironmentEligible( environment, environmentNamePrefix, environmentNameRegex ) ) { continue; } // Make sure the environment is old enough to be monitored if ( minLaunchAge.before( environment.getDateCreated() ) ) { continue; } // Get the notificaiton period override if there is one String notificationPeriod = GlobalAWSProperties.getEBServiceNotificationPeriod( environment.getEnvironmentName() ); // Define the host group object for this environment HostGroup hostGroup = new HostGroup( environment.getEnvironmentName(), environment.getDescription() ); hostGroups.add( hostGroup ); // Add the host for the environment Host host = new Host( environment.getEnvironmentName(), environment.getDescription(), environment.getEndpointURL(), contactGroups ); for ( EBCloudWatchMetric metric : ebCloudWatchMetrics ) { host.addService( metric.getService( environment.getEnvironmentName(), contactGroups ) ); } hostGroup.addHost( host ); // Add the associated hosts List<Instance> instances = environmentInstanceMap.get( environment.getEnvironmentName() ); if ( instances != null ) { for ( Instance instance : instances ) { // Make sure the instance is old enough to be monitored if ( minLaunchAge.after( instance.getLaunchTime() ) ) { hostGroup.addHost( createHostFromInstance( instance, notificationPeriod ) ); } } } } return hostGroups; }
@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; }
/** * Wait for the barrier to pass. * @param barrier The barrier * @return TRUE if passed, FALSE if timeout */ private boolean until(final Environment.Barrier barrier) { boolean passed = false; final long start = System.currentTimeMillis(); while (true) { final EnvironmentDescription desc = this.description(); if (barrier.allow(desc)) { passed = true; Logger.info( this, "Environment '%s/%s/%s': health=%s, status=%s", desc.getApplicationName(), desc.getEnvironmentName(), desc.getEnvironmentId(), desc.getHealth(), desc.getStatus() ); break; } Logger.info( this, // @checkstyle LineLength (1 line) "Environment '%s/%s/%s': health=%s, status=%s (waiting for %s, %[ms]s)", desc.getApplicationName(), desc.getEnvironmentName(), desc.getEnvironmentId(), desc.getHealth(), desc.getStatus(), barrier.message(), System.currentTimeMillis() - start ); if (System.currentTimeMillis() - start > Environment.DELAY_MS) { Logger.warn( this, "Environment failed to reach '%s' after %[ms]s", barrier.message(), System.currentTimeMillis() - start ); break; } try { TimeUnit.MINUTES.sleep(1); } catch (final InterruptedException ex) { Thread.currentThread().interrupt(); throw new DeploymentException(ex); } } return passed; }
/** * 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() ); }
public List<EnvironmentDescription> describeEnvironments(){ awsElasticBeanstalkAsyncClient.setRegion(region); DescribeEnvironmentsResult result = awsElasticBeanstalkAsyncClient.describeEnvironments(); return result.getEnvironments(); }
/** * Can we continue? * @param desc Description of environment * @return TRUE if we can continue, FALSE if extra cycle of waiting * is required */ boolean allow(EnvironmentDescription desc);
public List<EnvironmentDescription> getEnvironments(AbstractBuild<?, ?> build, BuildListener listener, AWSElasticBeanstalk awseb, String applicationName);