| 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.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 javax.inject.Named; |
| 25 | import javax.inject.Singleton; |
| 26 | |
| 27 | import org.jclouds.aws.ec2.compute.AWSEC2TemplateBuilderImpl; |
| 28 | import org.jclouds.aws.ec2.compute.functions.AWSRunningInstanceToNodeMetadata; |
| 29 | import org.jclouds.aws.ec2.compute.predicates.AWSEC2InstancePresent; |
| 30 | import org.jclouds.aws.ec2.compute.strategy.AWSEC2CreateNodesInGroupThenAddToSet; |
| 31 | import org.jclouds.aws.ec2.compute.strategy.AWSEC2DestroyNodeStrategy; |
| 32 | import org.jclouds.aws.ec2.compute.strategy.AWSEC2GetNodeMetadataStrategy; |
| 33 | import org.jclouds.aws.ec2.compute.strategy.AWSEC2ListNodesStrategy; |
| 34 | import org.jclouds.aws.ec2.compute.strategy.AWSEC2ReviseParsedImage; |
| 35 | import org.jclouds.aws.ec2.compute.strategy.CreateKeyPairPlacementAndSecurityGroupsAsNeededAndReturnRunOptions; |
| 36 | import org.jclouds.aws.ec2.compute.suppliers.AWSEC2HardwareSupplier; |
| 37 | import org.jclouds.aws.ec2.compute.suppliers.AWSRegionAndNameToImageSupplier; |
| 38 | import org.jclouds.compute.config.BaseComputeServiceContextModule; |
| 39 | import org.jclouds.compute.domain.Image; |
| 40 | import org.jclouds.compute.domain.TemplateBuilder; |
| 41 | import org.jclouds.compute.options.TemplateOptions; |
| 42 | import org.jclouds.ec2.compute.config.EC2BindComputeStrategiesByClass; |
| 43 | import org.jclouds.ec2.compute.config.EC2BindComputeSuppliersByClass; |
| 44 | import org.jclouds.ec2.compute.domain.RegionAndName; |
| 45 | import org.jclouds.ec2.compute.functions.RunningInstanceToNodeMetadata; |
| 46 | import org.jclouds.ec2.compute.internal.EC2TemplateBuilderImpl; |
| 47 | import org.jclouds.ec2.compute.options.EC2TemplateOptions; |
| 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.common.cache.Cache; |
| 60 | import com.google.inject.Injector; |
| 61 | import com.google.inject.Provides; |
| 62 | |
| 63 | /** |
| 64 | * |
| 65 | * @author Adrian Cole |
| 66 | */ |
| 67 | public class AWSEC2ComputeServiceContextModule extends BaseComputeServiceContextModule { |
| 68 | @Override |
| 69 | protected void configure() { |
| 70 | super.configure(); |
| 71 | installDependencies(); |
| 72 | install(new EC2BindComputeStrategiesByClass()); |
| 73 | install(new EC2BindComputeSuppliersByClass()); |
| 74 | bind(ReviseParsedImage.class).to(AWSEC2ReviseParsedImage.class); |
| 75 | bind(CreateKeyPairAndSecurityGroupsAsNeededAndReturnRunOptions.class).to( |
| 76 | CreateKeyPairPlacementAndSecurityGroupsAsNeededAndReturnRunOptions.class); |
| 77 | bind(EC2HardwareSupplier.class).to(AWSEC2HardwareSupplier.class); |
| 78 | bind(EC2TemplateBuilderImpl.class).to(AWSEC2TemplateBuilderImpl.class); |
| 79 | bind(EC2GetNodeMetadataStrategy.class).to(AWSEC2GetNodeMetadataStrategy.class); |
| 80 | bind(EC2ListNodesStrategy.class).to(AWSEC2ListNodesStrategy.class); |
| 81 | bind(EC2DestroyNodeStrategy.class).to(AWSEC2DestroyNodeStrategy.class); |
| 82 | bind(InstancePresent.class).to(AWSEC2InstancePresent.class); |
| 83 | bind(EC2CreateNodesInGroupThenAddToSet.class).to(AWSEC2CreateNodesInGroupThenAddToSet.class); |
| 84 | bind(RunningInstanceToNodeMetadata.class).to(AWSRunningInstanceToNodeMetadata.class); |
| 85 | } |
| 86 | |
| 87 | protected void installDependencies() { |
| 88 | install(new AWSEC2ComputeServiceDependenciesModule()); |
| 89 | } |
| 90 | |
| 91 | @Provides |
| 92 | @Singleton |
| 93 | protected Supplier<Cache<RegionAndName, ? extends Image>> provideRegionAndNameToImageSupplierCache( |
| 94 | @Named(PROPERTY_SESSION_INTERVAL) long seconds, final AWSRegionAndNameToImageSupplier supplier) { |
| 95 | return new MemoizedRetryOnTimeOutButNotOnAuthorizationExceptionSupplier<Cache<RegionAndName, ? extends Image>>( |
| 96 | authException, seconds, new Supplier<Cache<RegionAndName, ? extends Image>>() { |
| 97 | @Override |
| 98 | public Cache<RegionAndName, ? extends Image> get() { |
| 99 | return supplier.get(); |
| 100 | } |
| 101 | }); |
| 102 | } |
| 103 | |
| 104 | @Override |
| 105 | protected TemplateBuilder provideTemplate(Injector injector, TemplateBuilder template) { |
| 106 | return template.osFamily(AMZN_LINUX).os64Bit(true); |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * With amazon linux 2011.09, ssh starts after package updates, which slows the boot process and |
| 111 | * runs us out of ssh retries (context property {@code "jclouds.ssh.max-retries"}). |
| 112 | * |
| 113 | * @see <a href="http://aws.amazon.com/amazon-linux-ami/latest-release-notes/" /> |
| 114 | * @see AWSEC2PropertiesBuilder#defaultProperties |
| 115 | */ |
| 116 | @Override |
| 117 | protected TemplateOptions provideTemplateOptions(Injector injector, TemplateOptions options) { |
| 118 | return options.as(EC2TemplateOptions.class).userData("#cloud-config\nrepo_upgrade: none\n".getBytes()); |
| 119 | } |
| 120 | } |