Search in sources :

Example 11 with JsonNode

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()));
}
Also used : JsonNode(org.codehaus.jackson.JsonNode) IOException(java.io.IOException)

Example 12 with JsonNode

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;
}
Also used : JsonNode(org.codehaus.jackson.JsonNode) IOException(java.io.IOException)

Example 13 with 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));
            }
        }
    }
}
Also used : AWSResource(com.netflix.simianarmy.aws.AWSResource) Resource(com.netflix.simianarmy.Resource) JsonNode(org.codehaus.jackson.JsonNode) IOException(java.io.IOException)

Example 14 with JsonNode

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;
}
Also used : AWSResource(com.netflix.simianarmy.aws.AWSResource) Resource(com.netflix.simianarmy.Resource) JsonNode(org.codehaus.jackson.JsonNode)

Example 15 with JsonNode

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()));
}
Also used : JsonNode(org.codehaus.jackson.JsonNode)

Aggregations

JsonNode (org.codehaus.jackson.JsonNode)191 Test (org.junit.Test)51 ObjectMapper (org.codehaus.jackson.map.ObjectMapper)47 IOException (java.io.IOException)39 HashMap (java.util.HashMap)20 ArrayList (java.util.ArrayList)19 HTTP (org.neo4j.test.server.HTTP)18 Resource (com.netflix.simianarmy.Resource)17 AWSResource (com.netflix.simianarmy.aws.AWSResource)17 ObjectNode (org.codehaus.jackson.node.ObjectNode)14 Response (org.neo4j.test.server.HTTP.Response)12 Map (java.util.Map)9 ArrayNode (org.codehaus.jackson.node.ArrayNode)9 RpcException (cz.metacentrum.perun.core.api.exceptions.RpcException)8 ByteArrayOutputStream (java.io.ByteArrayOutputStream)8 JsonParseException (org.neo4j.server.rest.domain.JsonParseException)7 Date (java.util.Date)6 List (java.util.List)6 Description (org.hamcrest.Description)6 TypeSafeMatcher (org.hamcrest.TypeSafeMatcher)6