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

Implementation for com.rultor.agents.ec2.Amazon interface - Issue #755 #760

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from 5 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 @@ -99,7 +99,7 @@
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-core</artifactId>
<version>1.9.8</version>
<scope>runtime</scope>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.amazonaws</groupId>
Expand Down
6 changes: 0 additions & 6 deletions src/main/java/com/rultor/agents/ec2/Amazon.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,6 @@
* @author Yuriy Alevohin ([email protected])
* @version $Id$
* @since 2.0
* @todo #629 Add implementation for com.rultor.agents.ec2.Amazon.
* It must create com.amazonaws.services.ec2.AmazonEC2 client
* with config params "credentials", "zone", "type", "key". Use
* client.runInstances(com.amazonaws.services.ec2.model.RunInstancesRequest)
* to run on-demand instance. Method runOnDemand must finally
* wait for started instance and check if start was successful.
*/
@Immutable
public interface Amazon {
Expand Down
146 changes: 146 additions & 0 deletions src/main/java/com/rultor/agents/ec2/AmazonEC2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
/**
* Copyright (c) 2009-2015, rultor.com
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met: 1) Redistributions of source code must retain the above
* copyright notice, this list of conditions and the following
* disclaimer. 2) Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution. 3) Neither the name of the rultor.com nor
* the names of its contributors may be used to endorse or promote
* products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
* NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
* THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package com.rultor.agents.ec2;

import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.services.ec2.AmazonEC2Client;
import com.amazonaws.services.ec2.model.DescribeInstanceStatusRequest;
import com.amazonaws.services.ec2.model.DescribeInstanceStatusResult;
import com.amazonaws.services.ec2.model.Instance;
import com.amazonaws.services.ec2.model.InstanceState;
import com.amazonaws.services.ec2.model.RunInstancesRequest;
import com.jcabi.aspects.Tv;
import java.io.IOException;
import java.util.List;
import java.util.concurrent.TimeUnit;
import javax.validation.constraints.NotNull;

/**
* Amazon EC2 instance on demand. Takes all the mandatory data for running
* an EC2 instance in a defined region when it's needed.
*
* @author Marton Horvath ([email protected])
* @version $Id$
* @since 2.0
*/
public class AmazonEC2 implements Amazon {
/**
* The integer value of the Running {@link InstanceState}.
*/
private static final int RUNNING_STATE = 16;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can't we use a constant from AWS SDK?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the EC2 SDK I couldn't find a constant with this int value for this.

/**
* AWS credentials used when authenticating at AWS.
*/
private final transient AWSCredentials credentials;
/**
* AWS region to be used for the instance to be run.
*/
private final transient String region;
/**
* EC2 instance type (like <i>t1.micro</i>, <i>m1.small</i> etc.).
*/
private final transient String type;
/**
* EC2 instance key pair name.
*/
private final transient String keyname;

/**
* Public ctor.
* @param cred Credentials for AWS
* @param zone AWS region
* @param instype Instance type
* @param key Instance key pair name
* @checkstyle ParameterNumberCheck (12 lines)
*/
public AmazonEC2(
@NotNull final AWSCredentials cred,
@NotNull final String zone,
@NotNull final String instype,
@NotNull final String key) {
super();
this.credentials = cred;
this.region = zone;
this.type = instype;
this.keyname = key;
}

@Override
public final Instance runOnDemand() throws IOException {
Instance instance;
final AmazonEC2Client client = new AmazonEC2Client(
this.credentials
);
client.setEndpoint(this.region);
final List<Instance> instances = client.runInstances(
this.create()
).getReservation().getInstances();
if (instances.isEmpty()) {
throw new IllegalStateException(
"No Instance was available in the RunInstanceRequest reply!"
);
} else {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

instance = instances.get(0);
}
InstanceState state = instance.getState();
if (state.getCode() < RUNNING_STATE) {
final DescribeInstanceStatusRequest statusreq =
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

new DescribeInstanceStatusRequest();
statusreq.withInstanceIds(instance.getInstanceId());
while (state.getCode() < RUNNING_STATE) {
try {
TimeUnit.SECONDS.sleep(Tv.FIFTEEN);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why wait here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It checks the state of the instance and if it's not running (the value 16 is the code of the running instance state) we'll wait till it's state is running. The issue states that the 'runOnDemand()' method has to wait till the instance is started, so I tried to make it this way.

} catch (final InterruptedException ex) {
Thread.currentThread().interrupt();
break;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why swallowing the exception? why can't we just throw it up, wrapped into IllegalStateException

}
final DescribeInstanceStatusResult instanceStatus = client
.describeInstanceStatus(statusreq);
state = instanceStatus.getInstanceStatuses().get(0)
.getInstanceState();
}
}
return instance;
}

/**
* Creates a new {@link RunInstancesRequest} with the {@link #keyname} and
* {@link #type} set. The minimal and maximal number of instances created
* by the request is limited to one.
* @return The {@link RunInstancesRequest} created
*/
private RunInstancesRequest create() {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we really need a method for just a single statement?

return new RunInstancesRequest()
.withKeyName(this.keyname)
.withMinCount(1)
.withMaxCount(1)
.withInstanceType(this.type);
}
}