Java Code Examples for com.amazonaws.services.ec2.model.DescribeInstancesResult#getReservations()

The following examples show how to use com.amazonaws.services.ec2.model.DescribeInstancesResult#getReservations() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AWSProvider.java    From testgrid with Apache License 2.0 6 votes vote down vote up
/**
 *
 * Testgrid users may need to access the deployment's instances for debugging purposes.
 * Hence, we need to print the ssh access details by probing the internal details of the
 * cloudformation stack.
 *
 * @param stackName the cloudformation stack name
 * @param inputs properties instance that contain the aws region param
 */
private void logEC2SshAccessDetails(String stackName, Properties inputs) {
    try {
        String region = inputs.getProperty(AWS_REGION_PARAMETER);
        DescribeInstancesResult result = getDescribeInstancesResult(stackName, region);
        final long instanceCount = result.getReservations().stream()
                .map(Reservation::getInstances)
                .mapToLong(Collection::size)
                .sum();

        logger.info("");
        logger.info("Found " + instanceCount + " EC2 instances in this AWS Cloudformation stack: {");
        for (Reservation reservation : result.getReservations()) {
            for (Instance instance : reservation.getInstances()) {
                final String loginCommand = getLoginCommand(instance);
                logger.info(loginCommand);
            }
        }
        logger.info("}");
        logger.info("For information on login user names for Linux, please refer: "
                + "https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/AccessingInstancesLinux.html");
        logger.info("");
    } catch (RuntimeException e) {
        logger.warn("Error while trying to probe the cloudformation stack to find created ec2 instances.", e);
    }
}
 
Example 2
Source File: AWSProvider.java    From testgrid with Apache License 2.0 6 votes vote down vote up
/**
 * Get logs of all the ec2 instances created by this #stackName.
 *
 * @param stackName the stack that created the ec2 instances
 * @param region aws region of the stack
 */
private void getAllEC2InstanceConsoleLogs(String stackName, String region) {
    try {
        final DescribeInstancesResult result = getDescribeInstancesResult(stackName, region);
        final long instanceCount = result.getReservations().stream()
                .map(Reservation::getInstances)
                .mapToLong(Collection::size)
                .sum();

        logger.info("");
        logger.info("Downloading logs of " + instanceCount + " EC2 instances in this AWS Cloudformation stack: {");
        for (Reservation reservation : result.getReservations()) {
            for (Instance instance : reservation.getInstances()) {
                final String logs = getEC2InstanceConsoleLogs(instance, getAmazonEC2(region));
                logger.info(logs);
            }
        }
        logger.info("}");
        logger.info("Further details about this EC2 instance console outputs can be found at: "
                + "https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/instance-console.html");
        logger.info("");
    } catch (RuntimeException e) {
        logger.warn("Error while trying to download the instance console output from ec2 instances of {}@{}. "
                + "Error: ", stackName, region, e.getMessage());
    }
}
 
Example 3
Source File: BaseTest.java    From aws-mock with MIT License 6 votes vote down vote up
/**
 * Describe instances.
 *
  * @return list of instances
 */
protected final List<Instance> describeInstances() {

    DescribeInstancesRequest request = new DescribeInstancesRequest();
    DescribeInstancesResult result = amazonEC2Client
            .describeInstances(request);
    List<Instance> instanceList = new ArrayList<Instance>();
    if (result.getReservations().size() > 0) {
     Assert.assertTrue(result.getReservations().size() > 0);

     for (Reservation reservation : result.getReservations()) {
         List<Instance> instances = reservation.getInstances();
	
         if (null != instances) {
             for (Instance i : instances) {
                 instanceList.add(i);
             }
         }
     }
    }
    return instanceList;
}
 
Example 4
Source File: BaseTest.java    From aws-mock with MIT License 6 votes vote down vote up
/**
 * Describe instances.
 *
 * @param instanceIds            instances' IDs
 * @param enableLogging            log to standard out
 * @return list of instances
 */
protected final List<Instance> describeInstances(
        final Collection<String> instanceIds, final boolean enableLogging) {
    if (enableLogging) {
        log.info("Describe instances:" + toString(instanceIds));
    }
    DescribeInstancesRequest request = new DescribeInstancesRequest();
    request.setInstanceIds(instanceIds);
    DescribeInstancesResult result = amazonEC2Client
            .describeInstances(request);
    Assert.assertTrue(result.getReservations().size() > 0);

    List<Instance> instanceList = new ArrayList<Instance>();

    for (Reservation reservation : result.getReservations()) {
        List<Instance> instances = reservation.getInstances();

        if (null != instances) {
            for (Instance i : instances) {
                instanceList.add(i);
            }
        }
    }

    return instanceList;
}
 
