Package com.netflix.simianarmy

Examples of com.netflix.simianarmy.Resource


        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;
    }
View Full Code Here


    private Resource parseJsonElementToresource(String region, JsonNode jsonNode) {
        Validate.notNull(jsonNode);

        String imageId = jsonNode.get("imageId").getTextValue();

        Resource resource = new AWSResource().withId(imageId).withRegion(region)
                .withResourceType(AWSResourceType.IMAGE);

        Long creationTime = imageIdToCreationTime.get(imageId);
        if (creationTime != null) {
            resource.setLaunchTime(new Date(creationTime));
        }

        JsonNode tags = jsonNode.get("tags");
        if (tags == null || !tags.isArray() || tags.size() == 0) {
            LOGGER.debug(String.format("No tags is found for %s", resource.getId()));
        } else {
            for (Iterator<JsonNode> it = tags.getElements(); it.hasNext();) {
                JsonNode tag = it.next();
                String key = tag.get("key").getTextValue();
                String value = tag.get("value").getTextValue();
                resource.setTag(key, value);
            }
        }

        JsonNode descNode = jsonNode.get("description");
        if (descNode != null && !descNode.isNull()) {
            String description = descNode.getTextValue();
            resource.setDescription(description);
            String ancestorImageId = getBaseAmiIdFromDescription(description);
            if (ancestorImageId != null && !ancestorImageIds.contains(ancestorImageId)) {
                LOGGER.info(String.format("Found base AMI id %s from description '%s'", ancestorImageId, description));
                ancestorImageIds.add(ancestorImageId);
            }
        }
        ((AWSResource) resource).setAWSResourceState(jsonNode.get("state").getTextValue());

        String owner = getOwnerEmailForResource(resource);
        if (owner != null) {
            resource.setOwnerEmail(owner);
        }
        return resource;
    }
View Full Code Here

            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));
                }
            }
        }
    }
View Full Code Here

            String imageId = data.get("imageId").getTextValue();
            String launchConfigurationName = data.get("launchConfigurationName").getTextValue();
            JsonNode ltimeNode = elem.get("ltime");
            if (ltimeNode != null && !ltimeNode.isNull()) {
                long ltime = ltimeNode.asLong();
                Resource ami = idToResource.get(imageId);
                String lastRefTimeByLC = ami.getAdditionalField(AMI_FIELD_LAST_LC_REF_TIME);
                if (lastRefTimeByLC == null || Long.parseLong(lastRefTimeByLC) < ltime) {
                    LOGGER.info(String.format(
                            "The last time that the image %s was referenced by launch config %s is %d",
                            imageId, launchConfigurationName, ltime));
                    ami.setAdditionalField(AMI_FIELD_LAST_LC_REF_TIME, String.valueOf(ltime));
                }
            }
        }
    }
View Full Code Here

        if (rules.size() == 1) {
            nearestRule = rules.get(0);
        } else {
            Date nearestTerminationTime = null;
            for (Rule rule : rules) {
                Resource clone = resource.cloneResource();
                if (!rule.isValid(clone) && (nearestTerminationTime == null
                        || nearestTerminationTime.after(clone.getExpectedTerminationTime()))) {
                    nearestRule = rule;
                    nearestTerminationTime = clone.getExpectedTerminationTime();
                }
            }
        }
        if (nearestRule != null && !nearestRule.isValid(resource)) {
            LOGGER.info(String.format("Resource %s is marked as a cleanup candidate.", resource.getId()));
View Full Code Here

    public Event optOutResource(String resourceId) {
        return optInOrOutResource(resourceId, false);
    }

    private Event optInOrOutResource(String resourceId, boolean optIn) {
        Resource resource = resourceTracker.getResource(resourceId);
        if (resource == null) {
            return null;
        }
        EventTypes eventType = optIn ? EventTypes.OPT_IN_RESOURCE : EventTypes.OPT_OUT_RESOURCE;
        long timestamp = calendar.now().getTimeInMillis();
        // The same resource can have multiple events, so we add the timestamp to the id.
        Event evt = recorder.newEvent(Type.JANITOR, eventType, region, resourceId + "@" + timestamp);
        recorder.recordEvent(evt);
        resource.setOptOutOfJanitor(!optIn);
        resourceTracker.addOrUpdate(resource);
        return evt;
    }
View Full Code Here

        Validate.isTrue(items.size() <= 1);
        if (items.size() == 0) {
            LOGGER.info(String.format("Not found resource with id %s", resourceId));
            return null;
        } else {
            Resource resource = null;
            try {
                resource = parseResource(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 resource.", items.get(0)));
View Full Code Here

        return additionalFields.keySet();
    }

    @Override
    public Resource cloneResource() {
        Resource clone = new AWSResource()
        .withActualTerminationTime(getActualTerminationTime())
        .withDescription(getDescription())
        .withExpectedTerminationTime(getExpectedTerminationTime())
        .withId(getId())
        .withLaunchTime(getLaunchTime())
        .withMarkTime(getMarkTime())
        .withNnotificationTime(getNotificationTime())
        .withOwnerEmail(getOwnerEmail())
        .withRegion(getRegion())
        .withResourceType(getResourceType())
        .withState(getState())
        .withTerminationReason(getTerminationReason())
        .withOptOutOfJanitor(isOptOutOfJanitor());
        ((AWSResource) clone).setAWSResourceState(awsResourceState);

        ((AWSResource) clone).additionalFields.putAll(additionalFields);

        for (String key : this.getAllTagKeys()) {
            clone.setTag(key, this.getTag(key));
        }

        return clone;
    }
View Full Code Here

        List<Resource> crawledResources = crawler.resources(resourceType);
        LOGGER.info(String.format("Looking for cleanup candidate in %d crawled resources.",
                crawledResources.size()));
        Date now = calendar.now().getTime();
        for (Resource resource : crawledResources) {
            Resource trackedResource = trackedMarkedResources.get(resource.getId());
            if (!ruleEngine.isValid(resource)) {
                // If the resource is already marked, ignore it
                if (trackedResource != null) {
                    LOGGER.debug(String.format("Resource %s is already marked.", resource.getId()));
                    continue;
View Full Code Here

TOP

Related Classes of com.netflix.simianarmy.Resource

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.