Search in sources :

Example 1 with Cluster

use of com.netflix.simianarmy.conformity.Cluster in project SimianArmy by Netflix.

the class RDSConformityClusterTracker method deleteClusters.

@Override
public void deleteClusters(Cluster... clusters) {
    Validate.notNull(clusters);
    LOGGER.info(String.format("Deleting %d clusters", clusters.length));
    for (Cluster cluster : clusters) {
        LOGGER.info(String.format("Deleting cluster %s", cluster.getName()));
        String stmt = String.format("delete from %s where %s=? and %s=?", table, Cluster.CLUSTER, Cluster.REGION);
        jdbcTemplate.update(stmt, cluster.getName(), cluster.getRegion());
        LOGGER.info(String.format("Successfully deleted cluster %s", cluster.getName()));
    }
}
Also used : Cluster(com.netflix.simianarmy.conformity.Cluster)

Example 2 with Cluster

use of com.netflix.simianarmy.conformity.Cluster in project SimianArmy by Netflix.

the class RDSConformityClusterTracker method addOrUpdate.

/**
 * {@inheritDoc}
 */
@Override
public void addOrUpdate(Cluster cluster) {
    Cluster orig = getCluster(cluster.getName(), cluster.getRegion());
    LOGGER.debug(String.format("Saving cluster %s to RDB table %s in region %s", cluster.getName(), cluster.getRegion(), table));
    Map<String, String> map = cluster.getFieldToValueMap();
    String conformityJson;
    try {
        conformityJson = new ObjectMapper().writeValueAsString(conformitiesAsMap(cluster));
    } catch (JsonProcessingException e) {
        LOGGER.error("ERROR generating conformities JSON when saving cluster " + cluster.getName() + ", " + cluster.getRegion(), e);
        return;
    }
    if (orig == null) {
        StringBuilder sb = new StringBuilder();
        sb.append("insert into ").append(table);
        sb.append(" (");
        sb.append(Cluster.CLUSTER).append(",");
        sb.append(Cluster.REGION).append(",");
        sb.append(Cluster.OWNER_EMAIL).append(",");
        sb.append(Cluster.IS_CONFORMING).append(",");
        sb.append(Cluster.IS_OPTEDOUT).append(",");
        sb.append(Cluster.UPDATE_TIMESTAMP).append(",");
        sb.append(Cluster.EXCLUDED_RULES).append(",");
        sb.append("conformities").append(",");
        sb.append(Cluster.CONFORMITY_RULES);
        sb.append(") values (?,?,?,?,?,?,?,?,?)");
        LOGGER.debug(String.format("Insert statement is '%s'", sb));
        this.jdbcTemplate.update(sb.toString(), value(map.get(Cluster.CLUSTER)), value(map.get(Cluster.REGION)), emailValue(map.get(Cluster.OWNER_EMAIL)), value(map.get(Cluster.IS_CONFORMING)), value(map.get(Cluster.IS_OPTEDOUT)), value(cluster.getUpdateTime()), value(map.get(Cluster.EXCLUDED_RULES)), value(conformityJson), value(map.get(Cluster.CONFORMITY_RULES)));
    } else {
        StringBuilder sb = new StringBuilder();
        sb.append("update ").append(table).append(" set ");
        sb.append(Cluster.OWNER_EMAIL).append("=?,");
        sb.append(Cluster.IS_CONFORMING).append("=?,");
        sb.append(Cluster.IS_OPTEDOUT).append("=?,");
        sb.append(Cluster.UPDATE_TIMESTAMP).append("=?,");
        sb.append(Cluster.EXCLUDED_RULES).append("=?,");
        sb.append("conformities").append("=?,");
        sb.append(Cluster.CONFORMITY_RULES).append("=? where ");
        sb.append(Cluster.CLUSTER).append("=? and ");
        sb.append(Cluster.REGION).append("=?");
        LOGGER.debug(String.format("Update statement is '%s'", sb));
        this.jdbcTemplate.update(sb.toString(), emailValue(map.get(Cluster.OWNER_EMAIL)), value(map.get(Cluster.IS_CONFORMING)), value(map.get(Cluster.IS_OPTEDOUT)), value(cluster.getUpdateTime()), value(map.get(Cluster.EXCLUDED_RULES)), value(conformityJson), value(map.get(Cluster.CONFORMITY_RULES)), value(cluster.getName()), value(cluster.getRegion()));
    }
    LOGGER.debug("Successfully saved.");
}
Also used : Cluster(com.netflix.simianarmy.conformity.Cluster) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 3 with Cluster

