Search in sources :

Example 16 with Region

use of com.amazonaws.services.ec2.model.Region in project jackrabbit-oak by apache.

the class S3Backend method init.

public void init() throws DataStoreException {
    ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
    try {
        startTime = new Date();
        Thread.currentThread().setContextClassLoader(getClass().getClassLoader());
        LOG.debug("init");
        s3ReqDecorator = new S3RequestDecorator(properties);
        s3service = Utils.openService(properties);
        if (bucket == null || "".equals(bucket.trim())) {
            bucket = properties.getProperty(S3Constants.S3_BUCKET);
            // Alternately check if the 'container' property is set
            if (Strings.isNullOrEmpty(bucket)) {
                bucket = properties.getProperty(S3Constants.S3_CONTAINER);
            }
        }
        secret = properties.getProperty("secret");
        String region = properties.getProperty(S3Constants.S3_REGION);
        Region s3Region;
        if (StringUtils.isNullOrEmpty(region)) {
            com.amazonaws.regions.Region ec2Region = Regions.getCurrentRegion();
            if (ec2Region != null) {
                s3Region = Region.fromValue(ec2Region.getName());
            } else {
                throw new AmazonClientException("parameter [" + S3Constants.S3_REGION + "] not configured and cannot be derived from environment");
            }
        } else {
            if (Utils.DEFAULT_AWS_BUCKET_REGION.equals(region)) {
                s3Region = Region.US_Standard;
            } else if (Region.EU_Ireland.toString().equals(region)) {
                s3Region = Region.EU_Ireland;
            } else {
                s3Region = Region.fromValue(region);
            }
        }
        if (!s3service.doesBucketExist(bucket)) {
            s3service.createBucket(bucket, s3Region);
            LOG.info("Created bucket [{}] in [{}] ", bucket, region);
        } else {
            LOG.info("Using bucket [{}] in [{}] ", bucket, region);
        }
        int writeThreads = 10;
        String writeThreadsStr = properties.getProperty(S3Constants.S3_WRITE_THREADS);
        if (writeThreadsStr != null) {
            writeThreads = Integer.parseInt(writeThreadsStr);
        }
        LOG.info("Using thread pool of [{}] threads in S3 transfer manager.", writeThreads);
        tmx = new TransferManager(s3service, Executors.newFixedThreadPool(writeThreads, new NamedThreadFactory("s3-transfer-manager-worker")));
        String renameKeyProp = properties.getProperty(S3Constants.S3_RENAME_KEYS);
        boolean renameKeyBool = (renameKeyProp == null || "".equals(renameKeyProp)) ? false : Boolean.parseBoolean(renameKeyProp);
        LOG.info("Rename keys [{}]", renameKeyBool);
        if (renameKeyBool) {
            renameKeys();
        }
        LOG.debug("S3 Backend initialized in [{}] ms", +(System.currentTimeMillis() - startTime.getTime()));
    } catch (Exception e) {
        LOG.debug("  error ", e);
        Map<String, String> filteredMap = Maps.newHashMap();
        if (properties != null) {
            filteredMap = Maps.filterKeys(Maps.fromProperties(properties), new Predicate<String>() {

                @Override
                public boolean apply(String input) {
                    return !input.equals(S3Constants.ACCESS_KEY) && !input.equals(S3Constants.SECRET_KEY);
                }
            });
        }
        throw new DataStoreException("Could not initialize S3 from " + filteredMap, e);
    } finally {
        if (contextClassLoader != null) {
            Thread.currentThread().setContextClassLoader(contextClassLoader);
        }
    }
}
Also used : TransferManager(com.amazonaws.services.s3.transfer.TransferManager) DataStoreException(org.apache.jackrabbit.core.data.DataStoreException) NamedThreadFactory(org.apache.jackrabbit.core.data.util.NamedThreadFactory) AmazonClientException(com.amazonaws.AmazonClientException) Date(java.util.Date) DataStoreException(org.apache.jackrabbit.core.data.DataStoreException) AmazonServiceException(com.amazonaws.AmazonServiceException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) AmazonClientException(com.amazonaws.AmazonClientException) Region(com.amazonaws.services.s3.model.Region) Map(java.util.Map)

