Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create environment when none exists #71

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>token-macro</artifactId>
<version>2.1</version>
<version>2.3</version>
</dependency>
<dependency>
<groupId>org.jenkins-ci.main</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@
import com.amazonaws.services.elasticbeanstalk.model.ApplicationDescription;
import com.amazonaws.services.elasticbeanstalk.model.DescribeEnvironmentsRequest;
import com.amazonaws.services.elasticbeanstalk.model.DescribeEnvironmentsResult;
import com.amazonaws.services.route53.AmazonRoute53;
import com.amazonaws.services.route53.AmazonRoute53Client;
import com.amazonaws.services.route53.model.HostedZone;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3Client;
import com.cloudbees.jenkins.plugins.awscredentials.AmazonWebServicesCredentials;
Expand All @@ -49,6 +52,19 @@
import com.google.common.base.Function;
import com.google.common.collect.Lists;

import org.apache.commons.lang.StringUtils;
import org.kohsuke.stapler.AncestorInPath;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.QueryParameter;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.Arrays;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import edu.umd.cs.findbugs.annotations.NonNull;
import hudson.Extension;
import hudson.FilePath;
Expand Down Expand Up @@ -161,14 +177,56 @@ public class AWSEBDeploymentBuilder extends Builder implements SimpleBuildStep {
@Getter
private Integer maxAttempts;

/**
* Create Environment If Not Exist
*/
@Getter
private boolean createEnvironmentIfNotExist;

/**
* Environment CNAME Prefix
*/
@Getter
private String environmentCNAMEPrefix;

/**
* Environment Template Name
*/
@Getter
private String environmentTemplateName;

/**
* Environment Settings
*/
private List<AWSEBRawConfigurationOptionSetting> environmentSettings;

@Getter
private boolean route53UpdateRecordSet;

@Getter
private String route53HostedZoneId;

private List<AWSEBRoute53DomainName> route53DomainNames;

@Getter
private Long route53RecordTTL;

@Getter
private String route53RecordType;

@DataBoundConstructor
public AWSEBDeploymentBuilder(String credentialId, String awsRegion, String applicationName,
String environmentName, String bucketName, String keyPrefix,
String versionLabelFormat, String versionDescriptionFormat,
String rootObject, String includes, String excludes,
boolean zeroDowntime, Integer sleepTime, boolean checkHealth,
Integer maxAttempts) {
Integer maxAttempts, boolean createEnvironmentIfNotExist,
String environmentCNAMEPrefix,
String environmentTemplateName,
List<AWSEBRawConfigurationOptionSetting> environmentSettings,
boolean route53UpdateRecordSet, String route53HostedZoneId,
List<AWSEBRoute53DomainName> route53DomainNames, Long route53RecordTTL,
String route53RecordType) {
this.credentialId = credentialId;
this.awsRegion = awsRegion;
this.applicationName = applicationName;
Expand All @@ -184,6 +242,15 @@ public AWSEBDeploymentBuilder(String credentialId, String awsRegion, String appl
this.sleepTime = sleepTime;
this.checkHealth = checkHealth;
this.maxAttempts = maxAttempts;
this.createEnvironmentIfNotExist = createEnvironmentIfNotExist;
this.environmentCNAMEPrefix = environmentCNAMEPrefix;
this.environmentTemplateName = environmentTemplateName;
this.environmentSettings = environmentSettings;
this.route53UpdateRecordSet = route53UpdateRecordSet;
this.route53HostedZoneId = route53HostedZoneId;
this.route53DomainNames = route53DomainNames;
this.route53RecordTTL = route53RecordTTL;
this.route53RecordType = route53RecordType;
}

@Override
Expand Down Expand Up @@ -228,7 +295,30 @@ public AWSEBDeploymentConfig asConfig() {
sleepTime,
checkHealth,
maxAttempts,
null);
null,
createEnvironmentIfNotExist,
environmentCNAMEPrefix,
environmentTemplateName,
environmentSettings,
route53UpdateRecordSet,
route53HostedZoneId,
route53DomainNames,
route53RecordTTL,
route53RecordType);
}

public List<AWSEBRawConfigurationOptionSetting> getEnvironmentSettings() {
if (environmentSettings == null) {
return new ArrayList<AWSEBRawConfigurationOptionSetting>();
}
return environmentSettings;
}

