| 1 | /** |
| 2 | * Licensed to jclouds, Inc. (jclouds) under one or more |
| 3 | * contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. jclouds licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. 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, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
| 18 | */ |
| 19 | package org.jclouds.ec2.compute.config; |
| 20 | |
| 21 | import static org.jclouds.ec2.reference.EC2Constants.PROPERTY_EC2_TIMEOUT_SECURITYGROUP_PRESENT; |
| 22 | |
| 23 | import java.security.SecureRandom; |
| 24 | import java.util.Map; |
| 25 | import java.util.concurrent.ConcurrentMap; |
| 26 | import java.util.concurrent.TimeUnit; |
| 27 | |
| 28 | import javax.inject.Named; |
| 29 | import javax.inject.Singleton; |
| 30 | |
| 31 | import org.jclouds.compute.ComputeService; |
| 32 | import org.jclouds.compute.domain.Image; |
| 33 | import org.jclouds.compute.domain.NodeMetadata; |
| 34 | import org.jclouds.compute.domain.NodeState; |
| 35 | import org.jclouds.compute.domain.TemplateBuilder; |
| 36 | import org.jclouds.compute.options.TemplateOptions; |
| 37 | import org.jclouds.domain.Credentials; |
| 38 | import org.jclouds.ec2.compute.EC2ComputeService; |
| 39 | import org.jclouds.ec2.compute.domain.RegionAndName; |
| 40 | import org.jclouds.ec2.compute.functions.AddElasticIpsToNodemetadata; |
| 41 | import org.jclouds.ec2.compute.functions.CreateUniqueKeyPair; |
| 42 | import org.jclouds.ec2.compute.functions.CredentialsForInstance; |
| 43 | import org.jclouds.ec2.compute.functions.RunningInstanceToNodeMetadata; |
| 44 | import org.jclouds.ec2.compute.functions.WindowsLoginCredentialsFromEncryptedData; |
| 45 | import org.jclouds.ec2.compute.internal.EC2TemplateBuilderImpl; |
| 46 | import org.jclouds.ec2.compute.loaders.CreateSecurityGroupIfNeeded; |
| 47 | import org.jclouds.ec2.compute.loaders.LoadPublicIpForInstanceOrNull; |
| 48 | import org.jclouds.ec2.compute.loaders.RegionAndIdToImage; |
| 49 | import org.jclouds.ec2.compute.options.EC2TemplateOptions; |
| 50 | import org.jclouds.ec2.compute.predicates.SecurityGroupPresent; |
| 51 | import org.jclouds.ec2.domain.InstanceState; |
| 52 | import org.jclouds.ec2.domain.KeyPair; |
| 53 | import org.jclouds.ec2.domain.RunningInstance; |
| 54 | import org.jclouds.ec2.reference.EC2Constants; |
| 55 | import org.jclouds.predicates.RetryablePredicate; |
| 56 | |
| 57 | import com.google.common.base.Function; |
| 58 | import com.google.common.base.Functions; |
| 59 | import com.google.common.base.Predicate; |
| 60 | import com.google.common.base.Supplier; |
| 61 | import com.google.common.cache.CacheBuilder; |
| 62 | import com.google.common.cache.CacheLoader; |
| 63 | import com.google.common.cache.LoadingCache; |
| 64 | import com.google.common.collect.ImmutableMap; |
| 65 | import com.google.common.collect.Maps; |
| 66 | import com.google.inject.AbstractModule; |
| 67 | import com.google.inject.Injector; |
| 68 | import com.google.inject.Provides; |
| 69 | import com.google.inject.TypeLiteral; |
| 70 | import com.google.inject.name.Names; |
| 71 | |
| 72 | /** |
| 73 | * |
| 74 | * @author Adrian Cole |
| 75 | */ |
| 76 | public class EC2ComputeServiceDependenciesModule extends AbstractModule { |
| 77 | |
| 78 | public static final Map<InstanceState, NodeState> instanceToNodeState = ImmutableMap |
| 79 | .<InstanceState, NodeState> builder().put(InstanceState.PENDING, NodeState.PENDING).put( |
| 80 | InstanceState.RUNNING, NodeState.RUNNING).put(InstanceState.SHUTTING_DOWN, NodeState.PENDING).put( |
| 81 | InstanceState.TERMINATED, NodeState.TERMINATED).put(InstanceState.STOPPING, NodeState.PENDING) |
| 82 | .put(InstanceState.STOPPED, NodeState.SUSPENDED).put(InstanceState.UNRECOGNIZED, NodeState.UNRECOGNIZED) |
| 83 | .build(); |
| 84 | |
| 85 | @Singleton |
| 86 | @Provides |
| 87 | Map<InstanceState, NodeState> provideServerToNodeState() { |
| 88 | return instanceToNodeState; |
| 89 | } |
| 90 | |
| 91 | @Override |
| 92 | protected void configure() { |
| 93 | bind(TemplateBuilder.class).to(EC2TemplateBuilderImpl.class); |
| 94 | bind(TemplateOptions.class).to(EC2TemplateOptions.class); |
| 95 | bind(ComputeService.class).to(EC2ComputeService.class); |
| 96 | bind(new TypeLiteral<CacheLoader<RunningInstance, Credentials>>() { |
| 97 | }).to(CredentialsForInstance.class); |
| 98 | bind(new TypeLiteral<Function<RegionAndName, KeyPair>>() { |
| 99 | }).to(CreateUniqueKeyPair.class); |
| 100 | bind(new TypeLiteral<CacheLoader<RegionAndName, Image>>() { |
| 101 | }).to(RegionAndIdToImage.class); |
| 102 | bind(new TypeLiteral<CacheLoader<RegionAndName, String>>() { |
| 103 | }).annotatedWith(Names.named("SECURITY")).to(CreateSecurityGroupIfNeeded.class); |
| 104 | bind(new TypeLiteral<CacheLoader<RegionAndName, String>>() { |
| 105 | }).annotatedWith(Names.named("ELASTICIP")).to(LoadPublicIpForInstanceOrNull.class); |
| 106 | bind(WindowsLoginCredentialsFromEncryptedData.class); |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * only add the overhead of looking up ips when we have enabled the auto-allocate functionality |
| 111 | */ |
| 112 | @Provides |
| 113 | @Singleton |
| 114 | public Function<RunningInstance, NodeMetadata> bindNodeConverter(RunningInstanceToNodeMetadata baseConverter, |
| 115 | AddElasticIpsToNodemetadata addElasticIpsToNodemetadata, |
| 116 | @Named(EC2Constants.PROPERTY_EC2_AUTO_ALLOCATE_ELASTIC_IPS) boolean autoAllocateElasticIps) { |
| 117 | if (!autoAllocateElasticIps) |
| 118 | return baseConverter; |
| 119 | return Functions.compose(addElasticIpsToNodemetadata, baseConverter); |
| 120 | } |
| 121 | |
| 122 | @Provides |
| 123 | @Singleton |
| 124 | Supplier<String> provideSuffix() { |
| 125 | return new Supplier<String>() { |
| 126 | final SecureRandom random = new SecureRandom(); |
| 127 | |
| 128 | @Override |
| 129 | public String get() { |
| 130 | return random.nextInt(100) + ""; |
| 131 | } |
| 132 | }; |
| 133 | |
| 134 | } |
| 135 | |
| 136 | @Provides |
| 137 | @Singleton |
| 138 | protected LoadingCache<RunningInstance, Credentials> credentialsMap(CacheLoader<RunningInstance, Credentials> in) { |
| 139 | return CacheBuilder.newBuilder().build(in); |
| 140 | } |
| 141 | |
| 142 | @Provides |
| 143 | @Singleton |
| 144 | protected ConcurrentMap<RegionAndName, KeyPair> keypairMap(Injector i) { |
| 145 | return Maps.newConcurrentMap(); |
| 146 | } |
| 147 | |
| 148 | @Provides |
| 149 | @Singleton |
| 150 | @Named("SECURITY") |
| 151 | protected LoadingCache<RegionAndName, String> securityGroupMap( |
| 152 | @Named("SECURITY") CacheLoader<RegionAndName, String> in) { |
| 153 | return CacheBuilder.newBuilder().build(in); |
| 154 | } |
| 155 | |
| 156 | @Provides |
| 157 | @Singleton |
| 158 | @Named("ELASTICIP") |
| 159 | protected LoadingCache<RegionAndName, String> instanceToElasticIp( |
| 160 | @Named("ELASTICIP") CacheLoader<RegionAndName, String> in) { |
| 161 | return CacheBuilder.newBuilder().build(in); |
| 162 | } |
| 163 | |
| 164 | @Provides |
| 165 | @Singleton |
| 166 | @Named("SECURITY") |
| 167 | protected Predicate<RegionAndName> securityGroupEventualConsistencyDelay(SecurityGroupPresent in, |
| 168 | @Named(PROPERTY_EC2_TIMEOUT_SECURITYGROUP_PRESENT) long msDelay) { |
| 169 | return new RetryablePredicate<RegionAndName>(in, msDelay, 100l, TimeUnit.MILLISECONDS); |
| 170 | } |
| 171 | |
| 172 | } |