Search in sources :

Example 1 with AWSClient

use of com.netflix.simianarmy.client.aws.AWSClient in project SimianArmy by Netflix.

the class AWSClusterCrawler method clusters.

/**
 * In this implementation, every auto scaling group is considered a cluster.
 * @param clusterNames
 *          the cluster names
 * @return the list of clusters matching the names, when names are empty, return all clusters
 */
@Override
public List<Cluster> clusters(String... clusterNames) {
    List<Cluster> list = Lists.newArrayList();
    for (Map.Entry<String, AWSClient> entry : regionToAwsClient.entrySet()) {
        String region = entry.getKey();
        AWSClient awsClient = entry.getValue();
        Set<String> asgInstances = Sets.newHashSet();
        LOGGER.info(String.format("Crawling clusters in region %s", region));
        for (AutoScalingGroup asg : awsClient.describeAutoScalingGroups(clusterNames)) {
            List<String> instances = Lists.newArrayList();
            for (Instance instance : asg.getInstances()) {
                instances.add(instance.getInstanceId());
                asgInstances.add(instance.getInstanceId());
            }
            com.netflix.simianarmy.conformity.AutoScalingGroup conformityAsg = new com.netflix.simianarmy.conformity.AutoScalingGroup(asg.getAutoScalingGroupName(), instances.toArray(new String[instances.size()]));
            for (SuspendedProcess sp : asg.getSuspendedProcesses()) {
                if ("AddToLoadBalancer".equals(sp.getProcessName())) {
                    LOGGER.info(String.format("ASG %s is suspended: %s", asg.getAutoScalingGroupName(), asg.getSuspendedProcesses()));
                    conformityAsg.setSuspended(true);
                }
            }
            Cluster cluster = new Cluster(asg.getAutoScalingGroupName(), region, conformityAsg);
            List<TagDescription> tagDescriptions = asg.getTags();
            for (TagDescription tagDescription : tagDescriptions) {
                if (BasicSimianArmyContext.GLOBAL_OWNER_TAGKEY.equalsIgnoreCase(tagDescription.getKey())) {
                    String value = tagDescription.getValue();
                    if (value != null) {
                        cluster.setOwnerEmail(value);
                    }
                }
            }
            updateCluster(cluster);
            list.add(cluster);
        }
        // Cluster containing all solo instances
        Set<String> instances = Sets.newHashSet();
        for (com.amazonaws.services.ec2.model.Instance awsInstance : awsClient.describeInstances()) {
            if (!asgInstances.contains(awsInstance.getInstanceId())) {
                LOGGER.info(String.format("Adding instance %s to soloInstances cluster.", awsInstance.getInstanceId()));
                instances.add(awsInstance.getInstanceId());
            }
        }
        // Only create cluster if we have solo instances.
        if (!instances.isEmpty()) {
            Cluster cluster = new Cluster("SoloInstances", region, instances);
            updateCluster(cluster);
            list.add(cluster);
        }
    }
    return list;
}
Also used : AutoScalingGroup(com.amazonaws.services.autoscaling.model.AutoScalingGroup) Instance(com.amazonaws.services.autoscaling.model.Instance) TagDescription(com.amazonaws.services.autoscaling.model.TagDescription) Cluster(com.netflix.simianarmy.conformity.Cluster) AWSClient(com.netflix.simianarmy.client.aws.AWSClient) SuspendedProcess(com.amazonaws.services.autoscaling.model.SuspendedProcess) Map(java.util.Map)

Example 2 with AWSClient

use of com.netflix.simianarmy.client.aws.AWSClient in project SimianArmy by Netflix.

the class InstanceInSecurityGroup method getInstanceSecurityGroups.

/**
 * Gets the security groups for a list of instance ids of the same region. The default implementation
 * is using an AWS client. The method can be overridden in subclasses to get the security groups differently.
 * @param region
 *      the region of the instances
 * @param instanceIds
 *      the instance ids, all instances should be in the same region.
 * @return
 *      the map from instance id to the list of security group names the instance has
 */
protected Map<String, List<String>> getInstanceSecurityGroups(String region, String... instanceIds) {
    Map<String, List<String>> result = Maps.newHashMap();
    if (instanceIds == null || instanceIds.length == 0) {
        return result;
    }
    AWSClient awsClient = new AWSClient(region, awsCredentialsProvider);
    for (Instance instance : awsClient.describeInstances(instanceIds)) {
        // Ignore instances that are in VPC
        if (StringUtils.isNotEmpty(instance.getVpcId())) {
            LOGGER.info(String.format("Instance %s is in VPC and is ignored.", instance.getInstanceId()));
            continue;
        }
        if (!"running".equals(instance.getState().getName())) {
            LOGGER.info(String.format("Instance %s is not running, state is %s.", instance.getInstanceId(), instance.getState().getName()));
            continue;
        }
        List<String> sgs = Lists.newArrayList();
        for (GroupIdentifier groupId : instance.getSecurityGroups()) {
            sgs.add(groupId.getGroupName());
        }
        result.put(instance.getInstanceId(), sgs);
    }
    return result;
}
Also used : Instance(com.amazonaws.services.ec2.model.Instance) List(java.util.List) AWSClient(com.netflix.simianarmy.client.aws.AWSClient) GroupIdentifier(com.amazonaws.services.ec2.model.GroupIdentifier)

