| 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.functions; |
| 20 | |
| 21 | import static com.google.common.base.Preconditions.checkNotNull; |
| 22 | |
| 23 | import java.util.List; |
| 24 | import java.util.Map; |
| 25 | import java.util.Map.Entry; |
| 26 | import java.util.NoSuchElementException; |
| 27 | import java.util.Set; |
| 28 | |
| 29 | import javax.annotation.Resource; |
| 30 | import javax.inject.Inject; |
| 31 | import javax.inject.Singleton; |
| 32 | |
| 33 | import org.jclouds.collect.Memoized; |
| 34 | import org.jclouds.compute.domain.Hardware; |
| 35 | import org.jclouds.compute.domain.HardwareBuilder; |
| 36 | import org.jclouds.compute.domain.Image; |
| 37 | import org.jclouds.compute.domain.NodeMetadata; |
| 38 | import org.jclouds.compute.domain.NodeMetadataBuilder; |
| 39 | import org.jclouds.compute.domain.NodeState; |
| 40 | import org.jclouds.compute.domain.Volume; |
| 41 | import org.jclouds.compute.domain.internal.VolumeImpl; |
| 42 | import org.jclouds.domain.Credentials; |
| 43 | import org.jclouds.domain.Location; |
| 44 | import org.jclouds.ec2.compute.domain.RegionAndName; |
| 45 | import org.jclouds.ec2.domain.BlockDevice; |
| 46 | import org.jclouds.ec2.domain.InstanceState; |
| 47 | import org.jclouds.ec2.domain.RootDeviceType; |
| 48 | import org.jclouds.ec2.domain.RunningInstance; |
| 49 | import org.jclouds.logging.Logger; |
| 50 | import org.jclouds.util.NullSafeCollections; |
| 51 | |
| 52 | import com.google.common.annotations.VisibleForTesting; |
| 53 | import com.google.common.base.Function; |
| 54 | import com.google.common.base.Predicate; |
| 55 | import com.google.common.base.Supplier; |
| 56 | import com.google.common.cache.Cache; |
| 57 | import com.google.common.collect.Iterables; |
| 58 | import com.google.common.collect.Lists; |
| 59 | import com.google.common.util.concurrent.UncheckedExecutionException; |
| 60 | |
| 61 | /** |
| 62 | * @author Adrian Cole |
| 63 | */ |
| 64 | @Singleton |
| 65 | public class RunningInstanceToNodeMetadata implements Function<RunningInstance, NodeMetadata> { |
| 66 | |
| 67 | @Resource |
| 68 | protected Logger logger = Logger.NULL; |
| 69 | |
| 70 | protected final Supplier<Set<? extends Location>> locations; |
| 71 | protected final Supplier<Set<? extends Hardware>> hardware; |
| 72 | protected final Supplier<Cache<RegionAndName, ? extends Image>> imageMap; |
| 73 | protected final Map<String, Credentials> credentialStore; |
| 74 | protected final Map<InstanceState, NodeState> instanceToNodeState; |
| 75 | |
| 76 | @Inject |
| 77 | protected RunningInstanceToNodeMetadata(Map<InstanceState, NodeState> instanceToNodeState, |
| 78 | Map<String, Credentials> credentialStore, Supplier<Cache<RegionAndName, ? extends Image>> imageMap, |
| 79 | @Memoized Supplier<Set<? extends Location>> locations, @Memoized Supplier<Set<? extends Hardware>> hardware) { |
| 80 | this.locations = checkNotNull(locations, "locations"); |
| 81 | this.hardware = checkNotNull(hardware, "hardware"); |
| 82 | this.imageMap = checkNotNull(imageMap, "imageMap"); |
| 83 | this.instanceToNodeState = checkNotNull(instanceToNodeState, "instanceToNodeState"); |
| 84 | this.credentialStore = checkNotNull(credentialStore, "credentialStore"); |
| 85 | } |
| 86 | |
| 87 | @Override |
| 88 | public NodeMetadata apply(RunningInstance instance) { |
| 89 | if (instance == null || instance.getId() == null) |
| 90 | return null; |
| 91 | NodeMetadataBuilder builder = new NodeMetadataBuilder(); |
| 92 | builder = buildInstance(instance, builder); |
| 93 | return builder.build(); |
| 94 | } |
| 95 | |
| 96 | protected NodeMetadataBuilder buildInstance(RunningInstance instance, NodeMetadataBuilder builder) { |
| 97 | builder.providerId(instance.getId()); |
| 98 | builder.id(instance.getRegion() + "/" + instance.getId()); |
| 99 | String group = getGroupForInstance(instance); |
| 100 | builder.group(group); |
| 101 | // standard convention from aws-ec2, which might not be re-used outside. |
| 102 | if (instance.getPrivateDnsName() != null) |
| 103 | builder.hostname(instance.getPrivateDnsName().replaceAll("\\..*", "")); |
| 104 | addCredentialsForInstance(builder, instance); |
| 105 | builder.state(instanceToNodeState.get(instance.getInstanceState())); |
| 106 | builder.publicAddresses(NullSafeCollections.nullSafeSet(instance.getIpAddress())); |
| 107 | builder.privateAddresses(NullSafeCollections.nullSafeSet(instance.getPrivateIpAddress())); |
| 108 | builder.hardware(parseHardware(instance)); |
| 109 | Location location = getLocationForAvailabilityZoneOrRegion(instance); |
| 110 | builder.location(location); |
| 111 | builder.imageId(instance.getRegion() + "/" + instance.getImageId()); |
| 112 | |
| 113 | // extract the operating system from the image |
| 114 | RegionAndName regionAndName = new RegionAndName(instance.getRegion(), instance.getImageId()); |
| 115 | try { |
| 116 | Image image = imageMap.get().getUnchecked(regionAndName); |
| 117 | if (image != null) |
| 118 | builder.operatingSystem(image.getOperatingSystem()); |
| 119 | } catch (NullPointerException e) { |
| 120 | logger.debug("image not found for %s: %s", regionAndName, e); |
| 121 | } catch (UncheckedExecutionException e) { |
| 122 | logger.debug("error getting image for %s: %s", regionAndName, e); |
| 123 | } |
| 124 | return builder; |
| 125 | } |
| 126 | |
| 127 | protected void addCredentialsForInstance(NodeMetadataBuilder builder, RunningInstance instance) { |
| 128 | builder.credentials(credentialStore.get("node#" + instance.getRegion() + "/" + instance.getId())); |
| 129 | } |
| 130 | |
| 131 | protected Hardware parseHardware(final RunningInstance instance) { |
| 132 | Hardware hardware = getHardwareForInstance(instance); |
| 133 | |
| 134 | if (hardware != null) { |
| 135 | hardware = HardwareBuilder.fromHardware(hardware).volumes(addEBS(instance, hardware.getVolumes())).build(); |
| 136 | } |
| 137 | return hardware; |
| 138 | } |
| 139 | |
| 140 | @VisibleForTesting |
| 141 | static List<Volume> addEBS(final RunningInstance instance, Iterable<? extends Volume> volumes) { |
| 142 | Iterable<Volume> ebsVolumes = Iterables.transform(instance.getEbsBlockDevices().entrySet(), |
| 143 | new Function<Entry<String, BlockDevice>, Volume>() { |
| 144 | |
| 145 | @Override |
| 146 | public Volume apply(Entry<String, BlockDevice> from) { |
| 147 | return new VolumeImpl(from.getValue().getVolumeId(), Volume.Type.SAN, null, from.getKey(), |
| 148 | instance.getRootDeviceName() != null |
| 149 | && instance.getRootDeviceName().equals(from.getKey()), true); |
| 150 | } |
| 151 | }); |
| 152 | |
| 153 | if (instance.getRootDeviceType() == RootDeviceType.EBS) { |
| 154 | volumes = Iterables.filter(volumes, new Predicate<Volume>() { |
| 155 | |
| 156 | @Override |
| 157 | public boolean apply(Volume input) { |
| 158 | return !input.isBootDevice(); |
| 159 | } |
| 160 | |
| 161 | }); |
| 162 | |
| 163 | } |
| 164 | return Lists.newArrayList(Iterables.concat(volumes, ebsVolumes)); |
| 165 | |
| 166 | } |
| 167 | |
| 168 | @VisibleForTesting |
| 169 | String getGroupForInstance(final RunningInstance instance) { |
| 170 | String group = null; |
| 171 | try { |
| 172 | group = Iterables.getOnlyElement(Iterables.filter(instance.getGroupIds(), new Predicate<String>() { |
| 173 | |
| 174 | @Override |
| 175 | public boolean apply(String input) { |
| 176 | return input.startsWith("jclouds#") && input.endsWith("#" + instance.getRegion()); |
| 177 | } |
| 178 | |
| 179 | })).substring(8).replaceAll("#" + instance.getRegion() + "$", ""); |
| 180 | } catch (NoSuchElementException e) { |
| 181 | logger.debug("no group parsed from %s's security groups: %s", instance.getId(), instance.getGroupIds()); |
| 182 | } catch (IllegalArgumentException e) { |
| 183 | logger.debug("too many groups match %s; %s's security groups: %s", "jclouds#", instance.getId(), instance |
| 184 | .getGroupIds()); |
| 185 | } |
| 186 | return group; |
| 187 | } |
| 188 | |
| 189 | @VisibleForTesting |
| 190 | Hardware getHardwareForInstance(final RunningInstance instance) { |
| 191 | try { |
| 192 | return Iterables.find(hardware.get(), new Predicate<Hardware>() { |
| 193 | |
| 194 | @Override |
| 195 | public boolean apply(Hardware input) { |
| 196 | return input.getId().equals(instance.getInstanceType()); |
| 197 | } |
| 198 | |
| 199 | }); |
| 200 | } catch (NoSuchElementException e) { |
| 201 | logger.debug("couldn't match instance type %s in: %s", instance.getInstanceType(), hardware.get()); |
| 202 | return null; |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | private Location getLocationForAvailabilityZoneOrRegion(final RunningInstance instance) { |
| 207 | Location location = findLocationWithId(instance.getAvailabilityZone()); |
| 208 | if (location == null) |
| 209 | location = findLocationWithId(instance.getRegion()); |
| 210 | return location; |
| 211 | } |
| 212 | |
| 213 | private Location findLocationWithId(final String locationId) { |
| 214 | if (locationId == null) |
| 215 | return null; |
| 216 | try { |
| 217 | Location location = Iterables.find(locations.get(), new Predicate<Location>() { |
| 218 | |
| 219 | @Override |
| 220 | public boolean apply(Location input) { |
| 221 | return input.getId().equals(locationId); |
| 222 | } |
| 223 | |
| 224 | }); |
| 225 | return location; |
| 226 | |
| 227 | } catch (NoSuchElementException e) { |
| 228 | logger.debug("couldn't match instance location %s in: %s", locationId, locations.get()); |
| 229 | return null; |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | } |