| 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.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.Map; |
| 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.domain.RegionNameAndIngressRules; |
| 41 | import org.jclouds.ec2.compute.options.EC2TemplateOptions; |
| 42 | import org.jclouds.ec2.compute.strategy.CreateKeyPairAndSecurityGroupsAsNeededAndReturnRunOptions; |
| 43 | import org.jclouds.ec2.domain.KeyPair; |
| 44 | import org.jclouds.ec2.options.RunInstancesOptions; |
| 45 | import org.jclouds.logging.Logger; |
| 46 | |
| 47 | import com.google.common.annotations.VisibleForTesting; |
| 48 | import com.google.common.base.Function; |
| 49 | import com.google.common.base.Predicate; |
| 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 Map<RegionAndName, String> placementGroupMap; |
| 63 | @VisibleForTesting |
| 64 | final CreatePlacementGroupIfNeeded createPlacementGroupIfNeeded; |
| 65 | @VisibleForTesting |
| 66 | final Function<RegionNameAndPublicKeyMaterial, KeyPair> importExistingKeyPair; |
| 67 | |
| 68 | @Inject |
| 69 | public CreateKeyPairPlacementAndSecurityGroupsAsNeededAndReturnRunOptions( |
| 70 | Map<RegionAndName, KeyPair> credentialsMap, @Named("SECURITY") Map<RegionAndName, String> securityGroupMap, |
| 71 | @Named("PLACEMENT") Map<RegionAndName, String> placementGroupMap, |
| 72 | Function<RegionAndName, KeyPair> createUniqueKeyPair, |
| 73 | Function<RegionNameAndIngressRules, String> createSecurityGroupIfNeeded, |
| 74 | Provider<RunInstancesOptions> optionsProvider, CreatePlacementGroupIfNeeded createPlacementGroupIfNeeded, |
| 75 | Function<RegionNameAndPublicKeyMaterial, KeyPair> importExistingKeyPair) { |
| 76 | super(credentialsMap, securityGroupMap, createUniqueKeyPair, createSecurityGroupIfNeeded, 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()) : null; |
| 88 | |
| 89 | if (placementGroupName != null) |
| 90 | instanceOptions.inPlacementGroup(placementGroupName); |
| 91 | |
| 92 | if (AWSEC2TemplateOptions.class.cast(template.getOptions()).isMonitoringEnabled()) |
| 93 | instanceOptions.enableMonitoring(); |
| 94 | |
| 95 | return instanceOptions; |
| 96 | } |
| 97 | |
| 98 | @VisibleForTesting |
| 99 | String createNewPlacementGroupUnlessUserSpecifiedOtherwise(String region, String group, TemplateOptions options) { |
| 100 | String placementGroupName = null; |
| 101 | boolean shouldAutomaticallyCreatePlacementGroup = true; |
| 102 | if (options instanceof EC2TemplateOptions) { |
| 103 | placementGroupName = AWSEC2TemplateOptions.class.cast(options).getPlacementGroup(); |
| 104 | if (placementGroupName == null) |
| 105 | shouldAutomaticallyCreatePlacementGroup = AWSEC2TemplateOptions.class.cast(options) |
| 106 | .shouldAutomaticallyCreatePlacementGroup(); |
| 107 | } |
| 108 | if (placementGroupName == null && shouldAutomaticallyCreatePlacementGroup) { |
| 109 | // placementGroupName must be unique within an account per |
| 110 | // http://docs.amazonwebservices.com/AWSEC2/latest/UserGuide/index.html?using_cluster_computing.html |
| 111 | placementGroupName = String.format("jclouds#%s#%s", group, region); |
| 112 | RegionAndName regionAndName = new RegionAndName(region, placementGroupName); |
| 113 | if (!placementGroupMap.containsKey(regionAndName)) { |
| 114 | placementGroupMap.put(regionAndName, createPlacementGroupIfNeeded.apply(regionAndName)); |
| 115 | } |
| 116 | } |
| 117 | return placementGroupName; |
| 118 | } |
| 119 | |
| 120 | @Override |
| 121 | protected String createOrImportKeyPair(String region, String group, TemplateOptions options) { |
| 122 | RegionAndName key = new RegionAndName(region, "jclouds#" + group); |
| 123 | KeyPair pair = credentialsMap.get(key); |
| 124 | if (pair != null) |
| 125 | return pair.getKeyName(); |
| 126 | if (and(hasPublicKeyMaterial, or(doesntNeedSshAfterImportingPublicKey, hasLoginCredential)).apply(options)) { |
| 127 | pair = importExistingKeyPair.apply(new RegionNameAndPublicKeyMaterial(region, group, options.getPublicKey())); |
| 128 | options.dontAuthorizePublicKey(); |
| 129 | if (hasLoginCredential.apply(options)) |
| 130 | pair = pair.toBuilder().keyMaterial(options.getOverridingCredentials().credential).build(); |
| 131 | credentialsMap.put(key, pair); |
| 132 | } else { |
| 133 | if (hasPublicKeyMaterial.apply(options)) { |
| 134 | logger.warn("to avoid creating temporary keys in aws-ec2, use templateOption overrideLoginCredentialWith(id_rsa)"); |
| 135 | } |
| 136 | return createUniqueKeyPairAndPutIntoMap(region, group); |
| 137 | } |
| 138 | return pair.getKeyName(); |
| 139 | } |
| 140 | |
| 141 | public static final Predicate<TemplateOptions> hasPublicKeyMaterial = new Predicate<TemplateOptions>() { |
| 142 | |
| 143 | @Override |
| 144 | public boolean apply(TemplateOptions options) { |
| 145 | return options.getPublicKey() != null; |
| 146 | } |
| 147 | |
| 148 | }; |
| 149 | |
| 150 | public static final Predicate<TemplateOptions> doesntNeedSshAfterImportingPublicKey = new Predicate<TemplateOptions>() { |
| 151 | |
| 152 | @Override |
| 153 | public boolean apply(TemplateOptions options) { |
| 154 | return (options.getRunScript() == null && options.getPrivateKey() == null); |
| 155 | } |
| 156 | |
| 157 | }; |
| 158 | |
| 159 | public static final Predicate<TemplateOptions> hasLoginCredential = new Predicate<TemplateOptions>() { |
| 160 | |
| 161 | @Override |
| 162 | public boolean apply(TemplateOptions options) { |
| 163 | return options.getOverridingCredentials() != null && options.getOverridingCredentials().credential != null; |
| 164 | } |
| 165 | |
| 166 | }; |
| 167 | |
| 168 | @Override |
| 169 | protected boolean userSpecifiedTheirOwnGroups(TemplateOptions options) { |
| 170 | return options instanceof AWSEC2TemplateOptions |
| 171 | && AWSEC2TemplateOptions.class.cast(options).getGroupIds().size() > 0 |
| 172 | || super.userSpecifiedTheirOwnGroups(options); |
| 173 | } |
| 174 | |
| 175 | @Override |
| 176 | protected void addSecurityGroups(String region, String group, Template template, RunInstancesOptions instanceOptions) { |
| 177 | AWSEC2TemplateOptions awsTemplateOptions = AWSEC2TemplateOptions.class.cast(template.getOptions()); |
| 178 | AWSRunInstancesOptions awsInstanceOptions = AWSRunInstancesOptions.class.cast(instanceOptions); |
| 179 | if (awsTemplateOptions.getGroupIds().size() > 0) |
| 180 | awsInstanceOptions.withSecurityGroupIds(awsTemplateOptions.getGroupIds()); |
| 181 | String subnetId = awsTemplateOptions.getSubnetId(); |
| 182 | if (subnetId != null) { |
| 183 | AWSRunInstancesOptions.class.cast(instanceOptions).withSubnetId(subnetId); |
| 184 | } else { |
| 185 | super.addSecurityGroups(region, group, template, instanceOptions); |
| 186 | } |
| 187 | } |
| 188 | } |