public List<AWSEBRoute53DomainName> getRoute53DomainNames() {
if (route53DomainNames == null) {
return new ArrayList<AWSEBRoute53DomainName>();
}
return route53DomainNames;
}

@Extension
Expand Down Expand Up @@ -358,9 +448,30 @@ public String apply(ApplicationDescription input) {
w.printf("<li>Applications Found: %d (%s)</li>%n", applicationList.size(),
StringUtils.join(applicationList, ", "));

w.printf("</ul>%n");
w.printf("</ul>\n");
AmazonRoute53 amazonRoute53 = factory.getService(AmazonRoute53Client.class);
String route53Endpoint = factory.getEndpointFor((AmazonRoute53Client) amazonRoute53);

w.printf("<li>Testing Amazon Route 53 Service (endpoint: %s)</li>%n", route53Endpoint);

List<String>
hostedZoneList =
Lists.transform(amazonRoute53.listHostedZones().getHostedZones(),
new Function<HostedZone, String>() {
@Override
public String apply(HostedZone input) {
return input.getName();
}
});

w.printf("<li>Hosted Zones Found: %d (%s)</li>%n", hostedZoneList.size(),
StringUtils.join(hostedZoneList, ", "));

w.printf("</ul>\n");

return FormValidation.okWithMarkup(stringWriter.toString());
} catch (RuntimeException e) {
throw e;
} catch (Exception exc) {
return FormValidation.error(exc, "Failure");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@

import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import org.jenkinsci.plugins.tokenmacro.MacroEvaluationException;

Expand Down Expand Up @@ -111,13 +113,49 @@ public class AWSEBDeploymentConfig implements Serializable {
*/
private AmazonWebServicesCredentials credentials;

private boolean createEnvironmentIfNotExist;

private String environmentCNAMEPrefix;

private String environmentTemplateName;

private List<AWSEBRawConfigurationOptionSetting> environmentSettings;

private boolean route53UpdateRecordSet;

private String route53HostedZoneId;

private List<AWSEBRoute53DomainName> route53DomainNames;

private Long route53RecordTTL;

private String route53RecordType;

/**
* Copy Factory
*
* @param r replacer
* @return replaced copy
*/
public AWSEBDeploymentConfig replacedCopy(Utils.Replacer r) throws MacroEvaluationException, IOException, InterruptedException {
List<AWSEBRawConfigurationOptionSetting> evalSettings = new ArrayList<AWSEBRawConfigurationOptionSetting>();

if (this.getEnvironmentSettings() != null) {
for(AWSEBRawConfigurationOptionSetting setting :this.getEnvironmentSettings()){
AWSEBRawConfigurationOptionSetting evalSetting = new AWSEBRawConfigurationOptionSetting(r.r(setting.getNamespace()), r.r(setting.getOptionName()), r.r(setting.getValue()));
evalSettings.add(evalSetting);
}
}

List<AWSEBRoute53DomainName> evalDomainNames = new ArrayList<AWSEBRoute53DomainName>();

if (this.getRoute53DomainNames() != null) {
for (AWSEBRoute53DomainName domainName : this.getRoute53DomainNames()) {
AWSEBRoute53DomainName evalDomainName = new AWSEBRoute53DomainName(r.r(domainName.getName()));
evalDomainNames.add(evalDomainName);
}
}

return new AWSEBDeploymentConfig(
r.r(this.getCredentialId()),
r.r(this.getAwsRegion()),
Expand All @@ -134,7 +172,16 @@ public AWSEBDeploymentConfig replacedCopy(Utils.Replacer r) throws MacroEvaluati
this.getSleepTime(),
this.isCheckHealth(),
this.getMaxAttempts(),
this.credentials
this.credentials,
this.isCreateEnvironmentIfNotExist(),
r.r(this.getEnvironmentCNAMEPrefix()),
r.r(this.getEnvironmentTemplateName()),
evalSettings,
this.isRoute53UpdateRecordSet(),
r.r(this.getRoute53HostedZoneId()),
evalDomainNames,
this.route53RecordTTL,
r.r(this.getRoute53RecordType())
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package br.com.ingenieux.jenkins.plugins.awsebdeployment;

import java.io.Serializable;
import hudson.Extension;
import hudson.model.AbstractDescribableImpl;
import hudson.model.Descriptor;
import lombok.Getter;
import lombok.Setter;
import org.kohsuke.stapler.DataBoundConstructor;

public class AWSEBRawConfigurationOptionSetting extends AbstractDescribableImpl<AWSEBRawConfigurationOptionSetting> implements Serializable {
static final long serialVersionUID = 1L;

@Getter @Setter private String namespace;

@Getter @Setter private String optionName;

@Getter @Setter private String value;

@DataBoundConstructor
public AWSEBRawConfigurationOptionSetting(String namespace, String optionName, String value) {
this.namespace = namespace.trim();
this.optionName = optionName.trim();
this.value = value.trim();
}

@Extension
public static class DescriptorImpl extends Descriptor<AWSEBRawConfigurationOptionSetting> {
@Override
public String getDisplayName() {
return "Configuration Option Setting";
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package br.com.ingenieux.jenkins.plugins.awsebdeployment;

import hudson.Extension;
import hudson.model.AbstractDescribableImpl;
import hudson.model.Descriptor;
import lombok.Getter;
import lombok.Setter;
import org.kohsuke.stapler.DataBoundConstructor;

import java.io.Serializable;

public class AWSEBRoute53DomainName extends AbstractDescribableImpl<AWSEBRoute53DomainName> implements Serializable {
static final long serialVersionUID = 1L;

@Getter
@Setter
private String name;


@DataBoundConstructor
public AWSEBRoute53DomainName(String name) {
this.name = name.trim();
}

@Extension
public static class DescriptorImpl extends Descriptor<AWSEBRoute53DomainName> {
@Override
public String getDisplayName() {
return "Domain Name";
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
public interface Constants {
String DEFAULT_REGION = "us-east-1";

int MAX_ATTEMPTS = 30;
int MAX_ATTEMPTS = 90;

int SLEEP_TIME = 90;
int SLEEP_TIME = 30;

int MAX_ENVIRONMENT_NAME_LENGTH = 40;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package br.com.ingenieux.jenkins.plugins.awsebdeployment.cmd;


import br.com.ingenieux.jenkins.plugins.awsebdeployment.AWSEBRawConfigurationOptionSetting;
import com.amazonaws.services.elasticbeanstalk.model.*;

import java.util.ArrayList;
import java.util.List;

public class CreateOrUpdateEnvironment extends DeployerCommand {

@Override
public boolean perform() throws Exception {
DescribeEnvironmentsRequest deReq = new DescribeEnvironmentsRequest().
withApplicationName(getApplicationName()).
withEnvironmentNames(getEnvironmentName()).
withIncludeDeleted(false);

DescribeEnvironmentsResult deRes = getAwseb().describeEnvironments(deReq);
final String environmentId;

List<ConfigurationOptionSetting> list = new ArrayList<ConfigurationOptionSetting>();
for(AWSEBRawConfigurationOptionSetting setting :getEnvironmentSettings()){
ConfigurationOptionSetting configurationOptionSetting = new ConfigurationOptionSetting(
setting.getNamespace(),
setting.getOptionName(),
setting.getValue()
);
list.add(configurationOptionSetting);
}
ConfigurationOptionSetting[] configurationOptionSettings = list.toArray(new ConfigurationOptionSetting[list.size()]);

if (1 > deRes.getEnvironments().size()) {
CreateEnvironmentRequest ceReq = new CreateEnvironmentRequest()
.withApplicationName(getApplicationName())
.withEnvironmentName(getEnvironmentName())
.withCNAMEPrefix(getEnvironmentCNAMEPrefix())
.withTemplateName(getEnvironmentTemplateName())
.withVersionLabel(getVersionLabel())
.withOptionSettings(configurationOptionSettings);

final CreateEnvironmentResult ceRes = getAwseb().createEnvironment(ceReq);

environmentId = ceRes.getEnvironmentId();

log("Created environmentId '%s' with Version Label set to '%s'", environmentId, getVersionLabel());
} else {
environmentId = deRes.getEnvironments().get(0).getEnvironmentId();

UpdateEnvironmentRequest ueReq = new UpdateEnvironmentRequest()
.withEnvironmentId(environmentId)
.withVersionLabel(getVersionLabel())
.withOptionSettings(configurationOptionSettings);

getAwseb().updateEnvironment(ueReq);

log("Updated environmentId '%s' with Version Label set to '%s'", environmentId, getVersionLabel());
}

setEnvironmentId(environmentId);

return false;
}
}
Loading