Example 5
Source File: AWSSdkClient.java    From incubator-gobblin with Apache License 2.0 6 votes vote down vote up
/***
 * Get list of EC2 {@link Instance}s for a auto scaling group
 *
 * @param groupName Auto scaling group name
 * @param status Instance status (eg. running)
 * @return List of EC2 instances found for the input auto scaling group
 */
public List<Instance> getInstancesForGroup(String groupName,
    String status) {

  final AmazonEC2 amazonEC2 = getEc2Client();

  final DescribeInstancesResult instancesResult = amazonEC2.describeInstances(new DescribeInstancesRequest()
      .withFilters(new Filter().withName("tag:aws:autoscaling:groupName").withValues(groupName)));

  final List<Instance> instances = new ArrayList<>();
  for (Reservation reservation : instancesResult.getReservations()) {
    for (Instance instance : reservation.getInstances()) {
      if (null == status|| null == instance.getState()
          || status.equals(instance.getState().getName())) {
        instances.add(instance);
        LOGGER.info("Found instance: " + instance + " which qualified filter: " + status);
      } else {
        LOGGER.info("Found instance: " + instance + " but did not qualify for filter: " + status);
      }
    }
  }

  return instances;
}
 
Example 6
Source File: EC2Connector.java    From jenkins-deployment-dashboard-plugin with MIT License 6 votes vote down vote up
@Override
public boolean tagEnvironmentWithVersion(Region region, DeployJobVariables jobVariables) {
    String searchTag = jobVariables.getEnvironment();
    String version = jobVariables.getVersion();
    LOGGER.info("tagEnvironmentWithVersion " + region + " Tag " + searchTag + " version " + version);

    boolean environmentSuccessfulTagged = false;
    ec2.setRegion(region);
    DescribeInstancesResult instances = ec2.describeInstances();
    for (Reservation reservation : instances.getReservations()) {
        for (Instance instance : reservation.getInstances()) {
            for (Tag tag : instance.getTags()) {
                if (tag.getValue().equalsIgnoreCase(searchTag)) {
                    CreateTagsRequest createTagsRequest = new CreateTagsRequest();
                    createTagsRequest.withResources(instance.getInstanceId()).withTags(new Tag(VERSION_TAG, version));
                    LOGGER.info("Create Tag " + version + " for instance " + instance.getInstanceId());
                    ec2.createTags(createTagsRequest);
                    environmentSuccessfulTagged = true;
                }
            }
        }
    }
    return environmentSuccessfulTagged;
}
 
Example 7
Source File: EC2Connector.java    From jenkins-deployment-dashboard-plugin with MIT License 6 votes vote down vote up
public List<ServerEnvironment> getEnvironmentsByTag(Region region, String searchTag) {
    LOGGER.info("getEnvironmentsByTag " + region + " tag: " + searchTag);
    List<ServerEnvironment> environments = new ArrayList<ServerEnvironment>();

    ec2.setRegion(region);
    DescribeInstancesResult instances = ec2.describeInstances();
    for (Reservation reservation : instances.getReservations()) {
        for (Instance instance : reservation.getInstances()) {
            for (Tag tag : instance.getTags()) {
                if (tag.getValue().equalsIgnoreCase(searchTag)) {
                    environments.add(getEnvironmentFromInstance(instance));
                }
            }
        }
    }
    return environments;
}
 
Example 8
Source File: EC2Communication.java    From development with Apache License 2.0 5 votes vote down vote up
public List<Instance> getInstance(String instanceId) {
    DescribeInstancesResult result = getEC2().describeInstances(
            new DescribeInstancesRequest().withInstanceIds(instanceId));
    List<Reservation> reservations = result.getReservations();
    List<Instance> instances = new ArrayList<>();

    for (Reservation reservation : reservations) {
        instances.addAll(reservation.getInstances());
    }
    return instances;
}
 
Example 9
Source File: DescribeInstancesExample.java    From aws-mock with MIT License 5 votes vote down vote up
/**
 * Describe all mock instances within aws-mock.
 *
 * @return a list of all instances
 */
