Search in sources :

Example 31 with DescribeInstancesRequest

use of software.amazon.awssdk.services.ec2.model.DescribeInstancesRequest in project bazel-buildfarm by bazelbuild.

the class AwsAdmin method getInstanceId.

private Instance getInstanceId(String privateDnsName) {
    DescribeInstancesRequest describeInstancesRequest = new DescribeInstancesRequest().withFilters(new Filter().withName("private-dns-name").withValues(privateDnsName));
    DescribeInstancesResult instancesResult = ec2.describeInstances(describeInstancesRequest);
    for (Reservation r : instancesResult.getReservations()) {
        for (Instance e : r.getInstances()) {
            if (e.getPrivateDnsName() != null && e.getPrivateDnsName().equals(privateDnsName)) {
                return e;
            }
        }
    }
    return null;
}
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) DescribeInstancesRequest(com.amazonaws.services.ec2.model.DescribeInstancesRequest)

Example 32 with DescribeInstancesRequest

use of software.amazon.awssdk.services.ec2.model.DescribeInstancesRequest in project bazel-buildfarm by bazelbuild.

the class AwsAdmin method getHosts.

@Override
public GetHostsResult getHosts(String filter, int ageInMinutes, String status) {
    GetHostsResult.Builder resultBuilder = GetHostsResult.newBuilder();
    List<Host> hosts = new ArrayList<>();
    DescribeInstancesResult instancesResult = ec2.describeInstances(new DescribeInstancesRequest().withFilters(new Filter().withName("tag-value").withValues(filter)));
    long hostNum = 1L;
    for (Reservation r : instancesResult.getReservations()) {
        for (Instance e : r.getInstances()) {
            long uptime = getHostUptimeInMinutes(e.getLaunchTime());
            if (e.getPrivateIpAddress() != null && uptime > ageInMinutes && status.equalsIgnoreCase(e.getState().getName())) {
                Host.Builder hostBuilder = Host.newBuilder();
                hostBuilder.setHostNum(hostNum++);
                hostBuilder.setDnsName(e.getPrivateDnsName());
                hostBuilder.setHostId(e.getInstanceId());
                hostBuilder.setIpAddress(e.getPrivateIpAddress());
                hostBuilder.setLaunchTime(Timestamps.fromMillis(e.getLaunchTime().getTime()));
                hostBuilder.setLifecycle(e.getInstanceLifecycle() != null ? e.getInstanceLifecycle() : "on demand");
                hostBuilder.setNumCores(e.getCpuOptions().getCoreCount());
                hostBuilder.setState(e.getState().getName());
                hostBuilder.setType(e.getInstanceType());
                hostBuilder.setUptimeMinutes(uptime);
                hosts.add(hostBuilder.build());
            }
        }
    }
    resultBuilder.addAllHosts(hosts);
    resultBuilder.setNumHosts(hosts.size());
    logger.log(Level.FINE, String.format("Got %d hosts for filter: %s", hosts.size(), filter));
    return resultBuilder.build();
}
Also used : DescribeInstancesResult(com.amazonaws.services.ec2.model.DescribeInstancesResult) GetHostsResult(build.buildfarm.v1test.GetHostsResult) 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) Host(build.buildfarm.v1test.Host) DescribeInstancesRequest(com.amazonaws.services.ec2.model.DescribeInstancesRequest)

Example 33 with DescribeInstancesRequest

use of software.amazon.awssdk.services.ec2.model.DescribeInstancesRequest in project bazel-buildfarm by bazelbuild.

the class AdminServiceImpl method getInstanceIdByPrivateDnsName.

@Override
public String getInstanceIdByPrivateDnsName(String dnsName) {
    Filter filter = new Filter().withName("private-dns-name").withValues(dnsName);
    DescribeInstancesRequest describeInstancesRequest = new DescribeInstancesRequest().withFilters(filter);
    DescribeInstancesResult instancesResult = ec2.describeInstances(describeInstancesRequest);
    for (Reservation r : instancesResult.getReservations()) {
        for (com.amazonaws.services.ec2.model.Instance e : r.getInstances()) {
            if (e.getPrivateDnsName() != null && e.getPrivateDnsName().equals(dnsName)) {
                return e.getInstanceId();
            }
        }
    }
    return null;
}
Also used : DescribeInstancesResult(com.amazonaws.services.ec2.model.DescribeInstancesResult) Reservation(com.amazonaws.services.ec2.model.Reservation) Filter(com.amazonaws.services.ec2.model.Filter) DescribeInstancesRequest(com.amazonaws.services.ec2.model.DescribeInstancesRequest)

Example 34 with DescribeInstancesRequest

use of software.amazon.awssdk.services.ec2.model.DescribeInstancesRequest in project solarnetwork-central by SolarNetwork.

the class AwsVirtualMachineBiz method virtualMachinesForIds.

