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()));
}
}
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.");
}
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;
}
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;
}
}
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;
}
Aggregations