|
|
@@ -8,8 +8,15 @@ from boto.ec2.regioninfo import RegionInfo
|
|
|
|
|
|
from cloudbridge.providers.base import BaseCloudProvider
|
|
|
from cloudbridge.providers.base import BaseSecurityGroup
|
|
|
+from cloudbridge.providers.base import BaseKeyPair
|
|
|
from cloudbridge.providers.interfaces import SecurityService
|
|
|
+from cloudbridge.providers.interfaces import ComputeService
|
|
|
from cloudbridge.providers.interfaces import KeyPair
|
|
|
+from cloudbridge.providers.interfaces import MachineImage
|
|
|
+from cloudbridge.providers.interfaces import SecurityGroup
|
|
|
+from cloudbridge.providers.interfaces import PlacementZone
|
|
|
+from cloudbridge.providers.interfaces import InstanceType
|
|
|
+from cloudbridge.providers.interfaces import Instance
|
|
|
|
|
|
|
|
|
class EC2CloudProviderV1(BaseCloudProvider):
|
|
|
@@ -32,7 +39,7 @@ class EC2CloudProviderV1(BaseCloudProvider):
|
|
|
self.ec2_conn = self._connect_ec2()
|
|
|
|
|
|
# Initialize provider services
|
|
|
- self.compute = None # EC2ComputeService(self)
|
|
|
+ self.compute = EC2ComputeService(self)
|
|
|
self.images = None # EC2ImageService(self)
|
|
|
self.security = EC2SecurityService(self)
|
|
|
self.block_store = None # EC2BlockStore(self)
|
|
|
@@ -68,7 +75,7 @@ class EC2SecurityService(SecurityService):
|
|
|
:return: list of KeyPair objects
|
|
|
"""
|
|
|
key_pairs = self.provider.ec2_conn.get_all_key_pairs()
|
|
|
- return [KeyPair(kp.name) for kp in key_pairs]
|
|
|
+ return [BaseKeyPair(kp.name) for kp in key_pairs]
|
|
|
|
|
|
def list_security_groups(self):
|
|
|
"""
|
|
|
@@ -79,3 +86,130 @@ class EC2SecurityService(SecurityService):
|
|
|
"""
|
|
|
groups = self.provider.ec2_conn.get_all_security_groups()
|
|
|
return [BaseSecurityGroup(group.name) for group in groups]
|
|
|
+
|
|
|
+
|
|
|
+class EC2Instance(Instance):
|
|
|
+
|
|
|
+ def __init__(self, provider, ec2_instance):
|
|
|
+ self.provider = provider
|
|
|
+ self._ec2_instance = ec2_instance
|
|
|
+
|
|
|
+ def instance_id(self):
|
|
|
+ """
|
|
|
+ Get the instance identifier.
|
|
|
+ """
|
|
|
+ return self._ec2_instance.id
|
|
|
+
|
|
|
+ @property
|
|
|
+ def name(self):
|
|
|
+ """
|
|
|
+ Get the instance name.
|
|
|
+ """
|
|
|
+ return self._ec2_instance.tags['Name']
|
|
|
+
|
|
|
+ @name.setter
|
|
|
+ def name(self, value):
|
|
|
+ """
|
|
|
+ Set the instance name.
|
|
|
+ """
|
|
|
+ self._ec2_instance.add_tag('Name', value)
|
|
|
+
|
|
|
+ def public_ips(self):
|
|
|
+ """
|
|
|
+ Get all the public IP addresses for this instance.
|
|
|
+ """
|
|
|
+ return [self._ec2_instance.ip_address]
|
|
|
+
|
|
|
+ def private_ips(self):
|
|
|
+ """
|
|
|
+ Get all the private IP addresses for this instance.
|
|
|
+ """
|
|
|
+ return [self._ec2_instance.private_ip_address]
|
|
|
+
|
|
|
+ def instance_type(self):
|
|
|
+ """
|
|
|
+ Get the instance type.
|
|
|
+ """
|
|
|
+ return [self._ec2_instance.instance_type]
|
|
|
+
|
|
|
+ def reboot(self):
|
|
|
+ """
|
|
|
+ Reboot this instance (using the cloud middleware API).
|
|
|
+ """
|
|
|
+ self._ec2_instance.reboot()
|
|
|
+
|
|
|
+ def terminate(self):
|
|
|
+ """
|
|
|
+ Permanently terminate this instance.
|
|
|
+ """
|
|
|
+ self._ec2_instance.terminate()
|
|
|
+
|
|
|
+ def image_id(self):
|
|
|
+ """
|
|
|
+ Get the image ID for this insance.
|
|
|
+ """
|
|
|
+ return self._ec2_instance.id
|
|
|
+
|
|
|
+ def placement_zone(self):
|
|
|
+ """
|
|
|
+ Get the placement zone where this instance is running.
|
|
|
+ """
|
|
|
+ return self._ec2_instance.id
|
|
|
+
|
|
|
+ def mac_address(self):
|
|
|
+ """
|
|
|
+ Get the MAC address for this instance.
|
|
|
+ """
|
|
|
+ raise NotImplementedError(
|
|
|
+ 'mac_address not implemented by this provider')
|
|
|
+
|
|
|
+ def security_group_ids(self):
|
|
|
+ """
|
|
|
+ Get the security group IDs associated with this instance.
|
|
|
+ """
|
|
|
+ return [BaseSecurityGroup(group.name) for group in self._ec2_instance.groups]
|
|
|
+
|
|
|
+ def key_pair_name(self):
|
|
|
+ """
|
|
|
+ Get the name of the key pair associated with this instance.
|
|
|
+ """
|
|
|
+ return BaseKeyPair(self._ec2_instance.key_name)
|
|
|
+
|
|
|
+
|
|
|
+class EC2ComputeService(ComputeService):
|
|
|
+
|
|
|
+ def __init__(self, provider):
|
|
|
+ self.provider = provider
|
|
|
+
|
|
|
+ def create_instance(self, name, image, instance_type, zone=None, keypair=None, security_groups=None,
|
|
|
+ user_data=None, block_device_mapping=None, network_interfaces=None, **kwargs):
|
|
|
+ """
|
|
|
+ Creates a new virtual machine instance.
|
|
|
+ """
|
|
|
+ image_id = image.image_id if isinstance(image, MachineImage) else image
|
|
|
+ instance_size = instance_type.name if isinstance(
|
|
|
+ instance_type,
|
|
|
+ InstanceType) else instance_type
|
|
|
+ zone_name = zone.name if isinstance(zone, PlacementZone) else zone
|
|
|
+ keypair_name = keypair.name if isinstance(keypair, KeyPair) else keypair
|
|
|
+ if security_groups:
|
|
|
+ if isinstance(security_groups, list) and isinstance(security_groups[0], SecurityGroup):
|
|
|
+ security_groups_list = [sg.name for sg in security_groups]
|
|
|
+ else:
|
|
|
+ security_groups_list = security_groups
|
|
|
+ else:
|
|
|
+ security_groups_list = None
|
|
|
+
|
|
|
+ reservation = self.provider.ec2_conn.run_instances(image_id=image_id,
|
|
|
+ instance_type=instance_size,
|
|
|
+ min_count=1,
|
|
|
+ max_count=1,
|
|
|
+ placement=zone_name,
|
|
|
+ key_name=keypair_name,
|
|
|
+ security_groups=security_groups_list,
|
|
|
+ user_data=user_data
|
|
|
+ )
|
|
|
+ if reservation:
|
|
|
+ instance = EC2Instance(self.provider, reservation.instances[0])
|
|
|
+ instance.name = name
|
|
|
+ return instance
|