@Override
public Iterable<VirtualMachine> virtualMachinesForIds(Set<String> ids) {
    AmazonEC2 client = getEc2Client();
    DescribeInstancesRequest req = new DescribeInstancesRequest().withInstanceIds(ids);
    DescribeInstancesResult res;
    List<VirtualMachine> results = new ArrayList<>(ids.size());
    do {
        res = client.describeInstances(req);
        for (Reservation reservation : res.getReservations()) {
            for (Instance inst : reservation.getInstances()) {
                results.add(new Ec2VirtualMachine(inst));
            }
        }
        req.setNextToken(res.getNextToken());
    } while (res.getNextToken() != null);
    return results;
}
Also used : DescribeInstancesResult(com.amazonaws.services.ec2.model.DescribeInstancesResult) Reservation(com.amazonaws.services.ec2.model.Reservation) Instance(com.amazonaws.services.ec2.model.Instance) ArrayList(java.util.ArrayList) AmazonEC2(com.amazonaws.services.ec2.AmazonEC2) DescribeInstancesRequest(com.amazonaws.services.ec2.model.DescribeInstancesRequest) Ec2VirtualMachine(net.solarnetwork.central.cloud.aws.domain.Ec2VirtualMachine) VirtualMachine(net.solarnetwork.central.cloud.domain.VirtualMachine) Ec2VirtualMachine(net.solarnetwork.central.cloud.aws.domain.Ec2VirtualMachine)

Example 35 with DescribeInstancesRequest

use of software.amazon.awssdk.services.ec2.model.DescribeInstancesRequest in project ignite-extensions by apache.

the class TcpDiscoveryElbIpFinder method getRegisteredAddresses.

/**
 * {@inheritDoc}
 */
@Override
public Collection<InetSocketAddress> getRegisteredAddresses() throws IgniteSpiException {
    initClients();
    List<String> instanceIds = new ArrayList<>();
    DescribeLoadBalancersRequest req = new DescribeLoadBalancersRequest().withLoadBalancerNames(loadBalancerName);
    List<LoadBalancerDescription> descs = amazonELBClient.describeLoadBalancers(req).getLoadBalancerDescriptions();
    for (LoadBalancerDescription desc : descs) {
        for (Instance instance : desc.getInstances()) instanceIds.add(instance.getInstanceId());
    }
    DescribeInstancesRequest instReq = new DescribeInstancesRequest().withInstanceIds(instanceIds);
    List<Reservation> reservations = amazonEC2Client.describeInstances(instReq).getReservations();
    List<InetSocketAddress> addrs = new ArrayList<>();
    for (Reservation reservation : reservations) {
        List<com.amazonaws.services.ec2.model.Instance> instances = reservation.getInstances();
        for (com.amazonaws.services.ec2.model.Instance instance : instances) addrs.add(new InetSocketAddress(instance.getPrivateIpAddress(), 0));
    }
    return addrs;
}
Also used : Instance(com.amazonaws.services.elasticloadbalancing.model.Instance) InetSocketAddress(java.net.InetSocketAddress) ArrayList(java.util.ArrayList) DescribeInstancesRequest(com.amazonaws.services.ec2.model.DescribeInstancesRequest) DescribeLoadBalancersRequest(com.amazonaws.services.elasticloadbalancing.model.DescribeLoadBalancersRequest) Reservation(com.amazonaws.services.ec2.model.Reservation) LoadBalancerDescription(com.amazonaws.services.elasticloadbalancing.model.LoadBalancerDescription)

Aggregations

DescribeInstancesRequest (com.amazonaws.services.ec2.model.DescribeInstancesRequest)74 DescribeInstancesResult (com.amazonaws.services.ec2.model.DescribeInstancesResult)49 Reservation (com.amazonaws.services.ec2.model.Reservation)47 Instance (com.amazonaws.services.ec2.model.Instance)42 ArrayList (java.util.ArrayList)40 Filter (com.amazonaws.services.ec2.model.Filter)29 List (java.util.List)28 Map (java.util.Map)18 AmazonEC2 (com.amazonaws.services.ec2.AmazonEC2)17 Collectors (java.util.stream.Collectors)17 Tag (com.amazonaws.services.ec2.model.Tag)13 Inject (javax.inject.Inject)13 Set (java.util.Set)12 Logger (org.slf4j.Logger)12 LoggerFactory (org.slf4j.LoggerFactory)12 TerminateInstancesRequest (com.amazonaws.services.ec2.model.TerminateInstancesRequest)11 TerminateInstancesResult (com.amazonaws.services.ec2.model.TerminateInstancesResult)11 Collection (java.util.Collection)11 DescribeVolumesResult (com.amazonaws.services.ec2.model.DescribeVolumesResult)10 StopInstancesRequest (com.amazonaws.services.ec2.model.StopInstancesRequest)10