Search in sources :

Example 66 with Instance

use of com.google.api.services.notebooks.v1.model.Instance in project cloudbreak by hortonworks.

the class EC2ClientActions method listTagsByInstanceId.

public Map<String, Map<String, String>> listTagsByInstanceId(List<String> instanceIds) {
    DescribeInstancesRequest describeInstancesRequest = new DescribeInstancesRequest().withInstanceIds(instanceIds);
    DescribeInstancesResult describeInstancesResult = buildEC2Client().describeInstances(describeInstancesRequest);
    return describeInstancesResult.getReservations().stream().flatMap(reservation -> reservation.getInstances().stream()).collect(Collectors.toMap(Instance::getInstanceId, this::getTagsForInstance));
}
Also used : DescribeInstancesResult(com.amazonaws.services.ec2.model.DescribeInstancesResult) EC2UnexpectedException(com.amazonaws.services.lambda.model.EC2UnexpectedException) EbsInstanceBlockDevice(com.amazonaws.services.ec2.model.EbsInstanceBlockDevice) StopInstancesResult(com.amazonaws.services.ec2.model.StopInstancesResult) WaiterParameters(com.amazonaws.waiters.WaiterParameters) LoggerFactory(org.slf4j.LoggerFactory) TerminateInstancesResult(com.amazonaws.services.ec2.model.TerminateInstancesResult) BooleanUtils(org.apache.commons.lang3.BooleanUtils) StopInstancesRequest(com.amazonaws.services.ec2.model.StopInstancesRequest) ArrayList(java.util.ArrayList) Inject(javax.inject.Inject) EC2Client(com.sequenceiq.it.cloudbreak.util.aws.amazonec2.client.EC2Client) InstanceBlockDeviceMapping(com.amazonaws.services.ec2.model.InstanceBlockDeviceMapping) TestFailException(com.sequenceiq.it.cloudbreak.exception.TestFailException) DescribeInstancesRequest(com.amazonaws.services.ec2.model.DescribeInstancesRequest) Map(java.util.Map) AmazonEC2(com.amazonaws.services.ec2.AmazonEC2) DescribeVolumesRequest(com.amazonaws.services.ec2.model.DescribeVolumesRequest) Volume(com.amazonaws.services.ec2.model.Volume) InstanceState(com.amazonaws.services.ec2.model.InstanceState) Instance(com.amazonaws.services.ec2.model.Instance) WaiterUnrecoverableException(com.amazonaws.waiters.WaiterUnrecoverableException) SdxUtil(com.sequenceiq.it.cloudbreak.util.SdxUtil) Reservation(com.amazonaws.services.ec2.model.Reservation) FixedDelayStrategy(com.amazonaws.waiters.FixedDelayStrategy) Logger(org.slf4j.Logger) Collection(java.util.Collection) DescribeVolumesResult(com.amazonaws.services.ec2.model.DescribeVolumesResult) Log(com.sequenceiq.it.cloudbreak.log.Log) Set(java.util.Set) DescribeInstancesResult(com.amazonaws.services.ec2.model.DescribeInstancesResult) WaiterTimedOutException(com.amazonaws.waiters.WaiterTimedOutException) TerminateInstancesRequest(com.amazonaws.services.ec2.model.TerminateInstancesRequest) Collectors(java.util.stream.Collectors) String.format(java.lang.String.format) Objects(java.util.Objects) MaxAttemptsRetryStrategy(com.amazonaws.waiters.MaxAttemptsRetryStrategy) List(java.util.List) Component(org.springframework.stereotype.Component) PollingStrategy(com.amazonaws.waiters.PollingStrategy) Tag(com.amazonaws.services.ec2.model.Tag) DescribeInstancesRequest(com.amazonaws.services.ec2.model.DescribeInstancesRequest)

Example 67 with Instance

use of com.google.api.services.notebooks.v1.model.Instance in project cloudbreak by hortonworks.

the class EC2ClientActions method stopHostGroupInstances.

