Search in sources :

Example 6 with Reservation

use of software.amazon.awssdk.services.ec2.model.Reservation in project photon-model by vmware.

the class AWSRemoteCleanup method deleteAwsEc2instances.

private void deleteAwsEc2instances(List<String> vpcIdsToBeDeleted, DescribeInstancesResult describeInstancesResult, AmazonEC2 ec2Client) {
    List<String> instanceIdsToBeDeleted = new ArrayList<>();
    List<Reservation> reservations = describeInstancesResult.getReservations();
    for (Reservation reservation : reservations) {
        List<Instance> instances = reservation.getInstances();
        for (Instance instance : instances) {
            long instanceLaunchTimeMicros = TimeUnit.MILLISECONDS.toMicros(instance.getLaunchTime().getTime());
            long timeDifference = Utils.getNowMicrosUtc() - instanceLaunchTimeMicros;
            if (timeDifference > TimeUnit.HOURS.toMicros(1) && vpcIdsToBeDeleted.contains(instance.getVpcId()) && shouldDelete(instance)) {
                this.host.log(Level.INFO, "Marking %s instance for deletion", instance.getInstanceId());
                instanceIdsToBeDeleted.add(instance.getInstanceId());
            }
        }
    }
    triggerEC2Deletion(instanceIdsToBeDeleted, ec2Client);
}
Also used : Reservation(com.amazonaws.services.ec2.model.Reservation) Instance(com.amazonaws.services.ec2.model.Instance) ArrayList(java.util.ArrayList)

Example 7 with Reservation

use of software.amazon.awssdk.services.ec2.model.Reservation in project photon-model by vmware.

the class AWSComputeDiskDay2Service method getAvailableDeviceName.

private String getAvailableDeviceName(DiskContext context, String instanceId) {
    DescribeInstancesRequest describeInstancesRequest = new DescribeInstancesRequest().withInstanceIds(instanceId);
    DescribeInstancesResult instancesResult = context.amazonEC2Client.describeInstances(describeInstancesRequest);
    List<InstanceBlockDeviceMapping> blockDeviceMappings = null;
    AWSSupportedOS platform = null;
    AWSSupportedVirtualizationTypes virtualizationTypes = null;
    String instanceType = null;
    for (Reservation reservation : instancesResult.getReservations()) {
        for (Instance instance : reservation.getInstances()) {
            if (instance.getInstanceId().equals(instanceId)) {
                blockDeviceMappings = instance.getBlockDeviceMappings();
                platform = AWSSupportedOS.get(instance.getPlatform());
                virtualizationTypes = AWSSupportedVirtualizationTypes.get(instance.getVirtualizationType());
                instanceType = instance.getInstanceType();
                break;
            }
        }
    }
    String deviceName = null;
    if (blockDeviceMappings != null) {
        List<String> usedDeviceNames = getUsedDeviceNames(blockDeviceMappings);
        List<String> availableDiskNames = AWSBlockDeviceNameMapper.getAvailableNames(platform, virtualizationTypes, AWSStorageType.EBS, instanceType, usedDeviceNames);
        deviceName = availableDiskNames.get(0);
    }
    return deviceName;
}
Also used : DescribeInstancesResult(com.amazonaws.services.ec2.model.DescribeInstancesResult) Reservation(com.amazonaws.services.ec2.model.Reservation) Instance(com.amazonaws.services.ec2.model.Instance) AWSSupportedVirtualizationTypes(com.vmware.photon.controller.model.adapters.awsadapter.AWSConstants.AWSSupportedVirtualizationTypes) DescribeInstancesRequest(com.amazonaws.services.ec2.model.DescribeInstancesRequest) InstanceBlockDeviceMapping(com.amazonaws.services.ec2.model.InstanceBlockDeviceMapping) AWSSupportedOS(com.vmware.photon.controller.model.adapters.awsadapter.AWSConstants.AWSSupportedOS)

Example 8 with Reservation

use of software.amazon.awssdk.services.ec2.model.Reservation in project Gatekeeper by FINRAOS.

the class Ec2LookupServiceTests method fakeInstance.

private Reservation fakeInstance(String instanceID, String instanceIP, String instanceName, String instanceApplication, String platform) {
    Reservation container = new Reservation();
    List<Instance> instances = new ArrayList<>();
    Instance i = new Instance();
    List<Tag> tags = new ArrayList<>();
    i.setInstanceId(instanceID);
    i.setPrivateIpAddress(instanceIP);
    Tag nameTag = new Tag();
    nameTag.setKey("Name");
    nameTag.setValue(instanceName);
    Tag applicationTag = new Tag();
    applicationTag.setKey("Application");
    applicationTag.setValue(instanceApplication);
    tags.add(applicationTag);
    tags.add(nameTag);
    i.setTags(tags);
    i.setPlatform(platform);
    instances.add(i);
    container.setInstances(instances);
    return container;
}
Also used : Reservation(com.amazonaws.services.ec2.model.Reservation) GatekeeperAWSInstance(org.finra.gatekeeper.services.aws.model.GatekeeperAWSInstance) Instance(com.amazonaws.services.ec2.model.Instance) Tag(com.amazonaws.services.ec2.model.Tag)

Example 9 with Reservation

use of software.amazon.awssdk.services.ec2.model.Reservation in project Gatekeeper by FINRAOS.

the class Ec2LookupServiceTests method before.

