Skip to content

Commit

Permalink
#1906 allow EC2 config
Browse files Browse the repository at this point in the history
  • Loading branch information
yegor256 committed Feb 28, 2024
1 parent f5752de commit 243750b
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 3 deletions.
11 changes: 11 additions & 0 deletions src/jekyll/_posts/2014/jul/2014-07-13-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,17 @@ docker:
directory: repo/my-docker-image
{% endhighlight %}

## EC2

You can configure the type of EC2 instance that is used for your builds:

{% highlight yaml %}
ec2:
type: t2.medium
{% endhighlight %}

Please, be careful and don't use too large instances if you don't really need them.

## Environment Variables

You can specify environment variables common for all
Expand Down
1 change: 1 addition & 0 deletions src/main/java/com/rultor/agents/Agents.java
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,7 @@ public Agent agent(final Talk talk, final Profile profile)
new Agent.Quiet(
new Agent.Disabled(
new StartsInstance(
profile,
aws,
Manifests.read("Rultor-EC2Image"),
Manifests.read("Rultor-EC2Type"),
Expand Down
43 changes: 40 additions & 3 deletions src/main/java/com/rultor/agents/aws/StartsInstance.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import com.rultor.agents.AbstractAgent;
import com.rultor.spi.Profile;
import java.io.IOException;
import java.util.Arrays;
import lombok.ToString;
import org.xembly.Directive;
import org.xembly.Directives;
Expand All @@ -53,6 +54,18 @@
@ToString
public final class StartsInstance extends AbstractAgent {

/**
* Allowed instance types.
*/
private static final String[] ALLOWED_TYPES = {
"t2.nano", "t2.micro", "t2.small", "t2.medium", "t2.large",
};

/**
* Profile to get EC2 params.
*/
private final transient Profile profile;

/**
* AWS Client.
*/
Expand Down Expand Up @@ -80,17 +93,19 @@ public final class StartsInstance extends AbstractAgent {

/**
* Ctor.
* @param pfl Profile
* @param aws API
* @param image Instance AMI image name to run
* @param tpe Type of instance, like "t1.micro"
* @param grp Security group, like "sg-38924038290"
* @param net Subnet, like "subnet-0890890"
* @checkstyle ParameterNumberCheck (5 lines)
*/
public StartsInstance(final AwsEc2 aws,
public StartsInstance(final Profile pfl, final AwsEc2 aws,
final String image, final String tpe,
final String grp, final String net) {
super("/talk[daemon and not(ec2) and not(shell)]");
this.profile = pfl;
this.api = aws;
this.image = image;
this.type = tpe;
Expand Down Expand Up @@ -125,13 +140,14 @@ public Iterable<Directive> process(final XML xml) throws IOException {
* Run a new instance.
* @param talk Name of the talk
* @return Instance ID
* @throws IOException If fails
*/
private Instance run(final String talk) {
private Instance run(final String talk) throws IOException {
final RunInstancesRequest request = new RunInstancesRequest()
.withSecurityGroupIds(this.sgroup)
.withSubnetId(this.subnet)
.withImageId(this.image)
.withInstanceType(this.type)
.withInstanceType(this.instanceType())
.withMaxCount(1)
.withMinCount(1);
Logger.info(
Expand All @@ -154,4 +170,25 @@ private Instance run(final String talk) {
);
return instance;
}

/**
* Read one EC2 param from .rultor.xml.
* @return Value
* @throws IOException If fails
*/
private String instanceType() throws IOException {
final String required = new Profile.Defaults(this.profile).text(
"/p/entry[@key='ec2']/entry[@key='type']/text()",
this.type
);
if (!Arrays.asList(StartsInstance.ALLOWED_TYPES).contains(required)) {
throw new Profile.ConfigException(
Logger.format(
"EC2 instance type '%s' is not valid, use one of %[list]s",
required, StartsInstance.ALLOWED_TYPES
)
);
}
return required;
}
}

0 comments on commit 243750b

Please sign in to comment.