public void stopHostGroupInstances(List<String> instanceIds) {
    AmazonEC2 ec2Client = buildEC2Client();
    StopInstancesResult stopInstancesResult = ec2Client.stopInstances(new StopInstancesRequest().withInstanceIds(instanceIds));
    for (String instanceId : instanceIds) {
        try {
            Log.log(LOGGER, format(" EC2 instance [%s] state is [%s] ", instanceId, Objects.requireNonNull(stopInstancesResult.getStoppingInstances().stream().filter(instance -> instance.getInstanceId().equals(instanceId)).findAny().orElse(null)).getCurrentState().getName()));
            ec2Client.waiters().instanceStopped().run(new WaiterParameters<DescribeInstancesRequest>(new DescribeInstancesRequest().withInstanceIds(instanceId)).withPollingStrategy(new PollingStrategy(new MaxAttemptsRetryStrategy(80), new FixedDelayStrategy(30))));
            DescribeInstancesResult describeInstanceResult = ec2Client.describeInstances(new DescribeInstancesRequest().withInstanceIds(instanceId));
            InstanceState actualInstanceState = describeInstanceResult.getReservations().get(0).getInstances().get(0).getState();
            if (STOPPED_STATE.equals(actualInstanceState.getName())) {
                Log.log(LOGGER, format(" EC2 Instance: %s state is: %s ", instanceId, STOPPED_STATE));
            } else {
                LOGGER.error("EC2 Instance: {} stop has not been successful. So the actual state is: {} ", instanceId, actualInstanceState.getName());
                throw new TestFailException(" EC2 Instance: " + instanceId + " stop has not been successful, because of the actual state is: " + actualInstanceState.getName());
            }
        } catch (WaiterUnrecoverableException e) {
            LOGGER.error("EC2 Instance {} stop has not been successful, because of WaiterUnrecoverableException: {}", instanceId, e);
        } catch (WaiterTimedOutException e) {
            LOGGER.error("EC2 Instance {} stop has not been successful, because of WaiterTimedOutException: {}", instanceId, e);
        } catch (EC2UnexpectedException e) {
            LOGGER.error("EC2 Instance {} stop has not been successful, because of EC2UnexpectedException: {}", instanceId, e);
        }
    }
}
Also used : EC2UnexpectedException(com.amazonaws.services.lambda.model.EC2UnexpectedException) EbsInstanceBlockDevice(com.amazonaws.services.ec2.model.EbsInstanceBlockDevice) StopInstancesResult(com.amazonaws.services.ec2.model.StopInstancesResult) WaiterParameters(com.amazonaws.waiters.WaiterParameters) LoggerFactory(org.slf4j.LoggerFactory) TerminateInstancesResult(com.amazonaws.services.ec2.model.TerminateInstancesResult) BooleanUtils(org.apache.commons.lang3.BooleanUtils) StopInstancesRequest(com.amazonaws.services.ec2.model.StopInstancesRequest) ArrayList(java.util.ArrayList) Inject(javax.inject.Inject) EC2Client(com.sequenceiq.it.cloudbreak.util.aws.amazonec2.client.EC2Client) InstanceBlockDeviceMapping(com.amazonaws.services.ec2.model.InstanceBlockDeviceMapping) TestFailException(com.sequenceiq.it.cloudbreak.exception.TestFailException) DescribeInstancesRequest(com.amazonaws.services.ec2.model.DescribeInstancesRequest) Map(java.util.Map) AmazonEC2(com.amazonaws.services.ec2.AmazonEC2) DescribeVolumesRequest(com.amazonaws.services.ec2.model.DescribeVolumesRequest) Volume(com.amazonaws.services.ec2.model.Volume) InstanceState(com.amazonaws.services.ec2.model.InstanceState) Instance(com.amazonaws.services.ec2.model.Instance) WaiterUnrecoverableException(com.amazonaws.waiters.WaiterUnrecoverableException) SdxUtil(com.sequenceiq.it.cloudbreak.util.SdxUtil) Reservation(com.amazonaws.services.ec2.model.Reservation) FixedDelayStrategy(com.amazonaws.waiters.FixedDelayStrategy) Logger(org.slf4j.Logger) Collection(java.util.Collection) DescribeVolumesResult(com.amazonaws.services.ec2.model.DescribeVolumesResult) Log(com.sequenceiq.it.cloudbreak.log.Log) Set(java.util.Set) DescribeInstancesResult(com.amazonaws.services.ec2.model.DescribeInstancesResult) WaiterTimedOutException(com.amazonaws.waiters.WaiterTimedOutException) TerminateInstancesRequest(com.amazonaws.services.ec2.model.TerminateInstancesRequest) Collectors(java.util.stream.Collectors) String.format(java.lang.String.format) Objects(java.util.Objects) MaxAttemptsRetryStrategy(com.amazonaws.waiters.MaxAttemptsRetryStrategy) List(java.util.List) Component(org.springframework.stereotype.Component) PollingStrategy(com.amazonaws.waiters.PollingStrategy) Tag(com.amazonaws.services.ec2.model.Tag) EC2UnexpectedException(com.amazonaws.services.lambda.model.EC2UnexpectedException) WaiterTimedOutException(com.amazonaws.waiters.WaiterTimedOutException) MaxAttemptsRetryStrategy(com.amazonaws.waiters.MaxAttemptsRetryStrategy) WaiterUnrecoverableException(com.amazonaws.waiters.WaiterUnrecoverableException) AmazonEC2(com.amazonaws.services.ec2.AmazonEC2) DescribeInstancesRequest(com.amazonaws.services.ec2.model.DescribeInstancesRequest) WaiterParameters(com.amazonaws.waiters.WaiterParameters) DescribeInstancesResult(com.amazonaws.services.ec2.model.DescribeInstancesResult) InstanceState(com.amazonaws.services.ec2.model.InstanceState) PollingStrategy(com.amazonaws.waiters.PollingStrategy) StopInstancesResult(com.amazonaws.services.ec2.model.StopInstancesResult) TestFailException(com.sequenceiq.it.cloudbreak.exception.TestFailException) StopInstancesRequest(com.amazonaws.services.ec2.model.StopInstancesRequest) FixedDelayStrategy(com.amazonaws.waiters.FixedDelayStrategy)