Example 17 with Region

use of com.amazonaws.services.ec2.model.Region in project SimianArmy by Netflix.

the class EBSVolumeJanitorCrawler method getVolumeResources.

private List<Resource> getVolumeResources(String... volumeIds) {
    List<Resource> resources = new LinkedList<Resource>();
    AWSClient awsClient = getAWSClient();
    for (Volume volume : awsClient.describeVolumes(volumeIds)) {
        Resource volumeResource = new AWSResource().withId(volume.getVolumeId()).withRegion(getAWSClient().region()).withResourceType(AWSResourceType.EBS_VOLUME).withLaunchTime(volume.getCreateTime());
        for (Tag tag : volume.getTags()) {
            LOGGER.info(String.format("Adding tag %s = %s to resource %s", tag.getKey(), tag.getValue(), volumeResource.getId()));
            volumeResource.setTag(tag.getKey(), tag.getValue());
        }
        volumeResource.setOwnerEmail(getOwnerEmailForResource(volumeResource));
        volumeResource.setDescription(getVolumeDescription(volume));
        ((AWSResource) volumeResource).setAWSResourceState(volume.getState());
        resources.add(volumeResource);
    }
    return resources;
}
Also used : Volume(com.amazonaws.services.ec2.model.Volume) AWSResource(com.netflix.simianarmy.aws.AWSResource) Resource(com.netflix.simianarmy.Resource) AWSResource(com.netflix.simianarmy.aws.AWSResource) AWSClient(com.netflix.simianarmy.client.aws.AWSClient) Tag(com.amazonaws.services.ec2.model.Tag) LinkedList(java.util.LinkedList)

Example 18 with Region

use of com.amazonaws.services.ec2.model.Region in project SimianArmy by Netflix.

the class InstanceJanitorCrawler method getInstanceResources.

private List<Resource> getInstanceResources(String... instanceIds) {
    List<Resource> resources = new LinkedList<Resource>();
    AWSClient awsClient = getAWSClient();
    Map<String, AutoScalingInstanceDetails> idToASGInstance = new HashMap<String, AutoScalingInstanceDetails>();
    for (AutoScalingInstanceDetails instanceDetails : awsClient.describeAutoScalingInstances(instanceIds)) {
        idToASGInstance.put(instanceDetails.getInstanceId(), instanceDetails);
    }
    for (Instance instance : awsClient.describeInstances(instanceIds)) {
        Resource instanceResource = new AWSResource().withId(instance.getInstanceId()).withRegion(getAWSClient().region()).withResourceType(AWSResourceType.INSTANCE).withLaunchTime(instance.getLaunchTime());
        for (Tag tag : instance.getTags()) {
            instanceResource.setTag(tag.getKey(), tag.getValue());
        }
        String description = String.format("type=%s; host=%s", instance.getInstanceType(), instance.getPublicDnsName() == null ? "" : instance.getPublicDnsName());
        instanceResource.setDescription(description);
        instanceResource.setOwnerEmail(getOwnerEmailForResource(instanceResource));
        String asgName = getAsgName(instanceResource, idToASGInstance);
        if (asgName != null) {
            instanceResource.setAdditionalField(INSTANCE_FIELD_ASG_NAME, asgName);
            LOGGER.info(String.format("instance %s has a ASG tag name %s.", instanceResource.getId(), asgName));
        }
        String opsworksStackName = getOpsWorksStackName(instanceResource);
        if (opsworksStackName != null) {
            instanceResource.setAdditionalField(INSTANCE_FIELD_OPSWORKS_STACK_NAME, opsworksStackName);
            LOGGER.info(String.format("instance %s is part of an OpsWorks stack named %s.", instanceResource.getId(), opsworksStackName));
        }
        if (instance.getState() != null) {
            ((AWSResource) instanceResource).setAWSResourceState(instance.getState().getName());
        }
        resources.add(instanceResource);
    }
    return resources;
}
Also used : HashMap(java.util.HashMap) Instance(com.amazonaws.services.ec2.model.Instance) AWSResource(com.netflix.simianarmy.aws.AWSResource) Resource(com.netflix.simianarmy.Resource) AWSResource(com.netflix.simianarmy.aws.AWSResource) AutoScalingInstanceDetails(com.amazonaws.services.autoscaling.model.AutoScalingInstanceDetails) AWSClient(com.netflix.simianarmy.client.aws.AWSClient) Tag(com.amazonaws.services.ec2.model.Tag) LinkedList(java.util.LinkedList)