@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);
}
Also used : DescribeInstancesResult(com.amazonaws.services.ec2.model.DescribeInstancesResult) Reservation(com.amazonaws.services.ec2.model.Reservation) AWSEnvironment(org.finra.gatekeeper.services.aws.model.AWSEnvironment) Before(org.junit.Before)

Example 10 with Reservation

use of software.amazon.awssdk.services.ec2.model.Reservation in project cloudbreak by hortonworks.

the class AwsInstanceConnector method check.

@Override
public List<CloudVmInstanceStatus> check(AuthenticatedContext ac, List<CloudInstance> vms) {
    List<CloudVmInstanceStatus> cloudVmInstanceStatuses = new ArrayList<>();
    for (CloudInstance vm : vms) {
        try {
            String region = ac.getCloudContext().getLocation().getRegion().value();
            DescribeInstancesResult result = awsClient.createAccess(new AwsCredentialView(ac.getCloudCredential()), ac.getCloudContext().getLocation().getRegion().value()).describeInstances(new DescribeInstancesRequest().withInstanceIds(vm.getInstanceId()));
            for (Reservation reservation : result.getReservations()) {
                for (Instance instance : reservation.getInstances()) {
                    if ("Stopped".equalsIgnoreCase(instance.getState().getName())) {
                        LOGGER.info("AWS instance [{}] is in {} state, region: {}, stack: {}", instance.getInstanceId(), instance.getState().getName(), region, ac.getCloudContext().getId());
                        cloudVmInstanceStatuses.add(new CloudVmInstanceStatus(vm, InstanceStatus.STOPPED));
                    } else if ("Running".equalsIgnoreCase(instance.getState().getName())) {
                        LOGGER.info("AWS instance [{}] is in {} state, region: {}, stack: {}", instance.getInstanceId(), instance.getState().getName(), region, ac.getCloudContext().getId());
                        cloudVmInstanceStatuses.add(new CloudVmInstanceStatus(vm, InstanceStatus.STARTED));
                    } else if ("Terminated".equalsIgnoreCase(instance.getState().getName())) {
                        LOGGER.info("AWS instance [{}] is in {} state, region: {}, stack: {}", instance.getInstanceId(), instance.getState().getName(), region, ac.getCloudContext().getId());
                        cloudVmInstanceStatuses.add(new CloudVmInstanceStatus(vm, InstanceStatus.TERMINATED));
                    } else {
                        LOGGER.info("AWS instance [{}] is in {} state, region: {}, stack: {}", instance.getInstanceId(), instance.getState().getName(), region, ac.getCloudContext().getId());
                        cloudVmInstanceStatuses.add(new CloudVmInstanceStatus(vm, InstanceStatus.IN_PROGRESS));
                    }
                }
            }
        } catch (AmazonEC2Exception e) {
            LOGGER.warn("Instance does not exist with this id: {}, original message: {}", vm.getInstanceId(), e.getMessage());
        }
    }
    return cloudVmInstanceStatuses;
}
Also used : DescribeInstancesResult(com.amazonaws.services.ec2.model.DescribeInstancesResult) AwsCredentialView(com.sequenceiq.cloudbreak.cloud.aws.view.AwsCredentialView) Reservation(com.amazonaws.services.ec2.model.Reservation) CloudInstance(com.sequenceiq.cloudbreak.cloud.model.CloudInstance) Instance(com.amazonaws.services.ec2.model.Instance) CloudVmInstanceStatus(com.sequenceiq.cloudbreak.cloud.model.CloudVmInstanceStatus) ArrayList(java.util.ArrayList) CloudInstance(com.sequenceiq.cloudbreak.cloud.model.CloudInstance) DescribeInstancesRequest(com.amazonaws.services.ec2.model.DescribeInstancesRequest) AmazonEC2Exception(com.amazonaws.services.ec2.model.AmazonEC2Exception)

Aggregations

Reservation (com.amazonaws.services.ec2.model.Reservation)39 Instance (com.amazonaws.services.ec2.model.Instance)35 DescribeInstancesResult (com.amazonaws.services.ec2.model.DescribeInstancesResult)23 ArrayList (java.util.ArrayList)21 DescribeInstancesRequest (com.amazonaws.services.ec2.model.DescribeInstancesRequest)16 Filter (com.amazonaws.services.ec2.model.Filter)11 CloudInstance (com.sequenceiq.cloudbreak.cloud.model.CloudInstance)8 Test (org.junit.Test)8 Tag (com.amazonaws.services.ec2.model.Tag)7 Function (com.google.common.base.Function)6 AwsCredentialView (com.sequenceiq.cloudbreak.cloud.aws.view.AwsCredentialView)6 CloudVmMetaDataStatus (com.sequenceiq.cloudbreak.cloud.model.CloudVmMetaDataStatus)6 AmazonAutoScalingClient (com.amazonaws.services.autoscaling.AmazonAutoScalingClient)5 AmazonCloudFormationClient (com.amazonaws.services.cloudformation.AmazonCloudFormationClient)5 AmazonEC2 (com.amazonaws.services.ec2.AmazonEC2)5 CreateTagsRequest (com.amazonaws.services.ec2.model.CreateTagsRequest)5 AuthenticatedContext (com.sequenceiq.cloudbreak.cloud.context.AuthenticatedContext)5 InstanceAuthentication (com.sequenceiq.cloudbreak.cloud.model.InstanceAuthentication)5 InstanceTemplate (com.sequenceiq.cloudbreak.cloud.model.InstanceTemplate)5 Volume (com.sequenceiq.cloudbreak.cloud.model.Volume)5