| 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.ec2.compute; |
| 20 | |
| 21 | import static com.google.common.collect.Iterables.concat; |
| 22 | import static com.google.common.collect.Iterables.filter; |
| 23 | import static com.google.common.collect.Iterables.transform; |
| 24 | import static org.jclouds.util.Preconditions2.checkNotEmpty; |
| 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.util.AWSUtils; |
| 39 | import org.jclouds.collect.Memoized; |
| 40 | import org.jclouds.compute.ComputeServiceContext; |
| 41 | import org.jclouds.compute.callables.RunScriptOnNode; |
| 42 | import org.jclouds.compute.domain.Hardware; |
| 43 | import org.jclouds.compute.domain.Image; |
| 44 | import org.jclouds.compute.domain.NodeMetadata; |
| 45 | import org.jclouds.compute.domain.TemplateBuilder; |
| 46 | import org.jclouds.compute.internal.BaseComputeService; |
| 47 | import org.jclouds.compute.internal.PersistNodeCredentials; |
| 48 | import org.jclouds.compute.options.TemplateOptions; |
| 49 | import org.jclouds.compute.reference.ComputeServiceConstants.Timeouts; |
| 50 | import org.jclouds.compute.strategy.CreateNodesInGroupThenAddToSet; |
| 51 | import org.jclouds.compute.strategy.DestroyNodeStrategy; |
| 52 | import org.jclouds.compute.strategy.GetNodeMetadataStrategy; |
| 53 | import org.jclouds.compute.strategy.InitializeRunScriptOnNodeOrPlaceInBadMap; |
| 54 | import org.jclouds.compute.strategy.ListNodesStrategy; |
| 55 | import org.jclouds.compute.strategy.RebootNodeStrategy; |
| 56 | import org.jclouds.compute.strategy.ResumeNodeStrategy; |
| 57 | import org.jclouds.compute.strategy.SuspendNodeStrategy; |
| 58 | import org.jclouds.domain.Credentials; |
| 59 | import org.jclouds.domain.Location; |
| 60 | import org.jclouds.ec2.EC2Client; |
| 61 | import org.jclouds.ec2.compute.domain.RegionAndName; |
| 62 | import org.jclouds.ec2.compute.domain.RegionNameAndIngressRules; |
| 63 | import org.jclouds.ec2.compute.options.EC2TemplateOptions; |
| 64 | import org.jclouds.ec2.domain.KeyPair; |
| 65 | import org.jclouds.ec2.domain.RunningInstance; |
| 66 | import org.jclouds.scriptbuilder.functions.InitAdminAccess; |
| 67 | |
| 68 | import com.google.common.annotations.VisibleForTesting; |
| 69 | import com.google.common.base.Function; |
| 70 | import com.google.common.base.Predicate; |
| 71 | import com.google.common.base.Supplier; |
| 72 | import com.google.common.cache.Cache; |
| 73 | import com.google.common.collect.ImmutableMultimap; |
| 74 | import com.google.common.collect.ImmutableSet; |
| 75 | import com.google.common.collect.ImmutableMultimap.Builder; |
| 76 | |
| 77 | /** |
| 78 | * @author Adrian Cole |
| 79 | */ |
| 80 | @Singleton |
| 81 | public class EC2ComputeService extends BaseComputeService { |
| 82 | private final EC2Client ec2Client; |
| 83 | private final ConcurrentMap<RegionAndName, KeyPair> credentialsMap; |
| 84 | private final Cache<RegionAndName, String> securityGroupMap; |
| 85 | |
| 86 | @Inject |
| 87 | protected EC2ComputeService(ComputeServiceContext context, Map<String, Credentials> credentialStore, |
| 88 | @Memoized Supplier<Set<? extends Image>> images, @Memoized Supplier<Set<? extends Hardware>> sizes, |
| 89 | @Memoized Supplier<Set<? extends Location>> locations, ListNodesStrategy listNodesStrategy, |
| 90 | GetNodeMetadataStrategy getNodeMetadataStrategy, CreateNodesInGroupThenAddToSet runNodesAndAddToSetStrategy, |
| 91 | RebootNodeStrategy rebootNodeStrategy, DestroyNodeStrategy destroyNodeStrategy, |
| 92 | ResumeNodeStrategy startNodeStrategy, SuspendNodeStrategy stopNodeStrategy, |
| 93 | Provider<TemplateBuilder> templateBuilderProvider, Provider<TemplateOptions> templateOptionsProvider, |
| 94 | @Named("NODE_RUNNING") Predicate<NodeMetadata> nodeRunning, |
| 95 | @Named("NODE_TERMINATED") Predicate<NodeMetadata> nodeTerminated, |
| 96 | @Named("NODE_SUSPENDED") Predicate<NodeMetadata> nodeSuspended, |
| 97 | InitializeRunScriptOnNodeOrPlaceInBadMap.Factory initScriptRunnerFactory, |
| 98 | RunScriptOnNode.Factory runScriptOnNodeFactory, InitAdminAccess initAdminAccess, |
| 99 | PersistNodeCredentials persistNodeCredentials, Timeouts timeouts, |
| 100 | @Named(Constants.PROPERTY_USER_THREADS) ExecutorService executor, EC2Client ec2Client, |
| 101 | ConcurrentMap<RegionAndName, KeyPair> credentialsMap, @Named("SECURITY") Cache<RegionAndName, String> securityGroupMap) { |
| 102 | super(context, credentialStore, images, sizes, locations, listNodesStrategy, getNodeMetadataStrategy, |
| 103 | runNodesAndAddToSetStrategy, rebootNodeStrategy, destroyNodeStrategy, startNodeStrategy, stopNodeStrategy, |
| 104 | templateBuilderProvider, templateOptionsProvider, nodeRunning, nodeTerminated, nodeSuspended, |
| 105 | initScriptRunnerFactory, initAdminAccess, runScriptOnNodeFactory, persistNodeCredentials, timeouts, |
| 106 | executor); |
| 107 | this.ec2Client = ec2Client; |
| 108 | this.credentialsMap = credentialsMap; |
| 109 | this.securityGroupMap = securityGroupMap; |
| 110 | } |
| 111 | |
| 112 | @VisibleForTesting |
| 113 | void deleteSecurityGroup(String region, String group) { |
| 114 | checkNotEmpty(group, "group"); |
| 115 | String groupName = String.format("jclouds#%s#%s", group, region); |
| 116 | if (ec2Client.getSecurityGroupServices().describeSecurityGroupsInRegion(region, groupName).size() > 0) { |
| 117 | logger.debug(">> deleting securityGroup(%s)", groupName); |
| 118 | try { |
| 119 | ec2Client.getSecurityGroupServices().deleteSecurityGroupInRegion(region, groupName); |
| 120 | // TODO: test this clear happens |
| 121 | securityGroupMap.invalidate(new RegionNameAndIngressRules(region, groupName, null, false)); |
| 122 | logger.debug("<< deleted securityGroup(%s)", groupName); |
| 123 | } catch (IllegalStateException e) { |
| 124 | logger.debug("<< inUse securityGroup(%s)", groupName); |
| 125 | } |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | @VisibleForTesting |
| 130 | void deleteKeyPair(String region, String group) { |
| 131 | for (KeyPair keyPair : ec2Client.getKeyPairServices().describeKeyPairsInRegion(region)) { |
| 132 | if ( |
| 133 | // when the keypair is unique per group |
| 134 | keyPair.getKeyName().equals("jclouds#" + group) |
| 135 | || keyPair.getKeyName().matches(String.format("jclouds#%s#%s", group, "[0-9a-f]+")) |
| 136 | // old keypair pattern too verbose as it has an unnecessary |
| 137 | // region qualifier |
| 138 | || keyPair.getKeyName().matches(String.format("jclouds#%s#%s#%s", group, region, "[0-9a-f]+"))) { |
| 139 | Set<String> instancesUsingKeyPair = extractIdsFromInstances(filter(concat(ec2Client.getInstanceServices() |
| 140 | .describeInstancesInRegion(region)), usingKeyPairAndNotDead(keyPair))); |
| 141 | if (instancesUsingKeyPair.size() > 0) { |
| 142 | logger.debug("<< inUse keyPair(%s), by (%s)", keyPair.getKeyName(), instancesUsingKeyPair); |
| 143 | } else { |
| 144 | logger.debug(">> deleting keyPair(%s)", keyPair.getKeyName()); |
| 145 | ec2Client.getKeyPairServices().deleteKeyPairInRegion(region, keyPair.getKeyName()); |
| 146 | // TODO: test this clear happens |
| 147 | credentialsMap.remove(new RegionAndName(region, keyPair.getKeyName())); |
| 148 | credentialsMap.remove(new RegionAndName(region, group)); |
| 149 | logger.debug("<< deleted keyPair(%s)", keyPair.getKeyName()); |
| 150 | } |
| 151 | } |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | protected ImmutableSet<String> extractIdsFromInstances(Iterable<? extends RunningInstance> deadOnes) { |
| 156 | return ImmutableSet.copyOf(transform(deadOnes, new Function<RunningInstance, String>() { |
| 157 | |
| 158 | @Override |
| 159 | public String apply(RunningInstance input) { |
| 160 | return input.getId(); |
| 161 | } |
| 162 | |
| 163 | })); |
| 164 | } |
| 165 | |
| 166 | protected Predicate<RunningInstance> usingKeyPairAndNotDead(final KeyPair keyPair) { |
| 167 | return new Predicate<RunningInstance>() { |
| 168 | |
| 169 | @Override |
| 170 | public boolean apply(RunningInstance input) { |
| 171 | switch (input.getInstanceState()) { |
| 172 | case TERMINATED: |
| 173 | case SHUTTING_DOWN: |
| 174 | return false; |
| 175 | } |
| 176 | return keyPair.getKeyName().equals(input.getKeyName()); |
| 177 | } |
| 178 | |
| 179 | }; |
| 180 | } |
| 181 | |
| 182 | /** |
| 183 | * like {@link BaseComputeService#destroyNodesMatching} except that this will |
| 184 | * clean implicit keypairs and security groups. |
| 185 | */ |
| 186 | @Override |
| 187 | public Set<? extends NodeMetadata> destroyNodesMatching(Predicate<NodeMetadata> filter) { |
| 188 | Set<? extends NodeMetadata> deadOnes = super.destroyNodesMatching(filter); |
| 189 | Builder<String, String> regionGroups = ImmutableMultimap.<String, String> builder(); |
| 190 | for (NodeMetadata nodeMetadata : deadOnes) { |
| 191 | if (nodeMetadata.getGroup() != null) |
| 192 | regionGroups.put(AWSUtils.parseHandle(nodeMetadata.getId())[0], nodeMetadata.getGroup()); |
| 193 | } |
| 194 | for (Entry<String, String> regionGroup : regionGroups.build().entries()) { |
| 195 | cleanUpIncidentalResources(regionGroup); |
| 196 | } |
| 197 | return deadOnes; |
| 198 | } |
| 199 | |
| 200 | protected void cleanUpIncidentalResources(Entry<String, String> regionGroup) { |
| 201 | deleteKeyPair(regionGroup.getKey(), regionGroup.getValue()); |
| 202 | deleteSecurityGroup(regionGroup.getKey(), regionGroup.getValue()); |
| 203 | } |
| 204 | |
| 205 | /** |
| 206 | * returns template options, except of type {@link EC2TemplateOptions}. |
| 207 | */ |
| 208 | @Override |
| 209 | public EC2TemplateOptions templateOptions() { |
| 210 | return EC2TemplateOptions.class.cast(super.templateOptions()); |
| 211 | } |
| 212 | |
| 213 | } |