| 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.Predicates.and; |
| 22 | import static com.google.common.base.Predicates.or; |
| 23 | |
| 24 | import java.util.concurrent.ConcurrentMap; |
| 25 | |
| 26 | import javax.annotation.Resource; |
| 27 | import javax.inject.Inject; |
| 28 | import javax.inject.Named; |
| 29 | import javax.inject.Provider; |
| 30 | import javax.inject.Singleton; |
| 31 | |
| 32 | import org.jclouds.aws.ec2.compute.AWSEC2TemplateOptions; |
| 33 | import org.jclouds.aws.ec2.domain.RegionNameAndPublicKeyMaterial; |
| 34 | import org.jclouds.aws.ec2.functions.CreatePlacementGroupIfNeeded; |
| 35 | import org.jclouds.aws.ec2.options.AWSRunInstancesOptions; |
| 36 | import org.jclouds.compute.domain.Template; |
| 37 | import org.jclouds.compute.options.TemplateOptions; |
| 38 | import org.jclouds.compute.reference.ComputeServiceConstants; |
| 39 | import org.jclouds.ec2.compute.domain.RegionAndName; |
| 40 | import org.jclouds.ec2.compute.options.EC2TemplateOptions; |
| 41 | import org.jclouds.ec2.compute.strategy.CreateKeyPairAndSecurityGroupsAsNeededAndReturnRunOptions; |
| 42 | import org.jclouds.ec2.domain.KeyPair; |
| 43 | import org.jclouds.ec2.options.RunInstancesOptions; |
| 44 | import org.jclouds.logging.Logger; |
| 45 | |
| 46 | import com.google.common.annotations.VisibleForTesting; |
| 47 | import com.google.common.base.Function; |
| 48 | import com.google.common.base.Predicate; |
| 49 | import com.google.common.cache.Cache; |
| 50 | |
| 51 | /** |
| 52 | * |
| 53 | * @author Adrian Cole |
| 54 | */ |
| 55 | @Singleton |
| 56 | public class CreateKeyPairPlacementAndSecurityGroupsAsNeededAndReturnRunOptions extends |
| 57 | CreateKeyPairAndSecurityGroupsAsNeededAndReturnRunOptions { |
| 58 | @Resource |
| 59 | @Named(ComputeServiceConstants.COMPUTE_LOGGER) |
| 60 | protected Logger logger = Logger.NULL; |
| 61 | @VisibleForTesting |
| 62 | final Cache<RegionAndName, String> placementGroupMap; |
| 63 | @VisibleForTesting |
| 64 | final CreatePlacementGroupIfNeeded createPlacementGroupIfNeeded; |
| 65 | @VisibleForTesting |
| 66 | final Function<RegionNameAndPublicKeyMaterial, KeyPair> importExistingKeyPair; |
| 67 | |
| 68 | @Inject |
| 69 | public CreateKeyPairPlacementAndSecurityGroupsAsNeededAndReturnRunOptions( |
| 70 | Function<RegionAndName, KeyPair> makeKeyPair, ConcurrentMap<RegionAndName, KeyPair> credentialsMap, |
| 71 | @Named("SECURITY") Cache<RegionAndName, String> securityGroupMap, |
| 72 | Provider<RunInstancesOptions> optionsProvider, |
| 73 | @Named("PLACEMENT") Cache<RegionAndName, String> placementGroupMap, |
| 74 | CreatePlacementGroupIfNeeded createPlacementGroupIfNeeded, |
| 75 | Function<RegionNameAndPublicKeyMaterial, KeyPair> importExistingKeyPair) { |
| 76 | super(makeKeyPair, credentialsMap, securityGroupMap, optionsProvider); |
| 77 | this.placementGroupMap = placementGroupMap; |
| 78 | this.createPlacementGroupIfNeeded = createPlacementGroupIfNeeded; |
| 79 | this.importExistingKeyPair = importExistingKeyPair; |
| 80 | } |
| 81 | |
| 82 | public AWSRunInstancesOptions execute(String region, String group, Template template) { |
| 83 | AWSRunInstancesOptions instanceOptions = AWSRunInstancesOptions.class |
| 84 | .cast(super.execute(region, group, template)); |
| 85 | |
| 86 | String placementGroupName = template.getHardware().getId().startsWith("cc") ? createNewPlacementGroupUnlessUserSpecifiedOtherwise( |
| 87 | region, group, template.getOptions()) |
| 88 | : null; |
| 89 | |
| 90 | if (placementGroupName != null) |
| 91 | instanceOptions.inPlacementGroup(placementGroupName); |
| 92 | |
| 93 | if (AWSEC2TemplateOptions.class.cast(template.getOptions()).isMonitoringEnabled()) |
| 94 | instanceOptions.enableMonitoring(); |
| 95 | |
| 96 | return instanceOptions; |
| 97 | } |
| 98 | |
| 99 | @VisibleForTesting |
| 100 | String createNewPlacementGroupUnlessUserSpecifiedOtherwise(String region, String group, TemplateOptions options) { |
| 101 | String placementGroupName = null; |
| 102 | boolean shouldAutomaticallyCreatePlacementGroup = true; |
| 103 | if (options instanceof EC2TemplateOptions) { |
| 104 | placementGroupName = AWSEC2TemplateOptions.class.cast(options).getPlacementGroup(); |
| 105 | if (placementGroupName == null) |
| 106 | shouldAutomaticallyCreatePlacementGroup = AWSEC2TemplateOptions.class.cast(options) |
| 107 | .shouldAutomaticallyCreatePlacementGroup(); |
| 108 | } |
| 109 | if (placementGroupName == null && shouldAutomaticallyCreatePlacementGroup) { |
| 110 | // placementGroupName must be unique within an account per |
| 111 | // http://docs.amazonwebservices.com/AWSEC2/latest/UserGuide/index.html?using_cluster_computing.html |
| 112 | placementGroupName = String.format("jclouds#%s#%s", group, region); |
| 113 | RegionAndName regionAndName = new RegionAndName(region, placementGroupName); |
| 114 | // make this entry as needed |
| 115 | placementGroupMap.getUnchecked(regionAndName); |
| 116 | } |
| 117 | return placementGroupName; |
| 118 | } |
| 119 | |
| 120 | @Override |
| 121 | public String createNewKeyPairUnlessUserSpecifiedOtherwise(String region, String group, TemplateOptions options) { |
| 122 | RegionAndName key = new RegionAndName(region, group); |
| 123 | KeyPair pair; |
| 124 | if (and(hasPublicKeyMaterial, or(doesntNeedSshAfterImportingPublicKey, hasLoginCredential)).apply(options)) { |
| 125 | pair = importExistingKeyPair.apply(new RegionNameAndPublicKeyMaterial(region, group, options.getPublicKey())); |
| 126 | options.dontAuthorizePublicKey(); |
| 127 | if (hasLoginCredential.apply(options)) |
| 128 | pair = pair.toBuilder().keyMaterial(options.getOverridingCredentials().credential).build(); |
| 129 | credentialsMap.put(key, pair); |
| 130 | } else { |
| 131 | if (hasPublicKeyMaterial.apply(options)) { |
| 132 | logger |
| 133 | .warn("to avoid creating temporary keys in aws-ec2, use templateOption overrideLoginCredentialWith(id_rsa)"); |
| 134 | } |
| 135 | return super.createNewKeyPairUnlessUserSpecifiedOtherwise(region, group, options); |
| 136 | } |
| 137 | return pair.getKeyName(); |
| 138 | } |
| 139 | |
| 140 | public static final Predicate<TemplateOptions> hasPublicKeyMaterial = new Predicate<TemplateOptions>() { |
| 141 | |
| 142 | @Override |
| 143 | public boolean apply(TemplateOptions options) { |
| 144 | return options.getPublicKey() != null; |
| 145 | } |
| 146 | |
| 147 | }; |
| 148 | |
| 149 | public static final Predicate<TemplateOptions> doesntNeedSshAfterImportingPublicKey = new Predicate<TemplateOptions>() { |
| 150 | |
| 151 | @Override |
| 152 | public boolean apply(TemplateOptions options) { |
| 153 | return (options.getRunScript() == null && options.getPrivateKey() == null); |
| 154 | } |
| 155 | |
| 156 | }; |
| 157 | |
| 158 | public static final Predicate<TemplateOptions> hasLoginCredential = new Predicate<TemplateOptions>() { |
| 159 | |
| 160 | @Override |
| 161 | public boolean apply(TemplateOptions options) { |
| 162 | return options.getOverridingCredentials() != null && options.getOverridingCredentials().credential != null; |
| 163 | } |
| 164 | |
| 165 | }; |
| 166 | |
| 167 | @Override |
| 168 | protected boolean userSpecifiedTheirOwnGroups(TemplateOptions options) { |
| 169 | return options instanceof AWSEC2TemplateOptions |
| 170 | && AWSEC2TemplateOptions.class.cast(options).getGroupIds().size() > 0 |
| 171 | || super.userSpecifiedTheirOwnGroups(options); |
| 172 | } |
| 173 | |
| 174 | @Override |
| 175 | protected void addSecurityGroups(String region, String group, Template template, RunInstancesOptions instanceOptions) { |
| 176 | AWSEC2TemplateOptions awsTemplateOptions = AWSEC2TemplateOptions.class.cast(template.getOptions()); |
| 177 | AWSRunInstancesOptions awsInstanceOptions = AWSRunInstancesOptions.class.cast(instanceOptions); |
| 178 | if (awsTemplateOptions.getGroupIds().size() > 0) |
| 179 | awsInstanceOptions.withSecurityGroupIds(awsTemplateOptions.getGroupIds()); |
| 180 | String subnetId = awsTemplateOptions.getSubnetId(); |
| 181 | if (subnetId != null) { |
| 182 | AWSRunInstancesOptions.class.cast(instanceOptions).withSubnetId(subnetId); |
| 183 | } else { |
| 184 | super.addSecurityGroups(region, group, template, instanceOptions); |
| 185 | } |
| 186 | } |
| 187 | } |