Search in sources :

Example 6 with RunInstancesRequest

use of com.amazonaws.services.ec2.model.RunInstancesRequest in project camel by apache.

the class AmazonEC2ClientMock method runInstances.

@Override
public RunInstancesResult runInstances(RunInstancesRequest runInstancesRequest) {
    RunInstancesResult result = new RunInstancesResult();
    if (runInstancesRequest.getImageId().equals("test-1")) {
        Reservation res = new Reservation();
        res.setOwnerId("1");
        res.setRequesterId("user-test");
        res.setReservationId("res-1");
        Collection<Instance> instances = new ArrayList();
        Instance ins = new Instance();
        ins.setImageId(runInstancesRequest.getImageId());
        ins.setInstanceType(runInstancesRequest.getInstanceType());
        ins.setInstanceId("instance-1");
        if (runInstancesRequest.getSecurityGroups() != null) {
            if (runInstancesRequest.getSecurityGroups().contains("secgroup-1") && runInstancesRequest.getSecurityGroups().contains("secgroup-2")) {
                GroupIdentifier id1 = new GroupIdentifier();
                id1.setGroupId("id-1");
                id1.setGroupName("secgroup-1");
                GroupIdentifier id2 = new GroupIdentifier();
                id2.setGroupId("id-2");
                id2.setGroupName("secgroup-2");
                Collection secGroups = new ArrayList<GroupIdentifier>();
                secGroups.add(id1);
                secGroups.add(id2);
                ins.setSecurityGroups(secGroups);
            } else if (ObjectHelper.isNotEmpty(runInstancesRequest.getKeyName())) {
                if (ObjectHelper.isNotEmpty(runInstancesRequest.getKeyName().contains("keypair-1"))) {
                    GroupIdentifier id1 = new GroupIdentifier();
                    id1.setGroupId("id-3");
                    id1.setGroupName("secgroup-3");
                    GroupIdentifier id2 = new GroupIdentifier();
                    id2.setGroupId("id-4");
                    id2.setGroupName("secgroup-4");
                    Collection secGroups = new ArrayList<GroupIdentifier>();
                    secGroups.add(id1);
                    secGroups.add(id2);
                    ins.setSecurityGroups(secGroups);
                }
            }
        }
        instances.add(ins);
        res.setInstances(instances);
        result.setReservation(res);
    } else {
        throw new AmazonServiceException("The image-id doesn't exists");
    }
    return result;
}
Also used : Reservation(com.amazonaws.services.ec2.model.Reservation) Instance(com.amazonaws.services.ec2.model.Instance) RunInstancesResult(com.amazonaws.services.ec2.model.RunInstancesResult) ArrayList(java.util.ArrayList) AmazonServiceException(com.amazonaws.AmazonServiceException) Collection(java.util.Collection) GroupIdentifier(com.amazonaws.services.ec2.model.GroupIdentifier)

Example 7 with RunInstancesRequest

use of com.amazonaws.services.ec2.model.RunInstancesRequest in project GNS by MobilityFirst.

the class AWSEC2 method createInstanceAndWait.

/**
   * Create an Instance
   *
   * @param ec2
   * @param amiRecord
   * @param key
   * @param securityGroup
   * @return the instanceID string
   */
public static String createInstanceAndWait(AmazonEC2 ec2, AMIRecord amiRecord, String key, SecurityGroup securityGroup) {
    RunInstancesRequest runInstancesRequest;
    if (amiRecord.getVpcSubnet() != null) {
        System.out.println("subnet: " + amiRecord.getVpcSubnet() + " securityGroup: " + securityGroup.getGroupName());
        // new VPC
        runInstancesRequest = new RunInstancesRequest().withMinCount(1).withMaxCount(1).withImageId(amiRecord.getName()).withInstanceType(amiRecord.getInstanceType()).withKeyName(key).withSubnetId(amiRecord.getVpcSubnet()).withSecurityGroupIds(Arrays.asList(securityGroup.getGroupId()));
    } else {
        runInstancesRequest = new RunInstancesRequest(amiRecord.getName(), 1, 1);
        runInstancesRequest.setInstanceType(amiRecord.getInstanceType());
        runInstancesRequest.setSecurityGroups(new ArrayList<>(Arrays.asList(securityGroup.getGroupName())));
        runInstancesRequest.setKeyName(key);
    }
    RunInstancesResult runInstancesResult = ec2.runInstances(runInstancesRequest);
    Instance instance = runInstancesResult.getReservation().getInstances().get(0);
    String createdInstanceId = instance.getInstanceId();
    System.out.println("Waiting for instance " + amiRecord.getName() + " to start");
    long startTime = System.currentTimeMillis();
    do {
        ThreadUtils.sleep(1000);
        if (System.currentTimeMillis() - startTime > 90000) {
            // give it a minute and a half
            System.out.println(createdInstanceId + " timed out waiting for start.");
            return null;
        }
        // regrab the instance data from the server
        instance = findInstance(ec2, createdInstanceId);
        //System.out.print(instance.getState().getName());
        System.out.print(".");
    } while (instance != null && !instance.getState().getName().equals(InstanceStateRecord.RUNNING.getName()));
    System.out.println();
    return createdInstanceId;
}
Also used : Instance(com.amazonaws.services.ec2.model.Instance) RunInstancesResult(com.amazonaws.services.ec2.model.RunInstancesResult) RunInstancesRequest(com.amazonaws.services.ec2.model.RunInstancesRequest)

Aggregations

RunInstancesResult (com.amazonaws.services.ec2.model.RunInstancesResult)5 RunInstancesRequest (com.amazonaws.services.ec2.model.RunInstancesRequest)4 AmazonServiceException (com.amazonaws.AmazonServiceException)3 Instance (com.amazonaws.services.ec2.model.Instance)3 AmazonEC2 (com.amazonaws.services.ec2.AmazonEC2)2 Placement (com.amazonaws.services.ec2.model.Placement)2 Collection (java.util.Collection)2 AmazonEC2Client (com.amazonaws.services.ec2.AmazonEC2Client)1 CreateTagsRequest (com.amazonaws.services.ec2.model.CreateTagsRequest)1 CreateTagsResult (com.amazonaws.services.ec2.model.CreateTagsResult)1 GroupIdentifier (com.amazonaws.services.ec2.model.GroupIdentifier)1 InstanceNetworkInterfaceSpecification (com.amazonaws.services.ec2.model.InstanceNetworkInterfaceSpecification)1 InstanceType (com.amazonaws.services.ec2.model.InstanceType)1 Reservation (com.amazonaws.services.ec2.model.Reservation)1 Tag (com.amazonaws.services.ec2.model.Tag)1 Function (com.google.common.base.Function)1 AutoScalingData (io.druid.indexing.overlord.autoscaling.AutoScalingData)1 ArrayList (java.util.ArrayList)1 Endpoint (org.apache.camel.Endpoint)1 Message (org.apache.camel.Message)1