Example 3 with AWSClient

use of com.netflix.simianarmy.client.aws.AWSClient in project SimianArmy by Netflix.

the class SameZonesInElbAndAsg method getAwsClient.

private AWSClient getAwsClient(String region) {
    AWSClient awsClient = regionToAwsClient.get(region);
    if (awsClient == null) {
        awsClient = new AWSClient(region, awsCredentialsProvider);
        regionToAwsClient.put(region, awsClient);
    }
    return awsClient;
}
Also used : AWSClient(com.netflix.simianarmy.client.aws.AWSClient)

Example 4 with AWSClient

use of com.netflix.simianarmy.client.aws.AWSClient in project SimianArmy by Netflix.

the class EBSSnapshotJanitorCrawler method getSnapshotResources.

private List<Resource> getSnapshotResources(String... snapshotIds) {
    refreshSnapshotToAMIs();
    List<Resource> resources = new LinkedList<Resource>();
    AWSClient awsClient = getAWSClient();
    for (Snapshot snapshot : awsClient.describeSnapshots(snapshotIds)) {
        Resource snapshotResource = new AWSResource().withId(snapshot.getSnapshotId()).withRegion(getAWSClient().region()).withResourceType(AWSResourceType.EBS_SNAPSHOT).withLaunchTime(snapshot.getStartTime()).withDescription(snapshot.getDescription());
        for (Tag tag : snapshot.getTags()) {
            LOGGER.debug(String.format("Adding tag %s = %s to resource %s", tag.getKey(), tag.getValue(), snapshotResource.getId()));
            snapshotResource.setTag(tag.getKey(), tag.getValue());
        }
        snapshotResource.setOwnerEmail(getOwnerEmailForResource(snapshotResource));
        ((AWSResource) snapshotResource).setAWSResourceState(snapshot.getState());
        Collection<String> amis = snapshotToAMIs.get(snapshotResource.getId());
        if (amis != null) {
            snapshotResource.setAdditionalField(SNAPSHOT_FIELD_AMIS, StringUtils.join(amis, ","));
        }
        resources.add(snapshotResource);
    }
    return resources;
}
Also used : Snapshot(com.amazonaws.services.ec2.model.Snapshot) AWSResource(com.netflix.simianarmy.aws.AWSResource) Resource(com.netflix.simianarmy.Resource) AWSResource(com.netflix.simianarmy.aws.AWSResource) AWSClient(com.netflix.simianarmy.client.aws.AWSClient) Tag(com.amazonaws.services.ec2.model.Tag) LinkedList(java.util.LinkedList)

Example 5 with AWSClient

use of com.netflix.simianarmy.client.aws.AWSClient in project SimianArmy by Netflix.

the class CrossZoneLoadBalancing method getAwsClient.

private AWSClient getAwsClient(String region) {
    AWSClient awsClient = regionToAwsClient.get(region);
    if (awsClient == null) {
        awsClient = new AWSClient(region, awsCredentialsProvider);
        regionToAwsClient.put(region, awsClient);
    }
    return awsClient;
}
Also used : AWSClient(com.netflix.simianarmy.client.aws.AWSClient)

Aggregations

AWSClient (com.netflix.simianarmy.client.aws.AWSClient)37 Resource (com.netflix.simianarmy.Resource)18 Test (org.testng.annotations.Test)15 AutoScalingGroup (com.amazonaws.services.autoscaling.model.AutoScalingGroup)11 AWSResource (com.netflix.simianarmy.aws.AWSResource)10 Instance (com.amazonaws.services.ec2.model.Instance)7 LoadBalancerDescription (com.amazonaws.services.elasticloadbalancing.model.LoadBalancerDescription)6 AutoScalingInstanceDetails (com.amazonaws.services.autoscaling.model.AutoScalingInstanceDetails)5 LinkedList (java.util.LinkedList)4 Tag (com.amazonaws.services.ec2.model.Tag)3 Instance (com.amazonaws.services.autoscaling.model.Instance)2 LaunchConfiguration (com.amazonaws.services.autoscaling.model.LaunchConfiguration)2 SuspendedProcess (com.amazonaws.services.autoscaling.model.SuspendedProcess)2 TagDescription (com.amazonaws.services.autoscaling.model.TagDescription)2 AmazonSimpleDB (com.amazonaws.services.simpledb.AmazonSimpleDB)2 AWSResourceType (com.netflix.simianarmy.aws.AWSResourceType)2 Cluster (com.netflix.simianarmy.conformity.Cluster)2 GroupIdentifier (com.amazonaws.services.ec2.model.GroupIdentifier)1 Snapshot (com.amazonaws.services.ec2.model.Snapshot)1 Volume (com.amazonaws.services.ec2.model.Volume)1