| 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; |
| 20 | |
| 21 | import static com.google.common.base.Preconditions.checkNotNull; |
| 22 | import static com.google.common.base.Preconditions.checkState; |
| 23 | import static com.google.common.collect.Iterables.transform; |
| 24 | import static org.jclouds.aws.ec2.reference.AWSEC2Constants.PROPERTY_EC2_GENERATE_INSTANCE_NAMES; |
| 25 | |
| 26 | import java.util.Map; |
| 27 | import java.util.Set; |
| 28 | import java.util.Map.Entry; |
| 29 | import java.util.concurrent.ConcurrentMap; |
| 30 | import java.util.concurrent.ExecutorService; |
| 31 | |
| 32 | import javax.inject.Inject; |
| 33 | import javax.inject.Named; |
| 34 | import javax.inject.Provider; |
| 35 | import javax.inject.Singleton; |
| 36 | |
| 37 | import org.jclouds.Constants; |
| 38 | import org.jclouds.aws.ec2.AWSEC2AsyncClient; |
| 39 | import org.jclouds.aws.ec2.AWSEC2Client; |
| 40 | import org.jclouds.aws.ec2.domain.PlacementGroup; |
| 41 | import org.jclouds.aws.ec2.domain.PlacementGroup.State; |
| 42 | import org.jclouds.aws.util.AWSUtils; |
| 43 | import org.jclouds.collect.Memoized; |
| 44 | import org.jclouds.compute.ComputeServiceContext; |
| 45 | import org.jclouds.compute.RunNodesException; |
| 46 | import org.jclouds.compute.callables.RunScriptOnNode; |
| 47 | import org.jclouds.compute.domain.Hardware; |
| 48 | import org.jclouds.compute.domain.Image; |
| 49 | import org.jclouds.compute.domain.NodeMetadata; |
| 50 | import org.jclouds.compute.domain.NodeMetadataBuilder; |
| 51 | import org.jclouds.compute.domain.Template; |
| 52 | import org.jclouds.compute.domain.TemplateBuilder; |
| 53 | import org.jclouds.compute.internal.PersistNodeCredentials; |
| 54 | import org.jclouds.compute.options.TemplateOptions; |
| 55 | import org.jclouds.compute.reference.ComputeServiceConstants.Timeouts; |
| 56 | import org.jclouds.compute.strategy.CreateNodesInGroupThenAddToSet; |
| 57 | import org.jclouds.compute.strategy.DestroyNodeStrategy; |
| 58 | import org.jclouds.compute.strategy.GetNodeMetadataStrategy; |
| 59 | import org.jclouds.compute.strategy.InitializeRunScriptOnNodeOrPlaceInBadMap; |
| 60 | import org.jclouds.compute.strategy.ListNodesStrategy; |
| 61 | import org.jclouds.compute.strategy.RebootNodeStrategy; |
| 62 | import org.jclouds.compute.strategy.ResumeNodeStrategy; |
| 63 | import org.jclouds.compute.strategy.SuspendNodeStrategy; |
| 64 | import org.jclouds.domain.Credentials; |
| 65 | import org.jclouds.domain.Location; |
| 66 | import org.jclouds.ec2.compute.EC2ComputeService; |
| 67 | import org.jclouds.ec2.compute.domain.RegionAndName; |
| 68 | import org.jclouds.ec2.compute.options.EC2TemplateOptions; |
| 69 | import org.jclouds.ec2.domain.KeyPair; |
| 70 | import org.jclouds.scriptbuilder.functions.InitAdminAccess; |
| 71 | import org.jclouds.util.Preconditions2; |
| 72 | |
| 73 | import com.google.common.annotations.VisibleForTesting; |
| 74 | import com.google.common.base.Function; |
| 75 | import com.google.common.base.Predicate; |
| 76 | import com.google.common.base.Supplier; |
| 77 | import com.google.common.cache.Cache; |
| 78 | import com.google.common.collect.ImmutableMap; |
| 79 | import com.google.common.collect.ImmutableSet; |
| 80 | import com.google.common.collect.Iterables; |
| 81 | |
| 82 | /** |
| 83 | * @author Adrian Cole |
| 84 | */ |
| 85 | @Singleton |
| 86 | public class AWSEC2ComputeService extends EC2ComputeService { |
| 87 | |
| 88 | private final Cache<RegionAndName, String> placementGroupMap; |
| 89 | private final Predicate<PlacementGroup> placementGroupDeleted; |
| 90 | private final AWSEC2Client ec2Client; |
| 91 | private final AWSEC2AsyncClient aclient; |
| 92 | private final boolean generateInstanceNames; |
| 93 | |
| 94 | @Inject |
| 95 | protected AWSEC2ComputeService(ComputeServiceContext context, Map<String, Credentials> credentialStore, |
| 96 | @Memoized Supplier<Set<? extends Image>> images, @Memoized Supplier<Set<? extends Hardware>> sizes, |
| 97 | @Memoized Supplier<Set<? extends Location>> locations, ListNodesStrategy listNodesStrategy, |
| 98 | GetNodeMetadataStrategy getNodeMetadataStrategy, |
| 99 | CreateNodesInGroupThenAddToSet runNodesAndAddToSetStrategy, RebootNodeStrategy rebootNodeStrategy, |
| 100 | DestroyNodeStrategy destroyNodeStrategy, ResumeNodeStrategy startNodeStrategy, |
| 101 | SuspendNodeStrategy stopNodeStrategy, Provider<TemplateBuilder> templateBuilderProvider, |
| 102 | Provider<TemplateOptions> templateOptionsProvider, |
| 103 | @Named("NODE_RUNNING") Predicate<NodeMetadata> nodeRunning, |
| 104 | @Named("NODE_TERMINATED") Predicate<NodeMetadata> nodeTerminated, |
| 105 | @Named("NODE_SUSPENDED") Predicate<NodeMetadata> nodeSuspended, |
| 106 | InitializeRunScriptOnNodeOrPlaceInBadMap.Factory initScriptRunnerFactory, |
| 107 | RunScriptOnNode.Factory runScriptOnNodeFactory, InitAdminAccess initAdminAccess, |
| 108 | PersistNodeCredentials persistNodeCredentials, Timeouts timeouts, |
| 109 | @Named(Constants.PROPERTY_USER_THREADS) ExecutorService executor, AWSEC2Client ec2Client, |
| 110 | ConcurrentMap<RegionAndName, KeyPair> credentialsMap, |
| 111 | @Named("SECURITY") Cache<RegionAndName, String> securityGroupMap, |
| 112 | @Named("PLACEMENT") Cache<RegionAndName, String> placementGroupMap, |
| 113 | @Named("DELETED") Predicate<PlacementGroup> placementGroupDeleted, |
| 114 | @Named(PROPERTY_EC2_GENERATE_INSTANCE_NAMES) boolean generateInstanceNames, AWSEC2AsyncClient aclient) { |
| 115 | super(context, credentialStore, images, sizes, locations, listNodesStrategy, getNodeMetadataStrategy, |
| 116 | runNodesAndAddToSetStrategy, rebootNodeStrategy, destroyNodeStrategy, startNodeStrategy, |
| 117 | stopNodeStrategy, templateBuilderProvider, templateOptionsProvider, nodeRunning, nodeTerminated, |
| 118 | nodeSuspended, initScriptRunnerFactory, runScriptOnNodeFactory, initAdminAccess, persistNodeCredentials, |
| 119 | timeouts, executor, ec2Client, credentialsMap, securityGroupMap); |
| 120 | this.ec2Client = ec2Client; |
| 121 | this.placementGroupMap = placementGroupMap; |
| 122 | this.placementGroupDeleted = placementGroupDeleted; |
| 123 | this.generateInstanceNames = generateInstanceNames; |
| 124 | this.aclient = checkNotNull(aclient, "aclient"); |
| 125 | } |
| 126 | |
| 127 | @Override |
| 128 | public Set<? extends NodeMetadata> createNodesInGroup(String group, int count, final Template template) |
| 129 | throws RunNodesException { |
| 130 | Set<? extends NodeMetadata> nodes = super.createNodesInGroup(group, count, template); |
| 131 | // tags from spot requests do not propagate to running instances |
| 132 | // automatically |
| 133 | if (templateWasASpotRequestWithUserMetadata(template)) { |
| 134 | addTagsToNodesFromUserMetadataInTemplate(nodes, group, template); |
| 135 | nodes = addUserMetadataFromTemplateOptionsToNodes(template, group, nodes); |
| 136 | } |
| 137 | return nodes; |
| 138 | } |
| 139 | |
| 140 | protected void addTagsToNodesFromUserMetadataInTemplate(Set<? extends NodeMetadata> nodes, String group, |
| 141 | final Template template) { |
| 142 | String region = AWSUtils.getRegionFromLocationOrNull(template.getLocation()); |
| 143 | if (template.getOptions().getUserMetadata().size() > 0 || generateInstanceNames) { |
| 144 | for (String id : transform(nodes, new Function<NodeMetadata, String>() { |
| 145 | |
| 146 | @Override |
| 147 | public String apply(NodeMetadata arg0) { |
| 148 | return arg0.getProviderId(); |
| 149 | } |
| 150 | |
| 151 | })) |
| 152 | aclient.getTagServices().createTagsInRegion(region, ImmutableSet.of(id), |
| 153 | metadataForId(id, group, template.getOptions().getUserMetadata())); |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | private Map<String, String> metadataForId(String id, String group, Map<String, String> metadata) { |
| 158 | return generateInstanceNames && !metadata.containsKey("Name") ? ImmutableMap.<String, String> builder().putAll( |
| 159 | metadata).put("Name", id.replaceAll(".*-", group + "-")).build() : metadata; |
| 160 | } |
| 161 | |
| 162 | protected boolean templateWasASpotRequestWithUserMetadata(final Template template) { |
| 163 | return template.getOptions().getUserMetadata().size() > 0 |
| 164 | && AWSEC2TemplateOptions.class.cast(template.getOptions()).getSpotPrice() != null; |
| 165 | } |
| 166 | |
| 167 | protected Set<? extends NodeMetadata> addUserMetadataFromTemplateOptionsToNodes(final Template template, |
| 168 | final String group, Set<? extends NodeMetadata> nodes) { |
| 169 | nodes = ImmutableSet.copyOf(Iterables.transform(nodes, new Function<NodeMetadata, NodeMetadata>() { |
| 170 | |
| 171 | @Override |
| 172 | public NodeMetadata apply(NodeMetadata arg0) { |
| 173 | Map<String, String> md = metadataForId(arg0.getProviderId(), group, template.getOptions().getUserMetadata()); |
| 174 | return NodeMetadataBuilder.fromNodeMetadata(arg0).name(md.get("Name")).userMetadata(md).build(); |
| 175 | } |
| 176 | |
| 177 | })); |
| 178 | return nodes; |
| 179 | } |
| 180 | |
| 181 | @VisibleForTesting |
| 182 | void deletePlacementGroup(String region, String group) { |
| 183 | Preconditions2.checkNotEmpty(group, "group"); |
| 184 | // placementGroupName must be unique within an account per |
| 185 | // http://docs.amazonwebservices.com/AWSEC2/latest/UserGuide/index.html?using_cluster_computing.html |
| 186 | String placementGroup = String.format("jclouds#%s#%s", group, region); |
| 187 | try { |
| 188 | if (ec2Client.getPlacementGroupServices().describePlacementGroupsInRegion(region, placementGroup).size() > 0) { |
| 189 | logger.debug(">> deleting placementGroup(%s)", placementGroup); |
| 190 | try { |
| 191 | ec2Client.getPlacementGroupServices().deletePlacementGroupInRegion(region, placementGroup); |
| 192 | checkState(placementGroupDeleted.apply(new PlacementGroup(region, placementGroup, "cluster", |
| 193 | State.PENDING)), String.format("placementGroup region(%s) name(%s) failed to delete", region, |
| 194 | placementGroup)); |
| 195 | placementGroupMap.invalidate(new RegionAndName(region, placementGroup)); |
| 196 | logger.debug("<< deleted placementGroup(%s)", placementGroup); |
| 197 | } catch (IllegalStateException e) { |
| 198 | logger.debug("<< inUse placementGroup(%s)", placementGroup); |
| 199 | } |
| 200 | } |
| 201 | } catch (UnsupportedOperationException e) { |
| 202 | logger.trace("<< placementGroups unsupported in region %s", region); |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | @Override |
| 207 | protected void cleanUpIncidentalResources(Entry<String, String> regionTag) { |
| 208 | super.cleanUpIncidentalResources(regionTag); |
| 209 | deletePlacementGroup(regionTag.getKey(), regionTag.getValue()); |
| 210 | } |
| 211 | |
| 212 | /** |
| 213 | * returns template options, except of type {@link EC2TemplateOptions}. |
| 214 | */ |
| 215 | @Override |
| 216 | public EC2TemplateOptions templateOptions() { |
| 217 | return EC2TemplateOptions.class.cast(super.templateOptions()); |
| 218 | } |
| 219 | |
| 220 | } |