| 1 | /** |
| 2 | * |
| 3 | * Copyright (C) 2011 Cloud Conscious, LLC. <info@cloudconscious.com> |
| 4 | * |
| 5 | * ==================================================================== |
| 6 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | * you may not use this file except in compliance with the License. |
| 8 | * You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, software |
| 13 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | * See the License for the specific language governing permissions and |
| 16 | * limitations under the License. |
| 17 | * ==================================================================== |
| 18 | */ |
| 19 | package org.jclouds.aws.ec2.compute.config; |
| 20 | |
| 21 | import static org.jclouds.Constants.PROPERTY_SESSION_INTERVAL; |
| 22 | import static org.jclouds.compute.domain.OsFamily.AMZN_LINUX; |
| 23 | |
| 24 | import java.util.Map; |
| 25 | |
| 26 | import javax.inject.Named; |
| 27 | import javax.inject.Singleton; |
| 28 | |
| 29 | import org.jclouds.aws.ec2.compute.AWSEC2TemplateBuilderImpl; |
| 30 | import org.jclouds.aws.ec2.compute.functions.AWSRunningInstanceToNodeMetadata; |
| 31 | import org.jclouds.aws.ec2.compute.predicates.AWSEC2InstancePresent; |
| 32 | import org.jclouds.aws.ec2.compute.strategy.AWSEC2CreateNodesInGroupThenAddToSet; |
| 33 | import org.jclouds.aws.ec2.compute.strategy.AWSEC2DestroyNodeStrategy; |
| 34 | import org.jclouds.aws.ec2.compute.strategy.AWSEC2GetNodeMetadataStrategy; |
| 35 | import org.jclouds.aws.ec2.compute.strategy.AWSEC2ListNodesStrategy; |
| 36 | import org.jclouds.aws.ec2.compute.strategy.AWSEC2ReviseParsedImage; |
| 37 | import org.jclouds.aws.ec2.compute.strategy.CreateKeyPairPlacementAndSecurityGroupsAsNeededAndReturnRunOptions; |
| 38 | import org.jclouds.aws.ec2.compute.suppliers.AWSEC2HardwareSupplier; |
| 39 | import org.jclouds.aws.ec2.compute.suppliers.AWSRegionAndNameToImageSupplier; |
| 40 | import org.jclouds.compute.config.BaseComputeServiceContextModule; |
| 41 | import org.jclouds.compute.domain.Image; |
| 42 | import org.jclouds.compute.domain.TemplateBuilder; |
| 43 | import org.jclouds.ec2.compute.config.EC2BindComputeStrategiesByClass; |
| 44 | import org.jclouds.ec2.compute.config.EC2BindComputeSuppliersByClass; |
| 45 | import org.jclouds.ec2.compute.domain.RegionAndName; |
| 46 | import org.jclouds.ec2.compute.functions.RunningInstanceToNodeMetadata; |
| 47 | import org.jclouds.ec2.compute.internal.EC2TemplateBuilderImpl; |
| 48 | import org.jclouds.ec2.compute.predicates.InstancePresent; |
| 49 | import org.jclouds.ec2.compute.strategy.CreateKeyPairAndSecurityGroupsAsNeededAndReturnRunOptions; |
| 50 | import org.jclouds.ec2.compute.strategy.EC2CreateNodesInGroupThenAddToSet; |
| 51 | import org.jclouds.ec2.compute.strategy.EC2DestroyNodeStrategy; |
| 52 | import org.jclouds.ec2.compute.strategy.EC2GetNodeMetadataStrategy; |
| 53 | import org.jclouds.ec2.compute.strategy.EC2ListNodesStrategy; |
| 54 | import org.jclouds.ec2.compute.strategy.ReviseParsedImage; |
| 55 | import org.jclouds.ec2.compute.suppliers.EC2HardwareSupplier; |
| 56 | import org.jclouds.rest.suppliers.MemoizedRetryOnTimeOutButNotOnAuthorizationExceptionSupplier; |
| 57 | |
| 58 | import com.google.common.base.Supplier; |
| 59 | import com.google.inject.Injector; |
| 60 | import com.google.inject.Provides; |
| 61 | |
| 62 | /** |
| 63 | * |
| 64 | * @author Adrian Cole |
| 65 | */ |
| 66 | public class AWSEC2ComputeServiceContextModule extends BaseComputeServiceContextModule { |
| 67 | @Override |
| 68 | protected void configure() { |
| 69 | super.configure(); |
| 70 | installDependencies(); |
| 71 | install(new EC2BindComputeStrategiesByClass()); |
| 72 | install(new EC2BindComputeSuppliersByClass()); |
| 73 | bind(ReviseParsedImage.class).to(AWSEC2ReviseParsedImage.class); |
| 74 | bind(CreateKeyPairAndSecurityGroupsAsNeededAndReturnRunOptions.class).to( |
| 75 | CreateKeyPairPlacementAndSecurityGroupsAsNeededAndReturnRunOptions.class); |
| 76 | bind(EC2HardwareSupplier.class).to(AWSEC2HardwareSupplier.class); |
| 77 | bind(EC2TemplateBuilderImpl.class).to(AWSEC2TemplateBuilderImpl.class); |
| 78 | bind(EC2GetNodeMetadataStrategy.class).to(AWSEC2GetNodeMetadataStrategy.class); |
| 79 | bind(EC2ListNodesStrategy.class).to(AWSEC2ListNodesStrategy.class); |
| 80 | bind(EC2DestroyNodeStrategy.class).to(AWSEC2DestroyNodeStrategy.class); |
| 81 | bind(InstancePresent.class).to(AWSEC2InstancePresent.class); |
| 82 | bind(EC2CreateNodesInGroupThenAddToSet.class).to(AWSEC2CreateNodesInGroupThenAddToSet.class); |
| 83 | bind(RunningInstanceToNodeMetadata.class).to(AWSRunningInstanceToNodeMetadata.class); |
| 84 | } |
| 85 | |
| 86 | protected void installDependencies() { |
| 87 | install(new AWSEC2ComputeServiceDependenciesModule()); |
| 88 | } |
| 89 | |
| 90 | @Provides |
| 91 | @Singleton |
| 92 | protected Supplier<Map<RegionAndName, ? extends Image>> provideRegionAndNameToImageSupplierCache( |
| 93 | @Named(PROPERTY_SESSION_INTERVAL) long seconds, final AWSRegionAndNameToImageSupplier supplier) { |
| 94 | return new MemoizedRetryOnTimeOutButNotOnAuthorizationExceptionSupplier<Map<RegionAndName, ? extends Image>>( |
| 95 | authException, seconds, new Supplier<Map<RegionAndName, ? extends Image>>() { |
| 96 | @Override |
| 97 | public Map<RegionAndName, ? extends Image> get() { |
| 98 | return supplier.get(); |
| 99 | } |
| 100 | }); |
| 101 | } |
| 102 | |
| 103 | @Override |
| 104 | protected TemplateBuilder provideTemplate(Injector injector, TemplateBuilder template) { |
| 105 | return template.osFamily(AMZN_LINUX).os64Bit(true); |
| 106 | } |
| 107 | } |