| 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.ec2.compute.strategy; |
| 20 | |
| 21 | import static com.google.common.base.Preconditions.checkState; |
| 22 | |
| 23 | import java.util.Map; |
| 24 | import java.util.Set; |
| 25 | |
| 26 | import javax.annotation.Nullable; |
| 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.compute.domain.Template; |
| 33 | import org.jclouds.compute.options.TemplateOptions; |
| 34 | import org.jclouds.ec2.compute.domain.RegionAndName; |
| 35 | import org.jclouds.ec2.compute.domain.RegionNameAndIngressRules; |
| 36 | import org.jclouds.ec2.compute.options.EC2TemplateOptions; |
| 37 | import org.jclouds.ec2.domain.BlockDeviceMapping; |
| 38 | import org.jclouds.ec2.domain.KeyPair; |
| 39 | import org.jclouds.ec2.options.RunInstancesOptions; |
| 40 | |
| 41 | import com.google.common.annotations.VisibleForTesting; |
| 42 | import com.google.common.base.Function; |
| 43 | import com.google.common.collect.ImmutableSet; |
| 44 | import com.google.common.collect.ImmutableSet.Builder; |
| 45 | |
| 46 | /** |
| 47 | * |
| 48 | * @author Adrian Cole |
| 49 | */ |
| 50 | @Singleton |
| 51 | public class CreateKeyPairAndSecurityGroupsAsNeededAndReturnRunOptions { |
| 52 | |
| 53 | @VisibleForTesting |
| 54 | public final Map<RegionAndName, KeyPair> credentialsMap; |
| 55 | @VisibleForTesting |
| 56 | public final Map<RegionAndName, String> securityGroupMap; |
| 57 | @VisibleForTesting |
| 58 | public final Function<RegionAndName, KeyPair> createUniqueKeyPair; |
| 59 | @VisibleForTesting |
| 60 | public final Function<RegionNameAndIngressRules, String> createSecurityGroupIfNeeded; |
| 61 | protected final Provider<RunInstancesOptions> optionsProvider; |
| 62 | |
| 63 | @Inject |
| 64 | public CreateKeyPairAndSecurityGroupsAsNeededAndReturnRunOptions(Map<RegionAndName, KeyPair> credentialsMap, |
| 65 | @Named("SECURITY") Map<RegionAndName, String> securityGroupMap, Function<RegionAndName, KeyPair> createUniqueKeyPair, |
| 66 | Function<RegionNameAndIngressRules, String> createSecurityGroupIfNeeded, |
| 67 | Provider<RunInstancesOptions> optionsProvider) { |
| 68 | this.credentialsMap = credentialsMap; |
| 69 | this.securityGroupMap = securityGroupMap; |
| 70 | this.createUniqueKeyPair = createUniqueKeyPair; |
| 71 | this.createSecurityGroupIfNeeded = createSecurityGroupIfNeeded; |
| 72 | this.optionsProvider = optionsProvider; |
| 73 | } |
| 74 | |
| 75 | public RunInstancesOptions execute(String region, String group, Template template) { |
| 76 | |
| 77 | RunInstancesOptions instanceOptions = getOptionsProvider().get().asType(template.getHardware().getId()); |
| 78 | |
| 79 | String keyPairName = createNewKeyPairUnlessUserSpecifiedOtherwise(region, group, template.getOptions()); |
| 80 | |
| 81 | addSecurityGroups(region, group, template, instanceOptions); |
| 82 | if (template.getOptions() instanceof EC2TemplateOptions) { |
| 83 | |
| 84 | if (keyPairName != null) |
| 85 | instanceOptions.withKeyName(keyPairName); |
| 86 | |
| 87 | byte[] userData = EC2TemplateOptions.class.cast(template.getOptions()).getUserData(); |
| 88 | |
| 89 | if (userData != null) |
| 90 | instanceOptions.withUserData(userData); |
| 91 | |
| 92 | Set<BlockDeviceMapping> blockDeviceMappings = EC2TemplateOptions.class.cast(template.getOptions()) |
| 93 | .getBlockDeviceMappings(); |
| 94 | if (blockDeviceMappings.size() > 0) { |
| 95 | checkState("ebs".equals(template.getImage().getUserMetadata().get("rootDeviceType")), |
| 96 | "BlockDeviceMapping only available on ebs boot"); |
| 97 | instanceOptions.withBlockDeviceMappings(blockDeviceMappings); |
| 98 | } |
| 99 | } |
| 100 | return instanceOptions; |
| 101 | } |
| 102 | |
| 103 | protected void addSecurityGroups(String region, String group, Template template, RunInstancesOptions instanceOptions) { |
| 104 | Set<String> groups = getSecurityGroupsForTagAndOptions(region, group, template.getOptions()); |
| 105 | instanceOptions.withSecurityGroups(groups); |
| 106 | } |
| 107 | |
| 108 | @VisibleForTesting |
| 109 | public String createNewKeyPairUnlessUserSpecifiedOtherwise(String region, String group, TemplateOptions options) { |
| 110 | String keyPairName = null; |
| 111 | boolean shouldAutomaticallyCreateKeyPair = true; |
| 112 | if (options instanceof EC2TemplateOptions) { |
| 113 | keyPairName = EC2TemplateOptions.class.cast(options).getKeyPair(); |
| 114 | if (keyPairName == null) |
| 115 | shouldAutomaticallyCreateKeyPair = EC2TemplateOptions.class.cast(options) |
| 116 | .shouldAutomaticallyCreateKeyPair(); |
| 117 | } |
| 118 | if (keyPairName == null && shouldAutomaticallyCreateKeyPair) { |
| 119 | keyPairName = createOrImportKeyPair(region, group, options); |
| 120 | } |
| 121 | return keyPairName; |
| 122 | } |
| 123 | |
| 124 | // base EC2 driver currently does not support key import |
| 125 | protected String createOrImportKeyPair(String region, String group, TemplateOptions options) { |
| 126 | return createUniqueKeyPairAndPutIntoMap(region, group); |
| 127 | } |
| 128 | |
| 129 | protected String createUniqueKeyPairAndPutIntoMap(String region, String group) { |
| 130 | String keyPairName; |
| 131 | RegionAndName regionAndName = new RegionAndName(region, group); |
| 132 | KeyPair keyPair = createUniqueKeyPair.apply(regionAndName); |
| 133 | keyPairName = keyPair.getKeyName(); |
| 134 | // get or create incidental resources |
| 135 | // TODO race condition. we were using MapMaker, but it doesn't seem to |
| 136 | // refresh properly |
| 137 | // when |
| 138 | // another thread |
| 139 | // deletes a key |
| 140 | credentialsMap.put(new RegionAndName(regionAndName.getRegion(), keyPairName), keyPair); |
| 141 | return keyPairName; |
| 142 | } |
| 143 | |
| 144 | @VisibleForTesting |
| 145 | public Set<String> getSecurityGroupsForTagAndOptions(String region, @Nullable String group, TemplateOptions options) { |
| 146 | Builder<String> groups = ImmutableSet.<String> builder(); |
| 147 | |
| 148 | if (group != null) { |
| 149 | String markerGroup = String.format("jclouds#%s#%s", group, region); |
| 150 | groups.add(markerGroup); |
| 151 | |
| 152 | RegionNameAndIngressRules regionNameAndIngessRulesForMarkerGroup; |
| 153 | |
| 154 | if (options instanceof EC2TemplateOptions && EC2TemplateOptions.class.cast(options).getGroupIds().size() > 0) { |
| 155 | regionNameAndIngessRulesForMarkerGroup = new RegionNameAndIngressRules(region, markerGroup, new int[] {}, |
| 156 | false); |
| 157 | groups.addAll(EC2TemplateOptions.class.cast(options).getGroupIds()); |
| 158 | |
| 159 | } else { |
| 160 | regionNameAndIngessRulesForMarkerGroup = new RegionNameAndIngressRules(region, markerGroup, |
| 161 | options.getInboundPorts(), true); |
| 162 | } |
| 163 | |
| 164 | if (!securityGroupMap.containsKey(regionNameAndIngessRulesForMarkerGroup)) { |
| 165 | securityGroupMap.put(regionNameAndIngessRulesForMarkerGroup, |
| 166 | createSecurityGroupIfNeeded.apply(regionNameAndIngessRulesForMarkerGroup)); |
| 167 | } |
| 168 | } |
| 169 | return groups.build(); |
| 170 | } |
| 171 | |
| 172 | // allows us to mock this method |
| 173 | @VisibleForTesting |
| 174 | public javax.inject.Provider<RunInstancesOptions> getOptionsProvider() { |
| 175 | return optionsProvider; |
| 176 | } |
| 177 | } |