@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; }
/** * Environment can collect events from running environment. * @throws Exception If something is wrong */ @Test public void collectsEventsFromLiveEnvironment() throws Exception { Assume.assumeThat(EnvironmentTest.AWS_KEY, Matchers.notNullValue()); final AWSCredentials creds = new BasicAWSCredentials( EnvironmentTest.AWS_KEY, EnvironmentTest.AWS_SECRET ); final AWSElasticBeanstalk ebt = new AWSElasticBeanstalkClient(creds); final Environment env = new Environment(ebt, "e-nxmcbf3pvk"); Logger.info( this, "events: %[list]s", Arrays.asList(env.events()) ); }
private void createApplicationVersion(String applicationName, AWSElasticBeanstalk eb, String bucketName, String objectName, String versionLabel) { log.info("creating version label=" + versionLabel); CreateApplicationVersionRequest request = new CreateApplicationVersionRequest() .withApplicationName(applicationName).withAutoCreateApplication(true) .withSourceBundle(new S3Location(bucketName, objectName)).withVersionLabel(versionLabel); eb.createApplicationVersion(request); }
private void updateEnvironment(String applicationName, String environmentName, AWSElasticBeanstalk eb, String versionLabel) { log.info("requesting update of environment to new version label"); UpdateEnvironmentRequest request = new UpdateEnvironmentRequest().withApplicationName(applicationName) .withEnvironmentName(environmentName).withVersionLabel(versionLabel); eb.updateEnvironment(request); log.info("requested"); }
public void createApplicationVersion(AWSElasticBeanstalk awseb) { AWSEBUtils.log(listener, "Creating application version %s for application %s for path %s", versionLabel, applicationName, s3ObjectPath); CreateApplicationVersionRequest cavRequest = new CreateApplicationVersionRequest().withApplicationName(applicationName).withAutoCreateApplication(true) .withSourceBundle(new S3Location(bucketName, objectKey)).withVersionLabel(versionLabel); awseb.createApplicationVersion(cavRequest); }
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 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() ); } } } }
/** * Public ctor. * @param clnt The client * @param app Application name */ protected Application(@NotNull final AWSElasticBeanstalk clnt, @NotNull final String app) { this.client = clnt; this.name = app; Logger.info( Application.class, "Working with application '%s'", this.name ); }
/** * Public ctor. * @param clnt Client * @param app Application name * @param bndl Bundle */ protected OverridingVersion(@NotNull final AWSElasticBeanstalk clnt, @NotNull final String app, @NotNull final Bundle bndl) { this.client = clnt; this.application = app; this.bundle = bndl; }
/** * Environment can deploy and reverse with a broken WAR file. This test * has to be executed only if you have full access to AWS S3 bucket, and * AWS EBT for deployment. The test runs full cycle of deployment and then * destroying of a new environment. It won't hurt anything, but will * consume some EBT resources. Be careful. * * @throws Exception If something is wrong */ @Test public void deploysAndReversesWithLiveAccount() throws Exception { Assume.assumeThat(ApplicationTest.AWS_KEY, Matchers.notNullValue()); final AWSCredentials creds = new BasicAWSCredentials( ApplicationTest.AWS_KEY, ApplicationTest.AWS_SECRET ); final AWSElasticBeanstalk ebt = new AWSElasticBeanstalkClient(creds); final String name = "netbout"; final Application app = new Application(ebt, name); final File war = this.temp.newFile("temp.war"); FileUtils.writeStringToFile(war, "broken JAR file content"); final Environment candidate = app.candidate( new OverridingVersion( ebt, name, new OverridingBundle( new AmazonS3Client(creds), "webapps.netbout.com", war.getName(), war ) ), name ); MatcherAssert.assertThat(candidate.green(), Matchers.equalTo(false)); Logger.info(this, "tail report:\n%s", candidate.tail()); candidate.terminate(); }
/** * OverridingVersion can override a version in AWS EBT. * @throws Exception If something is wrong */ @Test public void overridesVersionInEbt() throws Exception { final String app = "some-app"; final String key = "some-bundle-key"; final Bundle bundle = Mockito.mock(Bundle.class); Mockito.doReturn(key).when(bundle).name(); final AWSElasticBeanstalk ebt = Mockito.mock(AWSElasticBeanstalk.class); Mockito.doReturn(new DescribeApplicationVersionsResult()) .when(ebt).describeApplicationVersions( Mockito.any(DescribeApplicationVersionsRequest.class) ); Mockito.doReturn( new CreateApplicationVersionResult() .withApplicationVersion( new ApplicationVersionDescription() .withVersionLabel(key) ) ).when(ebt) .createApplicationVersion( Mockito.any(CreateApplicationVersionRequest.class) ); final Version version = new OverridingVersion(ebt, app, bundle); MatcherAssert.assertThat( version.label(), Matchers.equalTo(key) ); }
/** * 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) ); }
/** * Environment can fetch TAIL report from live environment. * @throws Exception If something is wrong */ @Test public void fetchesTailReportFromLiveEnvironment() throws Exception { Assume.assumeThat(EnvironmentTest.AWS_KEY, Matchers.notNullValue()); final AWSCredentials creds = new BasicAWSCredentials( EnvironmentTest.AWS_KEY, EnvironmentTest.AWS_SECRET ); final AWSElasticBeanstalk ebt = new AWSElasticBeanstalkClient(creds); final Environment env = new Environment(ebt, "e-2n2mqauqae"); Logger.info(this, "tail report:\n%s", env.tail()); }
void deploy(File artifact, AwsKeyPair keyPair, String region, String applicationName, String environmentName, String versionLabel, Proxy proxy) { final AWSCredentialsProvider credentials = new AWSStaticCredentialsProvider( new BasicAWSCredentials(keyPair.key, keyPair.secret)); ClientConfiguration cc = Util.createConfiguration(proxy); AWSElasticBeanstalk eb = AWSElasticBeanstalkClientBuilder.standard().withCredentials(credentials) .withClientConfiguration(cc).withRegion(region).build(); String bucketName = getS3BucketName(eb); String dateTime = Util.formatDateTime(new Date()); String objectName = artifact.getName() + "_" + dateTime; uploadArtifact(artifact, credentials, region, cc, bucketName, objectName); createApplicationVersion(applicationName, eb, bucketName, objectName, versionLabel); updateEnvironment(applicationName, environmentName, eb, versionLabel); }
private String getS3BucketName(AWSElasticBeanstalk eb) { log.info("getting s3 bucket name to deploy artifact to"); String bucketName = eb.createStorageLocation().getS3Bucket(); log.info("s3Bucket=" + bucketName); return bucketName; }
public static List<ApplicationDescription> getApplications(AWSCredentialsProvider credentials, Regions region) { AWSElasticBeanstalk awseb = getElasticBeanstalk(credentials, Region.getRegion(region)); DescribeApplicationsResult result = awseb.describeApplications(); return result.getApplications(); }
public static AWSElasticBeanstalk getElasticBeanstalk(AWSCredentialsProvider credentials, Region region) { AWSElasticBeanstalk awseb = region.createClient(AWSElasticBeanstalkClient.class, credentials, getClientConfig()); return awseb; }
public FormValidation doValidateCredentials( @QueryParameter("credentialId") final String credentialId, @QueryParameter final String awsRegion) { for (String value : Arrays.asList(credentialId, awsRegion)) { if (value.contains("$")) { return FormValidation.warning("Validation skipped due to parameter usage ('$')"); } } StringWriter stringWriter = new StringWriter(); PrintWriter w = new PrintWriter(stringWriter, true); try { w.printf("<ul>%n"); w.printf("<li>Building Client (credentialId: '%s', region: '%s')</li>%n", credentialId, awsRegion); AWSClientFactory factory = AWSClientFactory.getClientFactory(credentialId, awsRegion); AmazonS3 amazonS3 = factory.getService(AmazonS3Client.class); String s3Endpoint = factory.getEndpointFor((AmazonS3Client) amazonS3); w.printf("<li>Testing Amazon S3 Service (endpoint: %s)</li>%n", s3Endpoint); w.printf("<li>Buckets Found: %d</li>%n", amazonS3.listBuckets().size()); AWSElasticBeanstalk awsElasticBeanstalk = factory.getService(AWSElasticBeanstalkClient.class); String awsEBEndpoint = factory.getEndpointFor((AWSElasticBeanstalkClient) awsElasticBeanstalk); w.printf("<li>Testing AWS Elastic Beanstalk Service (endpoint: %s)</li>%n", awsEBEndpoint); List<String> applicationList = Lists.transform(awsElasticBeanstalk.describeApplications().getApplications(), new Function<ApplicationDescription, String>() { @Override public String apply(ApplicationDescription input) { return input.getApplicationName(); } }); w.printf("<li>Applications Found: %d (%s)</li>%n", applicationList.size(), StringUtils.join(applicationList, ", ")); w.printf("</ul>%n"); return FormValidation.okWithMarkup(stringWriter.toString()); } catch (Exception exc) { return FormValidation.error(exc, "Failure"); } }
/** * 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> getEnvironments(AbstractBuild<?, ?> build, BuildListener listener, AWSElasticBeanstalk awseb, String applicationName);