mirror of https://github.com/apache/jclouds.git
Added size mapping for EC2 instances
This commit is contained in:
parent
1714051272
commit
dac0c67a50
|
@ -28,11 +28,13 @@ import java.util.Set;
|
|||
import javax.inject.Inject;
|
||||
import javax.inject.Singleton;
|
||||
|
||||
import com.google.common.collect.Maps;
|
||||
import org.jclouds.aws.ec2.compute.domain.RegionTag;
|
||||
import org.jclouds.aws.ec2.domain.Image;
|
||||
import org.jclouds.aws.ec2.domain.InstanceState;
|
||||
import org.jclouds.aws.ec2.domain.KeyPair;
|
||||
import org.jclouds.aws.ec2.domain.RunningInstance;
|
||||
import org.jclouds.aws.ec2.functions.InstanceTypeToStorageMappingUnix;
|
||||
import org.jclouds.aws.ec2.options.DescribeImagesOptions;
|
||||
import org.jclouds.aws.ec2.services.AMIClient;
|
||||
import org.jclouds.compute.domain.NodeMetadata;
|
||||
|
@ -86,12 +88,33 @@ public class RunningInstanceToNodeMetadata implements Function<RunningInstance,
|
|||
|
||||
String locationId = instance.getAvailabilityZone().toString();
|
||||
|
||||
Map<String, String> extra = ImmutableMap.<String, String> of();
|
||||
Map<String, String> extra = getExtra(instance);
|
||||
|
||||
return new NodeMetadataImpl(id, name, locationId, uri, userMetadata, tag, state,
|
||||
publicAddresses, privateAddresses, extra, credentials);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set extras for the node.
|
||||
*
|
||||
* Extras are derived from either additional API calls or
|
||||
* hard-coded values.
|
||||
* @param instance instance for which the extras are retrieved
|
||||
* @return map with extras
|
||||
*/
|
||||
@VisibleForTesting
|
||||
Map<String, String> getExtra(RunningInstance instance) {
|
||||
Map<String, String> extra = Maps.newHashMap();
|
||||
|
||||
//put storage info
|
||||
/* TODO: only valid for UNIX */
|
||||
InstanceTypeToStorageMappingUnix instanceToStorageMapping =
|
||||
new InstanceTypeToStorageMappingUnix();
|
||||
extra.putAll(instanceToStorageMapping.apply(instance.getInstanceType()));
|
||||
|
||||
return extra;
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
String getPrivateKeyOrNull(RunningInstance instance, String tag) {
|
||||
KeyPair keyPair = credentialsMap.get(new RegionTag(instance.getRegion(), instance
|
||||
|
|
|
@ -0,0 +1,177 @@
|
|||
/**
|
||||
*
|
||||
* Copyright (C) 2010 Cloud Conscious, LLC. <info@cloudconscious.com>
|
||||
*
|
||||
* ====================================================================
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
* ====================================================================
|
||||
*/
|
||||
package org.jclouds.aws.ec2.functions;
|
||||
|
||||
import com.google.common.base.Function;
|
||||
import com.google.common.collect.Maps;
|
||||
import org.jclouds.aws.ec2.domain.InstanceType;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import static org.jclouds.compute.reference.ComputeServiceConstants.LOCAL_PARTITION_GB_PATTERN;
|
||||
|
||||
/**
|
||||
* Map the instance type to storage information known about it. This information is statically set as described by Amazon.
|
||||
*
|
||||
* Note that having the partitions available doesn't mean they're formatted/mounted by default. The links below
|
||||
* describe what partitions are formatted initially. To format/mount an available device, refer to
|
||||
* <a href="http://meinit.nl/howto-use-amazon-elastic-compute-cloud-ec2">this article</a>.
|
||||
*
|
||||
* @see <a href="http://docs.amazonwebservices.com/AWSEC2/2009-11-30/UserGuide/index.html?instance-storage-concepts.html" />
|
||||
* @see <a href="http://docs.amazonwebservices.com/AWSEC2/latest/UserGuide/index.html?instance-types.html" />
|
||||
*
|
||||
* @author Oleksiy Yarmula
|
||||
*/
|
||||
public class InstanceTypeToStorageMappingUnix implements Function<InstanceType, Map<String, String>> {
|
||||
|
||||
public final static String ROOT_PARTITION_NAME_UNIX = "/dev/sda1";
|
||||
|
||||
@Override
|
||||
public Map<String, String> apply(InstanceType instanceType) {
|
||||
Map<String, String> mapping = Maps.newHashMap();
|
||||
|
||||
//root partition
|
||||
mapping.put(String.format(LOCAL_PARTITION_GB_PATTERN, ROOT_PARTITION_NAME_UNIX),
|
||||
getRootPartitionSizeForInstanceType(instanceType) + "");
|
||||
|
||||
//primary partition (always formatted/mounted)
|
||||
mapping.put(String.format(LOCAL_PARTITION_GB_PATTERN, getPrimaryPartitionDeviceName(instanceType)),
|
||||
getPrimaryPartitionSizeForInstanceType(instanceType) + "");
|
||||
|
||||
//additional partitions if any
|
||||
for(Map.Entry<String, Integer> entry : getAdditionalPartitionsMapping(instanceType).entrySet()) {
|
||||
mapping.put(String.format(LOCAL_PARTITION_GB_PATTERN, entry.getKey()),
|
||||
entry.getValue() + "");
|
||||
}
|
||||
|
||||
return mapping;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve the root partition size.
|
||||
* Note, this is usually a rather small partition. Refer to
|
||||
* {@link #getPrimaryPartitionSizeForInstanceType()} to determine the
|
||||
* size of the primary (usually, biggest) partition. In some cases,
|
||||
* for large instances there are several partitions of the size of primary partition.
|
||||
*
|
||||
* @param instanceType for which the root partition size is to be determined
|
||||
* @return size in GB
|
||||
*
|
||||
* @see <a href="http://docs.amazonwebservices.com/AWSEC2/2009-11-30/UserGuide/index.html?instance-storage-concepts.html" />
|
||||
* @see <a href="http://docs.amazonwebservices.com/AWSEC2/latest/UserGuide/index.html?instance-types.html" />
|
||||
*/
|
||||
public int getRootPartitionSizeForInstanceType(InstanceType instanceType) {
|
||||
/* per documentation at
|
||||
http://docs.amazonwebservices.com/AWSEC2/latest/UserGuide/index.html?instance-types.html
|
||||
M2 XLARGE doesn't have the root partition
|
||||
TODO verify
|
||||
*/
|
||||
if(InstanceType.M2_XLARGE == instanceType) return 0;
|
||||
|
||||
//other types have 10 GB root partition
|
||||
return 10;
|
||||
}
|
||||
|
||||
public static String getPrimaryPartitionDeviceName(InstanceType instanceType) {
|
||||
if(InstanceType.M1_SMALL == instanceType || InstanceType.C1_MEDIUM == instanceType)
|
||||
return "/dev/sda2";
|
||||
return "/dev/sdb";
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve the primary partition size.
|
||||
*
|
||||
* This is usually the biggest partition. In some cases,
|
||||
* for large instances there are several partitions of the size of primary partition.
|
||||
*
|
||||
* @param instanceType for which the primary partition size is to be determined
|
||||
* @return size in GB
|
||||
*/
|
||||
public static int getPrimaryPartitionSizeForInstanceType(InstanceType instanceType) {
|
||||
switch(instanceType) {
|
||||
case M1_SMALL:
|
||||
return 150;
|
||||
|
||||
case M1_LARGE:
|
||||
return 420;
|
||||
|
||||
case M1_XLARGE:
|
||||
return 420;
|
||||
|
||||
case C1_MEDIUM:
|
||||
return 340;
|
||||
|
||||
case C1_XLARGE:
|
||||
return 420;
|
||||
|
||||
case M2_XLARGE:
|
||||
return 420;
|
||||
|
||||
case M2_2XLARGE:
|
||||
return 840;
|
||||
|
||||
case M2_4XLARGE:
|
||||
return 840;
|
||||
}
|
||||
throw new RuntimeException("Unknown instance type");
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve additional devices mapping (non-root and non-primary) for the instance type.
|
||||
*
|
||||
* @param instanceType
|
||||
* @return map with device name(s) and size(s) or empty map if the instance doesn't
|
||||
* have any additional
|
||||
*
|
||||
* @see <a href="http://docs.amazonwebservices.com/AWSEC2/latest/UserGuide/index.html?concepts-amis-and-instances.html#instance-types" />
|
||||
*/
|
||||
public static Map<String, Integer> getAdditionalPartitionsMapping(InstanceType instanceType) {
|
||||
Map<String, Integer> mapping = Maps.newHashMap();
|
||||
|
||||
int size = 0;
|
||||
switch(instanceType) {
|
||||
case M1_LARGE:
|
||||
case M1_XLARGE:
|
||||
case C1_XLARGE:
|
||||
size = 420;
|
||||
break;
|
||||
|
||||
case M2_4XLARGE:
|
||||
size = 840;
|
||||
break;
|
||||
}
|
||||
|
||||
//m1.large, m1.xlarge, and c1.xlarge
|
||||
if(InstanceType.M1_LARGE == instanceType || InstanceType.M1_XLARGE == instanceType ||
|
||||
InstanceType.C1_XLARGE == instanceType || InstanceType.M2_4XLARGE == instanceType) {
|
||||
|
||||
mapping.put("/dev/sdc", size);
|
||||
}
|
||||
|
||||
if(InstanceType.M1_XLARGE == instanceType || InstanceType.C1_XLARGE == instanceType) {
|
||||
mapping.put("/dev/sdd", size);
|
||||
}
|
||||
|
||||
if(InstanceType.M1_XLARGE == instanceType || InstanceType.C1_XLARGE == instanceType) {
|
||||
mapping.put("/dev/sde", size);
|
||||
}
|
||||
|
||||
return mapping;
|
||||
}
|
||||
}
|
|
@ -25,5 +25,6 @@ package org.jclouds.compute.reference;
|
|||
public interface ComputeServiceConstants {
|
||||
|
||||
public static final String COMPUTE_LOGGER = "jclouds.compute";
|
||||
public static final String LOCAL_PARTITION_GB_PATTERN = "disk_drive/%s/gb";
|
||||
|
||||
}
|
||||
|
|
|
@ -317,6 +317,7 @@ public abstract class BaseComputeServiceLiveTest {
|
|||
assertNotNull(nodeMetadata.getPrivateAddresses());
|
||||
}
|
||||
}
|
||||
|
||||
public void testListImages() throws Exception {
|
||||
for (Entry<String, ? extends Image> image : client.getImages().entrySet()) {
|
||||
assertEquals(image.getKey(), image.getValue().getId());
|
||||
|
|
Loading…
Reference in New Issue