use of org.codehaus.jackson.JsonNode in project SimianArmy by Netflix.
the class EddaImageJanitorCrawler method refreshAMIsUsedByLC.
private void refreshAMIsUsedByLC() {
usedByLaunchConfig.clear();
for (String region : regions) {
LOGGER.info(String.format("Getting AMIs used by launch configs in region %s", region));
String url = eddaClient.getBaseUrl(region) + "/aws/launchConfigurations;_expand:(imageId)";
JsonNode jsonNode = null;
try {
jsonNode = eddaClient.getJsonNodeFromUrl(url);
} catch (Exception e) {
LOGGER.error(String.format("Failed to get Jason node from edda for AMIs used by launch configs in region %s.", region), e);
}
if (jsonNode == null || !jsonNode.isArray()) {
throw new RuntimeException(String.format("Failed to get valid document from %s, got: %s", url, jsonNode));
}
for (Iterator<JsonNode> it = jsonNode.getElements(); it.hasNext(); ) {
JsonNode img = it.next();
String id = img.get("imageId").getTextValue();
usedByLaunchConfig.add(id);
usedNames.add(imageIdToName.get(id));
}
}
LOGGER.info(String.format("Found %d image ids used by launch config from Edda", usedByLaunchConfig.size()));
}
use of org.codehaus.jackson.JsonNode in project SimianArmy by Netflix.
the class EddaImageJanitorCrawler method getImagesInJson.
private JsonNode getImagesInJson(String region, String... imageIds) {
String url = eddaClient.getBaseUrl(region) + "/aws/images";
if (imageIds != null && imageIds.length != 0) {
url += StringUtils.join(imageIds, ',');
LOGGER.info(String.format("Getting unreferenced AMIs in region %s for %d ids", region, imageIds.length));
} else {
LOGGER.info(String.format("Getting all unreferenced AMIs in region %s", region));
if (StringUtils.isNotBlank(ownerId)) {
url += ";ownerId=" + ownerId;
}
}
url += ";_expand:(imageId,name,description,state,tags:(key,value))";
JsonNode jsonNode = null;
try {
jsonNode = eddaClient.getJsonNodeFromUrl(url);
} catch (Exception e) {
LOGGER.error(String.format("Failed to get Jason node from edda for AMIs in region %s.", region), e);
}
if (jsonNode == null || !jsonNode.isArray()) {
throw new RuntimeException(String.format("Failed to get valid document from %s, got: %s", url, jsonNode));
}
return jsonNode;
}
use of org.codehaus.jackson.JsonNode in project SimianArmy by Netflix.
the class EddaImageJanitorCrawler method updateReferenceTimeByInstance.
private void updateReferenceTimeByInstance(String region, List<Resource> batch, long since) {
LOGGER.info(String.format("Getting the last reference time by instance for batch of size %d", batch.size()));
String batchUrl = getInstanceBatchUrl(region, batch, since);
JsonNode batchResult = null;
Map<String, Resource> idToResource = Maps.newHashMap();
for (Resource resource : batch) {
idToResource.put(resource.getId(), resource);
}
try {
batchResult = eddaClient.getJsonNodeFromUrl(batchUrl);
} catch (IOException e) {
LOGGER.error("Failed to get response for the batch.", e);
}
if (batchResult == null || !batchResult.isArray()) {
throw new RuntimeException(String.format("Failed to get valid document from %s, got: %s", batchUrl, batchResult));
}
for (Iterator<JsonNode> it = batchResult.getElements(); it.hasNext(); ) {
JsonNode elem = it.next();
JsonNode data = elem.get("data");
String imageId = data.get("imageId").getTextValue();
String instanceId = data.get("instanceId").getTextValue();
JsonNode ltimeNode = elem.get("ltime");
if (ltimeNode != null && !ltimeNode.isNull()) {
long ltime = ltimeNode.asLong();
Resource ami = idToResource.get(imageId);
String lastRefTimeByInstance = ami.getAdditionalField(AMI_FIELD_LAST_INSTANCE_REF_TIME);
if (lastRefTimeByInstance == null || Long.parseLong(lastRefTimeByInstance) < ltime) {
LOGGER.info(String.format("The last time that the image %s was referenced by instance %s is %d", imageId, instanceId, ltime));
ami.setAdditionalField(AMI_FIELD_LAST_INSTANCE_REF_TIME, String.valueOf(ltime));
}
}
}
}
use of org.codehaus.jackson.JsonNode in project SimianArmy by Netflix.
the class EddaImageJanitorCrawler method getAMIResourcesInRegion.
private List<Resource> getAMIResourcesInRegion(String region, Collection<String> excludedImageIds, String... imageIds) {
JsonNode jsonNode = getImagesInJson(region, imageIds);
List<Resource> resources = Lists.newArrayList();
for (Iterator<JsonNode> it = jsonNode.getElements(); it.hasNext(); ) {
JsonNode ami = it.next();
String imageId = ami.get("imageId").getTextValue();
Resource resource = parseJsonElementToresource(region, ami);
String name = ami.get("name").getTextValue();
if (excludedImageIds.contains(imageId)) {
LOGGER.info(String.format("Image %s is excluded from being managed by Janitor Monkey, ignore.", imageId));
continue;
}
if (usedByInstance.contains(imageId) || usedByLaunchConfig.contains(imageId)) {
LOGGER.info(String.format("AMI %s is referenced by existing instance or launch configuration.", imageId));
} else {
LOGGER.info(String.format("AMI %s is not referenced by existing instance or launch configuration.", imageId));
if (usedNames.contains(name)) {
LOGGER.info(String.format("The same AMI name %s is used in another region", name));
} else {
resources.add(resource);
}
}
}
long since = DateTime.now().minusDays(daysBack).getMillis();
addLastReferenceInfo(resources, since);
// Mark the base AMIs that are used as the ancestor of other images
for (Resource resource : resources) {
if (ancestorImageIds.contains(resource.getId())) {
resource.setAdditionalField(AMI_FIELD_BASE_IMAGE, "true");
}
}
return resources;
}
use of org.codehaus.jackson.JsonNode in project SimianArmy by Netflix.
the class EddaImageJanitorCrawler method refreshIdToNameMap.
private void refreshIdToNameMap() {
imageIdToName.clear();
for (String region : regions) {
JsonNode jsonNode = getImagesInJson(region);
for (Iterator<JsonNode> it = jsonNode.getElements(); it.hasNext(); ) {
JsonNode ami = it.next();
String imageId = ami.get("imageId").getTextValue();
String name = ami.get("name").getTextValue();
imageIdToName.put(imageId, name);
}
}
LOGGER.info(String.format("Got mapping from image id to name for %d ids", imageIdToName.size()));
}
Aggregations