public static List<Instance> describeAllInstances() {
    // pass any credentials as aws-mock does not authenticate them at all
    AWSCredentials credentials = new BasicAWSCredentials("foo", "bar");
    AmazonEC2Client amazonEC2Client = new AmazonEC2Client(credentials);

    // the mock endpoint for ec2 which runs on your computer
    String ec2Endpoint = "http://localhost:8000/aws-mock/ec2-endpoint/";
    amazonEC2Client.setEndpoint(ec2Endpoint);

    DescribeInstancesResult response = amazonEC2Client.describeInstances();
    List<Reservation> reservations = response.getReservations();

    List<Instance> ret = new ArrayList<Instance>();

    for (Reservation reservation : reservations) {
        List<Instance> instances = reservation.getInstances();

        if (null != instances) {

            for (Instance i : instances) {
                ret.add(i);
            }
        }
    }

    return ret;
}
 
Example 10
Source File: EC2InstanceManager.java    From usergrid with Apache License 2.0 5 votes vote down vote up
/**
 * Queries instances with given Ids on AWS
 *
 * @param instanceIds   List of instance IDs
 * @return
 */
protected Collection<com.amazonaws.services.ec2.model.Instance> getEC2Instances( Collection<String> instanceIds ) {
    if( instanceIds == null || instanceIds.size() == 0 ) {
        return new ArrayList<com.amazonaws.services.ec2.model.Instance>();
    }

    Collection<com.amazonaws.services.ec2.model.Instance> instances =
            new LinkedList<com.amazonaws.services.ec2.model.Instance>();

    DescribeInstancesRequest request = new DescribeInstancesRequest();
    request = request.withInstanceIds( instanceIds );

    DescribeInstancesResult result = null;
    try {
        result = client.describeInstances( request );
    }
    catch ( Exception e ) {
        LOG.error( "Error while getting instance information from AWS.", e );
        return Collections.EMPTY_LIST;
    }

    for ( Reservation reservation : result.getReservations() ) {
        for ( com.amazonaws.services.ec2.model.Instance in : reservation.getInstances() ) {
            instances.add( in );
        }
    }

    return instances;
}
 
Example 11
Source File: AwsInstanceConnector.java    From cloudbreak with Apache License 2.0 5 votes vote down vote up
private Collection<String> instanceIdsWhichAreNotInCorrectState(List<CloudInstance> vms, AmazonEC2 amazonEC2Client, String state) {
    Set<String> instances = vms.stream().map(CloudInstance::getInstanceId).collect(Collectors.toCollection(HashSet::new));
    DescribeInstancesResult describeInstances = amazonEC2Client.describeInstances(
            new DescribeInstancesRequest().withInstanceIds(instances));
    for (Reservation reservation : describeInstances.getReservations()) {
        for (Instance instance : reservation.getInstances()) {
            if (state.equalsIgnoreCase(instance.getState().getName())) {
                instances.remove(instance.getInstanceId());
            }
        }
    }
    return instances;
}
 
Example 12
Source File: EC2Connector.java    From jenkins-deployment-dashboard-plugin with MIT License 5 votes vote down vote up
public List<ServerEnvironment> getEnvironments(Region region) {
    List<ServerEnvironment> environments = new ArrayList<ServerEnvironment>();

    ec2.setRegion(region);
    DescribeInstancesResult instances = ec2.describeInstances();
    for (Reservation reservation : instances.getReservations()) {
        for (Instance instance : reservation.getInstances()) {
            environments.add(getEnvironmentFromInstance(instance));
        }
    }

    return environments;
}
 
Example 13
Source File: Ec2TableProvider.java    From aws-athena-query-federation with Apache License 2.0 5 votes vote down vote up
/**
 * Calls DescribeInstances on the AWS EC2 Client returning all instances that match the supplied predicate and attempting
 * to push down certain predicates (namely queries for specific ec2 instance) to EC2.
 *
 * @See TableProvider
 */
@Override
public void readWithConstraint(BlockSpiller spiller, ReadRecordsRequest recordsRequest, QueryStatusChecker queryStatusChecker)
{
    boolean done = false;
    DescribeInstancesRequest request = new DescribeInstancesRequest();

    ValueSet idConstraint = recordsRequest.getConstraints().getSummary().get("instance_id");
    if (idConstraint != null && idConstraint.isSingleValue()) {
        request.setInstanceIds(Collections.singletonList(idConstraint.getSingleValue().toString()));
    }

    while (!done) {
        DescribeInstancesResult response = ec2.describeInstances(request);

        for (Reservation reservation : response.getReservations()) {
            for (Instance instance : reservation.getInstances()) {
                instanceToRow(instance, spiller);
            }
        }

        request.setNextToken(response.getNextToken());

        if (response.getNextToken() == null || !queryStatusChecker.isQueryRunning()) {
            done = true;
        }
    }
}
 