Example 68 with Instance

use of com.google.api.services.notebooks.v1.model.Instance in project incubator-gobblin by apache.

the class AWSSdkClient method getInstancesForGroup.

/**
 * 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;
}
Also used : DescribeInstancesResult(com.amazonaws.services.ec2.model.DescribeInstancesResult) Reservation(com.amazonaws.services.ec2.model.Reservation) Filter(com.amazonaws.services.ec2.model.Filter) Instance(com.amazonaws.services.ec2.model.Instance) ArrayList(java.util.ArrayList) AmazonEC2(com.amazonaws.services.ec2.AmazonEC2) DescribeInstancesRequest(com.amazonaws.services.ec2.model.DescribeInstancesRequest)

Example 69 with Instance

use of com.google.api.services.notebooks.v1.model.Instance in project druid by apache.

the class EC2AutoScalerTest method setUp.

@Before
public void setUp() {
    amazonEC2Client = EasyMock.createMock(AmazonEC2Client.class);
    describeInstancesResult = EasyMock.createMock(DescribeInstancesResult.class);
    reservation = EasyMock.createMock(Reservation.class);
    instance = new Instance().withInstanceId(INSTANCE_ID).withLaunchTime(new Date()).withImageId(AMI_ID).withPrivateIpAddress(IP);
    managementConfig = new SimpleWorkerProvisioningConfig().setWorkerPort(8080).setWorkerVersion("");
}
Also used : AmazonEC2Client(com.amazonaws.services.ec2.AmazonEC2Client) DescribeInstancesResult(com.amazonaws.services.ec2.model.DescribeInstancesResult) Reservation(com.amazonaws.services.ec2.model.Reservation) Instance(com.amazonaws.services.ec2.model.Instance) SimpleWorkerProvisioningConfig(org.apache.druid.indexing.overlord.autoscaling.SimpleWorkerProvisioningConfig) Date(java.util.Date) Before(org.junit.Before)

Example 70 with Instance

use of com.google.api.services.notebooks.v1.model.Instance in project openvidu-loadtest by OpenVidu.

the class Ec2Client method terminateAllInstances.

public void terminateAllInstances() {
    Filter workerTagFilter = getTagWorkerFilter();
    List<Instance> instancesToTerminate = getInstanceWithFilters(workerTagFilter);
    List<String> instancesToTerminateIds = getInstanceIds(instancesToTerminate);
    terminateInstance(instancesToTerminateIds);
}
Also used : Filter(com.amazonaws.services.ec2.model.Filter) Instance(com.amazonaws.services.ec2.model.Instance)

Aggregations

Instance (com.amazonaws.services.ec2.model.Instance)185 Reservation (com.amazonaws.services.ec2.model.Reservation)84 ArrayList (java.util.ArrayList)82 DescribeInstancesResult (com.amazonaws.services.ec2.model.DescribeInstancesResult)71 DescribeInstancesRequest (com.amazonaws.services.ec2.model.DescribeInstancesRequest)48 List (java.util.List)48 Tag (com.amazonaws.services.ec2.model.Tag)41 Test (org.junit.jupiter.api.Test)38 CloudInstance (com.sequenceiq.cloudbreak.cloud.model.CloudInstance)36 Map (java.util.Map)36 Collectors (java.util.stream.Collectors)32 HashMap (java.util.HashMap)26 Filter (com.amazonaws.services.ec2.model.Filter)25 Set (java.util.Set)23 CloudResource (com.sequenceiq.cloudbreak.cloud.model.CloudResource)22 CloudVmMetaDataStatus (com.sequenceiq.cloudbreak.cloud.model.CloudVmMetaDataStatus)20 AmazonEC2 (com.amazonaws.services.ec2.AmazonEC2)18 InstanceState (com.amazonaws.services.ec2.model.InstanceState)18 Volume (com.amazonaws.services.ec2.model.Volume)18 ComputeState (com.vmware.photon.controller.model.resources.ComputeService.ComputeState)18