Example 19 with Region

use of com.amazonaws.services.ec2.model.Region in project SimianArmy by Netflix.

the class InstanceInVPC method checkInstancesInVPC.

private Set<String> checkInstancesInVPC(String region, Collection<String> instances) {
    Set<String> failedInstances = Sets.newHashSet();
    for (String instanceId : instances) {
        for (Instance awsInstance : getAWSInstances(region, instanceId)) {
            if (awsInstance.getVpcId() == null) {
                LOGGER.info(String.format("Instance %s is not in a virtual private cloud", instanceId));
                failedInstances.add(instanceId);
            }
        }
    }
    return failedInstances;
}
Also used : Instance(com.amazonaws.services.ec2.model.Instance)

Example 20 with Region

use of com.amazonaws.services.ec2.model.Region in project SimianArmy by Netflix.

the class InstanceTooOld method getInstanceLaunchTimes.

/**
     * Gets the launch time (in milliseconds) for a list of instance ids of the same region. The default
     * implementation is using an AWS client. The method can be overridden in subclasses to get the instance
     * launch times differently.
     * @param region
     *      the region of the instances
     * @param instanceIds
     *      the instance ids, all instances should be in the same region.
     * @return
     *      the map from instance id to the launch time in milliseconds
     */
protected Map<String, Long> getInstanceLaunchTimes(String region, String... instanceIds) {
    Map<String, Long> result = Maps.newHashMap();
    if (instanceIds == null || instanceIds.length == 0) {
        return result;
    }
    AWSClient awsClient = new AWSClient(region, awsCredentialsProvider);
    for (Instance instance : awsClient.describeInstances(instanceIds)) {
        if (instance.getLaunchTime() != null) {
            result.put(instance.getInstanceId(), instance.getLaunchTime().getTime());
        } else {
            LOGGER.warn(String.format("No launch time found for instance %s", instance.getInstanceId()));
        }
    }
    return result;
}
Also used : Instance(com.amazonaws.services.ec2.model.Instance) AWSClient(com.netflix.simianarmy.client.aws.AWSClient)

Aggregations

AmazonEC2 (com.amazonaws.services.ec2.AmazonEC2)17 Instance (com.amazonaws.services.ec2.model.Instance)12 AmazonEC2Client (com.amazonaws.services.ec2.AmazonEC2Client)10 AmazonServiceException (com.amazonaws.AmazonServiceException)7 AWSClient (com.netflix.simianarmy.client.aws.AWSClient)5 IOException (java.io.IOException)5 Date (java.util.Date)5 AWSCredentials (com.amazonaws.auth.AWSCredentials)4 PropertiesCredentials (com.amazonaws.auth.PropertiesCredentials)4 File (java.io.File)4 AmazonClientException (com.amazonaws.AmazonClientException)3 Tag (com.amazonaws.services.ec2.model.Tag)3 Region (com.amazonaws.services.s3.model.Region)3 TransferManager (com.amazonaws.services.s3.transfer.TransferManager)3 Resource (com.netflix.simianarmy.Resource)3 AWSResource (com.netflix.simianarmy.aws.AWSResource)3 HashMap (java.util.HashMap)3 BasicAWSCredentials (com.amazonaws.auth.BasicAWSCredentials)2 InstanceProfileCredentialsProvider (com.amazonaws.auth.InstanceProfileCredentialsProvider)2 AvailabilityZone (com.amazonaws.services.ec2.model.AvailabilityZone)2