Example 14
Source File: EC2Communication.java    From development with Apache License 2.0 5 votes vote down vote up
public String getPublicDNS(String instanceId) {
    DescribeInstancesResult result = getEC2().describeInstances(
            new DescribeInstancesRequest().withInstanceIds(instanceId));
    List<Reservation> reservations = result.getReservations();
    Set<Instance> instances = new HashSet<Instance>();

    for (Reservation reservation : reservations) {
        instances.addAll(reservation.getInstances());
        if (instances.size() > 0) {
            return instances.iterator().next().getPublicDnsName();
        }
    }
    return null;
}
 
Example 15
Source File: DescribeInstancesExample.java    From aws-mock with MIT License 5 votes vote down vote up
/**
 * Describe specified instances within aws-mock.
 *
 * @param instanceIDs
 *            a list of instance IDs to describe
 * @return a list of specified instances
 */
public static List<Instance> describeInstances(final List<String> instanceIDs) {
    // pass any credentials as aws-mock does not authenticate them at all
    AWSCredentials credentials = new BasicAWSCredentials("foo", "bar");
    AmazonEC2Client amazonEC2Client = new AmazonEC2Client(credentials);

    // the mock endpoint for ec2 which runs on your computer
    String ec2Endpoint = "http://localhost:8000/aws-mock/ec2-endpoint/";
    amazonEC2Client.setEndpoint(ec2Endpoint);

    DescribeInstancesRequest request = new DescribeInstancesRequest();
    request.withInstanceIds(instanceIDs);

    DescribeInstancesResult response = amazonEC2Client.describeInstances(request);
    List<Reservation> reservations = response.getReservations();

    List<Instance> ret = new ArrayList<Instance>();

    for (Reservation reservation : reservations) {
        List<Instance> instances = reservation.getInstances();

        if (null != instances) {

            for (Instance i : instances) {
                ret.add(i);
            }
        }
    }

    return ret;
}
 
Example 16
Source File: DescribeInstances.java    From aws-doc-sdk-examples with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args)
{
    final AmazonEC2 ec2 = AmazonEC2ClientBuilder.defaultClient();
    boolean done = false;

    DescribeInstancesRequest request = new DescribeInstancesRequest();
    while(!done) {
        DescribeInstancesResult response = ec2.describeInstances(request);

        for(Reservation reservation : response.getReservations()) {
            for(Instance instance : reservation.getInstances()) {
                System.out.printf(
                    "Found instance with id %s, " +
                    "AMI %s, " +
                    "type %s, " +
                    "state %s " +
                    "and monitoring state %s",
                    instance.getInstanceId(),
                    instance.getImageId(),
                    instance.getInstanceType(),
                    instance.getState().getName(),
                    instance.getMonitoring().getState());
            }
        }

        request.setNextToken(response.getNextToken());

        if(response.getNextToken() == null) {
            done = true;
        }
    }
}
 
Example 17
Source File: Ec2InstanceStore.java    From soundwave with Apache License 2.0 5 votes vote down vote up
/**
 * Get an EC2 instance from the instance id. It gets the Reservation and loop through the
 * instances
 * under the reservation for find the instane has the given id
 *
 * @param instanceId
 * @return EC2 instance
 * @throws Exception
 */
@Override
public Instance getInstance(String instanceId)
    throws Exception {
  Preconditions.checkNotNull(instanceId);
  Preconditions.checkNotNull(defaultClient);
  OperationStats ops = new OperationStats("es2InstanceStore", "getInstance");
  try {
    awsRateLimiter.acquire();
    DescribeInstancesRequest request = new DescribeInstancesRequest();
    request.setInstanceIds(Arrays.asList(instanceId));
    DescribeInstancesResult result = defaultClient.describeInstances(request);

    for (Reservation reservation : result.getReservations()) {
      //Reservation refers to one launch command in EC2. Most time it should
      //only contain one instance
      for (Instance inst : reservation.getInstances()) {
        if (StringUtils.equals(inst.getInstanceId(), instanceId)) {
          ops.succeed();
          return inst;
        }
      }
    }
  } catch (Exception ex) {
    ops.failed();
    throw ex;
  }
  return null;
}
 