use of com.netflix.simianarmy.conformity.Cluster 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 4 with Cluster

use of com.netflix.simianarmy.conformity.Cluster in project SimianArmy by Netflix.

the class SimpleDBConformityClusterTracker method getCluster.

@Override
public Cluster getCluster(String clusterName, String region) {
    Validate.notEmpty(clusterName);
    Validate.notEmpty(region);
    StringBuilder query = new StringBuilder();
    query.append(String.format("select * from `%s` where cluster = '%s' and region = '%s'", domain, clusterName, region));
    LOGGER.info(String.format("Query is to get the cluster is '%s'", query));
    List<Item> items = querySimpleDBItems(query.toString());
    Validate.isTrue(items.size() <= 1);
    if (items.size() == 0) {
        LOGGER.info(String.format("Not found cluster with name %s in region %s", clusterName, region));
        return null;
    } else {
        Cluster cluster = null;
        try {
            cluster = parseCluster(items.get(0));
        } catch (Exception e) {
            // Ignore the item that cannot be parsed.
            LOGGER.error(String.format("SimpleDB item %s cannot be parsed into a cluster.", items.get(0)));
        }
        return cluster;
    }
}
Also used : Item(com.amazonaws.services.simpledb.model.Item) Cluster(com.netflix.simianarmy.conformity.Cluster)

Example 5 with Cluster

use of com.netflix.simianarmy.conformity.Cluster in project SimianArmy by Netflix.

the class SimpleDBConformityClusterTracker method getClusters.

private List<Cluster> getClusters(Boolean conforming, String... regions) {
    Validate.notNull(regions);
    List<Cluster> clusters = Lists.newArrayList();
    StringBuilder query = new StringBuilder();
    query.append(String.format("select * from `%s` where cluster is not null and ", domain));
    boolean needsAnd = false;
    if (regions.length != 0) {
        query.append(String.format("region in ('%s') ", StringUtils.join(regions, "','")));
        needsAnd = true;
    }
    if (conforming != null) {
        if (needsAnd) {
            query.append(" and ");
        }
        query.append(String.format("isConforming = '%s'", conforming));
    }
    LOGGER.info(String.format("Query to retrieve clusters for regions %s is '%s'", StringUtils.join(regions, "','"), query.toString()));
    List<Item> items = querySimpleDBItems(query.toString());
    for (Item item : items) {
        try {
            clusters.add(parseCluster(item));
        } catch (Exception e) {
            // Ignore the item that cannot be parsed.
            LOGGER.error(String.format("SimpleDB item %s cannot be parsed into a cluster.", item), e);
        }
    }
    LOGGER.info(String.format("Retrieved %d clusters from SimpleDB in domain %s and regions %s", clusters.size(), domain, StringUtils.join(regions, "','")));
    return clusters;
}
Also used : Item(com.amazonaws.services.simpledb.model.Item) Cluster(com.netflix.simianarmy.conformity.Cluster)

Aggregations

Cluster (com.netflix.simianarmy.conformity.Cluster)18 Test (org.testng.annotations.Test)8 ArrayList (java.util.ArrayList)5 RowMapper (org.springframework.jdbc.core.RowMapper)5 Conformity (com.netflix.simianarmy.conformity.Conformity)4 Date (java.util.Date)3 AutoScalingGroup (com.amazonaws.services.autoscaling.model.AutoScalingGroup)2 Item (com.amazonaws.services.simpledb.model.Item)2 AWSClient (com.netflix.simianarmy.client.aws.AWSClient)2 Instance (com.amazonaws.services.autoscaling.model.Instance)1 SuspendedProcess (com.amazonaws.services.autoscaling.model.SuspendedProcess)1 TagDescription (com.amazonaws.services.autoscaling.model.TagDescription)1 DeleteAttributesRequest (com.amazonaws.services.simpledb.model.DeleteAttributesRequest)1 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 AWSClusterCrawler (com.netflix.simianarmy.aws.conformity.crawler.AWSClusterCrawler)1 BasicConfiguration (com.netflix.simianarmy.basic.BasicConfiguration)1 BasicConformityMonkeyContext (com.netflix.simianarmy.basic.conformity.BasicConformityMonkeyContext)1 AutoScalingGroup (com.netflix.simianarmy.conformity.AutoScalingGroup)1 ResultSet (java.sql.ResultSet)1