Java Code Examples for com.amazonaws.services.ec2.model.Reservation#getInstances()
The following examples show how to use
com.amazonaws.services.ec2.model.Reservation#getInstances() .
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 |
/** * * 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: BaseTest.java From aws-mock with MIT License | 6 votes |
/** * 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 3
Source File: AutomationReaperTask.java From SeleniumGridScaler with GNU General Public License v2.0 | 6 votes |
@Override public void doWork() { log.info("Running " + AutomationReaperTask.NAME); DescribeInstancesRequest describeInstancesRequest = new DescribeInstancesRequest(); Filter filter = new Filter("tag:LaunchSource"); filter.withValues("SeleniumGridScalerPlugin"); describeInstancesRequest.withFilters(filter); List<Reservation> reservations = ec2.describeInstances(describeInstancesRequest); for(Reservation reservation : reservations) { for(Instance instance : reservation.getInstances()) { // Look for orphaned nodes Date threshold = AutomationUtils.modifyDate(new Date(),-30, Calendar.MINUTE); String instanceId = instance.getInstanceId(); // If we found a node old enough AND we're not internally tracking it, this means this is an orphaned node and we should terminate it if(threshold.after(instance.getLaunchTime()) && !AutomationContext.getContext().nodeExists(instanceId)) { log.info("Terminating orphaned node: " + instanceId); ec2.terminateInstance(instanceId); } } } }
Example 4
Source File: EC2Utils.java From amazon-kinesis-connectors with Apache License 2.0 | 6 votes |
/** * Return the DNS name of one Amazon EC2 instance with the provided filter name and value. * * @param ec2Client * an Amazon EC2 instance * @param filterName * the name of the filter * @param filterValue * the value of the filter * @return the public DNS name of an instance with the filter name and value. Null if none exist. */ public static String getEndpointForFirstActiveInstanceWithTag(AmazonEC2 ec2Client, String filterName, String filterValue) { DescribeInstancesRequest describeInstancesRequest = new DescribeInstancesRequest().withFilters(new Filter().withName(filterName).withValues(filterValue)); DescribeInstancesResult describeInstancesResult = ec2Client.describeInstances(describeInstancesRequest); List<Reservation> reservations = describeInstancesResult.getReservations(); for (Reservation reservation : reservations) { List<Instance> ec2Instances = reservation.getInstances(); for (Instance ec2Instance : ec2Instances) { if (InstanceStateName.Running.toString().equals(ec2Instance.getState().getName())) { return ec2Instance.getPublicDnsName(); } } } return null; }
Example 5
Source File: AWSSdkClient.java From incubator-gobblin with Apache License 2.0 | 6 votes |
/*** * 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 |
@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 |
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: BaseTest.java From aws-mock with MIT License | 6 votes |
/** * 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 9
Source File: EC2Connector.java From jenkins-deployment-dashboard-plugin with MIT License | 5 votes |
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 10
Source File: DescribeInstances.java From aws-doc-sdk-examples with Apache License 2.0 | 5 votes |
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 11
Source File: Ec2InstanceStore.java From soundwave with Apache License 2.0 | 5 votes |
@Override public List<Instance> getInstancesForZone(AvailabilityZone zone, AmazonEC2Client client) throws Exception { OperationStats op = new OperationStats("ec2InstanceStore", "getInstancesForZone"); try { List<Instance> ret = new ArrayList<>(); DescribeInstancesRequest request = new DescribeInstancesRequest() .withMaxResults(1000) .withFilters(new Filter("availability-zone", Arrays.asList(zone.getZoneName()))) .withSdkClientExecutionTimeout( 600 * 1000) //10 minutes time out for total execution including retries .withSdkRequestTimeout(300 * 1000); //5 minutes time out for a single request List<Reservation> reservations = new ArrayList<>(); DescribeInstancesResult result = client.describeInstances(request); while (result != null) { reservations.addAll(result.getReservations()); if (result.getNextToken() != null) { request.setNextToken(result.getNextToken()); result = client.describeInstances(request); } else { result = null; } } for (Reservation reservation : reservations) { //Reservation refers to one launch command in EC2. Most time it should //only contains one instance for (Instance inst : reservation.getInstances()) { ret.add(inst); } } op.succeed(); return ret; } catch (Exception ex) { op.failed(); throw ex; } }
Example 12
Source File: DescribeInstancesExample.java From aws-mock with MIT License | 5 votes |
/** * 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 13
Source File: BaseTest.java From aws-mock with MIT License | 5 votes |
/** * 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 14
Source File: AwsInstanceConnector.java From cloudbreak with Apache License 2.0 | 5 votes |
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 15
Source File: Ec2TableProvider.java From aws-athena-query-federation with Apache License 2.0 | 5 votes |
/** * 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 16
Source File: EC2InstanceManager.java From usergrid with Apache License 2.0 | 5 votes |
/** * 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 17
Source File: DescribeInstancesExample.java From aws-mock with MIT License | 5 votes |
/** * 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 18
Source File: FindRunningInstances.java From aws-doc-sdk-examples with Apache License 2.0 | 4 votes |
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 19
Source File: Ec2Util.java From s3-bucket-loader with Apache License 2.0 | 4 votes |
public List<Instance> launchEc2Instances(AmazonEC2Client ec2Client, Properties props) throws Exception { Integer totalExpectedWorkers = Integer.valueOf(props.getProperty("master.workers.total")); // disk size Collection<BlockDeviceMapping> blockDevices = new ArrayList<BlockDeviceMapping>(); blockDevices.add( new BlockDeviceMapping() .withDeviceName(props.getProperty("master.workers.ec2.disk.deviceName")) .withEbs(new EbsBlockDevice() .withVolumeType(VolumeType.valueOf(props.getProperty("master.workers.ec2.disk.volumeType"))) .withDeleteOnTermination(true) .withVolumeSize(Integer.valueOf(props.getProperty("master.workers.ec2.disk.size.gigabytes"))))); // create our run request for the total workers we expect RunInstancesRequest runInstancesRequest = new RunInstancesRequest(); runInstancesRequest.withImageId(props.getProperty("master.workers.ec2.ami.id")) .withInstanceType(props.getProperty("master.workers.ec2.instanceType")) .withMinCount(totalExpectedWorkers) .withMaxCount(totalExpectedWorkers) .withBlockDeviceMappings(blockDevices) .withKeyName(props.getProperty("master.workers.ec2.keyName")) .withSecurityGroupIds(props.getProperty("master.workers.ec2.securityGroupId")) .withInstanceInitiatedShutdownBehavior(ShutdownBehavior.valueOf(props.getProperty("master.workers.ec2.shutdownBehavior"))) .withSubnetId(props.getProperty("master.workers.ec2.subnetId")) .withUserData(Base64.encodeAsString(readFile(props.getProperty("master.workers.ec2.userDataFile")).getBytes())); // launch logger.debug("Launching " + totalExpectedWorkers + " EC2 instances, " + "it may take few minutes for workers to come up...: \n" + "\tamiId:" + runInstancesRequest.getImageId() +"\n"+ "\tsecGrpId:" + runInstancesRequest.getSecurityGroupIds().get(0) +"\n"+ "\tsubnetId:" + runInstancesRequest.getSubnetId() +"\n"+ "\tinstanceType:" + runInstancesRequest.getInstanceType() +"\n"+ "\tshutdownBehavior:" + runInstancesRequest.getInstanceInitiatedShutdownBehavior() +"\n"+ "\tkeyName:" + runInstancesRequest.getKeyName() ); // as the instances come up, assuming the "userData" above launches the worker we will be good // they will auto register w/ us the master RunInstancesResult result = ec2Client.runInstances(runInstancesRequest); Reservation reservation = result.getReservation(); return reservation.getInstances(); }
Example 20
Source File: EC2InstanceManager.java From usergrid with Apache License 2.0 | 4 votes |
/** * Checks the state of all given instances in SLEEP_LENGTH intervals, returns when all instances are in expected * state or state check times out * * @param instanceIds List of instance IDs whose states are going to be checked * @param state Expected state to check * @param timeout Timeout length in milliseconds * @return true if all instances are in given state, false if timeout occured */ public boolean waitUntil ( Collection<String> instanceIds, InstanceState state, int timeout ) { List<String> instanceIdCopy = new ArrayList<String>( instanceIds ); Calendar cal = Calendar.getInstance(); cal.setTime( new Date() ); long startTime = cal.getTimeInMillis(); long timePassed; String stateStr; do { DescribeInstancesRequest dis = ( new DescribeInstancesRequest() ).withInstanceIds( instanceIdCopy ); DescribeInstancesResult disresult = client.describeInstances( dis ); // Since the request is filtered with instance IDs, there is always only one Reservation object Reservation reservation = disresult.getReservations().iterator().next(); for ( com.amazonaws.services.ec2.model.Instance in : reservation.getInstances() ) { stateStr = in.getState().getName(); LOG.info( "{} is {}", in.getInstanceId(), in.getState().getName() ); /** If expected state is ShuttingDown, also accept the Terminated ones */ if( state == InstanceState.ShuttingDown ) { if ( stateStr.equals( state.toString() ) || stateStr.equals( InstanceState.Terminated.toString() ) ) { instanceIdCopy.remove( in.getInstanceId() ); } } /** If expected state is Pending, also accept the Running ones */ else if( state == InstanceState.Pending ) { if ( stateStr.equals( state.toString() ) || stateStr.equals( InstanceState.Running.toString() ) ) { instanceIdCopy.remove( in.getInstanceId() ); } } /** If expected state is Stopping, also accept the Stopped ones */ else if( state == InstanceState.Stopping ) { if ( stateStr.equals( state.toString() ) || stateStr.equals( InstanceState.Stopped.toString() ) ) { instanceIdCopy.remove( in.getInstanceId() ); } } else { if ( in.getState().getName().equals( state.toString() ) ) { instanceIdCopy.remove( in.getInstanceId() ); } } } cal.setTime( new Date() ); timePassed = cal.getTimeInMillis() - startTime; try { Thread.sleep( SLEEP_LENGTH ); } catch ( InterruptedException e ) { LOG.warn( "Thread interrupted while sleeping", e ); } } while ( timePassed < timeout && instanceIdCopy.size() > 0 ); return ( timePassed < timeout ); }