Example 18
Source File: BaseTest.java    From aws-mock with MIT License 5 votes vote down vote up
/**
 * Describe non terminated instances.
 *
 * @param instanceIds
 *            instances' IDs
 * @return list of non terminated instances
 */
protected final List<Instance> describeNonTerminatedInstances(
        final List<Instance> instanceIds) {
    DescribeInstancesRequest request = new DescribeInstancesRequest();
    request.setInstanceIds(getInstanceIds(instanceIds));

    // set the request filter
    Filter nonTerminatedFilter = getNonTerminatedInstancesFilter();
    request.getFilters().add(nonTerminatedFilter);

    DescribeInstancesResult result = amazonEC2Client
            .describeInstances(request);
    Assert.assertTrue(result.getReservations().size() > 0);

    List<Instance> instanceList = new ArrayList<Instance>();

    for (Reservation reservation : result.getReservations()) {
        List<Instance> instances = reservation.getInstances();

        if (null != instances) {
            for (Instance i : instances) {
                instanceList.add(i);
            }
        }
    }

    return instanceList;
}
 
Example 19
Source File: FindRunningInstances.java    From aws-doc-sdk-examples with Apache License 2.0 4 votes vote down vote up
public static void main(String[] args) {

        // snippet-start:[ec2.java1.running_instances.main]
        AmazonEC2 ec2 = AmazonEC2ClientBuilder.defaultClient();

        try {
            //Create the Filter to use to find running instances
            Filter filter = new Filter("instance-state-name");
            filter.withValues("running");

            //Create a DescribeInstancesRequest
            DescribeInstancesRequest request = new DescribeInstancesRequest();
            request.withFilters(filter);

            // Find the running instances
            DescribeInstancesResult response = ec2.describeInstances(request);

            for (Reservation reservation : response.getReservations()){

                for (Instance instance : reservation.getInstances()) {

                    //Print out the results
                    System.out.printf(
                            "Found reservation with id %s, " +
                                    "AMI %s, " +
                                    "type %s, " +
                                    "state %s " +
                                    "and monitoring state %s",
                            instance.getInstanceId(),
                            instance.getImageId(),
                            instance.getInstanceType(),
                            instance.getState().getName(),
                            instance.getMonitoring().getState());
                }
            }
            System.out.print("Done");

        } catch (SdkClientException e) {
            e.getStackTrace();
        }
        // snippet-end:[ec2.java1.running_instances.main]
    }
 
Example 20
Source File: EC2Api.java    From ec2-spot-jenkins-plugin with Apache License 2.0 4 votes vote down vote up
private static void describeInstancesBatch(
        final AmazonEC2 ec2, final Map<String, Instance> described, final List<String> batch) {
    // we are going to modify list, so copy
    final List<String> copy = new ArrayList<>(batch);

    // just to simplify debug by having consist order
    Collections.sort(copy);

    // because instances could be terminated at any time we do multiple
    // retry to get status and all time remove from request all non found instances if any
    while (copy.size() > 0) {
        try {
            final DescribeInstancesRequest request = new DescribeInstancesRequest().withInstanceIds(copy);

            DescribeInstancesResult result;
            do {
                result = ec2.describeInstances(request);
                request.setNextToken(result.getNextToken());

                for (final Reservation r : result.getReservations()) {
                    for (final Instance instance : r.getInstances()) {
                        // if instance not in terminated state, add it to described
                        if (!TERMINATED_STATES.contains(instance.getState().getName())) {
                            described.put(instance.getInstanceId(), instance);
                        }
                    }
                }
            } while (result.getNextToken() != null);

            // all good, clear request batch to stop
            copy.clear();
        } catch (final AmazonEC2Exception exception) {
            // if we cannot find instance, that's fine assume them as terminated
            // remove from request and try again
            if (exception.getErrorCode().equals(NOT_FOUND_ERROR_CODE)) {
                final List<String> notFoundInstanceIds = parseInstanceIdsFromNotFoundException(exception.getMessage());
                if (notFoundInstanceIds.isEmpty()) {
                    // looks like we cannot parse correctly, rethrow
                    throw exception;
                }
                copy.removeAll(notFoundInstanceIds);
            }
        }
    }
}