| 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; |
| 20 | |
| 21 | import static com.google.common.base.Preconditions.checkState; |
| 22 | |
| 23 | import java.util.Map; |
| 24 | import java.util.Map.Entry; |
| 25 | import java.util.Set; |
| 26 | import java.util.concurrent.ExecutorService; |
| 27 | |
| 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.Constants; |
| 34 | import org.jclouds.aws.ec2.AWSEC2Client; |
| 35 | import org.jclouds.aws.ec2.domain.PlacementGroup; |
| 36 | import org.jclouds.aws.ec2.domain.PlacementGroup.State; |
| 37 | import org.jclouds.collect.Memoized; |
| 38 | import org.jclouds.compute.ComputeServiceContext; |
| 39 | import org.jclouds.compute.callables.RunScriptOnNode; |
| 40 | import org.jclouds.compute.domain.Hardware; |
| 41 | import org.jclouds.compute.domain.Image; |
| 42 | import org.jclouds.compute.domain.NodeMetadata; |
| 43 | import org.jclouds.compute.domain.TemplateBuilder; |
| 44 | import org.jclouds.compute.internal.PersistNodeCredentials; |
| 45 | import org.jclouds.compute.options.TemplateOptions; |
| 46 | import org.jclouds.compute.reference.ComputeServiceConstants.Timeouts; |
| 47 | import org.jclouds.compute.strategy.CreateNodesInGroupThenAddToSet; |
| 48 | import org.jclouds.compute.strategy.DestroyNodeStrategy; |
| 49 | import org.jclouds.compute.strategy.GetNodeMetadataStrategy; |
| 50 | import org.jclouds.compute.strategy.InitializeRunScriptOnNodeOrPlaceInBadMap; |
| 51 | import org.jclouds.compute.strategy.ListNodesStrategy; |
| 52 | import org.jclouds.compute.strategy.RebootNodeStrategy; |
| 53 | import org.jclouds.compute.strategy.ResumeNodeStrategy; |
| 54 | import org.jclouds.compute.strategy.SuspendNodeStrategy; |
| 55 | import org.jclouds.domain.Credentials; |
| 56 | import org.jclouds.domain.Location; |
| 57 | import org.jclouds.ec2.compute.EC2ComputeService; |
| 58 | import org.jclouds.ec2.compute.domain.RegionAndName; |
| 59 | import org.jclouds.ec2.compute.options.EC2TemplateOptions; |
| 60 | import org.jclouds.ec2.domain.KeyPair; |
| 61 | import org.jclouds.scriptbuilder.functions.InitAdminAccess; |
| 62 | import org.jclouds.util.Preconditions2; |
| 63 | |
| 64 | import com.google.common.annotations.VisibleForTesting; |
| 65 | import com.google.common.base.Predicate; |
| 66 | import com.google.common.base.Supplier; |
| 67 | |
| 68 | /** |
| 69 | * @author Adrian Cole |
| 70 | */ |
| 71 | @Singleton |
| 72 | public class AWSEC2ComputeService extends EC2ComputeService { |
| 73 | |
| 74 | private final Map<RegionAndName, String> placementGroupMap; |
| 75 | private final Predicate<PlacementGroup> placementGroupDeleted; |
| 76 | private final AWSEC2Client ec2Client; |
| 77 | |
| 78 | @Inject |
| 79 | protected AWSEC2ComputeService(ComputeServiceContext context, Map<String, Credentials> credentialStore, |
| 80 | @Memoized Supplier<Set<? extends Image>> images, @Memoized Supplier<Set<? extends Hardware>> sizes, |
| 81 | @Memoized Supplier<Set<? extends Location>> locations, ListNodesStrategy listNodesStrategy, |
| 82 | GetNodeMetadataStrategy getNodeMetadataStrategy, CreateNodesInGroupThenAddToSet runNodesAndAddToSetStrategy, |
| 83 | RebootNodeStrategy rebootNodeStrategy, DestroyNodeStrategy destroyNodeStrategy, |
| 84 | ResumeNodeStrategy startNodeStrategy, SuspendNodeStrategy stopNodeStrategy, |
| 85 | Provider<TemplateBuilder> templateBuilderProvider, Provider<TemplateOptions> templateOptionsProvider, |
| 86 | @Named("NODE_RUNNING") Predicate<NodeMetadata> nodeRunning, |
| 87 | @Named("NODE_TERMINATED") Predicate<NodeMetadata> nodeTerminated, |
| 88 | @Named("NODE_SUSPENDED") Predicate<NodeMetadata> nodeSuspended, |
| 89 | InitializeRunScriptOnNodeOrPlaceInBadMap.Factory initScriptRunnerFactory, |
| 90 | RunScriptOnNode.Factory runScriptOnNodeFactory, InitAdminAccess initAdminAccess, |
| 91 | PersistNodeCredentials persistNodeCredentials, Timeouts timeouts, |
| 92 | @Named(Constants.PROPERTY_USER_THREADS) ExecutorService executor, AWSEC2Client ec2Client, |
| 93 | Map<RegionAndName, KeyPair> credentialsMap, @Named("SECURITY") Map<RegionAndName, String> securityGroupMap, |
| 94 | @Named("PLACEMENT") Map<RegionAndName, String> placementGroupMap, |
| 95 | @Named("DELETED") Predicate<PlacementGroup> placementGroupDeleted) { |
| 96 | super(context, credentialStore, images, sizes, locations, listNodesStrategy, getNodeMetadataStrategy, |
| 97 | runNodesAndAddToSetStrategy, rebootNodeStrategy, destroyNodeStrategy, startNodeStrategy, stopNodeStrategy, |
| 98 | templateBuilderProvider, templateOptionsProvider, nodeRunning, nodeTerminated, nodeSuspended, |
| 99 | initScriptRunnerFactory, runScriptOnNodeFactory, initAdminAccess, persistNodeCredentials, timeouts, |
| 100 | executor, ec2Client, credentialsMap, securityGroupMap); |
| 101 | this.ec2Client = ec2Client; |
| 102 | this.placementGroupMap = placementGroupMap; |
| 103 | this.placementGroupDeleted = placementGroupDeleted; |
| 104 | } |
| 105 | |
| 106 | @VisibleForTesting |
| 107 | void deletePlacementGroup(String region, String group) { |
| 108 | Preconditions2.checkNotEmpty(group, "group"); |
| 109 | // placementGroupName must be unique within an account per |
| 110 | // http://docs.amazonwebservices.com/AWSEC2/latest/UserGuide/index.html?using_cluster_computing.html |
| 111 | String placementGroup = String.format("jclouds#%s#%s", group, region); |
| 112 | try { |
| 113 | if (ec2Client.getPlacementGroupServices().describePlacementGroupsInRegion(region, placementGroup).size() > 0) { |
| 114 | logger.debug(">> deleting placementGroup(%s)", placementGroup); |
| 115 | try { |
| 116 | ec2Client.getPlacementGroupServices().deletePlacementGroupInRegion(region, placementGroup); |
| 117 | checkState( |
| 118 | placementGroupDeleted.apply(new PlacementGroup(region, placementGroup, "cluster", State.PENDING)), |
| 119 | String.format("placementGroup region(%s) name(%s) failed to delete", region, placementGroup)); |
| 120 | placementGroupMap.remove(new RegionAndName(region, placementGroup)); |
| 121 | logger.debug("<< deleted placementGroup(%s)", placementGroup); |
| 122 | } catch (IllegalStateException e) { |
| 123 | logger.debug("<< inUse placementGroup(%s)", placementGroup); |
| 124 | } |
| 125 | } |
| 126 | } catch (UnsupportedOperationException e) { |
| 127 | logger.trace("<< placementGroups unsupported in region %s", region); |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | @Override |
| 132 | protected void cleanUpIncidentalResources(Entry<String, String> regionTag) { |
| 133 | super.cleanUpIncidentalResources(regionTag); |
| 134 | deletePlacementGroup(regionTag.getKey(), regionTag.getValue()); |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * returns template options, except of type {@link EC2TemplateOptions}. |
| 139 | */ |
| 140 | @Override |
| 141 | public EC2TemplateOptions templateOptions() { |
| 142 | return EC2TemplateOptions.class.cast(super.templateOptions()); |
| 143 | } |
| 144 | |
| 145 | } |