com.amazonaws.services.ec2.model.DescribeInstancesResult Java Examples
The following examples show how to use
com.amazonaws.services.ec2.model.DescribeInstancesResult.
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: 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 #2
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 #3
Source File: AwsTagReporterTest.java From SeleniumGridScaler with GNU General Public License v2.0 | 6 votes |
@Test() public void testThreadTimesOut() { MockAmazonEc2Client client = new MockAmazonEc2Client(null); Collection<Instance> instances = Arrays.asList(new Instance()); DescribeInstancesResult describeInstancesResult = new DescribeInstancesResult(); Reservation reservation = new Reservation(); describeInstancesResult.setReservations(Arrays.asList(reservation)); // Make count mismatch reservation.setInstances(Arrays.asList(new Instance(),new Instance())); client.setDescribeInstances(describeInstancesResult); Properties properties = new Properties(); properties.setProperty("accounting_tag","foo"); properties.setProperty("function_tag","foo2"); properties.setProperty("product_tag","foo3"); AwsTagReporter reporter = new AwsTagReporter("testUuid",client,instances,properties); AwsTagReporter.TIMEOUT_IN_SECONDS = 1; try{ reporter.run(); } catch(RuntimeException e) { Assert.assertEquals("Error waiting for instances to exist to add tags",e.getMessage()); return; } Assert.fail("Exception should have been thrown since tags were never filed"); }
Example #4
Source File: AwsTagReporterTest.java From SeleniumGridScaler with GNU General Public License v2.0 | 6 votes |
@Test public void testSleepThrowsErrors() { MockAmazonEc2Client client = new MockAmazonEc2Client(null); client.setDescribeInstancesToThrowError(); Collection<Instance> instances = Arrays.asList(new Instance()); DescribeInstancesResult describeInstancesResult = new DescribeInstancesResult(); Reservation reservation = new Reservation(); describeInstancesResult.setReservations(Arrays.asList(reservation)); reservation.setInstances(instances); client.setDescribeInstances(describeInstancesResult); Properties properties = new Properties(); properties.setProperty("accounting_tag","foo"); properties.setProperty("function_tag","foo2"); properties.setProperty("product_tag","foo3"); AwsTagReporter reporter = new AwsTagReporter("testUuid",client,instances,properties) { @Override void sleep() throws InterruptedException { throw new InterruptedException(); } }; reporter.run(); }
Example #5
Source File: AwsTagReporterTest.java From SeleniumGridScaler with GNU General Public License v2.0 | 6 votes |
@Test public void testClientThrowsErrors() { MockAmazonEc2Client client = new MockAmazonEc2Client(null); client.setDescribeInstancesToThrowError(); Collection<Instance> instances = Arrays.asList(new Instance()); DescribeInstancesResult describeInstancesResult = new DescribeInstancesResult(); Reservation reservation = new Reservation(); describeInstancesResult.setReservations(Arrays.asList(reservation)); reservation.setInstances(instances); client.setDescribeInstances(describeInstancesResult); Properties properties = new Properties(); properties.setProperty("accounting_tag","foo"); properties.setProperty("function_tag","foo2"); properties.setProperty("product_tag","foo3"); AwsTagReporter reporter = new AwsTagReporter("testUuid",client,instances,properties) { @Override void sleep() throws InterruptedException { // do nothing } }; reporter.run(); }
Example #6
Source File: AwsTagReporterTest.java From SeleniumGridScaler with GNU General Public License v2.0 | 6 votes |
@Test public void testExceptionCaught() { MockAmazonEc2Client client = new MockAmazonEc2Client(null); Collection<Instance> instances = Arrays.asList(new Instance()); DescribeInstancesResult describeInstancesResult = new DescribeInstancesResult(); Reservation reservation = new Reservation(); describeInstancesResult.setReservations(Arrays.asList(reservation)); reservation.setInstances(instances); client.setDescribeInstances(describeInstancesResult); Properties properties = new Properties(); properties.setProperty("tagAccounting","key"); properties.setProperty("function_tag","foo2"); properties.setProperty("product_tag","foo3"); AwsTagReporter reporter = new AwsTagReporter("testUuid",client,instances,properties); reporter.run(); }
Example #7
Source File: AwsTagReporterTest.java From SeleniumGridScaler with GNU General Public License v2.0 | 6 votes |
@Test public void testTagsAssociated() { MockAmazonEc2Client client = new MockAmazonEc2Client(null); Collection<Instance> instances = Arrays.asList(new Instance()); DescribeInstancesResult describeInstancesResult = new DescribeInstancesResult(); Reservation reservation = new Reservation(); describeInstancesResult.setReservations(Arrays.asList(reservation)); reservation.setInstances(instances); client.setDescribeInstances(describeInstancesResult); Properties properties = new Properties(); properties.setProperty("tagAccounting","key,value"); properties.setProperty("function_tag","foo2"); properties.setProperty("product_tag","foo3"); AwsTagReporter reporter = new AwsTagReporter("testUuid",client,instances,properties); reporter.run(); }
Example #8
Source File: EC2Communication.java From development with Apache License 2.0 | 6 votes |
public String getInstanceState(String instanceId) { LOGGER.debug("getInstanceState('{}') entered", 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) { String state = instances.iterator().next().getState().getName(); LOGGER.debug(" InstanceState: {}", state); return state; } } LOGGER.debug("getInstanceState('{}') left", instanceId); return null; }
Example #9
Source File: EC2ClientActions.java From cloudbreak with Apache License 2.0 | 6 votes |
public List<String> getInstanceVolumeIds(List<String> instanceIds) { AmazonEC2 ec2Client = buildEC2Client(); DescribeInstancesResult describeInstancesResult = ec2Client.describeInstances(new DescribeInstancesRequest().withInstanceIds(instanceIds)); Map<String, Set<String>> instanceIdVolumeIdMap = describeInstancesResult.getReservations() .stream() .map(Reservation::getInstances) .flatMap(Collection::stream) .collect(Collectors.toMap(Instance::getInstanceId, instance -> instance.getBlockDeviceMappings() .stream() .filter(dev -> !"/dev/xvda".equals(dev.getDeviceName())) .map(InstanceBlockDeviceMapping::getEbs) .map(EbsInstanceBlockDevice::getVolumeId) .collect(Collectors.toSet()) )); instanceIdVolumeIdMap.forEach((instanceId, volumeIds) -> Log.log(LOGGER, format(" Attached volume IDs are %s for [%s] EC2 instance ", volumeIds.toString(), instanceId))); return instanceIdVolumeIdMap .values() .stream() .flatMap(Collection::stream) .collect(Collectors.toList()); }
Example #10
Source File: ExamplePlugin.java From fullstop with Apache License 2.0 | 6 votes |
@Override // @HystrixCommand(fallback = my coole exception) // command for account id and client type -> generate new credentials public void processEvent(final CloudTrailEvent event) { final String parameters = event.getEventData().getRequestParameters(); final String instanceId = getFromParameters(parameters); final AmazonEC2 client = getClientForAccount( event.getEventData().getUserIdentity().getAccountId(), Region.getRegion(Regions.fromName(event.getEventData().getAwsRegion()))); final DescribeInstancesRequest request = new DescribeInstancesRequest(); request.setInstanceIds(Collections.singleton(instanceId)); // try final DescribeInstancesResult result = client.describeInstances(request); // catch credentials are old // throw new my coole exception ( account id, CLIENTTYPE.EC2, exception) -> this will trigger hystrix LOG.info("SAVING RESULT INTO MAGIC DB", result); }
Example #11
Source File: AwsInstanceCloudConnector.java From titus-control-plane with Apache License 2.0 | 6 votes |
@Override public Observable<List<Instance>> getInstances(List<String> instanceIds) { if (instanceIds.isEmpty()) { return Observable.just(Collections.emptyList()); } List<Observable<List<Instance>>> chunkObservable = CollectionsExt.chop(instanceIds, AWS_INSTANCE_ID_MAX).stream() .map(chunk -> { Observable<DescribeInstancesResult> ec2DescribeObservable = toObservable(new DescribeInstancesRequest().withInstanceIds(chunk), ec2Client::describeInstancesAsync); Observable<DescribeAutoScalingInstancesResult> asgDescribeObservable = toObservable(new DescribeAutoScalingInstancesRequest().withInstanceIds(chunk), autoScalingClient::describeAutoScalingInstancesAsync); return Observable.zip(ec2DescribeObservable, asgDescribeObservable, (ec2Data, autoScalerData) -> toInstances(ec2Data.getReservations(), autoScalerData.getAutoScalingInstances())); }) .collect(Collectors.toList()); return Observable.merge(chunkObservable, AWS_PARALLELISM) .timeout(configuration.getAwsRequestTimeoutMs(), TimeUnit.MILLISECONDS) .reduce(new ArrayList<>(), (acc, result) -> { acc.addAll(result); return acc; }); }
Example #12
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 #13
Source File: AWSProvider.java From testgrid with Apache License 2.0 | 6 votes |
@Override public Optional<String> getInstanceName(String region, String instanceId) { final AmazonEC2 amazonEC2 = AmazonEC2ClientBuilder.standard() .withCredentials(new PropertiesFileCredentialsProvider(configFilePath.toString())) .withRegion(region) .build(); List<String> instanceIds = new ArrayList<>(); instanceIds.add(instanceId); DescribeInstancesRequest request = new DescribeInstancesRequest(); request.setInstanceIds(instanceIds); DescribeInstancesResult result = amazonEC2.describeInstances(request); Optional<String> tagOptional = result.getReservations() .stream() .flatMap(reservation -> reservation.getInstances().stream()) .flatMap(instance -> instance.getTags().stream()) .filter(tag -> NAME_ENTRY.equals(tag.getKey())) .findFirst() .map(Tag::getValue); return tagOptional; }
Example #14
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 #15
Source File: AWSProvider.java From testgrid with Apache License 2.0 | 6 votes |
/** * 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 #16
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 #17
Source File: Ec2LookupServiceTest.java From Gatekeeper with Apache License 2.0 | 6 votes |
@Before public void before() { awsEnvironment = new AWSEnvironment("Dev", "us-west-2"); mockedResult = new DescribeInstancesResult(); mockedResult.setReservations(Arrays.asList(new Reservation[]{ fakeInstance("i-12345", "1.2.3.4", "TestOne", "TEP","Linux"), fakeInstance("i-abcde", "123.2.3.4", "TestOneTwo", "TST", "Linux"), fakeInstance("i-123ab", "456.2.3.4", "HelloOne", "TEP", "Linux"), fakeInstance("i-456cd", "123.22.3.4", "HelloTwo", "TEP", "Linux"), fakeInstance("i-12347", "132.23.43.4", "TestThree", "TEP", "Linux") })); Mockito.when(gatekeeperEC2Properties.getAppIdentityTag()).thenReturn("Application"); Mockito.when(awsSessionService.getEC2Session(any())).thenReturn(amazonEC2Client); Mockito.when(amazonEC2Client.describeInstances(any())).thenReturn(mockedResult); }
Example #18
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 #19
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 #20
Source File: PublicAccessAutoFix.java From pacbot with Apache License 2.0 | 6 votes |
/** * Gets the instance details for ec 2. * * @param clientMap the client map * @param resourceId the resource id * @return the instance details for ec 2 * @throws Exception the exception */ public static Instance getInstanceDetailsForEc2(Map<String,Object> clientMap,String resourceId) throws Exception { AmazonEC2 ec2Client = (AmazonEC2) clientMap.get("client"); DescribeInstancesRequest describeInstancesRequest = new DescribeInstancesRequest(); describeInstancesRequest.setInstanceIds(Arrays.asList(resourceId)); RetryConfig config = RetryConfig.custom().maxAttempts(MAX_ATTEMPTS).waitDuration(Duration.ofSeconds(WAIT_INTERVAL)).build(); RetryRegistry registry = RetryRegistry.of(config); Retry retry = registry.retry(describeInstancesRequest.toString()); Function<Integer, Instance> decorated = Retry.decorateFunction(retry, (Integer s) -> { DescribeInstancesResult describeInstancesResult = ec2Client.describeInstances(describeInstancesRequest); List<Reservation> reservations = describeInstancesResult.getReservations(); Reservation reservation = reservations.get(0); List<Instance> instances = reservation.getInstances(); return instances.get(0); }); return decorated.apply(1); }
Example #21
Source File: Ec2Facade.java From aws-service-catalog-terraform-reference-architecture with Apache License 2.0 | 6 votes |
public String getInstanceId(Tag instanceTag) { Filter tagFilter = new Filter("tag:" + instanceTag.getKey(), ImmutableList.of(instanceTag.getValue())); DescribeInstancesRequest request = new DescribeInstancesRequest().withFilters(tagFilter, RUNNING_INSTANCE_FILTER); DescribeInstancesResult result = ec2.describeInstances(request); List<String> instanceIds; if (result.getReservations() != null) { instanceIds = result.getReservations().stream() .flatMap(reservation -> reservation.getInstances().stream()) .map(Instance::getInstanceId) .collect(ImmutableList.toImmutableList()); } else { instanceIds = ImmutableList.of(); } if (instanceIds.isEmpty()) { String message = String.format( "Invalid FulfillmentConfig. No instances found with TagKey: %s and TagValue: %s", instanceTag.getKey(), instanceTag.getValue()); throw new RuntimeException(message); } int randomIndex = randomGenerator.nextInt(instanceIds.size()); return instanceIds.get(randomIndex); }
Example #22
Source File: AwsAutoScalingDeployUtils.java From vertx-deploy-tools with Apache License 2.0 | 5 votes |
public boolean checkEc2Instance(String instanceId) { boolean instanceTerminated = false; try { DescribeInstancesResult result = awsEc2Client.describeInstances(new DescribeInstancesRequest().withInstanceIds(instanceId)); List<com.amazonaws.services.ec2.model.Instance> instances = result.getReservations().stream() .flatMap(r -> r.getInstances().stream()) .filter(i -> i.getInstanceId().equals(instanceId)) .collect(Collectors.toList()); instanceTerminated = instances.isEmpty() || instances.stream() .map(com.amazonaws.services.ec2.model.Instance::getState) .anyMatch(s -> s.getCode().equals(48)); } catch (AmazonServiceException e) { log.info(e.toString(), e); if (e.getStatusCode() == 400) { instanceTerminated = true; } } if (instanceTerminated) { log.warn("Invalid instance " + instanceId + " in group " + activeConfiguration.getAutoScalingGroupId() + ". Detaching instance."); awsAsClient.detachInstances(new DetachInstancesRequest() .withAutoScalingGroupName(activeConfiguration.getAutoScalingGroupId()) .withInstanceIds(instanceId) .withShouldDecrementDesiredCapacity(false)); } return instanceTerminated; }
Example #23
Source File: AwsAutoScalingDeployUtils.java From vertx-deploy-tools with Apache License 2.0 | 5 votes |
public List<Ec2Instance> getInstancesForAutoScalingGroup(Log log, AutoScalingGroup autoScalingGroup) throws MojoFailureException { log.info("retrieving list of instanceId's for auto scaling group with id : " + activeConfiguration.getAutoScalingGroupId()); activeConfiguration.getHosts().clear(); log.debug("describing instances in auto scaling group"); if (autoScalingGroup.getInstances().isEmpty()) { return new ArrayList<>(); } Map<String, Instance> instanceMap = autoScalingGroup.getInstances().stream().collect(Collectors.toMap(Instance::getInstanceId, Function.identity())); try { DescribeInstancesResult instancesResult = awsEc2Client.describeInstances(new DescribeInstancesRequest().withInstanceIds(autoScalingGroup.getInstances().stream().map(Instance::getInstanceId).collect(Collectors.toList()))); List<Ec2Instance> ec2Instances = instancesResult.getReservations().stream().flatMap(r -> r.getInstances().stream()).map(this::toEc2Instance).collect(Collectors.toList()); log.debug("describing elb status"); autoScalingGroup.getLoadBalancerNames().forEach(elb -> this.updateInstancesStateOnLoadBalancer(elb, ec2Instances)); ec2Instances.forEach(i -> i.updateAsState(AwsState.map(instanceMap.get(i.getInstanceId()).getLifecycleState()))); ec2Instances.sort((o1, o2) -> { int sComp = o1.getAsState().compareTo(o2.getAsState()); if (sComp != 0) { return sComp; } else { return o1.getElbState().compareTo(o2.getElbState()); } }); if (activeConfiguration.isIgnoreInStandby()) { return ec2Instances.stream().filter(i -> i.getAsState() != AwsState.STANDBY).collect(Collectors.toList()); } return ec2Instances; } catch (AmazonClientException e) { log.error(e.getMessage(), e); throw new MojoFailureException(e.getMessage()); } }
Example #24
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 #25
Source File: AwsInstanceConnectorTest.java From cloudbreak with Apache License 2.0 | 5 votes |
private DescribeInstancesResult getDescribeInstancesResultOneRunning(String state, int code) { Instance instances1 = getAwsInstance("i-1", state, code); Instance instances2 = getAwsInstance("i-2", "running", 16); Reservation reservation1 = getReservation(instances1, "1"); Reservation reservation2 = getReservation(instances2, "2"); return new DescribeInstancesResult().withReservations(reservation1, reservation2); }
Example #26
Source File: AwsMetadataCollector.java From cloudbreak with Apache License 2.0 | 5 votes |
private List<Instance> collectInstancesForGroup(AuthenticatedContext ac, AmazonAutoScalingRetryClient amazonASClient, AmazonEC2Client amazonEC2Client, AmazonCloudFormationRetryClient amazonCFClient, String group) { LOGGER.debug("Collect aws instances for group: {}", group); String asGroupName = cloudFormationStackUtil.getAutoscalingGroupName(ac, amazonCFClient, group); List<String> instanceIds = cloudFormationStackUtil.getInstanceIds(amazonASClient, asGroupName); DescribeInstancesRequest instancesRequest = cloudFormationStackUtil.createDescribeInstancesRequest(instanceIds); DescribeInstancesResult instancesResult = amazonEC2Client.describeInstances(instancesRequest); return instancesResult.getReservations().stream() .flatMap(reservation -> reservation.getInstances().stream()) .collect(Collectors.toList()); }
Example #27
Source File: AwsTaggingService.java From cloudbreak with Apache License 2.0 | 5 votes |
public void tagRootVolumes(AuthenticatedContext ac, AmazonEC2Client ec2Client, List<CloudResource> instanceResources, Map<String, String> userDefinedTags) { String stackName = ac.getCloudContext().getName(); LOGGER.debug("Fetch AWS instances to collect all root volume ids for stack: {}", stackName); List<String> instanceIds = instanceResources.stream().map(CloudResource::getInstanceId).collect(Collectors.toList()); DescribeInstancesResult describeInstancesResult = ec2Client.describeInstances(new DescribeInstancesRequest().withInstanceIds(instanceIds)); List<Instance> instances = describeInstancesResult.getReservations().stream().flatMap(res -> res.getInstances().stream()).collect(Collectors.toList()); List<String> rootVolumeIds = instances.stream() .map(this::getRootVolumeId) .filter(Optional::isPresent) .map(blockDeviceMapping -> blockDeviceMapping.get().getEbs().getVolumeId()) .collect(Collectors.toList()); int instanceCount = instances.size(); int volumeCount = rootVolumeIds.size(); if (instanceCount != volumeCount) { LOGGER.debug("Did not find all root volumes, instanceResources: {}, found root volumes: {} for stack: {}", instanceCount, volumeCount, stackName); } else { LOGGER.debug("Found all ({}) root volumes for stack: {}", volumeCount, stackName); } AtomicInteger counter = new AtomicInteger(); Collection<List<String>> volumeIdChunks = rootVolumeIds.stream() .collect(Collectors.groupingBy(it -> counter.getAndIncrement() / MAX_RESOURCE_PER_REQUEST)).values(); Collection<Tag> tags = prepareEc2Tags(ac, userDefinedTags); for (List<String> volumeIds : volumeIdChunks) { LOGGER.debug("Tag {} root volumes for stack: {}", volumeIds.size(), stackName); ec2Client.createTags(new CreateTagsRequest().withResources(volumeIds).withTags(tags)); } }
Example #28
Source File: MockAmazonEc2Client.java From SeleniumGridScaler with GNU General Public License v2.0 | 5 votes |
@Override public DescribeInstancesResult describeInstances(DescribeInstancesRequest describeInstancesRequest) throws AmazonClientException { if(throwDescribeInstancesError) { throwDescribeInstancesError = false; throw new AmazonClientException("testError"); } return describeInstancesResult; }
Example #29
Source File: AwsTagReporter.java From SeleniumGridScaler with GNU General Public License v2.0 | 5 votes |
@Override public void run() { log.info("AwsTagReporter thread initialized"); DescribeInstancesRequest request = new DescribeInstancesRequest(); Collection<String> instanceIds = new ArrayList<>(); for(Instance instance : instances) { instanceIds.add(instance.getInstanceId()); } request.withInstanceIds(instanceIds); long startTime = System.currentTimeMillis(); boolean instancesFound = false; do{ // Wait up to 10 seconds for the instances to exist with AWS if(System.currentTimeMillis() > startTime + AwsTagReporter.TIMEOUT_IN_SECONDS) { throw new RuntimeException("Error waiting for instances to exist to add tags"); } try{ DescribeInstancesResult existingInstances = ec2Client.describeInstances(request); if(existingInstances.getReservations().get(0).getInstances().size() == instances.size()) { log.info("Correct instances were found to add tags to!"); instancesFound = true; } } catch(Throwable t) { log.error("Error finding instances. Sleeping."); try { sleep(); } catch (InterruptedException e) { log.error("Error sleeping for adding tags", e); } } } while(!instancesFound); associateTags(instances); log.info("AwsTagReporter thread completed successfully"); }
Example #30
Source File: Ec2MachineConfigurator.java From roboconf-platform with Apache License 2.0 | 5 votes |
/** * Checks whether a VM is started or not (which is stronger than {@link #checkVmIsKnown()}). * @return true if the VM is started, false otherwise */ private boolean checkVmIsStarted() { DescribeInstancesRequest dis = new DescribeInstancesRequest(); dis.setInstanceIds(Collections.singletonList(this.machineId)); DescribeInstancesResult disresult = this.ec2Api.describeInstances( dis ); // Obtain availability zone (for later use, eg. volume attachment). // Necessary if no availability zone is specified in configuration // (because volumes must be attached to instances in the same availability zone). this.availabilityZone = disresult.getReservations().get(0).getInstances().get(0).getPlacement().getAvailabilityZone(); return "running".equalsIgnoreCase( disresult.getReservations().get(0).getInstances().get(0).getState().getName()); }