/*
* Need to start EC2 instance before running it
*/
AWSCredentials credential = new BasicAWSCredentials(access_key, secret_key);
AmazonEC2Client ec2client = new AmazonEC2Client(credential);
try {
/*
* Build key pair before start instance
*/
buildKeyPair(ec2client);
// right now, we can run it on one host
if (ami_id != null)
this.instance = startInstances(ec2client, ami_id, ins_type, invocationContext.getExecutionContext().getNotificationService()).get(0);
else {
// already running instance
DescribeInstancesRequest describeInstancesRequest = new DescribeInstancesRequest();
DescribeInstancesResult describeInstancesResult = ec2client.describeInstances(describeInstancesRequest.withInstanceIds(ins_id));
if (describeInstancesResult.getReservations().size() == 0 || describeInstancesResult.getReservations().get(0).getInstances().size() == 0) {
throw new GfacException("Instance not found:" + ins_id, FaultCode.InvalidRequest);
}
this.instance = describeInstancesResult.getReservations().get(0).getInstances().get(0);
// check instance keypair
if (this.instance.getKeyName() == null || !this.instance.getKeyName().equals(KEY_PAIR_NAME))
throw new GfacException("Keypair for instance:" + ins_id + " is not valid", FaultCode.InvalidRequest);
}
//send out instance id
invocationContext.getExecutionContext().getNotificationService().sendResourceMappingNotifications(this.instance.getPublicDnsName(), "EC2 Instance " + this.instance.getInstanceId() + " is running with public name " + this.instance.getPublicDnsName(), this.instance.getInstanceId());
/*
* Make sure port 22 is connectable
*/
for (GroupIdentifier g : this.instance.getSecurityGroups()) {
IpPermission ip = new IpPermission();
ip.setIpProtocol("tcp");
ip.setFromPort(22);
ip.setToPort(22);
AuthorizeSecurityGroupIngressRequest r = new AuthorizeSecurityGroupIngressRequest();
r = r.withIpPermissions(ip.withIpRanges("0.0.0.0/0"));
r.setGroupId(g.getGroupId());
try {
ec2client.authorizeSecurityGroupIngress(r);
} catch (AmazonServiceException as) {
/*
* If exception is from duplicate room, ignore it.
*/
if (!as.getErrorCode().equals("InvalidPermission.Duplicate"))