| 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.strategy; |
| 20 | |
| 21 | import static com.google.common.base.Preconditions.checkNotNull; |
| 22 | import static com.google.common.collect.Iterables.transform; |
| 23 | import static org.jclouds.aws.ec2.reference.AWSEC2Constants.PROPERTY_EC2_GENERATE_INSTANCE_NAMES; |
| 24 | |
| 25 | import java.util.Map; |
| 26 | |
| 27 | import javax.annotation.Resource; |
| 28 | import javax.inject.Inject; |
| 29 | import javax.inject.Named; |
| 30 | import javax.inject.Provider; |
| 31 | import javax.inject.Singleton; |
| 32 | |
| 33 | import org.jclouds.aws.ec2.AWSEC2AsyncClient; |
| 34 | import org.jclouds.aws.ec2.AWSEC2Client; |
| 35 | import org.jclouds.aws.ec2.compute.AWSEC2TemplateOptions; |
| 36 | import org.jclouds.aws.ec2.compute.predicates.AWSEC2InstancePresent; |
| 37 | import org.jclouds.aws.ec2.domain.LaunchSpecification; |
| 38 | import org.jclouds.aws.ec2.functions.SpotInstanceRequestToAWSRunningInstance; |
| 39 | import org.jclouds.aws.ec2.options.AWSRunInstancesOptions; |
| 40 | import org.jclouds.aws.ec2.options.RequestSpotInstancesOptions; |
| 41 | import org.jclouds.compute.domain.NodeMetadata; |
| 42 | import org.jclouds.compute.domain.Template; |
| 43 | import org.jclouds.compute.domain.TemplateBuilder; |
| 44 | import org.jclouds.compute.options.TemplateOptions; |
| 45 | import org.jclouds.compute.reference.ComputeServiceConstants; |
| 46 | import org.jclouds.compute.util.ComputeUtils; |
| 47 | import org.jclouds.domain.Credentials; |
| 48 | import org.jclouds.ec2.compute.strategy.EC2CreateNodesInGroupThenAddToSet; |
| 49 | import org.jclouds.ec2.domain.RunningInstance; |
| 50 | import org.jclouds.ec2.options.RunInstancesOptions; |
| 51 | import org.jclouds.logging.Logger; |
| 52 | |
| 53 | import com.google.common.annotations.VisibleForTesting; |
| 54 | import com.google.common.base.Function; |
| 55 | import com.google.common.cache.Cache; |
| 56 | import com.google.common.collect.ImmutableMap; |
| 57 | import com.google.common.collect.ImmutableSet; |
| 58 | |
| 59 | /** |
| 60 | * |
| 61 | * @author Adrian Cole |
| 62 | */ |
| 63 | @Singleton |
| 64 | public class AWSEC2CreateNodesInGroupThenAddToSet extends EC2CreateNodesInGroupThenAddToSet { |
| 65 | |
| 66 | @Resource |
| 67 | @Named(ComputeServiceConstants.COMPUTE_LOGGER) |
| 68 | protected Logger logger = Logger.NULL; |
| 69 | |
| 70 | @VisibleForTesting |
| 71 | final AWSEC2Client client; |
| 72 | final SpotInstanceRequestToAWSRunningInstance spotConverter; |
| 73 | final AWSEC2AsyncClient aclient; |
| 74 | final boolean generateInstanceNames; |
| 75 | |
| 76 | @Inject |
| 77 | protected AWSEC2CreateNodesInGroupThenAddToSet( |
| 78 | AWSEC2Client client, |
| 79 | AWSEC2AsyncClient aclient, |
| 80 | @Named(PROPERTY_EC2_GENERATE_INSTANCE_NAMES) boolean generateInstanceNames, |
| 81 | Provider<TemplateBuilder> templateBuilderProvider, |
| 82 | CreateKeyPairPlacementAndSecurityGroupsAsNeededAndReturnRunOptions createKeyPairAndSecurityGroupsAsNeededAndReturncustomize, |
| 83 | AWSEC2InstancePresent instancePresent, |
| 84 | Function<RunningInstance, NodeMetadata> runningInstanceToNodeMetadata, |
| 85 | Cache<RunningInstance, Credentials> instanceToCredentials, Map<String, Credentials> credentialStore, |
| 86 | ComputeUtils utils, SpotInstanceRequestToAWSRunningInstance spotConverter) { |
| 87 | super(client, templateBuilderProvider, createKeyPairAndSecurityGroupsAsNeededAndReturncustomize, instancePresent, |
| 88 | runningInstanceToNodeMetadata, instanceToCredentials, credentialStore, utils); |
| 89 | this.client = checkNotNull(client, "client"); |
| 90 | this.aclient = checkNotNull(aclient, "aclient"); |
| 91 | this.spotConverter = checkNotNull(spotConverter, "spotConverter"); |
| 92 | this.generateInstanceNames = generateInstanceNames; |
| 93 | } |
| 94 | |
| 95 | @Override |
| 96 | protected Iterable<? extends RunningInstance> createNodesInRegionAndZone(String region, String zone, String group, |
| 97 | int count, Template template, RunInstancesOptions instanceOptions) { |
| 98 | Float spotPrice = getSpotPriceOrNull(template.getOptions()); |
| 99 | if (spotPrice != null) { |
| 100 | LaunchSpecification spec = AWSRunInstancesOptions.class.cast(instanceOptions).getLaunchSpecificationBuilder() |
| 101 | .imageId(template.getImage().getProviderId()).availabilityZone(zone).build(); |
| 102 | RequestSpotInstancesOptions options = AWSEC2TemplateOptions.class.cast(template.getOptions()).getSpotOptions(); |
| 103 | if (logger.isDebugEnabled()) |
| 104 | logger.debug(">> requesting %d spot instances region(%s) price(%f) spec(%s) options(%s)", count, region, |
| 105 | spotPrice, spec, options); |
| 106 | |
| 107 | return addTagsToInstancesInRegion(template.getOptions().getUserMetadata(), transform(client |
| 108 | .getSpotInstanceServices().requestSpotInstancesInRegion(region, spotPrice, count, spec, options), |
| 109 | spotConverter), region, group); |
| 110 | } else { |
| 111 | return addTagsToInstancesInRegion(template.getOptions().getUserMetadata(), super.createNodesInRegionAndZone( |
| 112 | region, zone, group, count, template, instanceOptions), region, group); |
| 113 | } |
| 114 | |
| 115 | } |
| 116 | |
| 117 | public Iterable<? extends RunningInstance> addTagsToInstancesInRegion(Map<String, String> metadata, |
| 118 | Iterable<? extends RunningInstance> iterable, String region, String group) { |
| 119 | if (metadata.size() > 0 || generateInstanceNames) { |
| 120 | for (String id : transform(iterable, new Function<RunningInstance, String>() { |
| 121 | |
| 122 | @Override |
| 123 | public String apply(RunningInstance arg0) { |
| 124 | return arg0.getId(); |
| 125 | } |
| 126 | |
| 127 | })) |
| 128 | aclient.getTagServices() |
| 129 | .createTagsInRegion(region, ImmutableSet.of(id), metadataForId(id, group, metadata)); |
| 130 | } |
| 131 | return iterable; |
| 132 | } |
| 133 | |
| 134 | private Map<String, String> metadataForId(String id, String group, Map<String, String> metadata) { |
| 135 | return generateInstanceNames && !metadata.containsKey("Name") ? ImmutableMap.<String, String> builder().putAll( |
| 136 | metadata).put("Name", id.replaceAll(".*-", group + "-")).build() : metadata; |
| 137 | } |
| 138 | |
| 139 | private Float getSpotPriceOrNull(TemplateOptions options) { |
| 140 | return options instanceof AWSEC2TemplateOptions ? AWSEC2TemplateOptions.class.cast(options).getSpotPrice() : null; |
| 141 | } |
| 142 | |
| 143 | } |