/** * Creates a snapshot and return the snapshot id. */ public static String createSnapshot(VerificationHost host, AmazonEC2Client client, String volumeId) { CreateSnapshotRequest req = new CreateSnapshotRequest() .withVolumeId(volumeId); CreateSnapshotResult res = client.createSnapshot(req); String snapshotId = res.getSnapshot().getSnapshotId(); Filter filter = new Filter().withName(SNAPSHOT_ID_ATTRIBUTE).withValues(snapshotId); DescribeSnapshotsRequest snapshotsRequest = new DescribeSnapshotsRequest() .withSnapshotIds(snapshotId) .withFilters(filter); host.waitFor("Timeout waiting for creating snapshot", () -> { DescribeSnapshotsResult snapshotsResult = client.describeSnapshots(snapshotsRequest); String state = snapshotsResult.getSnapshots().get(0).getState(); if (state.equalsIgnoreCase(SNAPSHOT_STATUS_COMPLETE)) { return true; } return false; }); return snapshotId; }
private DescribeInstancesRequest buildDescribeInstancesRequest() { DescribeInstancesRequest describeInstancesRequest = new DescribeInstancesRequest() .withFilters( new Filter("instance-state-name").withValues("running", "pending") ); for (Map.Entry<String, String> tagFilter : tags.entrySet()) { // for a given tag key, OR relationship for multiple different values describeInstancesRequest.withFilters( new Filter("tag:" + tagFilter.getKey()).withValues(tagFilter.getValue()) ); } if (!availabilityZones.isEmpty()) { // OR relationship amongst multiple values of the availability-zone filter describeInstancesRequest.withFilters( new Filter("availability-zone").withValues(availabilityZones) ); } return describeInstancesRequest; }
@Test public void testResolveAMIFound() throws Exception { ec2mock.createDescribeImagesResult("image1"); Image result = ec2comm.resolveAMI("image1"); assertEquals("image1", result.getImageId()); ArgumentCaptor<DescribeImagesRequest> argCaptor = ArgumentCaptor .forClass(DescribeImagesRequest.class); verify(ec2).describeImages(argCaptor.capture()); DescribeImagesRequest dir = argCaptor.getValue(); for (Filter filter : dir.getFilters()) { if (filter.getName().equals("name")) { assertEquals("image1", filter.getValues().get(0)); } } }
@Override public Map<AvailabilityZone, List<ReservedInstances>> getReservedInstancesForZone( AvailabilityZone zone, AmazonEC2Client client) throws Exception { OperationStats op = new OperationStats("ec2InstanceStore", "getReservedInstancesForZone"); try { Map<AvailabilityZone, List<ReservedInstances>> ret = new HashMap<>(); DescribeReservedInstancesRequest request = new DescribeReservedInstancesRequest() .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 DescribeReservedInstancesResult result = client.describeReservedInstances(request); ret.put(zone, result.getReservedInstances()); op.succeed(); return ret; } catch (Exception e) { op.failed(); logger.error(ExceptionUtils.getRootCauseMessage(e)); throw e; } }
public DeferredResult<DescribeSecurityGroupsResult> getSecurityGroups(List<String> secGroupIds, String vpcId, String nicName, String vmName) { DescribeSecurityGroupsRequest req = new DescribeSecurityGroupsRequest() .withFilters(new Filter(AWS_GROUP_ID_FILTER, secGroupIds)) .withFilters(new Filter(AWS_VPC_ID_FILTER, singletonList(vpcId))); String msg = "Getting AWS Security Groups by id [" + secGroupIds + "] for [" + nicName + "] NIC for [" + vmName + "] VM"; AWSDeferredResultAsyncHandler<DescribeSecurityGroupsRequest, DescribeSecurityGroupsResult> handler = new AWSDeferredResultAsyncHandler<>(this.service, msg); this.client.describeSecurityGroupsAsync(req, handler); return handler.toDeferredResult(); }
/** * Get the instances from AWS filtered by the instances Ids known to the local system. */ public void getRemoteInstances(EnumerationDeletionContext aws, AWSEnumerationDeletionSubStage next) { if (aws.localInstanceIds == null || aws.localInstanceIds.size() == 0) { logFine(() -> "No local records found. No states need to be fetched from the AWS" + " endpoint."); aws.subStage = next; deleteResourcesInLocalSystem(aws); return; } DescribeInstancesRequest request = new DescribeInstancesRequest(); Filter runningInstanceFilter = getAWSNonTerminatedInstancesFilter(); request.getFilters().add(runningInstanceFilter); // Get only the instances from the remote system for which a compute state exists in the // local system. logFine(() -> String.format("Fetching instance details for %d instances on the AWS" + " endpoint.", aws.localInstanceIds.keySet().size())); request.getInstanceIds().addAll(new ArrayList<>(aws.localInstanceIds.keySet())); AsyncHandler<DescribeInstancesRequest, DescribeInstancesResult> resultHandler = new AWSEnumerationAsyncHandler(this, aws, next); aws.amazonEC2Client.describeInstancesAsync(request, resultHandler); }
/** * Creates a volume and return the volume id. */ public static String createVolume(VerificationHost host, AmazonEC2Client client) { CreateVolumeRequest req = new CreateVolumeRequest() .withAvailabilityZone(zoneId + avalabilityZoneIdentifier) .withSize(1); CreateVolumeResult res = client.createVolume(req); String volumeId = res.getVolume().getVolumeId(); Filter filter = new Filter().withName(VOLUME_ID_ATTRIBUTE).withValues(volumeId); DescribeVolumesRequest volumesRequest = new DescribeVolumesRequest() .withVolumeIds(volumeId) .withFilters(filter); host.waitFor("Timeout waiting for creating volume", () -> { DescribeVolumesResult volumesResult = client.describeVolumes(volumesRequest); String state = volumesResult.getVolumes().get(0).getState(); if (state.equalsIgnoreCase(VOLUME_STATUS_AVAILABLE)) { return true; } return false; }); return volumeId; }
/** * Gets the instance count of non-terminated instances on the AWS endpoint. This is used to run * the asserts and validate the results for the data that is collected during enumeration.This * also calculates the compute descriptions that will be used to represent the instances that * were discovered on the AWS endpoint. Further factoring in the * * @throws Throwable */ public static BaseLineState getBaseLineInstanceCount(VerificationHost host, AmazonEC2AsyncClient client, List<String> testComputeDescriptions) throws Throwable { BaseLineState baseLineState = new BaseLineState(); AWSEnumerationAsyncHandler enumerationHandler = new AWSEnumerationAsyncHandler(host, AWSEnumerationAsyncHandler.MODE.GET_COUNT, null, null, null, testComputeDescriptions, baseLineState); DescribeInstancesRequest request = new DescribeInstancesRequest(); Filter runningInstanceFilter = getAWSNonTerminatedInstancesFilter(); request.getFilters().add(runningInstanceFilter); client.describeInstancesAsync(request, enumerationHandler); host.waitFor("Error waiting to get base line instance count from AWS in test ", () -> { return baseLineState.isCountPopulated; }); return baseLineState; }
public void deleteAwsSubnet() { if (this.isMock) { return; } DescribeSubnetsRequest subnetRequest = new DescribeSubnetsRequest() .withFilters( new Filter(AWS_VPC_ID_FILTER, singletonList(AWS_DEFAULT_VPC_ID))) .withFilters( new Filter(AWS_SUBNET_CIDR_FILTER, singletonList(AWS_NON_EXISTING_SUBNET_CIDR))); DescribeSubnetsResult subnetResult = this.client.describeSubnets(subnetRequest); subnetResult.getSubnets().forEach(subnet -> { DeleteSubnetRequest deleteRequest = new DeleteSubnetRequest(subnet.getSubnetId()); this.client.deleteSubnet(deleteRequest); }); }
public void deleteAwsPublicSubnet() { if (this.isMock) { return; } DescribeSubnetsRequest subnetRequest = new DescribeSubnetsRequest() .withFilters( new Filter(AWS_VPC_ID_FILTER, singletonList(AWS_DEFAULT_VPC_ID))) .withFilters( new Filter(AWS_SUBNET_CIDR_FILTER, singletonList(AWS_NON_EXISTING_PUBLIC_SUBNET_CIDR))); DescribeSubnetsResult subnetResult = this.client.describeSubnets(subnetRequest); subnetResult.getSubnets().forEach(subnet -> { DeleteSubnetRequest deleteRequest = new DeleteSubnetRequest(subnet.getSubnetId()); this.client.deleteSubnet(deleteRequest); }); }
/** * Validates if the given AMI has given tag and value. * * @return true if matches otherwise false */ public boolean isAmiWithTagExist(final String amiId, final String tagName, final String tagValue) { final DescribeImagesRequest request = new DescribeImagesRequest() .withFilters(new Filter().withName(tagName).withValues(tagValue)) .withFilters(new Filter().withName("image-id").withValues(amiId)); try { final DescribeImagesResult result = ec2Client.describeImages(request); return result.getImages().size() > 0; } catch (final AmazonServiceException ase) { if (ase.getErrorCode() == "InvalidAMIID.NotFound") { return false; } throw ase; } }
/** * Gets all EC2 instances with the given tag key/value pair * @param tagKey - Key of the tag * @param tagValue - Value of the tag * @param filters - Array of EC2 filters * @return - List of instances with the given tag */ public List<Instance> getInstancesByTag(final String tagKey, final String tagValue, final Filter... filters) { final String filterName = String.format(FILTER_NAME_TEMPL_FOR_EC2_TAGS, tagKey); final Filter tagFilter = new Filter().withName(filterName).withValues(tagValue); final Set<Filter> filterSet = Sets.newHashSet(filters); filterSet.add(tagFilter); final DescribeInstancesRequest request = new DescribeInstancesRequest().withFilters(filterSet); DescribeInstancesResult result = ec2Client.describeInstances(request); List<Instance> instances = Lists.newArrayList(); result.getReservations().forEach(reservation -> { instances.addAll(reservation.getInstances()); }); return instances; }
@Test public void isAmiWithTagExistTrue() { AmazonEC2 ec2Client = mock(AmazonEC2.class); AmiTagCheckService amiTagCheckService = new AmiTagCheckService(ec2Client); String amiId = "ami-1234abcd"; String tagName = "sometag"; String tagValue = "someval"; when(ec2Client.describeImages( new DescribeImagesRequest() .withFilters(new Filter().withName(tagName).withValues(tagValue)) .withFilters(new Filter().withName("image-id").withValues(amiId)) ) ).thenReturn( new DescribeImagesResult().withImages(new Image()) ); // invoke method under test assertTrue(amiTagCheckService.isAmiWithTagExist(amiId, tagName, tagValue)); }
@Test public void isAmiWithTagExistFalse() { AmazonEC2 ec2Client = mock(AmazonEC2.class); AmiTagCheckService amiTagCheckService = new AmiTagCheckService(ec2Client); String amiId = "ami-1234abcd"; String tagName = "sometag"; String tagValue = "someval"; when(ec2Client.describeImages( new DescribeImagesRequest() .withFilters(new Filter().withName(tagName).withValues(tagValue)) .withFilters(new Filter().withName("image-id").withValues(amiId)) ) ).thenReturn( new DescribeImagesResult() ); // invoke method under test assertFalse(amiTagCheckService.isAmiWithTagExist(amiId, tagName, tagValue)); }
@Test public void isAmiWithTagExistNotFound() { AmazonEC2 ec2Client = mock(AmazonEC2.class); AmiTagCheckService amiTagCheckService = new AmiTagCheckService(ec2Client); String amiId = "ami-1234abcd"; String tagName = "sometag"; String tagValue = "someval"; AmazonServiceException ex = new AmazonServiceException("fake-exception"); ex.setErrorCode("InvalidAMIID.NotFound"); when(ec2Client.describeImages( new DescribeImagesRequest() .withFilters(new Filter().withName(tagName).withValues(tagValue)) .withFilters(new Filter().withName("image-id").withValues(amiId)) ) ).thenThrow(ex); // invoke method under test assertFalse(amiTagCheckService.isAmiWithTagExist(amiId, tagName, tagValue)); }
@Test public void isAmiWithTagExistThrowException() { AmazonEC2 ec2Client = mock(AmazonEC2.class); AmiTagCheckService amiTagCheckService = new AmiTagCheckService(ec2Client); String amiId = "ami-1234abcd"; String tagName = "sometag"; String tagValue = "someval"; String unknownAwsExMessage = "Unknown AWS exception message"; when(ec2Client.describeImages( new DescribeImagesRequest() .withFilters(new Filter().withName(tagName).withValues(tagValue)) .withFilters(new Filter().withName("image-id").withValues(amiId)) ) ).thenThrow(new AmazonServiceException(unknownAwsExMessage)); try { // invoke method under test amiTagCheckService.isAmiWithTagExist(amiId, tagName, tagValue); fail("Expected exception message '" + unknownAwsExMessage + "'not received"); } catch (AmazonServiceException ex) { // pass assertEquals(unknownAwsExMessage, ex.getErrorMessage()); } }
@Test public void testGetInstancesByTagHappy() { String tagKey = "tag key"; String tagValue = "tag value"; Filter filter = new Filter().withName(INSTANCE_STATE_FILTER_NAME).withValues(INSTANCE_STATE_RUNNING_FILTER_VALUE); Instance instance = mock(Instance.class); when(ec2Client.describeInstances(new DescribeInstancesRequest() .withFilters( filter, new Filter() .withName(String.format(FILTER_NAME_TEMPL_FOR_EC2_TAGS, tagKey)) .withValues(tagValue) ) )).thenReturn( new DescribeInstancesResult() .withReservations( new Reservation() .withInstances(instance)) ); List<Instance> instances = ec2Service.getInstancesByTag(tagKey, tagValue, filter); assertTrue(instances.contains(instance)); }
public List<String> getAllRunningInstances() { logger.info("Get running instances request"); DescribeInstancesRequest request = new DescribeInstancesRequest(); List<String> valuesT1 = new ArrayList<String>(); valuesT1.add(_defaultTag); Filter filter1 = new Filter("tag:" + _tagKey, valuesT1); List<String> valuesT2 = new ArrayList<String>(); valuesT2.add("running"); valuesT2.add("pending"); Filter filter2 = new Filter("instance-state-name",valuesT2); DescribeInstancesResult result = _ec2.describeInstances(request.withFilters(filter1,filter2)); List<Reservation> reservations = result.getReservations(); List<String> instancesID = new ArrayList<String>(); for (Reservation reservation : reservations) { List<Instance> instances = reservation.getInstances(); for (Instance instance : instances) { instancesID.add(instance.getInstanceId()); } } return instancesID; }
public static List<String> getAllAvailabilityZones() { connect(); DescribeAvailabilityZonesRequest req = new DescribeAvailabilityZonesRequest(); ArrayList<Filter> filters = new ArrayList<Filter>(); ArrayList<String> regions = new ArrayList<String>(); regions.add(Configuration.REGION); filters.add(new Filter("region-name", regions)); req.setFilters(filters); DescribeAvailabilityZonesResult res = client.describeAvailabilityZones(req); List<AvailabilityZone> zones = res.getAvailabilityZones(); ArrayList<String> zonesStr = new ArrayList<String>(); for (AvailabilityZone zone : zones) zonesStr.add(zone.getZoneName()); return zonesStr; }
@Override public void load() { VPC vpc = resources.onlyOne(VPC.class).get(); if (vpc.remoteVPC != null) { List<NatGateway> gateways = AWS.vpc.ec2.describeNatGateways(new DescribeNatGatewaysRequest() .withFilter(new Filter("state").withValues("available"), new Filter("vpc-id").withValues(vpc.remoteVPC.getVpcId()))) .getNatGateways(); if (gateways.size() > 1) throw new Error(Strings.format("multiple nat gateway found, gateways={}", gateways)); if (!gateways.isEmpty()) { NATGateway gateway = resources.onlyOne(NATGateway.class).orElseGet(() -> resources.add(new NATGateway())); gateway.remoteNATGateway = gateways.get(0); gateway.foundInRemote(); } } }
private List<Instance> runningInstances(String resourceId) { Tag tag = new EC2TagHelper(env).resourceId(resourceId); DescribeTagsRequest request = new DescribeTagsRequest() .withFilters(new Filter("key").withValues(tag.getKey()), new Filter("value").withValues(tag.getValue()), new Filter("resource-type").withValues("instance")); List<TagDescription> remoteTags = AWS.ec2.describeTags(request); List<String> instanceIds = remoteTags.stream().map(TagDescription::getResourceId).collect(Collectors.toList()); if (instanceIds.isEmpty()) { com.amazonaws.services.autoscaling.model.AutoScalingGroup asGroup = AWS.as.describeASGroup(env.name + "-" + this.resourceId); if (asGroup == null) throw new Error("can not find any running instance or asGroup, id=" + this.resourceId); instanceIds = asGroup.getInstances().stream() .map(com.amazonaws.services.autoscaling.model.Instance::getInstanceId) .collect(Collectors.toList()); } logger.info("find instanceId, {} => {}", resourceId, instanceIds); List<Instance> instances = AWS.ec2.describeInstances(instanceIds) .stream().filter(instance -> "running".equals(instance.getState().getName())).collect(Collectors.toList()); if (instances.isEmpty()) throw new Error("can not find any running instance, id=" + resourceId); return instances; }
private static List<Filter> parseFilters(String value) { List<Filter> filters = newArrayList(); for (String filter : SPLITTER.split(value)) { String[] nameValues = filter.split("=", 2); checkArgument(nameValues.length == 2, format("Filter must be 'name=value[,value,value]: %s", filter)); String filterName = nameValues[0].trim(); checkArgument(filterName.length() > 0, format("Filter must be 'name=value[,value,value]: %s", filter)); filters.add(new Filter() .withName(filterName) .withValues(Iterables.toArray(COMMA_SPLITTER.split(nameValues[1]), String.class))); } return filters; }
private Zone getZoneByName(String zoneName) { checkState(!isNullOrEmpty(zoneName)); try { DescribeAvailabilityZonesResult zones = ec2_ .describeAvailabilityZones(new DescribeAvailabilityZonesRequest() .withZoneNames(zoneName) .withFilters(new Filter().withName("region-name").withValues(credentials_.getRegion().getName()))); if (zones != null && zones.getAvailabilityZones().size() == 1) { //available | impaired | unavailable AvailabilityZone availabilityZone = zones.getAvailabilityZones().get(0); return new Zone().setName(availabilityZone.getZoneName()).setRegion(credentials_.getRegion()).setStatus(availabilityZone.getState()); } } catch (AmazonClientException exception) { LOG.debug("Invalid zone [{}]! Error message: [{}]", zoneName, exception.getMessage(), exception); } return null; }
@Override public boolean hasSubnets(String vpcId) throws AutoException { if (StringUtils.isEmpty(vpcId)) { log.info(platform.getPlatformName() + " にvpcIdが有りません"); System.out.println("VPCID_EMPTY"); return false; } DescribeSubnetsRequest request = new DescribeSubnetsRequest(); request.withFilters(new Filter().withName("vpc-id").withValues(vpcId)); DescribeSubnetsResult result = ec2Client.describeSubnets(request); List<Subnet> subnets = result.getSubnets(); if (subnets.isEmpty()) { log.info(platform.getPlatformName() + " にサブネットが有りません"); System.out.println("SUBNET_EMPTY"); return false; } return true; }
/** * {@inheritDoc} */ @Override public List<SecurityGroup> getSecurityGroups(Long userNo, Long platformNo) { // セキュリティグループを取得 AwsProcessClient awsProcessClient = awsProcessClientFactory.createAwsProcessClient(userNo, platformNo); DescribeSecurityGroupsRequest request = new DescribeSecurityGroupsRequest(); PlatformAws platformAws = platformAwsDao.read(platformNo); if (BooleanUtils.isTrue(platformAws.getVpc())) { // VPCの場合、VPC IDが同じものを抽出 request.withFilters(new Filter().withName("vpc-id").withValues(platformAws.getVpcId())); } else { // 非VPCの場合、VPC IDが空のものを抽出 request.withFilters(new Filter().withName("vpc-id").withValues("")); } DescribeSecurityGroupsResult result = awsProcessClient.getEc2Client().describeSecurityGroups(request); List<SecurityGroup> securityGroups = result.getSecurityGroups(); // ソート Collections.sort(securityGroups, Comparators.COMPARATOR_SECURITY_GROUP); return securityGroups; }
/*** * 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; }
/** * Returns all {@link SpotInstanceRequest}s in the pool that are in any of a * given set of states. * * @param inStates * The spot request states of interest. * @return The {@link SpotInstanceRequest}s paired with their * {@link Instance}. * @throws CloudPoolDriverException */ private List<InstancePairedSpotRequest> getPoolSpotRequests(List<String> states) throws CloudPoolDriverException { try { // only include spot requests with cloud pool tag Filter poolFilter = new Filter().withName(ScalingFilters.CLOUD_POOL_TAG_FILTER).withValues(getPoolName()); // only include spot requests in any of the given states Filter stateFilter = new Filter().withName(ScalingFilters.SPOT_REQUEST_STATE_FILTER).withValues(states); List<SpotInstanceRequest> spotRequests = this.client .getSpotInstanceRequests(asList(poolFilter, stateFilter)); List<InstancePairedSpotRequest> requestInstancePairs = pairUpWithInstances(spotRequests); return requestInstancePairs; } catch (Exception e) { throw new CloudPoolDriverException( format("failed to retrieve machines in cloud pool \"%s\": %s", getPoolName(), e.getMessage()), e); } }
@Override public List<Instance> getInstances(List<Filter> filters) throws AmazonClientException { List<Instance> instances = new ArrayList<>(this.instances.values()); // filter out instances that don't match all filters Iterator<Instance> iterator = instances.iterator(); while (iterator.hasNext()) { Instance instance = iterator.next(); for (Filter filter : filters) { if (!matches(filter, instance)) { // filter out iterator.remove(); break; } } } // return sorted on identifier to ease verifications in tests Collections.sort(instances, (o1, o2) -> o1.getInstanceId().compareTo(o2.getInstanceId())); return instances; }
@Override public List<SpotInstanceRequest> getSpotInstanceRequests(Collection<Filter> filters) throws AmazonClientException { List<SpotInstanceRequest> requests = new ArrayList<>(this.spotRequests.values()); // filter out requests that don't match all filters Iterator<SpotInstanceRequest> iterator = requests.iterator(); while (iterator.hasNext()) { SpotInstanceRequest request = iterator.next(); for (Filter filter : filters) { if (!matches(filter, request)) { // filter out iterator.remove(); break; } } } // return sorted on identifier to ease verifications in tests Collections.sort(requests, (o1, o2) -> o1.getSpotInstanceRequestId().compareTo(o2.getSpotInstanceRequestId())); return requests; }
private List<Instance> listGroupInstances(AutoScalingGroup autoScalingGroup) throws Exception { List<String> instanceIds = autoScalingGroup.getInstances().stream() .map(com.amazonaws.services.autoscaling.model.Instance::getInstanceId).collect(Collectors.toList()); if (instanceIds.isEmpty()) { // note: we don't want to call get instances with an emtpy list // since this causes DescribeInstances to get *all* instances in the // region (not just the ones in our Auto Scaling Group, which is // what we want) return new ArrayList<>(); } List<Filter> filters = Collections.emptyList(); Callable<List<Instance>> requester = new GetInstances(getAwsCredentials(), getRegion(), getClientConfig(), instanceIds, filters); int initialDelay = 1; int maxAttempts = 10; // max 2 ^ 9 - 1 seconds = 511 seconds String name = String.format("await-describe-instances"); Retryable<List<Instance>> retryer = Retryers.exponentialBackoffRetryer(name, requester, initialDelay, TimeUnit.SECONDS, maxAttempts, instancesPresent(instanceIds)); return retryer.call(); }
private String autoDetectStackName(String instanceId) { Assert.notNull(instanceId, "No valid instance id defined"); DescribeStackResourcesResult describeStackResourcesResult = this.amazonCloudFormationClient.describeStackResources(new DescribeStackResourcesRequest().withPhysicalResourceId(instanceId)); if (describeStackResourcesResult != null && describeStackResourcesResult.getStackResources() != null && !describeStackResourcesResult.getStackResources().isEmpty()) { return describeStackResourcesResult.getStackResources().get(0).getStackName(); } if (this.amazonEc2Client != null) { DescribeTagsResult describeTagsResult = this.amazonEc2Client.describeTags(new DescribeTagsRequest().withFilters( new Filter("resource-id", Collections.singletonList(instanceId)), new Filter("resource-type", Collections.singletonList("instance")), new Filter("key", Collections.singletonList("aws:cloudformation:stack-name")))); if (describeTagsResult != null && describeTagsResult.getTags() != null && !describeTagsResult.getTags().isEmpty()) { return describeTagsResult.getTags().get(0).getValue(); } } return null; }
@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); } } } }
protected String findNonOverLappingCIDR(AuthenticatedContext ac, CloudStack stack) { AwsNetworkView awsNetworkView = new AwsNetworkView(stack.getNetwork()); String region = ac.getCloudContext().getLocation().getRegion().value(); AmazonEC2Client ec2Client = awsClient.createAccess(new AwsCredentialView(ac.getCloudCredential()), region); DescribeVpcsRequest vpcRequest = new DescribeVpcsRequest().withVpcIds(awsNetworkView.getExistingVPC()); Vpc vpc = ec2Client.describeVpcs(vpcRequest).getVpcs().get(0); String vpcCidr = vpc.getCidrBlock(); LOGGER.info("Subnet cidr is empty, find a non-overlapping subnet for VPC cidr: {}", vpcCidr); DescribeSubnetsRequest request = new DescribeSubnetsRequest().withFilters(new Filter("vpc-id", singletonList(awsNetworkView.getExistingVPC()))); List<Subnet> awsSubnets = ec2Client.describeSubnets(request).getSubnets(); List<String> subnetCidrs = awsSubnets.stream().map(Subnet::getCidrBlock).collect(Collectors.toList()); LOGGER.info("The selected VPCs: {}, has the following subnets: {}", vpc.getVpcId(), subnetCidrs.stream().collect(Collectors.joining(","))); return calculateSubnet(ac.getCloudContext().getName(), vpc, subnetCidrs); }
public AbstractAwsSecurityGroupHostSupplier( AmazonEC2Client client, String groupId, Region region, int defaultPort ) { this.client = client; this.filter = new Filter("group-id", Arrays.asList(groupId)); this.region = region; this.port = defaultPort; if (region == null) { setMyRegion(); } else { client.setRegion(region); } }
@Override public List<AbstractResource<?>> describeReservations(Account account, Region region, DateTime dt, Ec2Filter... filters) { AmazonEC2 ec2 = findClient(account, region); DescribeInstancesRequest req = new DescribeInstancesRequest(); for (Ec2Filter filter : filters) { Filter f = new Filter().withName(filter.getName()).withValues(filter.getValues()); req.withFilters(f); } log.debug("start describing instances for account:{} in region:{} via api", account.getId() + "=>" + account.getName(), region); DescribeInstancesResult res = ec2.describeInstances(req); List<Map<String, Ec2InstanceAttribute>> attributes = new ArrayList<>(); for (Reservation r : res.getReservations()) { Map<String, Ec2InstanceAttribute> attribute = new HashMap<>(); for (Instance i : r.getInstances()) { Boolean terminationProtection = findTerminationProtection(account, region, i.getInstanceId()); String shutdownBehavior = findShutdownBehavior(account, region, i.getInstanceId()); String userData = findUserData(account, region, i.getInstanceId()); Ec2InstanceAttribute ia = new Ec2InstanceAttribute(terminationProtection, shutdownBehavior, userData); attribute.put(i.getInstanceId(), ia); } attributes.add(attribute); } return converter.toEc2Reservations(res.getReservations(), attributes, account.getId(), region, dt); }
@Override public List<AbstractResource<?>> describeAMIs(Account account, Region region, DateTime dt, Ec2Filter... filters) { AmazonEC2 ec2 = findClient(account, region); DescribeImagesRequest req = new DescribeImagesRequest(); for (Ec2Filter filter : filters) { Filter f = new Filter().withName(filter.getName()).withValues(filter.getValues()); req.withFilters(f); } log.debug("start describing amis for account:{} in region:{} via api", account.getId() + "=>" + account.getName(), region); DescribeImagesResult res = ec2.describeImages(req); List<List<LaunchPermission>> imageLaunchPermissions = new ArrayList<>(); for (Image image : res.getImages()) { imageLaunchPermissions.add(findImageLaunchPermissions(account, region, image.getImageId())); } return converter.toEc2AMIs(res.getImages(), imageLaunchPermissions, account.getId(), region, dt); }
@Override public List<AbstractResource<?>> describeVolumes(Account account, Region region, DateTime dt, Ec2Filter... filters) { AmazonEC2 ec2 = findClient(account, region); DescribeVolumesRequest req = new DescribeVolumesRequest(); for (Ec2Filter filter : filters) { Filter f = new Filter().withName(filter.getName()).withValues(filter.getValues()); req.withFilters(f); } log.debug("start describing volumes for account:{} in region:{} via api", account.getId() + "=>" + account.getName(), region); DescribeVolumesResult res = ec2.describeVolumes(req); List<Boolean> autoEnableIOs = new ArrayList<>(); List<List<ProductCode>> productCodes = new ArrayList<>(); for (Volume volume : res.getVolumes()) { autoEnableIOs.add(findAutoEnableIO(account, region, volume.getVolumeId())); productCodes.add(findProductCodes(account, region, volume.getVolumeId())); } return converter.toEc2Volumes(res.getVolumes(), autoEnableIOs, productCodes, account.getId(), region, dt); }
@Override public List<AbstractResource<?>> describeSnapshots(Account account, Region region, DateTime dt, Ec2Filter... filters) { AmazonEC2 ec2 = findClient(account, region); DescribeSnapshotsRequest req = new DescribeSnapshotsRequest(); for (Ec2Filter filter : filters) { Filter f = new Filter().withName(filter.getName()).withValues(filter.getValues()); req.withFilters(f); } log.debug("start describing snapshots for account:{} in region:{} via api", account.getId() + "=>" + account.getName(), region); DescribeSnapshotsResult res = ec2.describeSnapshots(req); List<List<CreateVolumePermission>> createVolumePermissions = new ArrayList<>(); List<List<ProductCode>> productCodes = new ArrayList<>(); for (Snapshot snapShot : res.getSnapshots()) { productCodes.add(findSnapshotProductCodes(account, region, snapShot.getSnapshotId())); createVolumePermissions.add(findCreateVolumePermissions(account, region, snapShot.getSnapshotId())); } return converter.toEc2Snapshots(res.getSnapshots(), createVolumePermissions, productCodes, account.getId(), region, dt); }
@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; } }