| 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.strategy; |
| 20 | |
| 21 | import static com.google.common.base.Preconditions.checkNotNull; |
| 22 | |
| 23 | import java.util.Map; |
| 24 | |
| 25 | import javax.annotation.Resource; |
| 26 | import javax.inject.Inject; |
| 27 | import javax.inject.Named; |
| 28 | import javax.inject.Provider; |
| 29 | import javax.inject.Singleton; |
| 30 | |
| 31 | import org.jclouds.aws.ec2.AWSEC2Client; |
| 32 | import org.jclouds.aws.ec2.compute.AWSEC2TemplateOptions; |
| 33 | import org.jclouds.aws.ec2.compute.predicates.AWSEC2InstancePresent; |
| 34 | import org.jclouds.aws.ec2.domain.LaunchSpecification; |
| 35 | import org.jclouds.aws.ec2.functions.SpotInstanceRequestToAWSRunningInstance; |
| 36 | import org.jclouds.aws.ec2.options.AWSRunInstancesOptions; |
| 37 | import org.jclouds.aws.ec2.options.RequestSpotInstancesOptions; |
| 38 | import org.jclouds.compute.domain.NodeMetadata; |
| 39 | import org.jclouds.compute.domain.Template; |
| 40 | import org.jclouds.compute.domain.TemplateBuilder; |
| 41 | import org.jclouds.compute.options.TemplateOptions; |
| 42 | import org.jclouds.compute.reference.ComputeServiceConstants; |
| 43 | import org.jclouds.compute.util.ComputeUtils; |
| 44 | import org.jclouds.domain.Credentials; |
| 45 | import org.jclouds.ec2.compute.strategy.EC2CreateNodesInGroupThenAddToSet; |
| 46 | import org.jclouds.ec2.domain.RunningInstance; |
| 47 | import org.jclouds.ec2.options.RunInstancesOptions; |
| 48 | import org.jclouds.logging.Logger; |
| 49 | |
| 50 | import com.google.common.annotations.VisibleForTesting; |
| 51 | import com.google.common.base.Function; |
| 52 | import com.google.common.collect.Iterables; |
| 53 | |
| 54 | /** |
| 55 | * |
| 56 | * @author Adrian Cole |
| 57 | */ |
| 58 | @Singleton |
| 59 | public class AWSEC2CreateNodesInGroupThenAddToSet extends EC2CreateNodesInGroupThenAddToSet { |
| 60 | |
| 61 | @Resource |
| 62 | @Named(ComputeServiceConstants.COMPUTE_LOGGER) |
| 63 | protected Logger logger = Logger.NULL; |
| 64 | |
| 65 | @VisibleForTesting |
| 66 | final AWSEC2Client client; |
| 67 | final SpotInstanceRequestToAWSRunningInstance spotConverter; |
| 68 | |
| 69 | @Inject |
| 70 | protected AWSEC2CreateNodesInGroupThenAddToSet( |
| 71 | AWSEC2Client client, |
| 72 | Provider<TemplateBuilder> templateBuilderProvider, |
| 73 | CreateKeyPairPlacementAndSecurityGroupsAsNeededAndReturnRunOptions createKeyPairAndSecurityGroupsAsNeededAndReturncustomize, |
| 74 | AWSEC2InstancePresent instancePresent, |
| 75 | Function<RunningInstance, NodeMetadata> runningInstanceToNodeMetadata, |
| 76 | Function<RunningInstance, Credentials> instanceToCredentials, Map<String, Credentials> credentialStore, |
| 77 | ComputeUtils utils, SpotInstanceRequestToAWSRunningInstance spotConverter) { |
| 78 | super(client, templateBuilderProvider, createKeyPairAndSecurityGroupsAsNeededAndReturncustomize, instancePresent, |
| 79 | runningInstanceToNodeMetadata, instanceToCredentials, credentialStore, utils); |
| 80 | this.client = checkNotNull(client, "client"); |
| 81 | this.spotConverter = checkNotNull(spotConverter, "spotConverter"); |
| 82 | } |
| 83 | |
| 84 | protected Iterable<? extends RunningInstance> createNodesInRegionAndZone(String region, String zone, int count, |
| 85 | Template template, RunInstancesOptions instanceOptions) { |
| 86 | Float spotPrice = getSpotPriceOrNull(template.getOptions()); |
| 87 | if (spotPrice != null) { |
| 88 | LaunchSpecification spec = AWSRunInstancesOptions.class.cast(instanceOptions).getLaunchSpecificationBuilder() |
| 89 | .imageId(template.getImage().getProviderId()).availabilityZone(zone).build(); |
| 90 | RequestSpotInstancesOptions options = AWSEC2TemplateOptions.class.cast(template.getOptions()).getSpotOptions(); |
| 91 | if (logger.isDebugEnabled()) |
| 92 | logger.debug(">> requesting %d spot instances region(%s) price(%f) spec(%s) options(%s)", count, region, |
| 93 | spotPrice, spec, options); |
| 94 | |
| 95 | return Iterables.transform(client.getSpotInstanceServices().requestSpotInstancesInRegion(region, spotPrice, |
| 96 | count, spec, options), spotConverter); |
| 97 | } else { |
| 98 | return super.createNodesInRegionAndZone(region, zone, count, template, instanceOptions); |
| 99 | } |
| 100 | |
| 101 | } |
| 102 | |
| 103 | private Float getSpotPriceOrNull(TemplateOptions options) { |
| 104 | return options instanceof AWSEC2TemplateOptions ? AWSEC2TemplateOptions.class.cast(options).getSpotPrice() : null; |
| 105 | } |
| 106 | |
| 107 | } |